|
| 1 | +import Head from "next/head"; |
| 2 | +import { |
| 3 | + Flex, |
| 4 | + Text, |
| 5 | + Heading, |
| 6 | + Select, |
| 7 | + Spinner, |
| 8 | + Box, |
| 9 | + Button, |
| 10 | +} from "@chakra-ui/react"; |
| 11 | +import axios from "axios"; |
| 12 | +import { useRef, useState } from "react"; |
| 13 | +import { useIdleTimer } from "react-idle-timer"; |
| 14 | + |
| 15 | +export default function Home({ category, quote, start }) { |
| 16 | + const cat = useRef(); |
| 17 | + const [author, setAuthor] = useState(quote.author); |
| 18 | + const [quotes, setQuotes] = useState(quote.content); |
| 19 | + const [val, setVal] = useState(false); |
| 20 | + const [bgImage, setBgImage] = useState("white"); |
| 21 | + const [fontCol, setFontCol] = useState("black"); |
| 22 | + const { reset } = useIdleTimer({ |
| 23 | + timeout: 1000 * 60 * 3, |
| 24 | + onIdle: handleOnIdle, |
| 25 | + }); |
| 26 | + |
| 27 | + function handleOnIdle() { |
| 28 | + cat.current.value = |
| 29 | + category[Math.floor(Math.random() * category.length)]; |
| 30 | + genQuote(); |
| 31 | + reset(); |
| 32 | + } |
| 33 | + |
| 34 | + async function genQuote() { |
| 35 | + const response = await axios.get( |
| 36 | + `https://api.quotable.io/random?tags=${cat.current.value}` |
| 37 | + ); |
| 38 | + setAuthor(response.data.author); |
| 39 | + setQuotes(response.data.content); |
| 40 | + let x = Math.floor(Math.random() * 256); |
| 41 | + let y = Math.floor(Math.random() * 256); |
| 42 | + let z = Math.floor(Math.random() * 256); |
| 43 | + let bgColor = "rgb(" + x + "," + y + "," + z + ")"; |
| 44 | + setBgImage(bgColor); |
| 45 | + |
| 46 | + let luminance = (0.299 * x + 0.587 * y + 0.114 * z) / 255; |
| 47 | + let d = 0; |
| 48 | + if (luminance > 0.5) d = 0; |
| 49 | + // bright colors - black font |
| 50 | + else d = 255; // dark colors - white font |
| 51 | + |
| 52 | + setFontCol(`rgb(${d},${d},${d})`); |
| 53 | + |
| 54 | + setVal(!val); |
| 55 | + } |
| 56 | + |
| 57 | + return ( |
| 58 | + <> |
| 59 | + <Head> |
| 60 | + <title>Random Quote Generator</title> |
| 61 | + <meta |
| 62 | + name="description" |
| 63 | + content="Generated by create next app" |
| 64 | + /> |
| 65 | + <link rel="preconnect" href="https://fonts.googleapis.com" /> |
| 66 | + <link |
| 67 | + rel="preconnect" |
| 68 | + href="https://fonts.gstatic.com" |
| 69 | + crossOrigin="true" |
| 70 | + /> |
| 71 | + <link |
| 72 | + href="https://fonts.googleapis.com/css2?family=Prompt:wght@300&display=swap" |
| 73 | + rel="stylesheet" |
| 74 | + /> |
| 75 | + </Head> |
| 76 | + <Box minH="100vh" w="100vw" zIndex="-1" position="fixed"></Box> |
| 77 | + <Flex |
| 78 | + flexDir="column" |
| 79 | + alignItems="center" |
| 80 | + justifyContent="center" |
| 81 | + w="100vw" |
| 82 | + bg={bgImage} |
| 83 | + minH="100vh" |
| 84 | + > |
| 85 | + <Heading my="3rem" fontSize="2rem" color={fontCol}> |
| 86 | + Random Quote Generator |
| 87 | + </Heading> |
| 88 | + <Flex |
| 89 | + justifyContent="center" |
| 90 | + bg="purple" |
| 91 | + w="100vw" |
| 92 | + mb="3rem" |
| 93 | + p="1rem" |
| 94 | + > |
| 95 | + <Flex |
| 96 | + alignItems="center" |
| 97 | + justifyContent="space-around" |
| 98 | + w="30%" |
| 99 | + minW="200px" |
| 100 | + > |
| 101 | + <Text as="strong" color="white"> |
| 102 | + Select a category : |
| 103 | + </Text> |
| 104 | + <Select |
| 105 | + maxW="250px" |
| 106 | + textTransform="capitalize" |
| 107 | + ref={cat} |
| 108 | + defaultValue={start} |
| 109 | + variant="outline" |
| 110 | + color="black" |
| 111 | + bgColor="white" |
| 112 | + > |
| 113 | + {category && |
| 114 | + category.map((item) => ( |
| 115 | + <option value={item} key={item}> |
| 116 | + {item} |
| 117 | + </option> |
| 118 | + ))} |
| 119 | + {!category && <Spinner size="lg" />} |
| 120 | + </Select> |
| 121 | + </Flex> |
| 122 | + </Flex> |
| 123 | + <Box |
| 124 | + w="50%" |
| 125 | + minW="250px" |
| 126 | + bg="gray.100" |
| 127 | + boxShadow="inset 2px 2px 8px gray" |
| 128 | + borderRadius="lg" |
| 129 | + p="3rem" |
| 130 | + fontSize="1.8rem" |
| 131 | + fontFamily="Prompt" |
| 132 | + > |
| 133 | + <Text as="blockquote" fontWeight="semibold"> |
| 134 | + {quotes} |
| 135 | + </Text> |
| 136 | + <Text as="i" d="block" textAlign="right" mt="2rem"> |
| 137 | + ~ {author} |
| 138 | + </Text> |
| 139 | + </Box> |
| 140 | + <Button onClick={genQuote} colorScheme="facebook" mt="3rem"> |
| 141 | + Get New Quote |
| 142 | + </Button> |
| 143 | + </Flex> |
| 144 | + </> |
| 145 | + ); |
| 146 | +} |
| 147 | + |
| 148 | +export async function getStaticProps() { |
| 149 | + const resp = await axios.get("https://api.quotable.io/tags"); |
| 150 | + const categories = resp.data.map((each) => each.name); |
| 151 | + |
| 152 | + let tempCat = categories[Math.floor(Math.random() * categories.length)]; |
| 153 | + if (tempCat === "proverb") tempCat = "business"; |
| 154 | + |
| 155 | + const quoterR = await axios.get( |
| 156 | + "https://api.quotable.io/random?tags=" + tempCat |
| 157 | + ); |
| 158 | + |
| 159 | + return { |
| 160 | + props: { |
| 161 | + category: categories, |
| 162 | + start: tempCat, |
| 163 | + quote: quoterR.data, |
| 164 | + }, |
| 165 | + }; |
| 166 | +} |
0 commit comments