[BUG] Handle optimistic cache deletion operation (#9914)
# Introduction This PR is highly related to previous optimistic cache refactor: https://github.com/twentyhq/twenty/pull/9881 Here we've added some logic within the `triggerUpdateRelationsOptimisticEffect` which will now run if given recordInput `deletedAt` field is defined. If deletion, we will iterate over all the fields searching for `RELATION` for which deletion might implies necessity to detach the relation ## Known troubleshooting ( also on main )  We might have to refactor the `prefillRecord` to spread and overrides`inputValue` over defaultOne as inputValue could be a partial one for more info please a look to # Conclusion Any suggestions are welcomed ! fixes https://github.com/twentyhq/twenty/issues/9580 --------- Co-authored-by: Charles Bochet <charles@twenty.com>
This commit is contained in:
@ -27,6 +27,10 @@ export const triggerUpdateRelationsOptimisticEffect = ({
|
||||
updatedSourceRecord,
|
||||
objectMetadataItems,
|
||||
}: triggerUpdateRelationsOptimisticEffectArgs) => {
|
||||
const isDeletion =
|
||||
isDefined(updatedSourceRecord) &&
|
||||
isDefined(updatedSourceRecord['deletedAt']);
|
||||
|
||||
return sourceObjectMetadataItem.fields.forEach(
|
||||
(fieldMetadataItemOnSourceRecord) => {
|
||||
const notARelationField =
|
||||
@ -72,15 +76,15 @@ export const triggerUpdateRelationsOptimisticEffect = ({
|
||||
| RecordGqlNode
|
||||
| null = updatedSourceRecord?.[fieldMetadataItemOnSourceRecord.name];
|
||||
|
||||
if (
|
||||
isDeeplyEqual(
|
||||
currentFieldValueOnSourceRecord,
|
||||
updatedFieldValueOnSourceRecord,
|
||||
{ strict: true },
|
||||
)
|
||||
) {
|
||||
const noDiff = isDeeplyEqual(
|
||||
currentFieldValueOnSourceRecord,
|
||||
updatedFieldValueOnSourceRecord,
|
||||
{ strict: true },
|
||||
);
|
||||
if (noDiff && !isDeletion) {
|
||||
return;
|
||||
}
|
||||
|
||||
const extractTargetRecordsFromRelation = (
|
||||
value: RecordGqlConnection | RecordGqlNode | null,
|
||||
): RecordGqlNode[] => {
|
||||
@ -96,11 +100,12 @@ export const triggerUpdateRelationsOptimisticEffect = ({
|
||||
|
||||
return [value];
|
||||
};
|
||||
|
||||
const recordToExtractDetachFrom = isDeletion
|
||||
? updatedFieldValueOnSourceRecord
|
||||
: currentFieldValueOnSourceRecord;
|
||||
const targetRecordsToDetachFrom = extractTargetRecordsFromRelation(
|
||||
currentFieldValueOnSourceRecord,
|
||||
);
|
||||
const targetRecordsToAttachTo = extractTargetRecordsFromRelation(
|
||||
updatedFieldValueOnSourceRecord,
|
||||
recordToExtractDetachFrom,
|
||||
);
|
||||
|
||||
// TODO: see if we can de-hardcode this, put cascade delete in relation metadata item
|
||||
@ -129,7 +134,11 @@ export const triggerUpdateRelationsOptimisticEffect = ({
|
||||
});
|
||||
}
|
||||
|
||||
if (isDefined(updatedSourceRecord)) {
|
||||
if (!isDeletion && isDefined(updatedSourceRecord)) {
|
||||
const targetRecordsToAttachTo = extractTargetRecordsFromRelation(
|
||||
updatedFieldValueOnSourceRecord,
|
||||
);
|
||||
|
||||
targetRecordsToAttachTo.forEach((targetRecordToAttachTo) =>
|
||||
triggerAttachRelationOptimisticEffect({
|
||||
cache,
|
||||
|
||||
Reference in New Issue
Block a user