|
| 1 | +import { createCardApi, getCardsApi, getCardDetailApi, deleteCardApi } from '@/services/flashCard' |
| 2 | +import type { IGroup, IGroupCreate, IMemberGroup, IDetailGroup } from '@/types/group' |
| 3 | +import { showToast } from '@/utils/toast' |
| 4 | +import { defineStore } from 'pinia' |
| 5 | +import { apiError } from '@/utils/exceptionHandler' |
| 6 | +import type { IPaging } from '@/types' |
| 7 | + |
| 8 | +export const useCardStore = defineStore({ |
| 9 | + id: 'card', |
| 10 | + state: () => ({ |
| 11 | + groupInfo: {} as IDetailGroup, |
| 12 | + isUpdating: false, |
| 13 | + groups: [] as IGroup[], |
| 14 | + groupMeta: null as IPaging | null, |
| 15 | + listMembers: [] as IMemberGroup[], |
| 16 | + isFetching: false, |
| 17 | + }), |
| 18 | + actions: { |
| 19 | + setIsUpdating(val: boolean) { |
| 20 | + this.isUpdating = val |
| 21 | + }, |
| 22 | + async initCard(payload: IGroupCreate) { |
| 23 | + try { |
| 24 | + this.isUpdating = true |
| 25 | + const { data } = await createCardApi(payload) |
| 26 | + this.setGroupInfo(data) |
| 27 | + } catch (error) { |
| 28 | + console.error(error) |
| 29 | + showToast({ |
| 30 | + description: apiError(error).message, |
| 31 | + variant: 'destructive', |
| 32 | + }) |
| 33 | + } |
| 34 | + this.isUpdating = false |
| 35 | + }, |
| 36 | + async fetchCards({ page = 1, keyword = '' }) { |
| 37 | + try { |
| 38 | + this.isFetching = true |
| 39 | + const { data, meta } = await getCardsApi({ page, keyword }) |
| 40 | + this.groups = data |
| 41 | + this.groupMeta = meta as IPaging |
| 42 | + } catch (error) { |
| 43 | + console.error(error) |
| 44 | + showToast({ |
| 45 | + description: 'Fetch groups failed', |
| 46 | + variant: 'destructive', |
| 47 | + }) |
| 48 | + throw error |
| 49 | + } finally { |
| 50 | + this.isFetching = false |
| 51 | + } |
| 52 | + }, |
| 53 | + async getDetailCard(idGroup: string) { |
| 54 | + try { |
| 55 | + await getCardDetailApi(idGroup) |
| 56 | + // this.setGroupInfo(data) |
| 57 | + } catch (error) { |
| 58 | + showToast({ |
| 59 | + description: apiError(error).message, |
| 60 | + variant: 'destructive', |
| 61 | + }) |
| 62 | + } |
| 63 | + }, |
| 64 | + async handleDeleteCard(idGroup: string) { |
| 65 | + try { |
| 66 | + await deleteCardApi(idGroup) |
| 67 | + const index = this.groups.findIndex((i) => i.group.id === idGroup) |
| 68 | + index > -1 && this.groups.splice(index, 1) |
| 69 | + showToast({ |
| 70 | + description: 'Delete group success', |
| 71 | + variant: 'default', |
| 72 | + }) |
| 73 | + } catch (error) { |
| 74 | + showToast({ |
| 75 | + description: apiError(error).message, |
| 76 | + variant: 'destructive', |
| 77 | + }) |
| 78 | + } |
| 79 | + }, |
| 80 | + setGroupInfo(val: IGroup) { |
| 81 | + this.groupInfo = { ...this.groupInfo, ...val } |
| 82 | + }, |
| 83 | + setMembersGroup(val: any) { |
| 84 | + this.listMembers = val |
| 85 | + }, |
| 86 | + }, |
| 87 | + getters: { |
| 88 | + getGroupInfo: (state) => state.groupInfo, |
| 89 | + getIsUpdating: (state) => state.isUpdating, |
| 90 | + getGroups: (state) => state.groups, |
| 91 | + getGroupMeta: (state) => state.groupMeta, |
| 92 | + getMemberGroup: (state) => state.listMembers, |
| 93 | + getIsFetching: (state) => state.isFetching, |
| 94 | + }, |
| 95 | +}) |
0 commit comments