From bc39df27944f246e7bd34d8d5ff8ad32d60a77c2 Mon Sep 17 00:00:00 2001 From: Abhishek Kumar Date: Sun, 19 May 2024 15:09:23 +0530 Subject: [PATCH] Update EF of words in the entire questionaire when one instance of the word is updated Signed-off-by: Abhishek Kumar --- webapp/src/PlayPage/Word.tsx | 10 ++++++++++ webapp/src/PlayPage/playlistSlice.ts | 26 ++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 2 deletions(-) diff --git a/webapp/src/PlayPage/Word.tsx b/webapp/src/PlayPage/Word.tsx index aa594c5..678d519 100644 --- a/webapp/src/PlayPage/Word.tsx +++ b/webapp/src/PlayPage/Word.tsx @@ -18,6 +18,8 @@ import { CorrectedWordScoreType } from './Play'; import { useTranslation } from 'react-i18next'; import { useAuth0 } from '@auth0/auth0-react'; import { updateWordEasinessFactor } from '../queries'; +import { useAppDispatch } from '../redux/hooks'; +import { updatedEF } from './playlistSlice'; export default function Word({ wordText, wordScore }: WordProps) { const { color, setEF } = useWordColor(wordScore); @@ -66,6 +68,8 @@ function WordPopover({ wordScore.score.easinessFactor, ]); + const dispatch = useAppDispatch(); + const MIN_EF = 1.3; const DEFAULT_EF = 2.5; const MAX_EF = Math.max(3.0, wordScore.score.easinessFactor); @@ -75,6 +79,12 @@ function WordPopover({ const token = await getAccessTokenSilently(); await updateWordEasinessFactor(token, wordScore._id, ef); setParentEF(ef); + dispatch( + updatedEF({ + id: wordScore._id, + ef, + }), + ); }; const marks = [ diff --git a/webapp/src/PlayPage/playlistSlice.ts b/webapp/src/PlayPage/playlistSlice.ts index 383a359..6032887 100644 --- a/webapp/src/PlayPage/playlistSlice.ts +++ b/webapp/src/PlayPage/playlistSlice.ts @@ -14,6 +14,11 @@ interface ScoreUpdateInfo { grade: GradeType; } +interface EFUpdateInfo { + id: string; + ef: number; +} + const initialState: PlaylistState = { sentences: [], }; @@ -46,12 +51,29 @@ const playlistSlice = createSlice({ }); }, + updatedEF(state, action: PayloadAction) { + const { id: wordId, ef } = action.payload; + state.sentences.forEach((sentence, sidx) => { + sentence.words.forEach((word, widx) => { + if (word._id !== wordId) { + return; + } + + state.sentences[sidx].words[widx].score.easinessFactor = ef; + }); + }); + }, + destroyedPlaylist(state) { state.sentences = []; }, }, }); -export const { createdPlaylist, updatedWordScore, destroyedPlaylist } = - playlistSlice.actions; +export const { + createdPlaylist, + updatedWordScore, + updatedEF, + destroyedPlaylist, +} = playlistSlice.actions; export default playlistSlice.reducer;