Skip to content

Commit

Permalink
LPD-22246 Fix saving behavior with no fields selected
Browse files Browse the repository at this point in the history
  • Loading branch information
chestofwonders authored and brianchandotcom committed Apr 5, 2024
1 parent f7fa84a commit 3c4993e
Show file tree
Hide file tree
Showing 2 changed files with 84 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,25 @@ export default function Cards(props: IFDSViewSectionProps) {
);
};

const clearFDSCardSection = async (cardSection: ICardsSection) => {
const clearFDSCardSection = async ({
cardsSection,
closeModal,
}: {
cardsSection: ICardsSection;
closeModal?: Function;
}) => {
if (!cardsSection.externalReferenceCode) {
if (closeModal) {
closeModal();
}

return;
}

setSaveButtonDisabled(true);

const response = await fetch(
`${API_URL.FDS_CARDS_SECTIONS}/by-external-reference-code/${cardSection.externalReferenceCode}`,
`${API_URL.FDS_CARDS_SECTIONS}/by-external-reference-code/${cardsSection.externalReferenceCode}`,
{method: 'DELETE'}
);

Expand All @@ -104,20 +118,26 @@ export default function Cards(props: IFDSViewSectionProps) {
return;
}

if (closeModal) {
closeModal();
}

setCardsSections(
cardsSections.map((section) => {
if (section.name !== cardSection.name) {
if (section.name !== cardsSection.name) {
return section;
}

const nextCardSection = {...cardSection};
const nextCardSection = {...cardsSection};

delete nextCardSection.externalReferenceCode;
delete nextCardSection.field;

return nextCardSection;
})
);

openDefaultSuccessToast();
};

const saveFDSCardsSection = async ({
Expand Down Expand Up @@ -161,9 +181,25 @@ export default function Cards(props: IFDSViewSectionProps) {
return;
}

const fdsCardSection: IFDSCardsSection = await response.json();

closeModal();

getFDSCardsSections();
setCardsSections(
cardsSections.map((cardSection) => {
if (cardSection.name !== fdsCardSection.name) {
return cardSection;
}

return {
...cardSection,
externalReferenceCode: fdsCardSection.externalReferenceCode,
field: {
name: fdsCardSection.fieldName,
},
};
})
);

openDefaultSuccessToast();
};
Expand Down Expand Up @@ -209,14 +245,19 @@ export default function Cards(props: IFDSViewSectionProps) {
key={cardsSection.name}
modalProps={props}
onClearSelection={() => {
clearFDSCardSection(cardsSection);
clearFDSCardSection({cardsSection});
}}
onSelect={({closeModal, selectedField}) => {
saveFDSCardsSection({
cardsSection,
closeModal,
field: selectedField,
});
selectedField
? saveFDSCardsSection({
cardsSection,
closeModal,
field: selectedField,
})
: clearFDSCardSection({
cardsSection,
closeModal,
});
}}
saveButtonDisabled={saveButtonDisabled}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,21 @@ export default function List(props: IFDSViewSectionProps) {
);
};

const clearFDSListSection = async (listSection: IListSection) => {
const clearFDSListSection = async ({
closeModal,
listSection,
}: {
closeModal?: Function;
listSection: IListSection;
}) => {
if (!listSection.externalReferenceCode) {
if (closeModal) {
closeModal();
}

return;
}

setSaveButtonDisabled(true);

const response = await fetch(
Expand All @@ -103,6 +117,10 @@ export default function List(props: IFDSViewSectionProps) {
return;
}

if (closeModal) {
closeModal();
}

setListSections(
listSections.map((section) => {
if (section.name !== listSection.name) {
Expand All @@ -117,6 +135,8 @@ export default function List(props: IFDSViewSectionProps) {
return nextListSection;
})
);

openDefaultSuccessToast();
};

const saveFDSListSection = async ({
Expand Down Expand Up @@ -224,14 +244,19 @@ export default function List(props: IFDSViewSectionProps) {
listSection={listSection}
modalProps={props}
onClearSelection={() => {
clearFDSListSection(listSection);
clearFDSListSection({listSection});
}}
onSelect={({closeModal, selectedField}) => {
saveFDSListSection({
closeModal,
field: selectedField,
listSection,
});
selectedField
? saveFDSListSection({
closeModal,
field: selectedField,
listSection,
})
: clearFDSListSection({
closeModal,
listSection,
});
}}
saveButtonDisabled={saveButtonDisabled}
/>
Expand Down

0 comments on commit 3c4993e

Please sign in to comment.