Skip to content

Commit e8b13cf

Browse files
tumms2021389samhere06
authored andcommitted
fix: improve attachment handling and state updates in AttachmentComponent
1 parent 2039ec4 commit e8b13cf

File tree

2 files changed

+16
-8
lines changed

2 files changed

+16
-8
lines changed

packages/angular-sdk-components/src/lib/_components/widget/attachment/AttachmentUtils.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ export const updateReduxState = (
126126
valueRef: string,
127127
options: PageInstructionOptions
128128
) => {
129-
const { allowMultiple, isOldAttachment, insertRedux, deleteRedux, deleteIndex } = options;
129+
const { allowMultiple, isOldAttachment, insertRedux, deleteRedux } = options;
130+
let deleteIndex = -1;
130131

131132
if (allowMultiple || isOldAttachment) {
132133
transformedAttachments.forEach(attachment => {
@@ -146,6 +147,11 @@ export const updateReduxState = (
146147
};
147148
PCore.getStore()?.dispatch(actionPayLoad);
148149
} else if (deleteRedux) {
150+
const uniqueKey = getMappedValue('pzInsKey');
151+
deleteIndex = existingAttachments.findIndex(
152+
existingAttachment =>
153+
existingAttachment[uniqueKey as keyof ReduxAttachments] === transformedAttachments[0][uniqueKey as keyof ReduxAttachments]
154+
);
149155
const actionPayLoad = {
150156
type: 'LIST_ACTION',
151157
payload: {

packages/angular-sdk-components/src/lib/_components/widget/attachment/attachment.component.ts

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,12 @@ export class AttachmentComponent implements OnInit, OnDestroy {
159159
const rawValue = this.pConn$.getComponentConfig().value;
160160
const isAttachmentAnnotationPresent = typeof rawValue === 'object' ? false : rawValue?.includes('@ATTACHMENT');
161161
const { attachments, isOldAttachment } = isAttachmentAnnotationPresent ? value : PCore.getAttachmentUtils().prepareAttachmentData(value);
162+
const isAttachmentsChanged = !PCore.isDeepEqual(this.attachments, attachments); // JSON.stringify(this.attachments) !== JSON.stringify(attachments);
162163
this.isOldAttachment = isOldAttachment;
163164
this.attachments = attachments;
164165

165166
// update the attachments shown in the UI
166-
if (this.attachments.length) {
167+
if (isAttachmentsChanged) {
167168
this.updateAttachments();
168169
}
169170
}
@@ -339,12 +340,6 @@ export class AttachmentComponent implements OnInit, OnDestroy {
339340

340341
populateErrorAndUpdateRedux(file) {
341342
const fieldName = (this.pConn$.getStateProps() as any).value;
342-
insertAttachments([file], this.pConn$, this.multiAttachmentsInInlineEdit, {
343-
allowMultiple: this.allowMultiple$,
344-
isOldAttachment: this.isOldAttachment,
345-
isMultiAttachmentInInlineEditTable: this.isMultiAttachmentInInlineEditTable,
346-
attachmentCount: this.attachmentCount
347-
} as any);
348343
// set errors to property to block submit even on errors in file upload
349344
PCore.getMessageManager().addMessages({
350345
messages: [
@@ -357,6 +352,12 @@ export class AttachmentComponent implements OnInit, OnDestroy {
357352
pageReference: this.pConn$.getPageReference(),
358353
context: this.contextName
359354
});
355+
insertAttachments([file], this.pConn$, this.multiAttachmentsInInlineEdit, {
356+
allowMultiple: this.allowMultiple$,
357+
isOldAttachment: this.isOldAttachment,
358+
isMultiAttachmentInInlineEditTable: this.isMultiAttachmentInInlineEditTable,
359+
attachmentCount: this.attachmentCount
360+
} as any);
360361
}
361362

362363
errorHandler(isFetchCanceled, file) {
@@ -421,6 +422,7 @@ export class AttachmentComponent implements OnInit, OnDestroy {
421422
fileResponses[index].value.thumbnail = localFile.props.thumbnail;
422423
localFile.inProgress = false;
423424
localFile.ID = fileResponses[index].value.ID;
425+
localFile.props.id = fileResponses[index].value.ID;
424426
localFile.props.meta = this.localizationService.getLocalizedText('Uploaded successfully');
425427
localFile.props.progress = 100;
426428
localFile.handle = fileResponses[index].value.ID;

0 commit comments

Comments
 (0)