Skip to content

Commit f3e4723

Browse files
carolmariaabbbrianchandotcom
authored andcommitted
LPD-49638 Delete current file entry when the user clicks the delete button, selects a new file, or refreshes the page
1 parent fffed67 commit f3e4723

File tree

3 files changed

+68
-2
lines changed
  • modules/apps/object
    • object-dynamic-data-mapping-form-field-type/src/main/resources/META-INF/resources/js/Attachment
    • object-web/src/main/resources/META-INF/resources/object_entries/object_entry

3 files changed

+68
-2
lines changed

modules/apps/object/object-dynamic-data-mapping-form-field-type/src/main/resources/META-INF/resources/js/Attachment/Attachment.tsx

Lines changed: 59 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
* SPDX-License-Identifier: LGPL-2.1-or-later OR LicenseRef-Liferay-DXP-EULA-2.0.0-2023-06
44
*/
55

6+
import {useConfig} from 'data-engine-js-components-web';
67
import {ReactFieldBase as FieldBase} from 'dynamic-data-mapping-form-field-type';
78
import {
89
FieldChangeEventHandler,
@@ -12,7 +13,8 @@ import {
1213
AvailableLocale,
1314
EditingLocale,
1415
} from 'dynamic-data-mapping-form-field-type/src/main/resources/META-INF/resources/util/localizable/LocalesDropdown';
15-
import React, {useState} from 'react';
16+
import {fetch} from 'frontend-js-web';
17+
import React, {useCallback, useEffect, useState} from 'react';
1618

1719
import AttachmentBase, {
1820
AttachmentBaseProps,
@@ -25,6 +27,7 @@ export interface AttachmentProps
2527
availableLocales: AvailableLocale[];
2628
contentURL?: string;
2729
defaultLocale: EditingLocale;
30+
deleteURL?: string;
2831
fieldName: string;
2932
fileEntryProperties: AttachmentFile | LocalizedValue<AttachmentFile>;
3033
localizedObjectField: boolean;
@@ -34,6 +37,7 @@ export interface AttachmentProps
3437

3538
export default function Attachment({
3639
contentURL,
40+
deleteURL,
3741
fileEntryProperties,
3842
localizedObjectField,
3943
onChange,
@@ -46,6 +50,8 @@ export default function Attachment({
4650
const isLocalizedObjectField: boolean =
4751
Liferay.FeatureFlags['LPD-32050'] && !!localizedObjectField;
4852

53+
const {portletNamespace} = useConfig();
54+
4955
const getAttachment = () => {
5056
if (Liferay.FeatureFlags['LPD-32050']) {
5157
return fileEntryProperties;
@@ -61,17 +67,69 @@ export default function Attachment({
6167
getAttachment() as AttachmentFile | null
6268
);
6369
const [error, setError] = useState({});
70+
const [submitButtonClicked, setSubmitButtonClicked] = useState(false);
71+
72+
const deleteFileEntry = useCallback(async () => {
73+
if (!attachment || !deleteURL) {
74+
return;
75+
}
76+
77+
const {fileEntryId} = attachment;
78+
79+
if (!fileEntryId) {
80+
return;
81+
}
82+
83+
const formData = new FormData();
84+
85+
formData.append(`${portletNamespace}fileEntryId`, fileEntryId);
86+
87+
await fetch(deleteURL, {
88+
body: formData,
89+
method: 'POST',
90+
});
91+
}, [attachment, deleteURL, portletNamespace]);
92+
93+
useEffect(() => {
94+
window.onbeforeunload = function () {
95+
if (!submitButtonClicked) {
96+
deleteFileEntry();
97+
}
98+
};
99+
100+
return () => {
101+
window.onbeforeunload = null;
102+
};
103+
}, [deleteFileEntry, submitButtonClicked]);
104+
105+
useEffect(() => {
106+
Liferay.on(
107+
'submitButtonClicked',
108+
109+
() => {
110+
setSubmitButtonClicked(true);
111+
}
112+
);
113+
114+
return () => {
115+
Liferay.detach('submitButtonClicked');
116+
};
117+
}, []);
64118

65119
const handleAttachmentChange = (
66120
attachmentValue: AttachmentFile,
67121
fileId: string
68122
) => {
123+
deleteFileEntry();
124+
69125
setAttachment(attachmentValue);
70126

71127
onChange({target: {value: fileId}});
72128
};
73129

74130
const handleDelete = () => {
131+
deleteFileEntry();
132+
75133
setAttachment(null);
76134

77135
onChange({target: {value: ''}}); // TODO: fix backend to support null

modules/apps/object/object-dynamic-data-mapping-form-field-type/src/main/resources/META-INF/resources/js/Attachment/AttachmentBase.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {LocalizedValue} from 'dynamic-data-mapping-form-field-type/src/main/reso
2121

2222
export type AttachmentFile = {
2323
contentURL: string;
24+
fileEntryId: string;
2425
title: string;
2526
};
2627

@@ -90,6 +91,7 @@ export default function AttachmentBase({
9091
onAttachmentChange(
9192
{
9293
contentURL: selectedItemValue.url,
94+
fileEntryId: selectedItemValue.fileEntryId,
9395
title: selectedItemValue.title,
9496
},
9597
selectedItemValue.fileEntryId
@@ -133,7 +135,11 @@ export default function AttachmentBase({
133135
}
134136
else {
135137
onAttachmentChange(
136-
{contentURL: file.contentURL, title: file.title},
138+
{
139+
contentURL: file.contentURL,
140+
fileEntryId: file.fileEntryId,
141+
title: file.title,
142+
},
137143
file.fileEntryId
138144
);
139145
}

modules/apps/object/object-web/src/main/resources/META-INF/resources/object_entries/object_entry/form.jsp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,8 @@ portletDisplay.setURLBack(backURL);
252252
method: externalReferenceCode ? 'PATCH' : 'POST',
253253
})
254254
.then((response) => {
255+
Liferay.fire('submitButtonClicked');
256+
255257
if (response.status === 401) {
256258
window.location.reload();
257259
}

0 commit comments

Comments
 (0)