Skip to content

Commit

Permalink
Added Author selection option to the frontend for improved quote pers…
Browse files Browse the repository at this point in the history
…onalization. (#131)
  • Loading branch information
marceloams committed Dec 4, 2024
1 parent 6b736e6 commit 4d51c0f
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 3 deletions.
22 changes: 20 additions & 2 deletions frontend/src/components/Pages/Home/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { themes, animations, layouts, fonts, colorValues, quoteTypes } from '../
import TextField from '@material-ui/core/TextField';
import Autocomplete from '@material-ui/lab/Autocomplete';
import ContributorsCard from '../../ContributorsCard/ContributorCard'
import useQuoteAuthors from '../../../util/authors';
import { makeStyles } from '@material-ui/core/styles';

const useStyles = makeStyles({
Expand All @@ -24,6 +25,9 @@ const Home = () => {
const [bgColor, setBgColor] = useState(null);
const [borderColor, setBorderColor] = useState(null);
const [quoteType, setQuoteType] = useState("random");
const [quoteAuthor, setQuoteAuthor] = useState(null);

const { quoteAuthors, loadingQuoteAuthors } = useQuoteAuthors();

const classes = useStyles();

Expand Down Expand Up @@ -158,11 +162,25 @@ const Home = () => {
/>
</Grid>

<Grid item xs={12} sm={6} lg={3}>
<Autocomplete
id="author"
options={quoteAuthors}
value={quoteAuthor}
style={{ width: 300 }}
loading={loadingQuoteAuthors}
onChange={(_event, newValue) => {
setQuoteAuthor(newValue)
}}
renderInput={(params) => <TextField {...params} label="Author" placeholder="Select an author" variant="outlined" />}
/>
</Grid>

</Grid>

<Grid container spacing={4}>
<Grid item xs={12} style={{ marginTop: '20px' }}>
<TemplateCard theme={theme} animation={animation} layout={layout} font={font} fontColor={fontColor} bgColor={bgColor} borderColor={borderColor} quoteType={quoteType} />
<TemplateCard theme={theme} animation={animation} layout={layout} font={font} fontColor={fontColor} bgColor={bgColor} borderColor={borderColor} quoteType={quoteType} quoteAuthor={quoteAuthor}/>
</Grid>
<Grid item xs={12}>
<Typography align="center">Other layouts</Typography>
Expand All @@ -171,7 +189,7 @@ const Home = () => {
layouts.filter((item) => item !== layout).map((restLayout) => {
return (
<Grid key={restLayout} item xs={12} sm={12} md={6}>
<TemplateCard theme={theme} animation={animation} layout={restLayout} font={font} fontColor={fontColor} bgColor={bgColor} borderColor={borderColor} quoteType={quoteType} />
<TemplateCard theme={theme} animation={animation} layout={restLayout} font={font} fontColor={fontColor} bgColor={bgColor} borderColor={borderColor} quoteType={quoteType} quoteAuthor={quoteAuthor}/>
</Grid>
)
})
Expand Down
4 changes: 3 additions & 1 deletion frontend/src/components/organisms/TemplateCard/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@ const TemplateCard = (props) => {
const [snackbarMessage, setSnackbarMessage] = useState("");
const [isImageLoaded, setImageLoaded] = useState(false);
const originUrl = serverUrl; // Note: PORT 3004 since in server is served via that port. Frontend independently served on port 3000
const author = "Open Source";

const template = new Template();
const data = {
quote: "This is going to be the Github quote for your README",
author: "Open Source",
author: props.quoteAuthor ?? author,
};

const theme = { ...mainThemes[props.theme] };
Expand Down Expand Up @@ -78,6 +79,7 @@ const TemplateCard = (props) => {
...(props.bgColor && { bgColor: props.bgColor }),
...(props.fontColor && { fontColor: props.fontColor }),
...(isLayoutDefault && props.borderColor && { borderColor }),
...(props.quoteAuthor && { author: props.quoteAuthor }),
});
const quoteUrl = `${originUrl}/quote?${params.toString()}`;

Expand Down
39 changes: 39 additions & 0 deletions frontend/src/util/authors/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { useState, useCallback, useEffect } from "react";

import { serverUrl } from "../../components/Constants/urlConfig";

const useQuoteAuthors = () => {
const originUrl = serverUrl;
const [quoteAuthors, setQuoteAuthors] = useState([]);
const [loadingQuoteAuthors, setLoadingQuoteAuthors] = useState(false);

const fetchQuoteAuthors = useCallback(async () => {
setLoadingQuoteAuthors(true);
try {
const response = await fetch(`${originUrl}/authors`);
if (!response.ok) {
throw new Error(`HTTP error! status: ${response.status}`);
}

const data = await response.json();
if (data) {
setQuoteAuthors(data);
}
} catch (error) {
console.error("Failed to fetch quote authors:", error);
} finally {
setLoadingQuoteAuthors(false);
}
}, [originUrl]);

useEffect(() => {
fetchQuoteAuthors();
}, [fetchQuoteAuthors]);

return {
quoteAuthors,
loadingQuoteAuthors
};
}

export default useQuoteAuthors;

0 comments on commit 4d51c0f

Please sign in to comment.