From 4d51c0f725460ccbe78062a6b791502d83e0e8bf Mon Sep 17 00:00:00 2001 From: Marcelo Amorim Date: Sun, 13 Oct 2024 19:16:26 -0300 Subject: [PATCH] Added Author selection option to the frontend for improved quote personalization. (#131) --- frontend/src/components/Pages/Home/index.js | 22 ++++++++++- .../organisms/TemplateCard/index.js | 4 +- frontend/src/util/authors/index.js | 39 +++++++++++++++++++ 3 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 frontend/src/util/authors/index.js diff --git a/frontend/src/components/Pages/Home/index.js b/frontend/src/components/Pages/Home/index.js index 890a0aa..0b198ac 100644 --- a/frontend/src/components/Pages/Home/index.js +++ b/frontend/src/components/Pages/Home/index.js @@ -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({ @@ -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(); @@ -158,11 +162,25 @@ const Home = () => { /> + + { + setQuoteAuthor(newValue) + }} + renderInput={(params) => } + /> + + - + Other layouts @@ -171,7 +189,7 @@ const Home = () => { layouts.filter((item) => item !== layout).map((restLayout) => { return ( - + ) }) diff --git a/frontend/src/components/organisms/TemplateCard/index.js b/frontend/src/components/organisms/TemplateCard/index.js index 6e2fe0a..12c5171 100644 --- a/frontend/src/components/organisms/TemplateCard/index.js +++ b/frontend/src/components/organisms/TemplateCard/index.js @@ -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] }; @@ -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()}`; diff --git a/frontend/src/util/authors/index.js b/frontend/src/util/authors/index.js new file mode 100644 index 0000000..23e472f --- /dev/null +++ b/frontend/src/util/authors/index.js @@ -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; \ No newline at end of file