From: Eduardo San Martin Morote Date: Sat, 15 May 2021 12:00:16 +0000 (+0200) Subject: chore: refactor old examples X-Git-Tag: v0.5.0~6 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=1fa7222d937eb6a933fc1156061cca22fa261461;p=thirdparty%2Fvuejs%2Fpinia.git chore: refactor old examples --- diff --git a/__tests__/pinia/stores/cart.ts b/__tests__/pinia/stores/cart.ts index f17b96d4..d8f555ff 100644 --- a/__tests__/pinia/stores/cart.ts +++ b/__tests__/pinia/stores/cart.ts @@ -1,24 +1,31 @@ import { defineStore } from '../../../src' -import { useUserStore, UserStore } from './user' +import { useUserStore } from './user' + +export interface CartItem { + name: string + price: number +} export const useCartStore = defineStore({ id: 'cart', state: () => ({ - rawItems: [] as string[], + rawItems: [] as CartItem[], }), getters: { items: (state) => state.rawItems.reduce((items, item) => { - const existingItem = items.find((it) => it.name === item) + const existingItem = items.find((it) => it.item.name === item.name) if (!existingItem) { - items.push({ name: item, amount: 1 }) + items.push({ item, amount: 1 }) } else { existingItem.amount++ } return items - }, [] as { name: string; amount: number }[]), + }, [] as { item: CartItem; amount: number }[]), + price: (state) => + state.rawItems.reduce((total, item) => total + item.price, 0), }, }) @@ -35,21 +42,21 @@ export type CartStore = ReturnType // getters.items -export function addItem(name: string) { +export function addItem(name: string, price: number): void { const store = useCartStore() - store.$state.rawItems.push(name) + store.$state.rawItems.push({ name, price }) } -export function removeItem(name: string) { +export function removeItem(name: string): void { const store = useCartStore() - const i = store.$state.rawItems.indexOf(name) + const i = store.$state.rawItems.findIndex((item) => item.name === name) if (i > -1) store.$state.rawItems.splice(i, 1) } -export async function purchaseItems() { +export async function purchaseItems(): Promise { const cart = useCartStore() const user = useUserStore() - if (!user.$state.name) return + if (!user.$state.name) return 0 console.log('Purchasing', cart.items) const n = cart.items.length diff --git a/__tests__/pinia/stores/combined.ts b/__tests__/pinia/stores/combined.ts index e9457b00..ab3a785b 100644 --- a/__tests__/pinia/stores/combined.ts +++ b/__tests__/pinia/stores/combined.ts @@ -1,30 +1,31 @@ import { useUserStore } from './user' import { useCartStore } from './cart' -import { pinia } from '../../../src' +import { defineStore } from '../../../src' // in this file we could import other stores that use one each other while // avoiding any recursive import // TODO: not implemented -const useCartUserStore = pinia( - { user: useUserStore, cart: useCartStore }, - { - getters: { - combinedGetter: ({ user, cart }) => - `Hi ${user.state.name}, you have ${cart.state.list.length} items in your cart. It costs ${cart.price}.`, +const useCartUserStore = defineStore({ + id: 'user-cart', + getters: { + combinedGetter: () => { + const user = useUserStore() + const cart = useCartStore() + return `Hi ${user.name}, you have ${cart.items.length} items in your cart. It costs ${cart.price}.` }, - actions: { - async orderCart() { - try { - await apiOrderCart(this.user.state.token, this.cart.state.items) - this.cart.emptyCart() - } catch (err) { - displayError(err) - } - }, + }, + actions: { + async orderCart() { + try { + await apiOrderCart(this.user.token, this.cart.items) + this.cart.emptyCart() + } catch (err) { + displayError(err) + } }, - } -) + }, +}) const a = useCartUserStore() diff --git a/__tests__/pinia/stores/user.ts b/__tests__/pinia/stores/user.ts index 28b250d5..65cb4438 100644 --- a/__tests__/pinia/stores/user.ts +++ b/__tests__/pinia/stores/user.ts @@ -39,7 +39,7 @@ export type UserStore = ReturnType // let a: WrapStoreWithId -export function logout() { +export function logout(): void { const store = useUserStore() store.login('e', 'e').then(() => {})