Skip to content

Commit 4d51c0f

Browse files
committed
Added Author selection option to the frontend for improved quote personalization. (#131)
1 parent 6b736e6 commit 4d51c0f

3 files changed

Lines changed: 62 additions & 3 deletions

File tree

frontend/src/components/Pages/Home/index.js

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { themes, animations, layouts, fonts, colorValues, quoteTypes } from '../
55
import TextField from '@material-ui/core/TextField';
66
import Autocomplete from '@material-ui/lab/Autocomplete';
77
import ContributorsCard from '../../ContributorsCard/ContributorCard'
8+
import useQuoteAuthors from '../../../util/authors';
89
import { makeStyles } from '@material-ui/core/styles';
910

1011
const useStyles = makeStyles({
@@ -24,6 +25,9 @@ const Home = () => {
2425
const [bgColor, setBgColor] = useState(null);
2526
const [borderColor, setBorderColor] = useState(null);
2627
const [quoteType, setQuoteType] = useState("random");
28+
const [quoteAuthor, setQuoteAuthor] = useState(null);
29+
30+
const { quoteAuthors, loadingQuoteAuthors } = useQuoteAuthors();
2731

2832
const classes = useStyles();
2933

@@ -158,11 +162,25 @@ const Home = () => {
158162
/>
159163
</Grid>
160164

165+
<Grid item xs={12} sm={6} lg={3}>
166+
<Autocomplete
167+
id="author"
168+
options={quoteAuthors}
169+
value={quoteAuthor}
170+
style={{ width: 300 }}
171+
loading={loadingQuoteAuthors}
172+
onChange={(_event, newValue) => {
173+
setQuoteAuthor(newValue)
174+
}}
175+
renderInput={(params) => <TextField {...params} label="Author" placeholder="Select an author" variant="outlined" />}
176+
/>
177+
</Grid>
178+
161179
</Grid>
162180

163181
<Grid container spacing={4}>
164182
<Grid item xs={12} style={{ marginTop: '20px' }}>
165-
<TemplateCard theme={theme} animation={animation} layout={layout} font={font} fontColor={fontColor} bgColor={bgColor} borderColor={borderColor} quoteType={quoteType} />
183+
<TemplateCard theme={theme} animation={animation} layout={layout} font={font} fontColor={fontColor} bgColor={bgColor} borderColor={borderColor} quoteType={quoteType} quoteAuthor={quoteAuthor}/>
166184
</Grid>
167185
<Grid item xs={12}>
168186
<Typography align="center">Other layouts</Typography>
@@ -171,7 +189,7 @@ const Home = () => {
171189
layouts.filter((item) => item !== layout).map((restLayout) => {
172190
return (
173191
<Grid key={restLayout} item xs={12} sm={12} md={6}>
174-
<TemplateCard theme={theme} animation={animation} layout={restLayout} font={font} fontColor={fontColor} bgColor={bgColor} borderColor={borderColor} quoteType={quoteType} />
192+
<TemplateCard theme={theme} animation={animation} layout={restLayout} font={font} fontColor={fontColor} bgColor={bgColor} borderColor={borderColor} quoteType={quoteType} quoteAuthor={quoteAuthor}/>
175193
</Grid>
176194
)
177195
})

frontend/src/components/organisms/TemplateCard/index.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,12 @@ const TemplateCard = (props) => {
2121
const [snackbarMessage, setSnackbarMessage] = useState("");
2222
const [isImageLoaded, setImageLoaded] = useState(false);
2323
const originUrl = serverUrl; // Note: PORT 3004 since in server is served via that port. Frontend independently served on port 3000
24+
const author = "Open Source";
2425

2526
const template = new Template();
2627
const data = {
2728
quote: "This is going to be the Github quote for your README",
28-
author: "Open Source",
29+
author: props.quoteAuthor ?? author,
2930
};
3031

3132
const theme = { ...mainThemes[props.theme] };
@@ -78,6 +79,7 @@ const TemplateCard = (props) => {
7879
...(props.bgColor && { bgColor: props.bgColor }),
7980
...(props.fontColor && { fontColor: props.fontColor }),
8081
...(isLayoutDefault && props.borderColor && { borderColor }),
82+
...(props.quoteAuthor && { author: props.quoteAuthor }),
8183
});
8284
const quoteUrl = `${originUrl}/quote?${params.toString()}`;
8385

frontend/src/util/authors/index.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
import { useState, useCallback, useEffect } from "react";
2+
3+
import { serverUrl } from "../../components/Constants/urlConfig";
4+
5+
const useQuoteAuthors = () => {
6+
const originUrl = serverUrl;
7+
const [quoteAuthors, setQuoteAuthors] = useState([]);
8+
const [loadingQuoteAuthors, setLoadingQuoteAuthors] = useState(false);
9+
10+
const fetchQuoteAuthors = useCallback(async () => {
11+
setLoadingQuoteAuthors(true);
12+
try {
13+
const response = await fetch(`${originUrl}/authors`);
14+
if (!response.ok) {
15+
throw new Error(`HTTP error! status: ${response.status}`);
16+
}
17+
18+
const data = await response.json();
19+
if (data) {
20+
setQuoteAuthors(data);
21+
}
22+
} catch (error) {
23+
console.error("Failed to fetch quote authors:", error);
24+
} finally {
25+
setLoadingQuoteAuthors(false);
26+
}
27+
}, [originUrl]);
28+
29+
useEffect(() => {
30+
fetchQuoteAuthors();
31+
}, [fetchQuoteAuthors]);
32+
33+
return {
34+
quoteAuthors,
35+
loadingQuoteAuthors
36+
};
37+
}
38+
39+
export default useQuoteAuthors;

0 commit comments

Comments
 (0)