Skip to content

Commit

Permalink
Merge pull request #256 from Tampere/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
jlaamanen authored Dec 30, 2024
2 parents 1e45740 + 5159346 commit 605fdf6
Show file tree
Hide file tree
Showing 7 changed files with 120 additions and 53 deletions.
1 change: 1 addition & 0 deletions client/src/components/admin/ConditionRow.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ export function ConditionRow({
/>
) : (
<Select
disabled={options.every((option) => option.id < 0)}
multiple
sx={{
flex: 1,
Expand Down
75 changes: 49 additions & 26 deletions client/src/components/admin/EditSurveySideBar.tsx
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
import {
Divider,
IconButton,
List,
ListItem,
ListItemButton,
ListItemIcon,
ListItemText,
Theme,
} from '@mui/material';
import {
Add,
ArrowBack,
ContentCopy,
ContentPaste,
DragIndicator,
FavoriteBorder,
InfoOutlined,
InsertDriveFileOutlined,
Language,
Mail,
Preview,
ContentCopy,
ContentPaste,
} from '@mui/icons-material';
import {
Divider,
IconButton,
List,
ListItem,
ListItemButton,
ListItemIcon,
ListItemText,
Theme,
} from '@mui/material';
import { makeStyles } from '@mui/styles';
import { useSurvey } from '@src/stores/SurveyContext';
import { useToasts } from '@src/stores/ToastContext';
Expand All @@ -31,12 +31,12 @@ import { useHistory, useRouteMatch } from 'react-router-dom';
import ListItemLink from '../ListItemLink';
import SideBar from '../SideBar';

import { Conditions, SurveyPage } from '@interfaces/survey';
import { useClipboard } from '@src/stores/ClipboardContext';
import {
replaceIdsWithNull,
replaceTranslationsWithNull,
} from '@src/utils/schemaValidation';
import { useClipboard } from '@src/stores/ClipboardContext';
import { SurveyPage } from '@interfaces/survey';
import { BranchIcon } from '../icons/BranchIcon';

const useStyles = makeStyles((theme: Theme) => ({
Expand Down Expand Up @@ -164,18 +164,36 @@ export default function EditSurveySideBar(props: Props) {
event.stopPropagation();
event.preventDefault();
// Deep copy page to avoid changes to current context
const copiedSurveyPage =
replaceTranslationsWithNull(
replaceIdsWithNull({
...structuredClone(page),
id: -1,
sidebar: {
...structuredClone(page.sidebar),
mapLayers: [],
},
}),
) as SurveyPage;

const deepCopy = replaceTranslationsWithNull(
replaceIdsWithNull({
...structuredClone(page),
id: -1,
sidebar: {
...structuredClone(page.sidebar),
mapLayers: [],
},
}),
) as SurveyPage;
// Remove conditions from Follow up question
const copiedSurveyPage: SurveyPage = {
...deepCopy,
sections: deepCopy.sections.map((section) => {
return {
...section,
followUpSections:
section.followUpSections?.map((fus) => {
return {
...fus,
conditions: {
equals: [],
lessThan: [],
greaterThan: [],
} as Conditions,
};
}),
};
}),
};
// Store section to locale storage for other browser tabs to get access to it
localStorage.setItem(
'clipboard-content',
Expand Down Expand Up @@ -274,6 +292,11 @@ export default function EditSurveySideBar(props: Props) {
message: tr.EditSurvey.pageAttached,
autoHideDuration: 30000,
});
showToast({
severity: 'warning',
message: tr.EditSurvey.checkConditionalSections,
autoHideDuration: 30000,
});
} catch (error) {
showToast({
severity: 'error',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import {
Conditions,
SurveyCheckboxQuestion,
SurveyDocumentSection,
SurveyFollowUpSection,
SurveyFreeTextQuestion,
SurveyGroupedCheckboxQuestion,
SurveyImageSection,
Expand Down Expand Up @@ -325,12 +327,27 @@ export default function SurveySectionAccordion(props: Props) {

// Remove all IDs from the section JSON to prevent unwanted references
// Create deep copy to avoid unwanted side effects on original
const copiedSurveySection = replaceTranslationsWithNull(

const deepCopy = replaceTranslationsWithNull(
replaceIdsWithNull({
...structuredClone(props.section),
}),
);

const copiedSurveySection: SurveyPageSection = {
...deepCopy,
followUpSections: deepCopy.followUpSections?.map(
(fus: SurveyFollowUpSection) => {
return {
...fus,
conditions: {
equals: [],
lessThan: [],
greaterThan: [],
} as Conditions,
};
},
),
};
// Store section to locale storage for other browser tabs to get access to it
localStorage.setItem(
'clipboard-content',
Expand Down
14 changes: 10 additions & 4 deletions client/src/stores/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"copyPage": "Copy page",
"attachNewPage": "Attach page",
"pageAttached": "Please note that selected map layers are not copied. Check map layer selections and fix published map url if needed.",
"pageCopied": "Page copied to clipboard. Use can now attach the page to any survey."
"pageCopied": "Page copied to clipboard. Use can now attach the page to any survey.",
"checkConditionalSections": "Please note that conditions for follow up questions are not copied."
},
"EditSurveyInfo": {
"title": "Survey title",
Expand Down Expand Up @@ -114,7 +115,10 @@
"warningNoAvailableMapLayers": "No available map layers found from the Oskari map publication.",
"mapLayers": "Map layers shown on the survey page",
"defaultMapView": "Default map view for this page",
"defaultMapViewStatus": { "set": "Set", "notSet": "Not set" },
"defaultMapViewStatus": {
"set": "Set",
"notSet": "Not set"
},
"defaultMapViewInfo": "Select the area from the map with mouse clicks.",
"mapViewButtons": {
"set": "Set",
Expand Down Expand Up @@ -163,7 +167,6 @@
"changeType": "Change question type",
"cancel": "Cancel"
},

"FileUpload": {
"deleteFile": "Delete file",
"addedFile": "Uploaded file",
Expand Down Expand Up @@ -593,7 +596,10 @@
"tooltipWeekLabel": "Week {x}, Year",
"tooltipTotal": "Total answers",
"tooltipCurrent": "Answers",
"xAxisLabel": { "byDay": "Day", "byWeek": "Week" },
"xAxisLabel": {
"byDay": "Day",
"byWeek": "Week"
},
"yAxisTotalLabel": "Total answers",
"yAxisLabel": "Answers"
}
Expand Down
9 changes: 6 additions & 3 deletions client/src/stores/fi.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,8 @@
"copyPage": "Kopioi sivu",
"attachNewPage": "Liitä sivu",
"pageAttached": "Huomaa, että karttatasovalinnat eivät kopioidu. Tarkista karttatasovalinnat ja korjaa tarvittaessa karttajulkaisuosoitetta.",
"pageCopied": "Sivu kopioitu leikepöydälle. Voit nyt liittää kopioidun sivun haluamallesi kyselylle."
"pageCopied": "Sivu kopioitu leikepöydälle. Voit nyt liittää kopioidun sivun haluamallesi kyselylle.",
"checkConditionalSections": "Huomaa, että jatkokysymyksien ehdot eivät kopioidu. Tarkista ehdot tarvittaessa."
},
"EditSurveyInfo": {
"title": "Kyselyn otsikko",
Expand Down Expand Up @@ -114,7 +115,10 @@
"warningNoAvailableMapLayers": "Oskari-karttajulkaisusta ei löytynyt näytettäviä karttatasoja.",
"mapLayers": "Sivulla näytettävät karttatasot",
"defaultMapView": "Kartan oletusnäkymä tällä sivulla",
"defaultMapViewStatus": { "set": "Asetettu", "notSet": "Ei asetettu" },
"defaultMapViewStatus": {
"set": "Asetettu",
"notSet": "Ei asetettu"
},
"defaultMapViewInfo": "Merkitse hiirellä klikkaamalla kartalta alue, jonka haluat näkyvän kysymyssivulla.",
"mapViewButtons": {
"set": "Aseta",
Expand Down Expand Up @@ -163,7 +167,6 @@
"changeType": "Vaihda kysymystyyppi",
"cancel": "Peruuta"
},

"FileUpload": {
"deleteFile": "Poista tiedosto",
"addedFile": "Valittu tiedosto",
Expand Down
24 changes: 18 additions & 6 deletions server/src/application/submission.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import {
MapQuestionAnswer,
Submission,
SurveyMapSubQuestionAnswer,
SurveyPageSection
SurveyPageSection,
} from '@interfaces/survey';
import {
getColumnSet,
Expand Down Expand Up @@ -91,7 +91,8 @@ async function validateEntriesByAnswerLimits(answerEntries: AnswerEntry[]) {
FROM data.page_section
WHERE
id = ANY ($1) AND
details->'answerLimits' IS NOT NULL`,
details->'answerLimits' IS NOT NULL AND
predecessor_section = NULL`,
[answerEntries.map((entry) => entry.sectionId)],
);
// Validate each entry against the question answer limits
Expand Down Expand Up @@ -834,17 +835,28 @@ export async function getSubmissionsForSurvey(surveyId: number) {
{ surveyId },
);
const result = [];
let currentSubmission: {id: number, timestamp: Date, entries: DBAnswerEntry[]} | null = null;
let currentSubmission: {
id: number;
timestamp: Date;
entries: DBAnswerEntry[];
} | null = null;
for (const row of rows) {
if (currentSubmission?.id !== row.submission_id) {
currentSubmission = {
id: row.submission_id,
timestamp: row.updated_at,
entries: []
}
entries: [],
};
result.push(currentSubmission);
}
currentSubmission.entries.push(row);
}
return result.map(x => ({id: x.id, timestamp: x.timestamp, answerEntries: dbAnswerEntriesToAnswerEntries(x.entries)} as Submission));
return result.map(
(x) =>
({
id: x.id,
timestamp: x.timestamp,
answerEntries: dbAnswerEntriesToAnswerEntries(x.entries),
}) as Submission,
);
}
29 changes: 17 additions & 12 deletions server/src/application/survey.ts
Original file line number Diff line number Diff line change
Expand Up @@ -987,7 +987,7 @@ async function getSectionConditions(sectionIds: number[]) {
}

/**
* When updating a survey, deletes all sections that should be removed from DB.
* When updating a survey, deletes all sections (and subsections linked to them) that should be removed from DB.
* @param surveyId Survey ID
* @param newSections New sections
*/
Expand All @@ -1010,7 +1010,7 @@ async function deleteRemovedSections(
);
if (removedSectionIds.length) {
await getDb().none(
`DELETE FROM data.page_section WHERE id = ANY ($1) OR parent_section = ANY ($1)`,
`DELETE FROM data.page_section WHERE id = ANY ($1) OR parent_section = ANY ($1) OR predecessor_section = ANY($1)`,
[removedSectionIds],
);
}
Expand Down Expand Up @@ -1213,9 +1213,9 @@ export async function updateSurvey(survey: Survey) {
Object.entries(page.conditions).map(async ([sectionId, conditions]) => {
// Filter out null values from conditions caused by trying to save "-" as numeric condition
const validConditions = {
equals: conditions.equals.filter(Boolean),
lessThan: conditions.lessThan.filter(Boolean),
greaterThan: conditions.greaterThan.filter(Boolean),
equals: conditions.equals.filter((value) => value != null),
lessThan: conditions.lessThan.filter((value) => value != null),
greaterThan: conditions.greaterThan.filter((value) => value != null),
};
await upsertSectionConditions(
Number(sectionId),
Expand Down Expand Up @@ -1314,10 +1314,15 @@ export async function updateSurvey(survey: Survey) {
await deleteSectionConditions(null, [sectionRow.id]);
// Filter out null values from conditions caused by trying to save "-" as numeric condition
const validConditions = {
equals: linkedSection.conditions.equals.filter(Boolean),
lessThan: linkedSection.conditions.lessThan.filter(Boolean),
greaterThan:
linkedSection.conditions.greaterThan.filter(Boolean),
equals: linkedSection.conditions.equals.filter(
(value) => value != null,
),
lessThan: linkedSection.conditions.lessThan.filter(
(value) => value != null,
),
greaterThan: linkedSection.conditions.greaterThan.filter(
(value) => value != null,
),
};
await upsertSectionConditions(
sectionRow.id,
Expand Down Expand Up @@ -1557,17 +1562,17 @@ function dbSectionConditionsToConditions(
): Conditions {
return dbSectionConditions.reduce(
(conditions, condition) => {
if (condition.equals) {
if (condition.equals != null) {
return {
...conditions,
equals: [...conditions.equals, condition.equals],
};
} else if (condition.less_than) {
} else if (condition.less_than != null) {
return {
...conditions,
lessThan: [...conditions.lessThan, condition.less_than],
};
} else if (condition.greater_than) {
} else if (condition.greater_than != null) {
return {
...conditions,
greaterThan: [...conditions.greaterThan, condition.greater_than],
Expand Down

0 comments on commit 605fdf6

Please sign in to comment.