Skip to content

Commit 31c66dc

Browse files
authored
Merge pull request #475 from BinaryStudioAcademy/472-fix-selected-plan-style-enhancement
fix: Selected plan style enhancement cy-472
2 parents 8b1bbb2 + 7763ddd commit 31c66dc

File tree

3 files changed

+21
-2
lines changed

3 files changed

+21
-2
lines changed

apps/frontend/src/modules/auth/slices/actions.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { AppRoute, ErrorMessage } from "~/libs/enums/enums.js";
55
import { HTTPError } from "~/libs/modules/http/http.js";
66
import { StorageKey } from "~/libs/modules/storage/storage.js";
77
import { type AsyncThunkConfig } from "~/libs/types/types.js";
8+
import { actions as planActions } from "~/modules/plans/plans.js";
89
import {
910
type ForgotPasswordRequestDto,
1011
type ResetPasswordRequestDto,
@@ -177,11 +178,13 @@ const logout = createAsyncThunk<null, undefined, AsyncThunkConfig>(
177178

178179
try {
179180
await storage.drop(StorageKey.TOKEN);
181+
await storage.drop(StorageKey.PLAN_ID);
180182
} catch {
181183
notifications.error(MESSAGES.AUTH.LOGOUT_FAILED);
182184
}
183185

184186
dispatch(authSliceActions.resetAuthState());
187+
dispatch(planActions.resetPlanState());
185188

186189
return null;
187190
},

apps/frontend/src/modules/plans/slices/actions.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { createAsyncThunk } from "@reduxjs/toolkit";
22

3-
import { StorageKey } from "~/libs/modules/storage/storage.js";
3+
import { storage, StorageKey } from "~/libs/modules/storage/storage.js";
44
import { type AsyncThunkConfig } from "~/libs/types/types.js";
55

66
import {
@@ -77,7 +77,11 @@ const getPlan = createAsyncThunk<
7777
>(`${sliceName}/get`, async (_, { extra }) => {
7878
const { planApi } = extra;
7979

80-
const plan = await planApi.getByUserId();
80+
const currentPlanId = (await storage.get(StorageKey.PLAN_ID)) as string;
81+
82+
const plan = currentPlanId
83+
? await planApi.findWithRelations(Number.parseInt(currentPlanId))
84+
: await planApi.getByUserId();
8185

8286
return plan;
8387
});

apps/frontend/src/modules/plans/slices/plan.slice.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { createSlice, isAnyOf, type PayloadAction } from "@reduxjs/toolkit";
22
import { type TaskDto } from "shared";
33

44
import { DataStatus, PlanStyle } from "~/libs/enums/enums.js";
5+
import { storage, StorageKey } from "~/libs/modules/storage/storage.js";
56
import { type PlanStyleOption, type ValueOf } from "~/libs/types/types.js";
67

78
import { type PlanWithCategoryDto } from "../libs/types/types.js";
@@ -166,8 +167,19 @@ const { actions, name, reducer } = createSlice({
166167
].tasks.filter((task) => task.id !== taskId);
167168
}
168169
},
170+
resetPlanState: (state) => {
171+
state.dataStatus = initialState.dataStatus;
172+
state.days = initialState.days;
173+
state.pendingDayRegenerations = initialState.pendingDayRegenerations;
174+
state.pendingTaskRegenerations = initialState.pendingTaskRegenerations;
175+
state.plan = initialState.plan;
176+
state.selectedStyle = initialState.selectedStyle;
177+
state.userPlans = initialState.userPlans;
178+
state.userPlansDataStatus = initialState.userPlansDataStatus;
179+
},
169180
setCurrentPlan: (state, action: PayloadAction<PlanWithCategoryDto>) => {
170181
state.plan = action.payload;
182+
void storage.set(StorageKey.PLAN_ID, action.payload.id.toString());
171183
},
172184
setSelectedStyle: (state, action: PayloadAction<PlanStyleOption>) => {
173185
state.selectedStyle = action.payload;

0 commit comments

Comments
 (0)