Skip to content

Commit

Permalink
Create langPref validator, send langPref param to backend
Browse files Browse the repository at this point in the history
  • Loading branch information
Andrewy-gh committed Sep 17, 2024
1 parent 17457cb commit cc2f282
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
13 changes: 9 additions & 4 deletions frontend/front/src/api/apiSlice.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";

import { AUTHORIZATION_HEADER } from "../features/login/loginUtils";
import { transformSurveyorKeys } from "../features/surveyor/surveyorUtils";
import { validateLanguage } from "../util/surveyUtils";

const baseUrl = process.env.REACT_APP_API_URL || "http://localhost:3000";

Expand Down Expand Up @@ -140,10 +141,14 @@ export const apiSlice = createApi({
],
}),
getSurveyStructure: builder.query({
query: (id) => ({
url: `/surveys/${id}`,
method: "GET",
}),
query: (id) => {
const validatedLangPref = validateLanguage();
return {
url: `/surveys/${id}`,
method: "GET",
params: { langPref: validatedLangPref },
};
},
providesTags: (result, error, arg) => [{ type: "Survey", id: arg }],
}),
createSurvey: builder.mutation({
Expand Down
12 changes: 12 additions & 0 deletions frontend/front/src/util/surveyUtils.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
// Current supported survey languages
const supportedLanguages = ["en"];

export const validateLanguage = () => {
const langPref = localStorage.getItem("langPref");
if (typeof langPref !== "string") {
return "en";
}
const generalLang = langPref.split("-")[0];
return supportedLanguages.includes(generalLang) ? generalLang : "en";
};

export const buildSurveyCacheKey = (surveyId, homeId) =>
`survey${surveyId}-home${homeId}`;

Expand Down

0 comments on commit cc2f282

Please sign in to comment.