Skip to content

Commit

Permalink
Merge pull request #170 from CovOpen/persist_questionnaire_selection
Browse files Browse the repository at this point in the history
fix: persist questionnaire selection
  • Loading branch information
DanielHabenicht authored Apr 11, 2021
2 parents da2fdbe + e7c27fe commit c492cd3
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
9 changes: 9 additions & 0 deletions covquestions-editor-app/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ import { useSelector } from "react-redux";
import { getAllQuestionnaires, getQuestionnaireByIdVersionAndLanguage } from "./api/api-client";
import { QuestionnaireBaseData } from "./models/QuestionnairesList";
import { SettingSelection } from "./components/questionnaireSelection/SettingSelection";
import { getQueryParams, setQueryParams } from "./utils/queryParams";

const theme = createMuiTheme({
palette: {
Expand Down Expand Up @@ -149,6 +150,8 @@ export const App: React.FC = () => {
currentQuestionnaireSelection.version !== undefined &&
currentQuestionnaireSelection.language !== undefined
) {
setQueryParams(currentQuestionnaireSelection);

getQuestionnaireByIdVersionAndLanguage(
currentQuestionnaireSelection.id,
currentQuestionnaireSelection.version,
Expand Down Expand Up @@ -181,6 +184,12 @@ export const App: React.FC = () => {
}
}, [currentQuestionnaire, hasAnyError]);

// Select Questionnaire that is saved in the query params
const querySelection: QuestionnaireSelection = getQueryParams();
if (querySelection.id != null && currentQuestionnaireSelection.id == null) {
setCurrentQuestionnaireSelection(querySelection);
}

return (
<ThemeProvider theme={theme}>
<AppBar position="static">
Expand Down
20 changes: 20 additions & 0 deletions covquestions-editor-app/src/utils/queryParams.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { QuestionnaireSelection } from "../components/questionnaireSelection/QuestionnaireSelection";

let persistedParameters = ["id", "version", "language"];

export function getQueryParams(): QuestionnaireSelection {
const queryParams = new URLSearchParams(window.location.search);
return persistedParameters.reduce((obj, key) => {
return { ...obj, [key]: queryParams.get(key) };
}, {});
}

export function setQueryParams(selection: QuestionnaireSelection): void {
const queryParams = new URLSearchParams(window.location.search);
persistedParameters.forEach((key) => {
if ((selection as any)[key] != null) {
queryParams.set(key, (selection as any)[key]);
}
});
window.history.replaceState({}, "", `${window.location.pathname}?${queryParams}`);
}

0 comments on commit c492cd3

Please sign in to comment.