You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
42 lines
1.1 KiB
TypeScript
42 lines
1.1 KiB
TypeScript
import { defineStore } from 'pinia';
|
|
import { dummyList, users } from './dummyData';
|
|
|
|
type User = {
|
|
name: string;
|
|
};
|
|
|
|
type ShoppingItem = {
|
|
name: string;
|
|
price: number;
|
|
isBought: boolean;
|
|
user: User;
|
|
id: number;
|
|
};
|
|
|
|
export const useShoppingListStore = defineStore('shoppingList', {
|
|
state() {
|
|
return {
|
|
items: dummyList as Array<ShoppingItem>,
|
|
users: users as Array<User>,
|
|
};
|
|
},
|
|
actions: {
|
|
deleteItem(id: number) {
|
|
this.items = this.items.filter(item => item.id !== id);
|
|
},
|
|
},
|
|
getters: {
|
|
openItems: state => state.items.filter(item => !item.isBought),
|
|
boughtItems: state => state.items.filter(item => !!item.isBought),
|
|
itemsByUser: state => groupItemsByUser(state.items),
|
|
openItemsByUser: state => groupItemsByUser(state.items.filter(item => !item.isBought)),
|
|
boughtItemsByUser: state => groupItemsByUser(state.items.filter(item => !!item.isBought)),
|
|
},
|
|
});
|
|
|
|
export const groupItemsByUser = items => items.reduce((groups, item) => {
|
|
if (!groups[item.user.name]) groups[item.user.name] = [];
|
|
groups[item.user.name].push(item);
|
|
return groups;
|
|
}, {});
|