Skip to content

Commit 0b96997

Browse files
fix: attach accept-language header on all requests
1 parent 3477ac8 commit 0b96997

File tree

8 files changed

+121
-32
lines changed

8 files changed

+121
-32
lines changed

src/components/sections/learning-modules/InteractiveModule/index.tsx

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,10 @@ import InteractiveModuleItem from "@/components/sections/learning-modules/Intera
1010
import { authCheck } from "@/store/feature/auth.slice";
1111
import { Course, InteractiveModule as InteractiveModuleType } from "@/types/course";
1212
import { hidePageNavigation, showPageNavigation } from "@/store/feature/communities/navigation.slice";
13-
import { checkAnswer, submitModuleAnswer } from "@/store/feature/learningModules.slice";
13+
// import { checkAnswer } from "@/store/feature/learningModules.slice";
1414
import { IRootState } from "@/store";
15+
import { checkAnswer, submitModuleAnswer } from "@/store/services/learningModules.service";
16+
import { useRouter } from "next/router";
1517

1618
/**
1719
* interface for InteractiveModule multiSelector
@@ -51,6 +53,7 @@ export default function InteractiveModule({ data }: interactiveModuleProps): Rea
5153
const [ended, setEnded] = useState(false);
5254
const [loading, setLoading] = useState(true);
5355
const [answering, setAnswering] = useState(false);
56+
const router = useRouter()
5457
const dispatch = useDispatch();
5558

5659
const { isLoggedIn, course } = useMultiSelector<unknown, InteractiveModuleMultiSelector>({
@@ -62,7 +65,7 @@ export default function InteractiveModule({ data }: interactiveModuleProps): Rea
6265
const checkIfAnswered = useCallback(async () => {
6366
try {
6467
if (!isLoggedIn) return;
65-
const answers = await checkAnswer(data.ref);
68+
const answers = await checkAnswer({ ref: data.ref, locale: router.locale });
6669
if (!answers.length) return;
6770
setCurrent(items.length);
6871
setEnded(true);

src/pages/communities/[slug]/challenges/[challenge_id]/learning-modules/[id].tsx

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import Head from "next/head";
44
import { useDispatch } from "@/hooks/useTypedDispatch";
55
import { Community } from "@/types/community";
66
import { Challenge, LearningModule } from "@/types/course";
7-
import { findLearningModule, setCurrentLearningModule } from "@/store/feature/learningModules.slice";
7+
import { setCurrentLearningModule } from "@/store/feature/learningModules.slice";
88
import { setCurrentCommunity } from "@/store/feature/community.slice";
99
import { getMetadataDescription, getMetadataTitle } from "@/utilities/Metadata";
1010
import DefaultLayout from "@/components/layout/Default";
@@ -22,6 +22,8 @@ import { fetchChallenge } from "@/store/services/communities/challenges";
2222
import PageNavigation from "@/components/sections/courses/PageNavigation";
2323
import ChallengeCard from "@/components/cards/challenge/Challenge";
2424
import { useTranslation } from "next-i18next";
25+
import { findLearningModule } from "@/store/services/learningModules.service";
26+
2527

2628
/**
2729
* Learning module page props interfae
@@ -110,10 +112,10 @@ export const getServerSideProps: GetServerSideProps = wrapper.getServerSideProps
110112
const challenge_id = params?.challenge_id as string;
111113
const learningModule_id = params?.id as string;
112114

113-
const [{ data: community }, { data: challenge }, { payload: learningModule }, translations] = await Promise.all([
115+
const [{ data: community }, { data: challenge }, { data: learningModule }, translations] = await Promise.all([
114116
store.dispatch(fetchCurrentCommunity({ slug: communitySlug, locale })),
115-
store.dispatch(fetchChallenge({ id: challenge_id, relations: ["learning-modules", "rubric"] })),
116-
store.dispatch(findLearningModule(learningModule_id)),
117+
store.dispatch(fetchChallenge({ id: challenge_id, relations: ["learning-modules", "rubric"], locale })),
118+
store.dispatch(findLearningModule({ id: learningModule_id, locale })),
117119
serverSideTranslations(locale as string),
118120
]);
119121

src/pages/communities/[slug]/courses/[course_slug]/learning-modules/[id].tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import Head from "next/head";
44
import { useDispatch } from "@/hooks/useTypedDispatch";
55
import { Community } from "@/types/community";
66
import { Challenge, Course, LearningModule } from "@/types/course";
7-
import { findLearningModule } from "@/store/feature/learningModules.slice";
87
import { getMetadataDescription, getMetadataTitle } from "@/utilities/Metadata";
98
import DefaultLayout from "@/components/layout/Default";
109
import Header from "@/components/sections/learning-modules/Header";
@@ -20,6 +19,7 @@ import ChallengeCard from "@/components/cards/challenge/Challenge";
2019
import { useTranslation } from "next-i18next";
2120
import PageNavigation from "@/components/sections/courses/PageNavigation";
2221
import { NotFoundError } from "@/utilities/errors/NotFoundError";
22+
import { findLearningModule } from "@/store/services/learningModules.service";
2323

2424
/**
2525
* Learning module page props interfae
@@ -108,11 +108,11 @@ export const getServerSideProps = wrapper.getServerSideProps((store) => async ({
108108
const communitySlug = params?.slug as string;
109109
const courseSlug = params?.course_slug as string;
110110
const id = params?.id as string;
111-
112-
const [{ data: community }, { data: course }, { payload: learningModule }, translations] = await Promise.all([
111+
console.log("me sending data")
112+
const [{ data: community }, { data: course }, { data: learningModule }, translations] = await Promise.all([
113113
store.dispatch(fetchCurrentCommunity({ slug: communitySlug, locale })),
114114
store.dispatch(fetchCourse({ slug: courseSlug, locale })),
115-
store.dispatch(findLearningModule(id)),
115+
store.dispatch(findLearningModule({ id, locale })),
116116
serverSideTranslations(locale as string),
117117
]);
118118
if (!community || !course || !learningModule) throw new NotFoundError();

src/pages/communities/[slug]/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const getServerSideProps = wrapper.getServerSideProps((store) => async ({
4747

4848
const [{ data: community }, { data: challenges }, { data: scoreboard }, translations] = await Promise.all([
4949
store.dispatch(fetchCurrentCommunity({ slug, locale })),
50-
store.dispatch(fetchAllChallenges({ slug })),
50+
store.dispatch(fetchAllChallenges({ slug, locale })),
5151
store.dispatch(fetchAllScoreboards({ slug, locale: locale || "en" })),
5252
serverSideTranslations(locale as string),
5353
]);

src/store/feature/learningModules.slice.ts

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,12 @@ export const learningModulesSlice = createSlice({
3333
},
3434
extraReducers: (builder) => {
3535
builder
36-
.addCase(findLearningModule.fulfilled, (state, action) => {
37-
state.current = action.payload;
38-
})
39-
.addCase(getAllLearningModules.fulfilled, (state, action) => {
40-
state.list = action.payload;
41-
})
36+
// .addCase(findLearningModule.fulfilled, (state, action) => {
37+
// state.current = action.payload;
38+
// })
39+
// .addCase(getAllLearningModules.fulfilled, (state, action) => {
40+
// state.list = action.payload;
41+
// })
4242
.addCase(HYDRATE, (state, action) => {
4343
return {
4444
...state,
@@ -50,15 +50,15 @@ export const learningModulesSlice = createSlice({
5050
},
5151
});
5252

53-
export const findLearningModule = createAsyncThunk("learningModules/find", async (id: string) => {
54-
const { data } = await api().server.get(`learning-modules/${id}`);
55-
return data;
56-
});
53+
// export const findLearningModule = createAsyncThunk("learningModules/find", async (id: string) => {
54+
// const { data } = await api().server.get(`learning-modules/${id}`);
55+
// return data;
56+
// });
5757

58-
export const getAllLearningModules = createAsyncThunk("learningModules/all", async (slug: string) => {
59-
const { data } = await api().server.get<LearningModule[]>(`courses/${slug}/learning-modules`);
60-
return data;
61-
});
58+
// export const getAllLearningModules = createAsyncThunk("learningModules/all", async (slug: string) => {
59+
// const { data } = await api().server.get<LearningModule[]>(`courses/${slug}/learning-modules`);
60+
// return data;
61+
// });
6262

6363
export const submitModuleAnswer = createAsyncThunk("learningModules/submitAnswer", async ({ ref, course }: { ref: string; course: string }) => {
6464
await api().client.put("interactive-modules/answer", {

src/store/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ import challengeService from "@/store/services/communities/challenges";
4747
import scoreboardService from "./services/communities/scoreboard.service";
4848
import communitiesProfile from "./feature/profile/communities.slice";
4949
import reputationSlice from "./feature/profile/reputation.slice";
50+
import { learningModulesService } from "./services/learningModules.service";
5051

5152
export interface IRootState {
5253
communities: ReturnType<typeof communities.reducer>;
@@ -95,6 +96,7 @@ export interface IRootState {
9596
teams: ReturnType<typeof teamsSlice.reducer>;
9697
teamsService: ReturnType<typeof teamsService.reducer>;
9798
invites: ReturnType<typeof invitesSlice.reducer>;
99+
learningModuleService: ReturnType<typeof learningModulesService.reducer>;
98100
}
99101

100102
export const makeStore = () =>
@@ -145,6 +147,7 @@ export const makeStore = () =>
145147
[invitesSlice.name]: invitesSlice.reducer,
146148
[communitiesProfile.name]: communitiesProfile.reducer,
147149
[reputationSlice.name]: reputationSlice.reducer,
150+
[learningModulesService.reducerPath]: learningModulesService.reducer,
148151
},
149152

150153
middleware: (getDefaultMiddleware) => {
@@ -164,7 +167,8 @@ export const makeStore = () =>
164167
authService.middleware,
165168
teamsService.middleware,
166169
challengeService.middleware,
167-
scoreboardService.middleware
170+
scoreboardService.middleware,
171+
learningModulesService.middleware
168172
);
169173
},
170174
});

src/store/services/communities/challenges/index.ts

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,13 @@ export const challengeService = createApi({
1616
refetchOnMountOrArgChange: true,
1717
endpoints: (builder) => ({
1818
findChallengeById: builder.query({
19-
query: ({ id, relations }) => {
19+
query: ({ id, relations, locale }) => {
2020
const params = queryString.stringify({ relations: relations || [] }, { arrayFormat: "bracket" });
2121
return {
2222
url: `challenges/${id}?${params}`,
23+
headers: {
24+
"accept-language": locale,
25+
},
2326
};
2427
},
2528
onQueryStarted: async (_, { dispatch, queryFulfilled }) => {
@@ -36,7 +39,14 @@ export const challengeService = createApi({
3639
}),
3740

3841
getAllChallenges: builder.query({
39-
query: (slug) => `communities/${slug}/challenges`,
42+
query: ({ slug, locale }) => {
43+
return {
44+
url: `communities/${slug}/challenges`,
45+
headers: {
46+
"accept-language": locale,
47+
},
48+
};
49+
},
4050
onQueryStarted: async (_, { dispatch, queryFulfilled }) => {
4151
try {
4252
const { data } = await queryFulfilled;
@@ -49,10 +59,13 @@ export const challengeService = createApi({
4959
}),
5060

5161
fetchChallengeByIdAuthenticated: builder.query({
52-
query: ({ id, relations }) => {
62+
query: ({ id, relations, locale }) => {
5363
const params = queryString.stringify({ relations: relations || [] }, { arrayFormat: "bracket" });
5464
return {
5565
url: `challenges/${id}?${params}`,
66+
headers: {
67+
"accept-language": locale,
68+
},
5669
};
5770
},
5871
onQueryStarted: async (_, { dispatch, queryFulfilled }) => {
@@ -95,14 +108,15 @@ export const challengeService = createApi({
95108
}),
96109
});
97110

98-
export const fetchChallenge = ({ id, relations }: { id: string; relations?: string[] }) =>
111+
export const fetchChallenge = ({ id, relations, locale }: { id: string; relations?: string[]; locale?: string }) =>
99112
challengeService.endpoints.findChallengeById.initiate({
100113
id,
101114
relations: relations || [],
115+
locale,
102116
});
103117

104-
export const fetchAllChallenges = ({ slug }: { slug: string }) => {
105-
return challengeService.endpoints.getAllChallenges.initiate(slug);
118+
export const fetchAllChallenges = ({ slug, locale }: { slug: string; locale?: string }) => {
119+
return challengeService.endpoints.getAllChallenges.initiate({ slug, locale });
106120
};
107121

108122
/**
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import { createApi } from "@reduxjs/toolkit/query/react";
2+
import baseQuery from "@/config/baseQuery";
3+
4+
export const learningModulesService = createApi({
5+
reducerPath: "learningModulesService",
6+
baseQuery: baseQuery(),
7+
refetchOnMountOrArgChange: true,
8+
endpoints: (builder) => ({
9+
findLearningModule: builder.query({
10+
query: ({ id, locale }: any) => ({
11+
url: `learning-modules/${id}`,
12+
headers: {
13+
"accept-language": locale,
14+
},
15+
}),
16+
}),
17+
18+
getAllLearningModules: builder.query({
19+
query: ({ slug, locale }: { slug: string; locale?: string }) => ({
20+
url: `courses/${slug}/learning-modules}`,
21+
headers: {
22+
"accept-language": locale,
23+
},
24+
}),
25+
}),
26+
27+
submitModuleAnswer: builder.query({
28+
query: ({ ref, course, locale }: { ref: string; course: string; locale?: string }) => ({
29+
url: `interactive-modules/answer`,
30+
headers: {
31+
"accept-language": locale,
32+
},
33+
body: {
34+
module: ref,
35+
course,
36+
score: 100,
37+
},
38+
method: "PUT",
39+
}),
40+
}),
41+
42+
checkAnswer: builder.query({
43+
query: ({ ref, locale }: { ref: string; locale?: string }) => ({
44+
url: "interactive-modules/check-answer",
45+
body: {
46+
module: ref,
47+
},
48+
headers: {
49+
"accept-language": locale,
50+
},
51+
}),
52+
}),
53+
}),
54+
});
55+
56+
export const findLearningModule = ({ id, locale }: { id: string; locale?: string }) => learningModulesService.endpoints.findLearningModule.initiate({ locale, id });
57+
58+
export const getAllLearningModules = ({ slug, locale }: { slug: string; locale?: string }) => learningModulesService.endpoints.getAllLearningModules.initiate({ slug, locale });
59+
60+
export const submitModuleAnswer = ({ ref, course, locale }: { ref: string; course: string; locale?: string }) =>
61+
learningModulesService.endpoints.submitModuleAnswer.initiate({ ref, course, locale });
62+
63+
export const checkAnswer = ({ ref, locale }: { ref: string; locale?: string }) => {
64+
console.log("the locale from check answer in the learning modules");
65+
return learningModulesService.endpoints.checkAnswer.initiate({ ref, locale });
66+
};

0 commit comments

Comments
 (0)