Skip to content

Commit bcff797

Browse files
committed
Added Author selection option to the frontend for improved quote personalization. (#131)
1 parent c2559e2 commit bcff797

File tree

3 files changed

+54
-4
lines changed

3 files changed

+54
-4
lines changed

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ 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';
9+
810
const Home = () => {
911

1012
const [theme, setTheme] = useState(themes[0]);
@@ -14,6 +16,9 @@ const Home = () => {
1416
const [fontColor, setFontColor] = useState(null);
1517
const [bgColor, setBgColor] = useState(null);
1618
const [quoteType, setQuoteType] = useState("random");
19+
const [quoteAuthor, setQuoteAuthor] = useState(null);
20+
21+
const { quoteAuthors } = useQuoteAuthors();
1722

1823
return (
1924
<React.Fragment>
@@ -126,11 +131,24 @@ const Home = () => {
126131
/>
127132
</Grid>
128133

134+
<Grid item xs={12} sm={6} lg={3}>
135+
<Autocomplete
136+
id="author"
137+
options={quoteAuthors}
138+
value={quoteAuthor}
139+
style={{ width: 300 }}
140+
onChange={(_event, newValue) => {
141+
setQuoteAuthor(newValue)
142+
}}
143+
renderInput={(params) => <TextField {...params} label="Author" placeholder="Select an author" variant="outlined" />}
144+
/>
145+
</Grid>
146+
129147
</Grid>
130148

131149
<Grid container spacing={4}>
132150
<Grid item xs={12} style={{ margin: '20px' }}>
133-
<TemplateCard theme={theme} animation={animation} layout={layout} font={font} fontColor={fontColor} bgColor={bgColor} quoteType={quoteType} />
151+
<TemplateCard theme={theme} animation={animation} layout={layout} font={font} fontColor={fontColor} bgColor={bgColor} quoteType={quoteType} quoteAuthor={quoteAuthor}/>
134152
</Grid>
135153
<Grid item xs={12}>
136154
<Typography align="center">Other layouts</Typography>
@@ -139,7 +157,7 @@ const Home = () => {
139157
layouts.filter((item) => item !== layout).map((restLayout) => {
140158
return (
141159
<Grid key={restLayout} item xs={12} sm={12} md={6}>
142-
<TemplateCard theme={theme} animation={animation} layout={restLayout} font={font} fontColor={fontColor} bgColor={bgColor} quoteType={quoteType} />
160+
<TemplateCard theme={theme} animation={animation} layout={restLayout} font={font} fontColor={fontColor} bgColor={bgColor} quoteType={quoteType} quoteAuthor={quoteAuthor}/>
143161
</Grid>
144162
)
145163
})

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

Lines changed: 4 additions & 2 deletions
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] };
@@ -72,7 +73,8 @@ const TemplateCard = (props) => {
7273
font: props.font,
7374
quoteType: props.quoteType,
7475
...(props.bgColor && { bgColor: props.bgColor }),
75-
...(props.fontColor && { fontColor: props.fontColor })
76+
...(props.fontColor && { fontColor: props.fontColor }),
77+
...(props.quoteAuthor && { author: props.quoteAuthor })
7678
});
7779
const quoteUrl = `${originUrl}/quote?${params.toString()}`;
7880

frontend/src/util/authors/index.js

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

0 commit comments

Comments
 (0)