Skip to content

Commit 90b5517

Browse files
Merge pull request #20 from federicofrasca796/feat/update-menu
Update menu and fetch data from parsed CSV
2 parents c9f34be + 19367cf commit 90b5517

File tree

6 files changed

+146
-117
lines changed

6 files changed

+146
-117
lines changed

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "crema-cioccolato-bt",
3-
"version": "2.2.0",
3+
"version": "2.3.0",
44
"private": true,
55
"author": {
66
"name": "Federico Frascà",

public/items.csv

Lines changed: 40 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,46 +8,53 @@ Coppetta gelato grande ,No servizio al tavolo ,"€ 3,50",,,,Si,Gelati
88
Cialda Croccante ,No servizio al tavolo ,"€ 3,50",,,,Si,Gelati
99
Panna ,,"€ 0,50",,,,Si,
1010
,,,,,,,
11-
Coppa gelato media,,"€ 4,00",,,,Si,Gelati Maxi
12-
Coppa gelato grande ,,"€ 6,00",,,,Si,Gelati Maxi
13-
Frappè ,,"€ 3,50",,,,Si,Gelati Maxi
14-
Brioches farcita gelato ,,"€ 3,50",,,,Si,Gelati Maxi
15-
Crepes farcita con gelato ,,"€ 5,00",,,,Si,Gelati Maxi
16-
Cornetto Farcito con gelato ,,"€ 3,00",,,,Si,Gelati Maxi
11+
Coppa gelato media,,"€ 4,00",,,,Si,Coppe e farciture gelato
12+
Coppa gelato grande ,,"€ 6,00",,,,Si,Coppe e farciture gelato
13+
Frappè ,,"€ 3,50",,,,Si,Coppe e farciture gelato
14+
Brioches farcita gelato ,,"€ 3,50",,,,Si,Coppe e farciture gelato
15+
Crepes farcita con gelato ,,"€ 5,00",,,,Si,Coppe e farciture gelato
16+
Cornetto Farcito con gelato ,,"€ 3,00",,,,Si,Coppe e farciture gelato
1717
Panna ,,"€ 1,00",,,,Si,
1818
,,,,,,,
19-
Caffè ,,"€ 1,00",,,,Si,Caffe'
20-
Caffè decaffeinato ,,"€ 1,20",,,,Si,Caffe'
21-
Caffè al ghiaccio ,,"€ 1,20",,,,Si,Caffe'
22-
Caffè corretto ,,"€ 1,50",,,,Si,Caffe'
23-
Caffè leccese,,"€ 2,00",,,,Si,Caffe'
24-
Espressino,,"€ 1,20",,,,Si,Caffe'
19+
Caffè ,,"€ 1,20",,,,Si,Caffe'
20+
Caffè decaffeinato ,,"€ 1,40",,,,Si,Caffe'
21+
Caffè al ghiaccio ,,"€ 1,50",,,,Si,Caffe'
22+
Caffè freddo ,,"€ 2,00",,,,Si,Caffe'
23+
Caffè corretto nazionale,,"€ 2,00",,,,Si,Caffe'
24+
Caffè leccese,,"€ 2,50",,,,Si,Caffe'
25+
Espressino,,"€ 1,40",,,,Si,Caffe'
2526
Espressino decaffeinato ,,"€ 1,40",,,,Si,Caffe'
26-
Espressino senza lattosio ,,"€ 1,40",,,,Si,Caffe'
27-
Espressino di soia ,,"1,50",,,,Si,Caffe'
28-
Ginseng ,,"€ 1,20",,,,Si,Caffe'
27+
Espressino senza lattosio ,,"€ 1,60",,,,Si,Caffe'
28+
Espressino freddo sconvolto ,,"3,00",,,,Si,Caffe'
29+
Ginseng ,,"€ 1,40",,,,Si,Caffe'
2930
Caffè d'orzo,,"€ 1,50",,,,Si,Caffe'
3031
Cappuccino ,,"€ 1,50",,,,Si,Caffe'
31-
Cappuccino senza lattosio ,,"€ 1,70",,,,Si,Caffe'
32-
Cappuccino di soia,,"€ 1,80",,,,Si,Caffe'
33-
Caffè freddo ,,"€ 1,50",,,,Si,Caffe'
32+
Cappuccino senza lattosio ,,"€ 1,90",,,,Si,Caffe'
33+
Cappuccino ginseng,,"€ 2,00",,,,Si,Caffe'
34+
Latte e menta,,"€ 2,00",,,,Si,Caffe'
35+
Latte bianco ,,"€ 1,20",,,,Si,Caffe'
36+
Latte bianco senza lattosio ,,"€ 1,50",,,,Si,Caffe'
37+
Latte macchiato,,"€ 1,40",,,,Si,Caffe'
38+
Cioccolata calda ,,"€ 3,00",,,,Si,Caffe'
39+
Tisane,,"€ 2,50",,,,Si,Caffe'
40+
The caldo ,,"€ 2,00",,,,Si,Caffe'
41+
,,,,,,,
42+
,,,,,,,
43+
,,,,,,,
44+
,,,,,,,
3445
,,,,,,,
35-
Latte e menta,,"€ 2,00",,,,Si,Latte e cioccolato
36-
Latte bianco ,,"€ 1,20",,,,Si,Latte e cioccolato
37-
Latte bianco senza lattosio ,,"€ 1,50",,,,Si,Latte e cioccolato
38-
Latte macchiato,,"€ 1,20",,,,Si,Latte e cioccolato
39-
Cioccolata calda ,,"€ 3,00",,,,Si,Latte e cioccolato
4046
,,,,,,,
4147
Ciambella ,,"€ 1,50",,,,Si,Dolci e Paste
4248
Muffin Nutella ,,"€ 2,50",,,,Si,Dolci e Paste
4349
Biscotti,Gusti assortiti,"€ 1,00",,,,Si,Dolci e Paste
4450
Crepes ,,"€ 3,00",,,,Si,Dolci e Paste
4551
Pancakes,,"€ 4,00",,,,Si,Dolci e Paste
46-
Cornetto,,"€ 1,20",,,,Si,Dolci e Paste
52+
Cornetto,Vuoto o farcito,"€ 1,20",,,,Si,Dolci e Paste
4753
Cornetto vegano,,"€ 1,30",,,,Si,Dolci e Paste
4854
Cornetto ace,,"€ 1,50",,,,Si,Dolci e Paste
49-
Treccia,,"€ 1,50",,,,Si,Dolci e Paste
55+
Treccia,Con noci pecan,"€ 1,50",,,,Si,Dolci e Paste
5056
Conchiglia ,,"€ 1,50",,,,Si,Dolci e Paste
57+
Krapfen,,"€ 1,20",,,,Si,Dolci e Paste
5158
Creme assortite,,"€ 0,50",,,,Si,
5259
Smarties,,"€ 0,50",,,,Si,
5360
Granella nocciola,,"€ 0,50",,,,Si,
@@ -72,22 +79,23 @@ Succhi di frutta ,Gusti assortiti,"€ 2,00",,,,Si,Bibite
7279
Succo di frutti rossi,,"€ 2,50",,,,Si,Bibite
7380
Acqua,,"€ 1,00",,,,Si,Bibite
7481
Spremuta di arancia,,"€ 3,00",,,,Si,Bibite
75-
Tisane,,"€ 2,00",,,,Si,Bibite
76-
The caldo ,,"€ 2,00",,,,Si,Bibite
82+
,,,,,,,
83+
,,,,,,,
7784
,,,,,,,
7885
Nastro Azzurro,,"€ 2,00",,,,Si,Birre
86+
Heineken,,"€ 2,50",,,,Si,Birre
7987
Beck's,,"€ 2,50",,,,Si,Birre
8088
Peroni al limone ,,"€ 2,00",,,,Si,Birre
81-
Menabrea,,"€ 3,00",,,,Si,Birre
89+
Alambra,,"€ 3,00",,,,Si,Birre
8290
Corona ,,"€ 3,00",,,,Si,Birre
8391
Bjorne,,"€ 3,00",,,,Si,Birre
8492
Tourtel analcolica,,"€ 2,00",,,,Si,Birre
8593
,,,,,,,
86-
Kraken,,"€ 5,00",,,,Si,Rum
94+
Diplomatico Mantuano,,"€ 5,00",,,,Si,Rum
8795
Don Papa Baroko,,"€ 5,00",,,,Si,Rum
8896
Don Papa Masskara,,"€ 7,00",,,,Si,Rum
8997
Diplomatico riserva ,,"€ 7,00",,,,Si,Rum
90-
Havana club especial ,,"2,50",,,,Si,Rum
98+
Havana club especial ,,"3,00",,,,Si,Rum
9199
,,,,,,,
92100
903 bianca,,"€ 3,00",,,,Si,Grappe
93101
903 barricata,,"€ 3,50",,,,Si,Grappe
@@ -133,8 +141,8 @@ Pinsa,,"€ 8,00",,,,Si,Snacks
133141
Coni fritti,2 pezzi,"€ 5,00",,,,Si,Snacks
134142
Bruschette,4 pezzi,"€ 3,50",,,,Si,Snacks
135143
,,,,,,,
136-
Aperitivo grande,"Tris, Sandwich, Mini Hamburger, Piadina, Tramezzino, Bruschetta, Wurstel, 4pz Fritti ","€ 8,00",,,,Si,Taglieri
137-
Aperitivo analcolico,"Tris, Sandwich, Mini Hamburger, Tramezzino, Bruschetta, Wurstel + Bevanda analcolica (chiedere allo staff)","€ 8,00",,,,Si,Taglieri
144+
Aperitivo grande,"Tris, Sandwich, Mini Hamburger, Piadina, Tramezzino, Bruschetta, Wurstel, 4pz Fritti ","€ 8,00",,,,Si,Taglieri e panini
145+
Aperitivo analcolico,"Tris, Sandwich, Mini Hamburger, Tramezzino, Bruschetta, Wurstel + Bevanda analcolica (chiedere allo staff)","€ 8,00",,,,Si,Taglieri e panini
138146
,,,,,,,
139147
,,,,,,,
140148
,,,,,,,

src/app/menu/[[...topic]]/page.tsx

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export function generateStaticParams() {
99
return params;
1010
}
1111

12-
export default function MenuByTopic({
12+
export default async function MenuByTopic({
1313
params
1414
}: {
1515
// TODO: fix topic type, this is not typesafe as I can change it to string, while for nextjs it's an array
@@ -18,9 +18,13 @@ export default function MenuByTopic({
1818
const { topic } = params;
1919
if (topic?.length > 1) throw new Error(`Questa non è una sezione valida`);
2020

21+
const items = await (
22+
await fetch(`${process.env.NEXT_PUBLIC_API_URL}/api/menu/items`)
23+
).json();
24+
2125
return (
2226
<article className='container min-h-48'>
23-
<MenuRenderer topic={topic} />
27+
<MenuRenderer topic={topic} items={items} />
2428
</article>
2529
);
2630
}

src/components/MenuRenderer.tsx

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,27 @@
22

33
import { CategoryWithItems } from '@/data/menu/categories';
44
import { Topic } from '@/data/menu/topics';
5-
import buildMenu from '@/service/menu';
65
import { SearchContext } from '@/store/searchword';
76
import { useContext, useMemo } from 'react';
87
import { CategoryAccordionList } from './CategoryAccordion/CategoryAccordionList';
8+
import { MenuItem } from '@/data/menu/items';
9+
import { useMenuBuilder } from '@/hooks/useMenuBuilder';
910

10-
export default function MenuRenderer({ topic }: { topic: Topic['slug'] }) {
11+
export default function MenuRenderer({
12+
topic,
13+
items
14+
}: {
15+
topic: Topic['slug'];
16+
items: MenuItem[];
17+
}) {
1118
const searchword = useContext(SearchContext);
12-
19+
const menuData = useMenuBuilder(items, {
20+
topic: topic?.[0] ?? 'all',
21+
searchword
22+
});
1323
const menuDataByTopic: { itemsCount: number; data: CategoryWithItems[] } =
1424
useMemo(() => {
15-
const menuData = buildMenu(topic?.[0] ?? 'all', searchword);
25+
// const menuData = buildMenu(topic?.[0] ?? 'all', searchword);
1626
return {
1727
itemsCount: menuData.itemsCount,
1828
data: Object.values(menuData.categories)

src/hooks/useMenuBuilder.ts

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
import {
2+
categories,
3+
Category,
4+
CategoryWithItems
5+
} from '@/data/menu/categories';
6+
import { MenuItem } from '@/data/menu/items';
7+
import { Topic, topics } from '@/data/menu/topics';
8+
import build from 'next/dist/build';
9+
import { useCallback } from 'react';
10+
11+
export const useMenuBuilder = (
12+
items: MenuItem[],
13+
{ topic, searchword }: { topic: Topic['slug']; searchword?: string }
14+
) => {
15+
const getCategoriesByTopic = (topicSlug: Topic['slug']) => {
16+
if (topicSlug === 'all') return categories;
17+
18+
const topic = topics.find((topic: Topic) => topic.slug === topicSlug);
19+
20+
if (!topic) throw new Error(`Sezione '${topicSlug}' non trovata`);
21+
22+
const activeCategories = categories.filter((category) =>
23+
category.topics.includes(topic.id)
24+
);
25+
26+
return activeCategories;
27+
};
28+
29+
const getItemsByCategory = (categorySlug: Category['slug']): MenuItem[] =>
30+
items.filter((item: MenuItem) => {
31+
return item.category_id === categorySlug;
32+
});
33+
34+
const getFilteredItems = (
35+
categorySlug: Category['slug'],
36+
searchword?: string
37+
) => {
38+
const itemsByCategory = getItemsByCategory(categorySlug);
39+
40+
if (searchword) {
41+
const filteredCategoryItems = itemsByCategory.filter((item) =>
42+
item.label.toLowerCase().includes(searchword.toLowerCase())
43+
);
44+
45+
return filteredCategoryItems;
46+
}
47+
48+
return itemsByCategory;
49+
};
50+
51+
const buildMenu = useCallback(
52+
(
53+
topicFilter: Topic['slug'],
54+
searchword?: string
55+
): {
56+
itemsCount: number;
57+
categories: Record<Category['slug'], CategoryWithItems>;
58+
} => {
59+
const topicCategories = getCategoriesByTopic(topicFilter);
60+
61+
return topicCategories.reduce(
62+
(acc, cat) => {
63+
const items = getFilteredItems(cat.slug, searchword);
64+
65+
return {
66+
itemsCount: acc.itemsCount + items.length,
67+
categories: {
68+
...acc.categories,
69+
...(items.length && {
70+
[cat.slug]: {
71+
...cat,
72+
items: items
73+
}
74+
})
75+
}
76+
};
77+
},
78+
{ itemsCount: 0, categories: {} }
79+
);
80+
},
81+
[]
82+
);
83+
84+
return buildMenu(topic, searchword);
85+
};

src/service/menu.ts

Lines changed: 0 additions & 78 deletions
This file was deleted.

0 commit comments

Comments
 (0)