Skip to content

Commit

Permalink
Merge pull request #8454 from CitizenLabDotCo/fix-phase-config
Browse files Browse the repository at this point in the history
Fix phase config state sync
  • Loading branch information
IvaKop authored Jul 18, 2024
2 parents f275788 + 90a9d3d commit ddd0dde
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import {
Input,
IOption,
} from '@citizenlab/cl2-component-library';
import { pick } from 'lodash-es';
import { filter } from 'rxjs/operators';
import { CLErrors, Multiloc } from 'typings';

Expand Down Expand Up @@ -91,7 +92,13 @@ const PhaseParticipationConfig = ({
const { formatMessage } = useIntl();
const [participationConfig, setParticipationConfig] =
useState<IPhaseParticipationConfig>(
phase ? phase.data.attributes : ideationDefaultConfig
// Only use the attributes from phase that are relevant to participation config
phase
? (pick(
phase.data.attributes,
Object.keys(defaultParticipationConfig)
) as IPhaseParticipationConfig)
: ideationDefaultConfig
);

const [noLikingLimitError, setNoLikingLimitError] =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,8 @@ export interface IPhaseParticipationConfig {
document_annotation_embed_url?: string | null;
}

export const defaultParticipationConfig: Omit<
IPhaseParticipationConfig,
'participation_method'
> = {
export const defaultParticipationConfig: IPhaseParticipationConfig = {
participation_method: 'ideation',
posting_enabled: true,
commenting_enabled: true,
reacting_enabled: true,
Expand Down
29 changes: 8 additions & 21 deletions front/app/containers/Admin/projects/project/timeline/edit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -140,20 +140,6 @@ const AdminPhaseEdit = () => {
}
}, [phaseFiles]);

const getAttributeDiff = useCallback(
(
participationContextConfig: IPhaseParticipationConfig,
attributeDiff: IUpdatedPhaseProperties
) => {
// Important to keep the order of the spread operators
return {
...participationContextConfig,
...attributeDiff,
};
},
[]
);

const handlePhaseParticipationConfigChange = useCallback(
(participationContextConfig: IPhaseParticipationConfig) => {
const surveyCTALabel = tenantLocales?.reduce((acc, locale) => {
Expand All @@ -173,8 +159,10 @@ const AdminPhaseEdit = () => {
}, {});

setSubmitState('enabled');
// Important to keep the order of the spread operators
setAttributeDiff((attributeDiff) => ({
...getAttributeDiff(participationContextConfig, attributeDiff),
...attributeDiff,
...participationContextConfig,
...(participationContextConfig.participation_method ===
'native_survey' &&
!attributeDiff.native_survey_button_multiloc &&
Expand All @@ -193,7 +181,6 @@ const AdminPhaseEdit = () => {
formatMessageWithLocale,
phase?.data.attributes.native_survey_button_multiloc,
tenantLocales,
getAttributeDiff,
]
);

Expand Down Expand Up @@ -311,11 +298,11 @@ const AdminPhaseEdit = () => {
const handlePhaseParticipationConfigSubmit = (
participationContextConfig: IPhaseParticipationConfig
) => {
const newAttributeDiff = getAttributeDiff(
participationContextConfig,
attributeDiff
);
save(projectId, phase?.data, newAttributeDiff);
// Important to keep the order of the spread operators
save(projectId, phase?.data, {
...attributeDiff,
...participationContextConfig,
});
};

const handleError = (error: { errors: CLErrors }) => {
Expand Down

0 comments on commit ddd0dde

Please sign in to comment.