chairemobilite / evolution

Online survey platform for travel survey
MIT License
4 stars 9 forks source link

Support easy handling of group objects #422

Open tahini opened 5 months ago

tahini commented 5 months ago

Adding/deleting empty group objects is done manually in the preload function of the sections that contain groups. But this code is generic enough that it can depend on some group parameters and be done in the preload base function.

Here is for example a copy paste of that code from one of the surveys:

   // TODO: This code only depends on the grouped object path and group count path and section if count is invalid. Move to evolution, to make it generic.
    /** Start of group related code to move to evolution */
    const groupedObjects = getResponse(interview, 'household.persons');
    const groupedObjectIds = groupedObjects ? Object.keys(groupedObjects) : [];
    const householdSize = getResponse(interview, 'household.size', 0) as number;
    const householdSizeIsValid =
        !isNaN(Number(householdSize)) && householdSize >= 1 && householdSize <= 18;

    const emptyGroupedObjects = groupedObjectIds.filter((groupedObjectId) => {
        const { _uuid, _sequence, ...restOfGroup } = groupedObjects[groupedObjectId];
        return _isEmpty(restOfGroup);
    });

    if (householdSizeIsValid && householdSize) {
        if (groupedObjectIds.length < householdSize) {

            // auto create objects according to household size:
            startAddGroupedObjects(
                householdSize - groupedObjectIds.length,
                -1,
                'household.persons',
                null,
                (_interview) => {
                    startUpdateInterview(currentSectionName, responsesContent, null, null, callback);
                }
            );
        } else if (groupedObjectIds.length > householdSize) {
            const pathsToDelete = [];
            // auto remove empty objects according to household size:
            for (let i = 0; i < groupedObjectIds.length; i++) {
                if (emptyGroupedObjects[i]) {
                    pathsToDelete.push(`household.persons.${emptyGroupedObjects[i]}`);
                }
            }
            if (pathsToDelete.length > 0) {
                startRemoveGroupedObjects(pathsToDelete, (_interview) => {
                    startUpdateInterview(currentSectionName, responsesContent, null, null, callback);
                });
            } else {
                startUpdateInterview(currentSectionName, responsesContent, null, null, callback);
            }
        } else {
            startUpdateInterview(currentSectionName, responsesContent, null, null, callback);
        }
    } else {
        responsesContent['responses._activeSection'] = 'home';
        startUpdateInterview(currentSectionName, responsesContent, null, null, callback);
    }
    /** End of group related code to move to evolution */
tahini commented 2 months ago

But first, make sure all group related stuff is in typescript, so we know what we're dealing with...

samuel-duhaime commented 2 months ago

Yes, this is great. Can't wait to review this. :)