Skip to content

Commit

Permalink
chore: reset quiz kyc after app restart
Browse files Browse the repository at this point in the history
  • Loading branch information
ice-orion committed Feb 5, 2024
1 parent 184549c commit 360bb05
Show file tree
Hide file tree
Showing 4 changed files with 44 additions and 4 deletions.
6 changes: 5 additions & 1 deletion src/store/modules/Quiz/reducer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ export interface State {
quiz: Quiz | null;
status: QuizStatus | null;
quizNotificationShownIndex: number;
hasUnfinishedQuiz: boolean;
}

type Actions = ReturnType<
Expand All @@ -26,13 +27,15 @@ const INITIAL_STATE: State = {
quiz: null,
status: null,
quizNotificationShownIndex: -1,
hasUnfinishedQuiz: false,
};

function reducer(state = INITIAL_STATE, action: Actions): State {
return produce(state, draft => {
switch (action.type) {
case QuizActions.START_OR_CONTINUE_QUIZ.SUCCESS.type:
draft.quiz = action.payload.quiz;
draft.hasUnfinishedQuiz = true;
break;
case QuizActions.CHECK_QUIZ_STATUS.SUCCESS.type:
draft.status = action.payload.status;
Expand All @@ -43,6 +46,7 @@ function reducer(state = INITIAL_STATE, action: Actions): State {
case QuizActions.START_OR_CONTINUE_QUIZ.COMPLETED.type:
case QuizActions.RESET_QUIZ_KYC_STEP.START.type:
draft.quiz = null;
draft.hasUnfinishedQuiz = false;
break;
case AccountActions.SIGN_OUT.SUCCESS.type:
return {...INITIAL_STATE};
Expand All @@ -54,7 +58,7 @@ export const quizReducer = persistReducer(
{
key: 'quiz',
storage: AsyncStorage,
whitelist: ['quizNotificationShownIndex'],
whitelist: ['quizNotificationShownIndex', 'hasUnfinishedQuiz'],
},
reducer,
);
8 changes: 7 additions & 1 deletion src/store/modules/Quiz/sagas/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,11 @@ export const quizWatchers = [
],
showQuizNotificationSaga,
),
takeLatest(QuizActions.RESET_QUIZ_KYC_STEP.START.type, resetQuizKycStepSaga),
takeLatest(
[
AppCommonActions.APP_INITIALIZED.SUCCESS.type,
QuizActions.RESET_QUIZ_KYC_STEP.START.type,
],
resetQuizKycStepSaga,
),
];
30 changes: 28 additions & 2 deletions src/store/modules/Quiz/sagas/resetQuizKycStep.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,38 @@

import {Api} from '@api/index';
import {QUIZ_KYC_STEP} from '@api/tokenomics/constants';
import {userIdSelector} from '@store/modules/Account/selectors';
import {
isAuthorizedSelector,
userIdSelector,
} from '@store/modules/Account/selectors';
import {AppCommonActions} from '@store/modules/AppCommon/actions';
import {QuizActions} from '@store/modules/Quiz/actions';
import {hasUnfinishedQuizSelector} from '@store/modules/Quiz/selectors';
import {getErrorMessage} from '@utils/errors';
import {call, put, SagaReturnType, select} from 'redux-saga/effects';

export function* resetQuizKycStepSaga() {
type Action = ReturnType<
| typeof AppCommonActions.APP_INITIALIZED.SUCCESS.create
| typeof QuizActions.RESET_QUIZ_KYC_STEP.START.create
>;

export function* resetQuizKycStepSaga(action: Action) {
const isAuthorized: ReturnType<typeof isAuthorizedSelector> = yield select(
isAuthorizedSelector,
);

if (!isAuthorized) {
return;
}

if (action.type === AppCommonActions.APP_INITIALIZED.SUCCESS.type) {
const hasUnfinishedQuiz: ReturnType<typeof hasUnfinishedQuizSelector> =
yield select(hasUnfinishedQuizSelector);
if (!hasUnfinishedQuiz) {
return;
}
}

const userId: ReturnType<typeof userIdSelector> = yield select(
userIdSelector,
);
Expand Down
4 changes: 4 additions & 0 deletions src/store/modules/Quiz/selectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,7 @@ export const quizAttemptsLeftSelector = (state: RootState) => {
export const quizNotificationShownIndexSelector = (state: RootState) => {
return state.quiz.quizNotificationShownIndex;
};

export const hasUnfinishedQuizSelector = (state: RootState) => {
return state.quiz.hasUnfinishedQuiz;
};

0 comments on commit 360bb05

Please sign in to comment.