-
Notifications
You must be signed in to change notification settings - Fork 76
fix: upsert attachmemts only when attachment exist and handle FileList for react native #1539
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
Size Change: +322 B (+0.09%) Total Size: 379 kB
|
@@ -448,7 +448,11 @@ export class AttachmentManager { | |||
}, | |||
}; | |||
|
|||
this.upsertAttachments([failedAttachment]); | |||
const isAttachmentPresent = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@khushal87 we could prevent duplication of these lines by adding a new method updateAttachmet
to AttachmentManager. A proposal of a refactor:
private prepareAttachmentUpdate = (attachmentToUpdate: LocalAttachment) => {
const stateAttachments = this.attachments;
const attachments = [...this.attachments];
const attachmentIndex = this.getAttachmentIndex(attachmentToUpdate.localMetadata.id);
if (attachmentIndex === -1) return null;
const merged = mergeWithDiff<LocalAttachment>(
stateAttachments[attachmentIndex] ?? {},
attachmentToUpdate,
);
const updatesOnMerge = merged.diff && Object.keys(merged.diff.children).length;
if (updatesOnMerge) {
const localAttachment = ensureIsLocalAttachment(merged.result);
if (localAttachment) {
attachments.splice(attachmentIndex, 1, localAttachment);
return attachments;
}
}
return null;
};
updateAttachment = (attachmentToUpdate: LocalAttachment) => {
const updatedAttachments = this.prepareAttachmentUpdate(attachmentToUpdate);
if (updatedAttachments) {
this.state.partialNext({ attachments: updatedAttachments });
}
};
upsertAttachments = (attachmentsToUpsert: LocalAttachment[]) => {
if (!attachmentsToUpsert.length) return;
let attachments = [...this.attachments];
let hasUpdates = false;
attachmentsToUpsert.forEach((attachment) => {
const updatedAttachments = this.prepareAttachmentUpdate(attachment);
if (updatedAttachments) {
attachments = updatedAttachments;
hasUpdates = true;
} else {
const localAttachment = ensureIsLocalAttachment(attachment);
if (localAttachment) {
attachments.push(localAttachment);
hasUpdates = true;
}
}
});
if (hasUpdates) {
this.state.partialNext({ attachments });
}
};
the in uploadAttachment
you would only do this.updateAttachment(failedAttachment)
. WDYT?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, this can do the trick as well. My bad I was testing upsertAttachments
and thought this would not work but using the updateAttachments
can be useful here and avoid duplication.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I have pushed the code and added a few relevant tests. Let me know if its fine to merge it. Thanks 😄
The goal of the PR is to upsert attachments only when they exist. This is especially important because on React Native, we allow cancelling uploading by calling removeAttachments, but when the upload still happens, the upsert proceeds. We will only update you if the attachment is present.
The other fix is that the
FileList
is not supported on RN. Thanks to @MartinCupela for a prompt suggestion on this issue.