From 360bb05388293b3288ed05e0a7550ca1b634cb50 Mon Sep 17 00:00:00 2001 From: ice-orion <102020833+ice-orion@users.noreply.github.com> Date: Mon, 5 Feb 2024 14:17:29 +0400 Subject: [PATCH] chore: reset quiz kyc after app restart --- src/store/modules/Quiz/reducer/index.ts | 6 +++- src/store/modules/Quiz/sagas/index.ts | 8 ++++- .../modules/Quiz/sagas/resetQuizKycStep.ts | 30 +++++++++++++++++-- src/store/modules/Quiz/selectors/index.ts | 4 +++ 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/src/store/modules/Quiz/reducer/index.ts b/src/store/modules/Quiz/reducer/index.ts index e96c833d5..3dac4afdf 100644 --- a/src/store/modules/Quiz/reducer/index.ts +++ b/src/store/modules/Quiz/reducer/index.ts @@ -11,6 +11,7 @@ export interface State { quiz: Quiz | null; status: QuizStatus | null; quizNotificationShownIndex: number; + hasUnfinishedQuiz: boolean; } type Actions = ReturnType< @@ -26,6 +27,7 @@ const INITIAL_STATE: State = { quiz: null, status: null, quizNotificationShownIndex: -1, + hasUnfinishedQuiz: false, }; function reducer(state = INITIAL_STATE, action: Actions): State { @@ -33,6 +35,7 @@ function reducer(state = INITIAL_STATE, action: Actions): State { 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; @@ -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}; @@ -54,7 +58,7 @@ export const quizReducer = persistReducer( { key: 'quiz', storage: AsyncStorage, - whitelist: ['quizNotificationShownIndex'], + whitelist: ['quizNotificationShownIndex', 'hasUnfinishedQuiz'], }, reducer, ); diff --git a/src/store/modules/Quiz/sagas/index.ts b/src/store/modules/Quiz/sagas/index.ts index a60eeeec4..38e8d81b2 100644 --- a/src/store/modules/Quiz/sagas/index.ts +++ b/src/store/modules/Quiz/sagas/index.ts @@ -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, + ), ]; diff --git a/src/store/modules/Quiz/sagas/resetQuizKycStep.ts b/src/store/modules/Quiz/sagas/resetQuizKycStep.ts index 51c092e92..da87b8b42 100644 --- a/src/store/modules/Quiz/sagas/resetQuizKycStep.ts +++ b/src/store/modules/Quiz/sagas/resetQuizKycStep.ts @@ -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 = yield select( + isAuthorizedSelector, + ); + + if (!isAuthorized) { + return; + } + + if (action.type === AppCommonActions.APP_INITIALIZED.SUCCESS.type) { + const hasUnfinishedQuiz: ReturnType = + yield select(hasUnfinishedQuizSelector); + if (!hasUnfinishedQuiz) { + return; + } + } + const userId: ReturnType = yield select( userIdSelector, ); diff --git a/src/store/modules/Quiz/selectors/index.ts b/src/store/modules/Quiz/selectors/index.ts index 63dfde7d7..141fe7a78 100644 --- a/src/store/modules/Quiz/selectors/index.ts +++ b/src/store/modules/Quiz/selectors/index.ts @@ -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; +};