Skip to content

Commit

Permalink
Data files refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
KiraLT committed Oct 31, 2023
1 parent e04cb34 commit fd56b53
Show file tree
Hide file tree
Showing 11 changed files with 80 additions and 195 deletions.
7 changes: 4 additions & 3 deletions src/app/list/content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { Recipes } from '@/components/recipes'
import { useRouter, useSearchParams } from 'next/navigation'
import {
getListFromUrlQuery,
getListRecipes,
getListUrl,
replaceList,
} from '@/controllers/lists'
import { getRecipesWithMetaByIds } from '@/controllers/recipes'
import { getAllRecipes } from '@/controllers/recipes'
import { Share } from '@/components/share'
import { Alert, Modal, Button } from 'react-daisyui'
import { EditList } from '@/app/list/edit-list'
Expand All @@ -25,8 +26,8 @@ export default function Content(): JSX.Element {
}, [searchParams])

const recipes = useMemo(() => {
return getRecipesWithMetaByIds(list.recipes)
}, [list.recipes])
return getListRecipes(list)
}, [list])

return (
<>
Expand Down
39 changes: 19 additions & 20 deletions src/app/list/edit-list.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { List } from '@/controllers/lists'
import { List, getListRecipes } from '@/controllers/lists'
import {
RecipeWithMeta,
getAllRecipesWithMeta,
getRecipesWithMetaByIds,
Recipe,
getAllRecipes,
} from '@/controllers/recipes'
import Fuse from 'fuse.js'
import { useState, useMemo } from 'react'
Expand All @@ -20,12 +19,12 @@ export function EditList({
const [list, setList] = useState(originalList)
const [addMode, setAddMode] = useState(false)

const allRecipes = useMemo(() => getAllRecipesWithMeta(), [])
const allRecipes = useMemo(() => getAllRecipes(), [])
const listRecipes = useMemo(() => {
return getRecipesWithMetaByIds(list.recipes)
}, [list.recipes])
return getListRecipes(list)
}, [list])
const nonListRecipes = useMemo(() => {
return allRecipes.filter((v) => !list.recipes.includes(v.meta.id))
return allRecipes.filter((v) => !list.recipes.includes(v.slug))
}, [allRecipes, list.recipes])

return (
Expand Down Expand Up @@ -77,10 +76,10 @@ export function EditList({
recipes: list.recipes.filter((v) => v !== id),
})
}}
onReorder={(ids) => {
onReorder={(recipes) => {
setList({
...list,
recipes: ids,
recipes,
})
}}
/>
Expand All @@ -104,8 +103,8 @@ function AddView({
recipes,
onAdd,
}: {
recipes: RecipeWithMeta[]
onAdd(id: number): void
recipes: Recipe[]
onAdd(slug: string): void
}): JSX.Element {
const [query, setQuery] = useState('')

Expand Down Expand Up @@ -134,9 +133,9 @@ function AddView({
<ul className="text-sm font-medium divide-y border-primary-content">
{result.map((v) => (
<li
key={v.meta.id}
key={v.slug}
className="w-full px-2 py-4"
onClick={() => onAdd(v.meta.id)}
onClick={() => onAdd(v.slug)}
style={{ cursor: 'pointer' }}
>
{v.data.name}
Expand All @@ -156,9 +155,9 @@ function EditView({
onRemove,
onReorder,
}: {
recipes: RecipeWithMeta[]
onReorder(recipes: number[]): void
onRemove(id: number): void
recipes: Recipe[]
onReorder(recipes: string[]): void
onRemove(slug: string): void
}): JSX.Element {
return (
<>
Expand All @@ -167,7 +166,7 @@ function EditView({
<ReactSortable
tag="li"
list={recipes.map((v) => ({
id: v.meta.id,
id: v.slug,
}))}
setList={(state) => {
onReorder(state.map((v) => v.id))
Expand All @@ -176,7 +175,7 @@ function EditView({
>
{recipes.map((v) => (
<div
key={v.meta.id}
key={v.slug}
className="flex justify-between items-center my-2"
>
<div className="handle cursor-move mx-4 text-lg">
Expand All @@ -188,7 +187,7 @@ function EditView({
color="error"
variant="outline"
onClick={() => {
onRemove(v.meta.id)
onRemove(v.slug)
}}
>
Delete
Expand Down
2 changes: 1 addition & 1 deletion src/app/lists/content.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default function Content(): JSX.Element {
<li className="flex justify-between items-center my-2 border-primary-content">
<div>
<Link href={getListUrl(v)}>
<span className="text-truncate">{v.name}</span>{' '}
<span className="text-truncate">{v.name || 'Unnamed list'}</span>{' '}
</Link>
<small className="text-muted">
({v.recipes.length} recipes)
Expand Down
4 changes: 2 additions & 2 deletions src/app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import type { Metadata } from 'next/types'
import { useMemo } from 'react'

import { Recipes } from '../components/recipes'
import { getAllRecipesWithMeta } from '../controllers/recipes'
import { getAllRecipes } from '../controllers/recipes'

export const metadata: Metadata = {
title: 'Signature Cocktails',
Expand All @@ -11,7 +11,7 @@ export const metadata: Metadata = {
}

export default function Page(): JSX.Element {
const recipes = useMemo(() => getAllRecipesWithMeta(), [])
const recipes = useMemo(() => getAllRecipes(), [])

return (
<>
Expand Down
4 changes: 2 additions & 2 deletions src/app/recipes/[slug]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useMemo } from 'react'
import {
Recipe,
formatQuantity,
getAllRecipeSlugs,
getAllRecipes,
getRecipeBySlug,
} from '@/controllers/recipes'
import { Metadata } from 'next/types'
Expand All @@ -18,7 +18,7 @@ export interface Props {
}

export async function generateStaticParams(): Promise<Props['params'][]> {
return getAllRecipeSlugs().map((slug) => ({ slug }))
return getAllRecipes().map((recipe) => ({ slug: recipe.slug }))
}

export async function generateMetadata({
Expand Down
4 changes: 2 additions & 2 deletions src/app/search/content.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
'use client'
import { Recipes } from '@/components/recipes'
import { getAllRecipesWithMeta } from '@/controllers/recipes'
import { getAllRecipes } from '@/controllers/recipes'
import Fuse from 'fuse.js'
import { useMemo } from 'react'
import { useSearchParams } from 'next/navigation'

export default function Content(): JSX.Element {
const searchParams = useSearchParams()
const recipes = useMemo(() => getAllRecipesWithMeta(), [])
const recipes = useMemo(() => getAllRecipes(), [])
const fuse = useMemo(() => {
return new Fuse(recipes, {
keys: ['data.name'],
Expand Down
8 changes: 4 additions & 4 deletions src/components/recipes.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { RecipeWithMeta } from '@/controllers/recipes'
import { Recipe } from '@/controllers/recipes'
import Image from 'next-image-export-optimizer'
import Link from 'next/link'

export function Recipes({
recipes,
}: {
recipes: RecipeWithMeta[]
recipes: Recipe[]
}): JSX.Element {
return (
<>
Expand All @@ -17,8 +17,8 @@ export function Recipes({
<div className="grid grid-cols-1 md:grid-cols-3 gap-4">
{recipes.map((recipe) => (
<Link
key={recipe.meta.id}
href={`/recipes/${recipe.meta.slug}`}
key={recipe.slug}
href={`/recipes/${recipe.slug}`}
className="bg-base-200 rounded-md"
>
<Image
Expand Down
10 changes: 7 additions & 3 deletions src/controllers/lists/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { deduplicateBy, deduplicate } from 'common-stuff'
import { Recipe } from '../recipes'
import { recipes } from '../recipes/data'

export interface List {
name: string
recipes: number[]
recipes: string[]
}

const localStorageKey = 'my-lists'
Expand Down Expand Up @@ -43,8 +45,6 @@ export function getListFromUrlQuery(
typeof query.r === 'string'
? query.r
.split(' ')
.map((v) => parseInt(v, 10))
.filter((v) => !isNaN(v))
: []

return {
Expand All @@ -59,3 +59,7 @@ export function replaceList(oldList: List, newList: List): void {
newList,
])
}

export function getListRecipes(list: List): Recipe[] {
return list.recipes.flatMap((recipeId) => recipes.filter((v) => v.slug === recipeId))
}
Loading

0 comments on commit fd56b53

Please sign in to comment.