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. (shravan20#131)
  • Loading branch information
marceloams committed Oct 13, 2024
1 parent c2559e2 commit 024aa3d
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
23 changes: 21 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,8 @@ 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';

const Home = () => {

const [theme, setTheme] = useState(themes[0]);
Expand All @@ -14,6 +16,9 @@ const Home = () => {
const [fontColor, setFontColor] = useState(null);
const [bgColor, setBgColor] = useState(null);
const [quoteType, setQuoteType] = useState("random");
const [quoteAuthor, setQuoteAuthor] = useState(null);

const { quoteAuthors, loadingQuoteAuthors } = useQuoteAuthors();

return (
<React.Fragment>
Expand Down Expand Up @@ -126,11 +131,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={{ margin: '20px' }}>
<TemplateCard theme={theme} animation={animation} layout={layout} font={font} fontColor={fontColor} bgColor={bgColor} quoteType={quoteType} />
<TemplateCard theme={theme} animation={animation} layout={layout} font={font} fontColor={fontColor} bgColor={bgColor} quoteType={quoteType} quoteAuthor={quoteAuthor}/>
</Grid>
<Grid item xs={12}>
<Typography align="center">Other layouts</Typography>
Expand All @@ -139,7 +158,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} quoteType={quoteType} />
<TemplateCard theme={theme} animation={animation} layout={restLayout} font={font} fontColor={fontColor} bgColor={bgColor} quoteType={quoteType} quoteAuthor={quoteAuthor}/>
</Grid>
)
})
Expand Down
6 changes: 4 additions & 2 deletions 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 @@ -72,7 +73,8 @@ const TemplateCard = (props) => {
font: props.font,
quoteType: props.quoteType,
...(props.bgColor && { bgColor: props.bgColor }),
...(props.fontColor && { fontColor: props.fontColor })
...(props.fontColor && { fontColor: props.fontColor }),
...(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 024aa3d

Please sign in to comment.