Replace entire repo content with correct code from /root/shahikitchen-website/
This commit is contained in:
@@ -0,0 +1,22 @@
|
||||
export interface WishlistItem {
|
||||
id: string;
|
||||
name: string;
|
||||
price: number;
|
||||
image?: string;
|
||||
}
|
||||
|
||||
export type NewWishlistItem = WishlistItem;
|
||||
|
||||
export interface WishlistState {
|
||||
items: WishlistItem[];
|
||||
isOpen: boolean;
|
||||
}
|
||||
|
||||
export type WishlistAction =
|
||||
| { type: 'TOGGLE_ITEM'; payload: NewWishlistItem }
|
||||
| { type: 'REMOVE_ITEM'; payload: string }
|
||||
| { type: 'CLEAR_WISHLIST' }
|
||||
| { type: 'TOGGLE_DRAWER' }
|
||||
| { type: 'OPEN_DRAWER' }
|
||||
| { type: 'CLOSE_DRAWER' }
|
||||
| { type: 'RESTORE_ITEMS'; payload: WishlistItem[] };
|
||||
@@ -0,0 +1,6 @@
|
||||
import type { WishlistItem } from './entities';
|
||||
|
||||
export interface WishlistRepository {
|
||||
load(): WishlistItem[];
|
||||
save(items: WishlistItem[]): void;
|
||||
}
|
||||
@@ -0,0 +1,48 @@
|
||||
import type { WishlistAction, WishlistItem, WishlistState } from './entities';
|
||||
|
||||
export function wishlistReducer(state: WishlistState, action: WishlistAction): WishlistState {
|
||||
switch (action.type) {
|
||||
case 'TOGGLE_ITEM': {
|
||||
const exists = state.items.some((item) => item.id === action.payload.id);
|
||||
if (exists) {
|
||||
return {
|
||||
...state,
|
||||
items: state.items.filter((item) => item.id !== action.payload.id),
|
||||
};
|
||||
}
|
||||
return { ...state, items: [...state.items, action.payload] };
|
||||
}
|
||||
|
||||
case 'REMOVE_ITEM':
|
||||
return {
|
||||
...state,
|
||||
items: state.items.filter((item) => item.id !== action.payload),
|
||||
};
|
||||
|
||||
case 'CLEAR_WISHLIST':
|
||||
return { ...state, items: [] };
|
||||
|
||||
case 'RESTORE_ITEMS':
|
||||
return { ...state, items: action.payload };
|
||||
|
||||
case 'TOGGLE_DRAWER':
|
||||
return { ...state, isOpen: !state.isOpen };
|
||||
|
||||
case 'OPEN_DRAWER':
|
||||
return { ...state, isOpen: true };
|
||||
|
||||
case 'CLOSE_DRAWER':
|
||||
return { ...state, isOpen: false };
|
||||
|
||||
default:
|
||||
return state;
|
||||
}
|
||||
}
|
||||
|
||||
export function isInWishlist(items: WishlistItem[], id: string): boolean {
|
||||
return items.some((item) => item.id === id);
|
||||
}
|
||||
|
||||
export function getWishlistCount(items: WishlistItem[]): number {
|
||||
return items.length;
|
||||
}
|
||||
Reference in New Issue
Block a user