Bắt đầu sửa lỗi
This commit is contained in:
@@ -0,0 +1,40 @@
|
||||
import AsyncStorage from '@react-native-async-storage/async-storage';
|
||||
import { Recipe } from '../types';
|
||||
import { DEFAULT_RECIPES } from './defaultRecipes';
|
||||
|
||||
const CUSTOM_RECIPES_KEY = '@camrecipe_pro:custom_recipes';
|
||||
|
||||
export async function getCustomRecipes(): Promise<Recipe[]> {
|
||||
try {
|
||||
const data = await AsyncStorage.getItem(CUSTOM_RECIPES_KEY);
|
||||
if (!data) return [];
|
||||
return JSON.parse(data);
|
||||
} catch (error) {
|
||||
console.error('Failed to load custom recipes:', error);
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
export async function getAllRecipes(): Promise<Recipe[]> {
|
||||
const custom = await getCustomRecipes();
|
||||
return [...DEFAULT_RECIPES, ...custom];
|
||||
}
|
||||
|
||||
export async function saveCustomRecipe(recipe: Omit<Recipe, 'id' | 'isCustom'>): Promise<Recipe> {
|
||||
const custom = await getCustomRecipes();
|
||||
const newRecipe: Recipe = {
|
||||
...recipe,
|
||||
id: `custom_${Date.now()}_${Math.random().toString(36).substr(2, 9)}`,
|
||||
isCustom: true,
|
||||
};
|
||||
|
||||
const updated = [...custom, newRecipe];
|
||||
await AsyncStorage.setItem(CUSTOM_RECIPES_KEY, JSON.stringify(updated));
|
||||
return newRecipe;
|
||||
}
|
||||
|
||||
export async function deleteCustomRecipe(id: string): Promise<void> {
|
||||
const custom = await getCustomRecipes();
|
||||
const updated = custom.filter(r => r.id !== id);
|
||||
await AsyncStorage.setItem(CUSTOM_RECIPES_KEY, JSON.stringify(updated));
|
||||
}
|
||||
Reference in New Issue
Block a user