(undefined) 23 | const [id, setId] = useState(10000) 24 | 25 | 26 | useEffect(() => { 27 | 28 | const getQuote = async () => { 29 | const res = await fetch( 30 | `/api/randomquote/${id}`, { cache: 'no-store' } 31 | ) 32 | const json = await res.json() 33 | setQuote(json) 34 | } 35 | 36 | getQuote() 37 | }, [id]) 38 | 39 | if (!quote) returnLoading...
40 | 41 | return ( 42 |43 | 44 | 67 | ) 68 | } -------------------------------------------------------------------------------- /src/app/favicon.ico: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/gitdagray/rqm-refactor/b1d44185db99eca81eedf98bccee69447554ae8e/src/app/favicon.ico -------------------------------------------------------------------------------- /src/app/globals.css: -------------------------------------------------------------------------------- 1 | @tailwind base; 2 | @tailwind components; 3 | @tailwind utilities; 4 | 5 | :root { 6 | --foreground-rgb: 0, 0, 0; 7 | --background-start-rgb: 214, 219, 220; 8 | --background-end-rgb: 255, 255, 255; 9 | } 10 | 11 | @media (prefers-color-scheme: dark) { 12 | :root { 13 | --foreground-rgb: 255, 255, 255; 14 | --background-start-rgb: 0, 0, 0; 15 | --background-end-rgb: 0, 0, 0; 16 | } 17 | } 18 | 19 | body { 20 | color: rgb(var(--foreground-rgb)); 21 | background: linear-gradient( 22 | to bottom, 23 | transparent, 24 | rgb(var(--background-end-rgb)) 25 | ) 26 | rgb(var(--background-start-rgb)); 27 | } 28 | -------------------------------------------------------------------------------- /src/app/layout.tsx: -------------------------------------------------------------------------------- 1 | import './globals.css' 2 | import { Inter } from 'next/font/google' 3 | 4 | const inter = Inter({ subsets: ['latin'] }) 5 | 6 | export const metadata = { 7 | title: 'Random Quote Machine', 8 | description: 'This page generates a random quote.', 9 | } 10 | 11 | 12 | export default function RootLayout({ 13 | children, 14 | }: { 15 | children: React.ReactNode 16 | }) { 17 | return ( 18 | 19 | 20 |45 | 56 | 57 |60 | 61 |Author: {quote.author}
58 |Category: {quote.category}
59 |62 |66 |{quote.quote}
65 |21 | {children} 22 | 23 | 24 | 25 | ) 26 | } 27 | -------------------------------------------------------------------------------- /src/app/page.tsx: -------------------------------------------------------------------------------- 1 | import Quote from './components/Quote' 2 | 3 | export default async function Home() { 4 | 5 | return6 | 7 | } 8 | -------------------------------------------------------------------------------- /src/db/config.ts: -------------------------------------------------------------------------------- 1 | export const config = { 2 | host: process.env.DATABASE_HOST, 3 | username: process.env.DATABASE_USERNAME, 4 | password: process.env.DATABASE_PASSWORD 5 | } 6 | -------------------------------------------------------------------------------- /src/db/schema.ts: -------------------------------------------------------------------------------- 1 | import { mysqlTable, mysqlSchema, AnyMySqlColumn, int, varchar, index, text } from "drizzle-orm/mysql-core" 2 | import { sql } from "drizzle-orm" 3 | 4 | 5 | export const authors = mysqlTable("authors", { 6 | id: int("id").autoincrement().primaryKey().notNull(), 7 | author: varchar("author", { length: 255 }).notNull(), 8 | }); 9 | 10 | export const categories = mysqlTable("categories", { 11 | id: int("id").autoincrement().primaryKey().notNull(), 12 | category: varchar("category", { length: 255 }).notNull(), 13 | }); 14 | 15 | export const quotes = mysqlTable("quotes", { 16 | id: int("id").autoincrement().primaryKey().notNull(), 17 | authorId: int("author_id").notNull(), 18 | categoryId: int("category_id").notNull(), 19 | quote: text("quote").notNull(), 20 | }, 21 | (table) => { 22 | return { 23 | authorIdIdx: index("author_id_idx").on(table.authorId), 24 | categoryIdIdx: index("category_id_idx").on(table.categoryId), 25 | } 26 | }); -------------------------------------------------------------------------------- /src/lib/getAllQuotes.ts: -------------------------------------------------------------------------------- 1 | import { connect } from '@planetscale/database' 2 | import { config } from '@/db/config' 3 | 4 | import { drizzle } from "drizzle-orm/planetscale-serverless"; 5 | import { quotes, authors, categories } from "@/db/schema" 6 | import { eq } from 'drizzle-orm' 7 | 8 | export default async function getAllQuotes(): Promise{ 9 | 10 | const conn = connect(config) 11 | const db = drizzle(conn) 12 | 13 | const results: Quote[] = await db.select({ 14 | id: quotes.id, 15 | quote: quotes.quote, 16 | author: authors.author, 17 | category: categories.category, 18 | }) 19 | .from(quotes) 20 | .innerJoin(authors, eq(quotes.authorId, authors.id)) 21 | .innerJoin(categories, eq(quotes.categoryId, categories.id)) 22 | 23 | 24 | return results 25 | } -------------------------------------------------------------------------------- /src/lib/getRandomQuote.ts: -------------------------------------------------------------------------------- 1 | 2 | import getAllQuotes from './getAllQuotes' 3 | 4 | export default async function getRandomQuote(id: string): Promise{ 5 | 6 | const prevQuoteId = parseInt(id) 7 | 8 | const results: Quote[] = await getAllQuotes() 9 | 10 | const ids: number[] = results.map(q => q.id) 11 | 12 | let randomId = prevQuoteId 13 | 14 | while (randomId === prevQuoteId) { 15 | const randomIndex = Math.floor(Math.random() * ids.length) 16 | randomId = ids[randomIndex] 17 | } 18 | 19 | const newQuote = results.find(q => q.id === randomId) as Quote 20 | 21 | return newQuote 22 | 23 | } -------------------------------------------------------------------------------- /tailwind.config.js: -------------------------------------------------------------------------------- 1 | /** @type {import('tailwindcss').Config} */ 2 | module.exports = { 3 | content: [ 4 | './src/pages/**/*.{js,ts,jsx,tsx,mdx}', 5 | './src/components/**/*.{js,ts,jsx,tsx,mdx}', 6 | './src/app/**/*.{js,ts,jsx,tsx,mdx}', 7 | ], 8 | theme: { 9 | extend: { 10 | backgroundImage: { 11 | 'gradient-radial': 'radial-gradient(var(--tw-gradient-stops))', 12 | 'gradient-conic': 13 | 'conic-gradient(from 180deg at 50% 50%, var(--tw-gradient-stops))', 14 | }, 15 | }, 16 | }, 17 | plugins: [], 18 | } 19 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "lib": ["dom", "dom.iterable", "esnext"], 5 | "allowJs": true, 6 | "skipLibCheck": true, 7 | "strict": true, 8 | "forceConsistentCasingInFileNames": true, 9 | "noEmit": true, 10 | "esModuleInterop": true, 11 | "module": "esnext", 12 | "moduleResolution": "node", 13 | "resolveJsonModule": true, 14 | "isolatedModules": true, 15 | "jsx": "preserve", 16 | "incremental": true, 17 | "plugins": [ 18 | { 19 | "name": "next" 20 | } 21 | ], 22 | "paths": { 23 | "@/*": ["./src/*"] 24 | } 25 | }, 26 | "include": ["next-env.d.ts", "**/*.ts", "**/*.tsx", ".next/types/**/*.ts"], 27 | "exclude": ["node_modules"] 28 | } 29 | -------------------------------------------------------------------------------- /types.d.ts: -------------------------------------------------------------------------------- 1 | 2 | type Quote = { 3 | id: number, 4 | quote: string, 5 | author: string, 6 | category: string, 7 | } 8 | --------------------------------------------------------------------------------