Skip to content

Commit

Permalink
Add donations stats to the API
Browse files Browse the repository at this point in the history
Related to Issue #42
  • Loading branch information
BobChao87 committed Feb 23, 2022
1 parent d2db730 commit db223e2
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 7 deletions.
15 changes: 13 additions & 2 deletions plugins/vuex-persist.client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import { Plugin } from '@nuxt/types';
import { VuexPersistence } from 'vuex-persist';

import { DonationState } from '~/types/api/donation';
import { IncentiveState } from '~/types/api/incentive';
import { MarathonState } from '~/types/api/marathon';
import { PatreonState } from '~/types/api/patreon';
Expand All @@ -12,7 +13,7 @@ import { UserState } from '~/types/api/user';
interface State {
api: {
category: { }; // CategoryState;
donation: { }; // DonationState;
donation: DonationState;
incentive: IncentiveState;
marathon: MarathonState;
patreon: PatreonState;
Expand Down Expand Up @@ -46,7 +47,17 @@ const vuexPersistPlugin: Plugin = ({ $localForage, store }) => {
api: {
...store.state.api,
category: store.state.api.category,
donation: store.state.api.donation,
donation: {
...store.state.api.donation,
donations: {
...state.api.donation.donations,
...store.state.api.donation.donations,
},
stats: {
...state.api.donation.stats,
...store.state.api.donation.stats,
},
},
incentive: {
...store.state.api.incentive,
incentives: {
Expand Down
12 changes: 7 additions & 5 deletions store/api/donation.ts
Original file line number Diff line number Diff line change
@@ -1,24 +1,26 @@
import Vue from 'vue';
import { ActionTree, MutationTree } from 'vuex';
import { OengusAPI } from '~/plugins/oengus';
import { Donation, DonationState } from '~/types/api/donation';
import { Donation, DonationState, DonationStats } from '~/types/api/donation';
import { OengusPagination } from '~/types/api/oengus-api';

const DonationOengusAPI = new OengusAPI<DonationState>('marathons');

export const state = (): DonationState => ({
donations: { },
stats: { },
});

export const mutations: MutationTree<DonationState> = {
addDonations(state, { id, value: donations, data }): void {
Vue.set(state.donations, `${id}-${data.page}-${data.size}`, donations);
},
addStats(state, { id, value: stats }): void {
Vue.set(state.stats, id, stats);
},
};

export const actions: ActionTree<DonationState, DonationState> = {
get: DonationOengusAPI.get<OengusPagination<Donation>>({
path: 'donations',
key: 'donations',
}),
get: DonationOengusAPI.get<OengusPagination<Donation>>({ path: 'donations', key: 'donations' }),
stats: DonationOengusAPI.get<DonationStats>({ path: 'donations/stats', key: 'stats' }),
};
8 changes: 8 additions & 0 deletions types/api/donation.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,16 @@ export interface Donation {
answers: Array<string>;
}

export interface DonationStats {
average: number;
count: number;
max: number;
total: number;
}

export interface DonationState extends OengusState {
donations: { [id: string]: OengusPagination<Donation> };
stats: { [id: string]: DonationStats };
}

export interface DonationPageParams {
Expand Down

0 comments on commit db223e2

Please sign in to comment.