Skip to content

Commit 5069b8c

Browse files
authored
feat: actions for votes (#525)
1 parent c3febdb commit 5069b8c

File tree

3 files changed

+66
-7
lines changed

3 files changed

+66
-7
lines changed

src/config/appActionsTypes.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import { AppAction, AppData, LocalContext } from '@graasp/sdk';
44
import { ActivityStep } from '@/interfaces/interactionProcess';
55
import { Prompt } from '@/interfaces/prompt';
66
import { ResponseData } from '@/interfaces/response';
7-
import { CurrentStateData, ResponseAppData } from './appDataTypes';
7+
import { CurrentStateData, ResponseAppData, VoteAppData } from './appDataTypes';
88

99
export enum AppActionTypes {
1010
SubmitNewResponse = 'submit-new-response',
@@ -19,6 +19,8 @@ export enum AppActionTypes {
1919
PlayActivity = 'play-activity',
2020
PauseActivity = 'pause-activity',
2121
RequestPrompt = 'request-prompt',
22+
VoteFor = 'vote-for',
23+
RemoveVote = 'remove-vote',
2224
}
2325

2426
type AppDataRef<T extends Data> = Pick<AppData<T>, 'id' | 'type' | 'data'> &
@@ -76,3 +78,13 @@ export type RequestPromptAction = Pick<AppAction, 'type' | 'data'> & {
7678
type: AppActionTypes.RequestPrompt;
7779
data: { prompt: Prompt; promptRequestNumber: number };
7880
};
81+
82+
export type VoteForAction = Pick<AppAction, 'type' | 'data'> & {
83+
type: AppActionTypes.VoteFor;
84+
data: AppDataRef<VoteAppData['data']>;
85+
};
86+
87+
export type RemoveVoteAction = Pick<AppAction, 'type' | 'data'> & {
88+
type: AppActionTypes.RemoveVote;
89+
data: AppDataRef<VoteAppData['data']>;
90+
};

src/hooks/useActions.ts

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,16 @@ import {
1111
PauseActivityAction,
1212
PlayActivityAction,
1313
PreviousStepAction,
14+
RemoveVoteAction,
1415
RequestPromptAction,
1516
SubmitNewResponseAction,
17+
VoteForAction,
1618
} from '@/config/appActionsTypes';
17-
import { CurrentStateData, ResponseAppData } from '@/config/appDataTypes';
19+
import {
20+
CurrentStateData,
21+
ResponseAppData,
22+
VoteAppData,
23+
} from '@/config/appDataTypes';
1824
import { mutations } from '@/config/queryClient';
1925
import { ActivityStep } from '@/interfaces/interactionProcess';
2026
import { LocalContext } from '@graasp/sdk';
@@ -33,6 +39,8 @@ interface UseActionsValues {
3339
postPlayActivityAction: (data?: Data) => void;
3440
postPauseActivityAction: (data?: Data) => void;
3541
postRequestPromptAction: (data: RequestPromptAction['data']) => void;
42+
postVoteForAction: (data: VoteAppData) => void;
43+
postRemoveVoteAction: (data: VoteAppData) => void;
3644
}
3745

3846
const useActions = (): UseActionsValues => {
@@ -103,6 +111,35 @@ const useActions = (): UseActionsValues => {
103111
// [postAppAction],
104112
// );
105113

114+
const postVoteForAction = useCallback(
115+
(data: VoteAppData): void => {
116+
const action: VoteForAction = {
117+
type: AppActionTypes.VoteFor,
118+
data: {
119+
id: data.id,
120+
type: data.type,
121+
data: data.data,
122+
},
123+
};
124+
postAppAction(action);
125+
},
126+
[postAppAction],
127+
);
128+
const postRemoveVoteAction = useCallback(
129+
(data: VoteAppData): void => {
130+
const action: RemoveVoteAction = {
131+
type: AppActionTypes.RemoveVote,
132+
data: {
133+
id: data.id,
134+
type: data.type,
135+
data: data.data,
136+
},
137+
};
138+
postAppAction(action);
139+
},
140+
[postAppAction],
141+
);
142+
106143
const postNextStepAction = useCallback(
107144
(step: ActivityStep, stepIndex: number) => {
108145
const action: NextStepAction = {
@@ -175,6 +212,8 @@ const useActions = (): UseActionsValues => {
175212
postPlayActivityAction,
176213
postPauseActivityAction,
177214
postRequestPromptAction,
215+
postVoteForAction,
216+
postRemoveVoteAction,
178217
};
179218
};
180219

src/modules/context/VoteContext.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { EvaluationParameters } from '@/interfaces/evaluation';
44
import { AppDataTypes, VoteAppData } from '@/config/appDataTypes';
55
import { useLocalContext } from '@graasp/apps-query-client';
66
import { AppDataVisibility } from '@graasp/sdk';
7+
import useActions from '@/hooks/useActions';
78
import { useAppDataContext } from './AppDataContext';
89

910
type VoteContextType = {
@@ -41,7 +42,8 @@ export const VoteProvider: FC<VoteContextProps> = ({
4142
[evaluationParameters],
4243
);
4344

44-
const { appData, postAppData, deleteAppData } = useAppDataContext();
45+
const { appData, postAppDataAsync, deleteAppDataAsync } = useAppDataContext();
46+
const { postVoteForAction, postRemoveVoteAction } = useActions();
4547

4648
const allVotes = useMemo(
4749
() =>
@@ -72,10 +74,14 @@ export const VoteProvider: FC<VoteContextProps> = ({
7274
responseRef: responseId,
7375
},
7476
};
75-
postAppData(newAppData);
77+
postAppDataAsync(newAppData).then((recordedAppData) => {
78+
if (recordedAppData) {
79+
postVoteForAction(recordedAppData as VoteAppData);
80+
}
81+
});
7682
}
7783
},
78-
[availableVotes, postAppData],
84+
[availableVotes, postAppDataAsync, postVoteForAction],
7985
);
8086

8187
const findVoteFor = useCallback(
@@ -88,12 +94,14 @@ export const VoteProvider: FC<VoteContextProps> = ({
8894
async (responseId: string): Promise<void> => {
8995
const voteToRemove = findVoteFor(responseId);
9096
if (voteToRemove) {
91-
deleteAppData({ id: voteToRemove?.id });
97+
deleteAppDataAsync({ id: voteToRemove?.id }).then(() => {
98+
postRemoveVoteAction(voteToRemove);
99+
});
92100
} else {
93101
throw Error(`Response with id ${responseId} not found.`);
94102
}
95103
},
96-
[deleteAppData, findVoteFor],
104+
[deleteAppDataAsync, findVoteFor, postRemoveVoteAction],
97105
);
98106

99107
const checkIfHasVote = useCallback(

0 commit comments

Comments
 (0)