diff --git a/frontend/src/components/Pages/Home/index.js b/frontend/src/components/Pages/Home/index.js index 0d71bfe..dbf0fbb 100644 --- a/frontend/src/components/Pages/Home/index.js +++ b/frontend/src/components/Pages/Home/index.js @@ -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]); @@ -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 ( @@ -126,11 +131,25 @@ const Home = () => { /> + + { + setQuoteAuthor(newValue) + }} + renderInput={(params) => } + /> + + - + Other layouts @@ -139,7 +158,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 f20d33d..176d726 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] }; @@ -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()}`; 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