mirror of
https://github.com/urosran/cally.git
synced 2025-11-26 00:24:53 +00:00
Shopping List backend implementation
- Implemented fetching, create and update of groceries in db
This commit is contained in:
@ -1,6 +1,9 @@
|
||||
import { MaterialCommunityIcons } from "@expo/vector-icons";
|
||||
import { createContext, useContext, useState } from "react";
|
||||
import {createContext, useContext, useState} from "react";
|
||||
import fuzzysort from "fuzzysort";
|
||||
import {useCreateGrocery} from "@/hooks/firebase/useCreateGrocery";
|
||||
import {IGrocery} from "@/hooks/firebase/types/groceryData";
|
||||
import {useUpdateGrocery} from "@/hooks/firebase/useUpdateGrocery";
|
||||
import {useGetGroceries} from "@/hooks/firebase/useGetGroceries";
|
||||
|
||||
export enum GroceryFrequency {
|
||||
Never = "Never",
|
||||
@ -11,16 +14,6 @@ export enum GroceryFrequency {
|
||||
Quarterly = "Quarterly",
|
||||
}
|
||||
|
||||
export interface IGrocery {
|
||||
id: number;
|
||||
title: string;
|
||||
category: GroceryCategory;
|
||||
approved: boolean;
|
||||
recurring: boolean;
|
||||
frequency: GroceryFrequency;
|
||||
bought: boolean;
|
||||
}
|
||||
|
||||
export enum GroceryCategory {
|
||||
Fruit = "Fruit",
|
||||
Dairy = "Dairy",
|
||||
@ -52,10 +45,136 @@ const iconMapping: { [key in GroceryCategory]: MaterialIconNames } = {
|
||||
[GroceryCategory.Frozen]: "snowflake",
|
||||
};*/
|
||||
|
||||
const initialGroceryList = [
|
||||
{
|
||||
id: 0,
|
||||
title: "Carrots",
|
||||
category: GroceryCategory.Vegetables,
|
||||
approved: false,
|
||||
bought: false,
|
||||
recurring: false,
|
||||
frequency: GroceryFrequency.Never,
|
||||
},
|
||||
{
|
||||
id: 1,
|
||||
title: "Steak",
|
||||
category: GroceryCategory.Meat,
|
||||
approved: true,
|
||||
bought: false,
|
||||
recurring: false,
|
||||
frequency: GroceryFrequency.Never,
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
title: "Chicken Breast",
|
||||
category: GroceryCategory.Poultry,
|
||||
approved: true,
|
||||
bought: false,
|
||||
recurring: false,
|
||||
frequency: GroceryFrequency.Never,
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
title: "Greek Yoghurt",
|
||||
category: GroceryCategory.Dairy,
|
||||
approved: false,
|
||||
bought: false,
|
||||
recurring: false,
|
||||
frequency: GroceryFrequency.Never,
|
||||
},
|
||||
];
|
||||
|
||||
const groceryExamples = {
|
||||
[GroceryCategory.Fruit]: [
|
||||
'apple', 'apples', 'banana', 'bananas', 'orange', 'oranges', 'grape', 'grapes',
|
||||
'pear', 'pears', 'pineapple', 'pineapples', 'kiwi', 'kiwis', 'strawberry',
|
||||
'strawberries', 'blueberry', 'blueberries', 'mango', 'mangoes', 'watermelon',
|
||||
'watermelons', 'peach', 'peaches', 'plum', 'plums', 'cherry', 'cherries',
|
||||
'raspberry', 'raspberries', 'blackberry', 'blackberries', 'pomegranate',
|
||||
'pomegranates', 'lemon', 'lemons', 'lime', 'limes', 'coconut', 'coconuts'
|
||||
],
|
||||
[GroceryCategory.Dairy]: [
|
||||
'milk', 'whole milk', 'skim milk', 'almond milk', 'soy milk', 'cheese',
|
||||
'cheeses', 'yoghurt', 'yogurt', 'greek yoghurt', 'greek yogurt', 'butter',
|
||||
'margarine', 'cream', 'whipping cream', 'heavy cream', 'ice cream', 'ice creams',
|
||||
'sour cream', 'whipped cream', 'cream cheese', 'cream cheeses', 'buttermilk',
|
||||
'cottage cheese', 'ghee', 'kefir'
|
||||
],
|
||||
[GroceryCategory.Vegetables]: [
|
||||
'carrot', 'carrots', 'broccoli', 'lettuce', 'lettuces', 'spinach', 'kale',
|
||||
'cabbage', 'cabbages', 'cauliflower', 'zucchini', 'zucchinis', 'onion', 'onions',
|
||||
'garlic', 'pepper', 'peppers', 'bell pepper', 'bell peppers', 'tomato', 'tomatoes',
|
||||
'cucumber', 'cucumbers', 'potato', 'potatoes', 'sweet potato', 'sweet potatoes',
|
||||
'beet', 'beets', 'eggplant', 'eggplants', 'celery', 'radish', 'radishes',
|
||||
'asparagus', 'mushroom', 'mushrooms'
|
||||
],
|
||||
[GroceryCategory.Meat]: [
|
||||
'steak', 'steaks', 'beef', 'pork', 'lamb', 'veal', 'ribeye', 'tenderloin',
|
||||
'filet mignon', 'bacon', 'ham', 'sausage', 'sausages', 'salami', 'ground beef',
|
||||
'beef mince', 'hamburger meat', 'prosciutto', 'brisket', 'pork chop', 'pork chops'
|
||||
],
|
||||
[GroceryCategory.Poultry]: [
|
||||
'chicken', 'whole chicken', 'chickens', 'chicken breast', 'chicken breasts',
|
||||
'breast of chicken', 'chicken thigh', 'chicken thighs', 'chicken leg', 'chicken legs',
|
||||
'chicken wing', 'chicken wings', 'wing', 'wings', 'drumsticks', 'chicken drumstick',
|
||||
'turkey', 'whole turkey', 'ground turkey', 'turkey breast', 'turkey breasts',
|
||||
'turkey leg', 'duck', 'ducks', 'goose', 'quail', 'pheasant'
|
||||
],
|
||||
[GroceryCategory.Bakery]: [
|
||||
'bread', 'breads', 'whole wheat bread', 'sourdough bread', 'bagel', 'bagels',
|
||||
'croissant', 'croissants', 'muffin', 'muffins', 'buns', 'hamburger bun',
|
||||
'hamburger buns', 'hotdog bun', 'hotdog buns', 'donut', 'donuts', 'roll', 'rolls',
|
||||
'dinner roll', 'dinner rolls', 'scone', 'scones', 'toast', 'ciabatta',
|
||||
'focaccia', 'brioche', 'pita', 'pitas', 'naan', 'baguette', 'baguettes',
|
||||
'pastry', 'pastries', 'pretzel', 'pretzels'
|
||||
],
|
||||
[GroceryCategory.Beverages]: [
|
||||
'coffee', 'decaf coffee', 'iced coffee', 'cold brew', 'tea', 'iced tea',
|
||||
'black tea', 'green tea', 'herbal tea', 'juice', 'apple juice', 'orange juice',
|
||||
'grape juice', 'water', 'sparkling water', 'flavored water', 'soda', 'cola',
|
||||
'diet soda', 'beer', 'lager', 'ale', 'wine', 'red wine', 'white wine',
|
||||
'whiskey', 'whisky', 'vodka', 'rum', 'gin', 'smoothie', 'smoothies', 'milkshake',
|
||||
'milkshakes', 'energy drink', 'energy drinks', 'sports drink', 'sports drinks',
|
||||
'lemonade', 'sparkling lemonade', 'iced lemonade', 'sparkling water', 'cider',
|
||||
'hard cider', 'kombucha'
|
||||
],
|
||||
[GroceryCategory.Snacks]: [
|
||||
'chips', 'potato chips', 'tortilla chips', 'corn chips', 'candy', 'candies',
|
||||
'chocolate', 'chocolates', 'dark chocolate', 'milk chocolate', 'white chocolate',
|
||||
'cookies', 'popcorn', 'pretzel', 'pretzels', 'nuts', 'mixed nuts', 'almonds',
|
||||
'cashews', 'trail mix', 'granola bar', 'granola bars', 'protein bar', 'protein bars',
|
||||
'crackers', 'gummies', 'gummy bears', 'fruit snacks', 'dried fruit',
|
||||
'peanut butter', 'rice cakes', 'snack cakes'
|
||||
],
|
||||
[GroceryCategory.Household]: [
|
||||
'detergent', 'laundry detergent', 'dish soap', 'dishwasher detergent',
|
||||
'toilet paper', 'paper towels', 'trash bags', 'bin liners', 'broom', 'mop',
|
||||
'cleaner', 'surface cleaner', 'multi-purpose cleaner', 'disinfectant', 'bleach',
|
||||
'fabric softener', 'vacuum bags', 'aluminum foil', 'plastic wrap', 'cling wrap',
|
||||
'light bulbs', 'batteries', 'laundry soap', 'sponges', 'scrubbers',
|
||||
'dishwashing liquid'
|
||||
],
|
||||
[GroceryCategory.PersonalCare]: [
|
||||
'shampoo', 'conditioner', 'hair shampoo', 'hair conditioner', 'soap', 'bar soap',
|
||||
'liquid soap', 'toothpaste', 'toothbrush', 'toothbrushes', 'electric toothbrush',
|
||||
'deodorant', 'antiperspirant', 'lotion', 'moisturizer', 'razor', 'shaving razor',
|
||||
'shaving cream', 'body wash', 'face wash', 'sunscreen', 'hair gel', 'hair spray',
|
||||
'nail polish', 'cotton swabs', 'lip balm', 'chapstick', 'hand cream', 'sanitizer'
|
||||
],
|
||||
[GroceryCategory.Frozen]: [
|
||||
'ice cream', 'ice creams', 'frozen pizza', 'frozen pizzas', 'frozen vegetables',
|
||||
'frozen veg', 'frozen peas', 'frozen fruit', 'frozen fish', 'frozen chicken',
|
||||
'frozen wings', 'frozen fries', 'frozen french fries', 'frozen shrimp',
|
||||
'frozen prawns', 'frozen dumplings', 'frozen waffles', 'frozen pancakes',
|
||||
'frozen pie', 'frozen pies', 'frozen lasagna', 'frozen burrito', 'frozen burritos',
|
||||
'frozen nuggets', 'frozen pastry', 'frozen pastries', 'frozen meals'
|
||||
],
|
||||
};
|
||||
|
||||
interface IGroceryContext {
|
||||
groceries: IGrocery[];
|
||||
//iconMapping: { [key in GroceryCategory]: MaterialIconNames };
|
||||
updateGroceryItem: (id: number, changes: Partial<IGrocery>) => void;
|
||||
updateGroceryItem: (changes: Partial<IGrocery>) => void;
|
||||
isAddingGrocery: boolean;
|
||||
setIsAddingGrocery: (value: boolean) => void;
|
||||
addGrocery: (grocery: IGrocery) => void;
|
||||
@ -68,142 +187,14 @@ export const GroceryProvider: React.FC<{ children: React.ReactNode }> = ({
|
||||
children,
|
||||
}) => {
|
||||
const [isAddingGrocery, setIsAddingGrocery] = useState<boolean>(false);
|
||||
const [groceries, setGroceries] = useState<IGrocery[]>([
|
||||
{
|
||||
id: 0,
|
||||
title: "Carrots",
|
||||
category: GroceryCategory.Vegetables,
|
||||
approved: false,
|
||||
bought: false,
|
||||
recurring: false,
|
||||
frequency: GroceryFrequency.Never,
|
||||
},
|
||||
{
|
||||
id: 1,
|
||||
title: "Steak",
|
||||
category: GroceryCategory.Meat,
|
||||
approved: true,
|
||||
bought: false,
|
||||
recurring: false,
|
||||
frequency: GroceryFrequency.Never,
|
||||
},
|
||||
{
|
||||
id: 2,
|
||||
title: "Chicken Breast",
|
||||
category: GroceryCategory.Poultry,
|
||||
approved: true,
|
||||
bought: false,
|
||||
recurring: false,
|
||||
frequency: GroceryFrequency.Never,
|
||||
},
|
||||
{
|
||||
id: 3,
|
||||
title: "Greek Yoghurt",
|
||||
category: GroceryCategory.Dairy,
|
||||
approved: false,
|
||||
bought: false,
|
||||
recurring: false,
|
||||
frequency: GroceryFrequency.Never,
|
||||
},
|
||||
]);
|
||||
|
||||
const { mutateAsync: createGrocery } = useCreateGrocery();
|
||||
const { mutateAsync: updateGrocery } = useUpdateGrocery();
|
||||
const { data: groceries } = useGetGroceries();
|
||||
|
||||
|
||||
const addGrocery = (grocery: IGrocery) => {
|
||||
setGroceries((prevGroceries) => [
|
||||
...prevGroceries,
|
||||
{
|
||||
...grocery,
|
||||
id: prevGroceries.length
|
||||
? prevGroceries[prevGroceries.length - 1].id + 1
|
||||
: 0,
|
||||
},
|
||||
]);
|
||||
};
|
||||
|
||||
const groceryExamples = {
|
||||
[GroceryCategory.Fruit]: [
|
||||
'apple', 'apples', 'banana', 'bananas', 'orange', 'oranges', 'grape', 'grapes',
|
||||
'pear', 'pears', 'pineapple', 'pineapples', 'kiwi', 'kiwis', 'strawberry',
|
||||
'strawberries', 'blueberry', 'blueberries', 'mango', 'mangoes', 'watermelon',
|
||||
'watermelons', 'peach', 'peaches', 'plum', 'plums', 'cherry', 'cherries',
|
||||
'raspberry', 'raspberries', 'blackberry', 'blackberries', 'pomegranate',
|
||||
'pomegranates', 'lemon', 'lemons', 'lime', 'limes', 'coconut', 'coconuts'
|
||||
],
|
||||
[GroceryCategory.Dairy]: [
|
||||
'milk', 'whole milk', 'skim milk', 'almond milk', 'soy milk', 'cheese',
|
||||
'cheeses', 'yoghurt', 'yogurt', 'greek yoghurt', 'greek yogurt', 'butter',
|
||||
'margarine', 'cream', 'whipping cream', 'heavy cream', 'ice cream', 'ice creams',
|
||||
'sour cream', 'whipped cream', 'cream cheese', 'cream cheeses', 'buttermilk',
|
||||
'cottage cheese', 'ghee', 'kefir'
|
||||
],
|
||||
[GroceryCategory.Vegetables]: [
|
||||
'carrot', 'carrots', 'broccoli', 'lettuce', 'lettuces', 'spinach', 'kale',
|
||||
'cabbage', 'cabbages', 'cauliflower', 'zucchini', 'zucchinis', 'onion', 'onions',
|
||||
'garlic', 'pepper', 'peppers', 'bell pepper', 'bell peppers', 'tomato', 'tomatoes',
|
||||
'cucumber', 'cucumbers', 'potato', 'potatoes', 'sweet potato', 'sweet potatoes',
|
||||
'beet', 'beets', 'eggplant', 'eggplants', 'celery', 'radish', 'radishes',
|
||||
'asparagus', 'mushroom', 'mushrooms'
|
||||
],
|
||||
[GroceryCategory.Meat]: [
|
||||
'steak', 'steaks', 'beef', 'pork', 'lamb', 'veal', 'ribeye', 'tenderloin',
|
||||
'filet mignon', 'bacon', 'ham', 'sausage', 'sausages', 'salami', 'ground beef',
|
||||
'beef mince', 'hamburger meat', 'prosciutto', 'brisket', 'pork chop', 'pork chops'
|
||||
],
|
||||
[GroceryCategory.Poultry]: [
|
||||
'chicken', 'whole chicken', 'chickens', 'chicken breast', 'chicken breasts',
|
||||
'breast of chicken', 'chicken thigh', 'chicken thighs', 'chicken leg', 'chicken legs',
|
||||
'chicken wing', 'chicken wings', 'wing', 'wings', 'drumsticks', 'chicken drumstick',
|
||||
'turkey', 'whole turkey', 'ground turkey', 'turkey breast', 'turkey breasts',
|
||||
'turkey leg', 'duck', 'ducks', 'goose', 'quail', 'pheasant'
|
||||
],
|
||||
[GroceryCategory.Bakery]: [
|
||||
'bread', 'breads', 'whole wheat bread', 'sourdough bread', 'bagel', 'bagels',
|
||||
'croissant', 'croissants', 'muffin', 'muffins', 'buns', 'hamburger bun',
|
||||
'hamburger buns', 'hotdog bun', 'hotdog buns', 'donut', 'donuts', 'roll', 'rolls',
|
||||
'dinner roll', 'dinner rolls', 'scone', 'scones', 'toast', 'ciabatta',
|
||||
'focaccia', 'brioche', 'pita', 'pitas', 'naan', 'baguette', 'baguettes',
|
||||
'pastry', 'pastries', 'pretzel', 'pretzels'
|
||||
],
|
||||
[GroceryCategory.Beverages]: [
|
||||
'coffee', 'decaf coffee', 'iced coffee', 'cold brew', 'tea', 'iced tea',
|
||||
'black tea', 'green tea', 'herbal tea', 'juice', 'apple juice', 'orange juice',
|
||||
'grape juice', 'water', 'sparkling water', 'flavored water', 'soda', 'cola',
|
||||
'diet soda', 'beer', 'lager', 'ale', 'wine', 'red wine', 'white wine',
|
||||
'whiskey', 'whisky', 'vodka', 'rum', 'gin', 'smoothie', 'smoothies', 'milkshake',
|
||||
'milkshakes', 'energy drink', 'energy drinks', 'sports drink', 'sports drinks',
|
||||
'lemonade', 'sparkling lemonade', 'iced lemonade', 'sparkling water', 'cider',
|
||||
'hard cider', 'kombucha'
|
||||
],
|
||||
[GroceryCategory.Snacks]: [
|
||||
'chips', 'potato chips', 'tortilla chips', 'corn chips', 'candy', 'candies',
|
||||
'chocolate', 'chocolates', 'dark chocolate', 'milk chocolate', 'white chocolate',
|
||||
'cookies', 'popcorn', 'pretzel', 'pretzels', 'nuts', 'mixed nuts', 'almonds',
|
||||
'cashews', 'trail mix', 'granola bar', 'granola bars', 'protein bar', 'protein bars',
|
||||
'crackers', 'gummies', 'gummy bears', 'fruit snacks', 'dried fruit',
|
||||
'peanut butter', 'rice cakes', 'snack cakes'
|
||||
],
|
||||
[GroceryCategory.Household]: [
|
||||
'detergent', 'laundry detergent', 'dish soap', 'dishwasher detergent',
|
||||
'toilet paper', 'paper towels', 'trash bags', 'bin liners', 'broom', 'mop',
|
||||
'cleaner', 'surface cleaner', 'multi-purpose cleaner', 'disinfectant', 'bleach',
|
||||
'fabric softener', 'vacuum bags', 'aluminum foil', 'plastic wrap', 'cling wrap',
|
||||
'light bulbs', 'batteries', 'laundry soap', 'sponges', 'scrubbers',
|
||||
'dishwashing liquid'
|
||||
],
|
||||
[GroceryCategory.PersonalCare]: [
|
||||
'shampoo', 'conditioner', 'hair shampoo', 'hair conditioner', 'soap', 'bar soap',
|
||||
'liquid soap', 'toothpaste', 'toothbrush', 'toothbrushes', 'electric toothbrush',
|
||||
'deodorant', 'antiperspirant', 'lotion', 'moisturizer', 'razor', 'shaving razor',
|
||||
'shaving cream', 'body wash', 'face wash', 'sunscreen', 'hair gel', 'hair spray',
|
||||
'nail polish', 'cotton swabs', 'lip balm', 'chapstick', 'hand cream', 'sanitizer'
|
||||
],
|
||||
[GroceryCategory.Frozen]: [
|
||||
'ice cream', 'ice creams', 'frozen pizza', 'frozen pizzas', 'frozen vegetables',
|
||||
'frozen veg', 'frozen peas', 'frozen fruit', 'frozen fish', 'frozen chicken',
|
||||
'frozen wings', 'frozen fries', 'frozen french fries', 'frozen shrimp',
|
||||
'frozen prawns', 'frozen dumplings', 'frozen waffles', 'frozen pancakes',
|
||||
'frozen pie', 'frozen pies', 'frozen lasagna', 'frozen burrito', 'frozen burritos',
|
||||
'frozen nuggets', 'frozen pastry', 'frozen pastries', 'frozen meals'
|
||||
],
|
||||
createGrocery(grocery);
|
||||
};
|
||||
|
||||
const fuzzyMatchGroceryCategory = (groceryTitle: string): GroceryCategory => {
|
||||
@ -221,12 +212,8 @@ export const GroceryProvider: React.FC<{ children: React.ReactNode }> = ({
|
||||
return bestMatchCategory;
|
||||
};
|
||||
|
||||
const updateGroceryItem = (id: number, changes: Partial<IGrocery>) => {
|
||||
setGroceries((prevGroceries) =>
|
||||
prevGroceries.map((grocery) =>
|
||||
grocery.id === id ? { ...grocery, ...changes } : grocery
|
||||
)
|
||||
);
|
||||
const updateGroceryItem = (changes: Partial<IGrocery>) => {
|
||||
updateGrocery(changes);
|
||||
};
|
||||
|
||||
return (
|
||||
|
||||
Reference in New Issue
Block a user