@akemona-org/strapi-plugin-upload 3.7.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +22 -0
- package/README.md +19 -0
- package/admin/src/assets/images/logo.svg +1 -0
- package/admin/src/components/BrowseAssets/Wrapper.js +8 -0
- package/admin/src/components/BrowseAssets/index.js +182 -0
- package/admin/src/components/Card/index.js +146 -0
- package/admin/src/components/CardBorder/index.js +28 -0
- package/admin/src/components/CardControl/Wrapper.js +89 -0
- package/admin/src/components/CardControl/index.js +61 -0
- package/admin/src/components/CardControlsWrapper/index.js +22 -0
- package/admin/src/components/CardEmpty/Bar.js +19 -0
- package/admin/src/components/CardEmpty/Wrapper.js +7 -0
- package/admin/src/components/CardEmpty/index.js +16 -0
- package/admin/src/components/CardErrorMessage/index.js +9 -0
- package/admin/src/components/CardImgWrapper/index.js +52 -0
- package/admin/src/components/CardPreview/Image.js +12 -0
- package/admin/src/components/CardPreview/Wrapper.js +25 -0
- package/admin/src/components/CardPreview/index.js +66 -0
- package/admin/src/components/CardTitle/index.js +14 -0
- package/admin/src/components/CardWrapper/index.js +23 -0
- package/admin/src/components/CheckControl/Button.js +49 -0
- package/admin/src/components/CheckControl/CustomDropdownSection.js +9 -0
- package/admin/src/components/CheckControl/Spacer.js +7 -0
- package/admin/src/components/CheckControl/index.js +74 -0
- package/admin/src/components/Container/index.js +7 -0
- package/admin/src/components/ContainerFluid/index.js +12 -0
- package/admin/src/components/ControlsWrapper/index.js +9 -0
- package/admin/src/components/DragLayer/Wrapper.js +13 -0
- package/admin/src/components/DragLayer/index.js +68 -0
- package/admin/src/components/DropdownButton/index.js +63 -0
- package/admin/src/components/DropdownSection/index.js +27 -0
- package/admin/src/components/Duration/index.js +25 -0
- package/admin/src/components/Duration/utils/formatDuration.js +13 -0
- package/admin/src/components/EditForm/CropWrapper.js +21 -0
- package/admin/src/components/EditForm/ErrorMessage.js +10 -0
- package/admin/src/components/EditForm/FileDetailsBox.js +95 -0
- package/admin/src/components/EditForm/FileDetailsBoxWrapper.js +14 -0
- package/admin/src/components/EditForm/FileWrapper.js +41 -0
- package/admin/src/components/EditForm/FormWrapper.js +7 -0
- package/admin/src/components/EditForm/SizeBox.js +13 -0
- package/admin/src/components/EditForm/Wrapper.js +8 -0
- package/admin/src/components/EditForm/index.js +425 -0
- package/admin/src/components/EditForm/utils/form.js +34 -0
- package/admin/src/components/EditForm/utils/isImageType.js +3 -0
- package/admin/src/components/EditForm/utils/isVideoType.js +3 -0
- package/admin/src/components/FileIcon/Wrapper.js +20 -0
- package/admin/src/components/FileIcon/index.js +33 -0
- package/admin/src/components/FileIcon/utils/extensions.json +4 -0
- package/admin/src/components/FileInfos/index.js +32 -0
- package/admin/src/components/Filters/index.js +30 -0
- package/admin/src/components/FiltersList/index.js +34 -0
- package/admin/src/components/FiltersList/utils/formatFilter.js +36 -0
- package/admin/src/components/FiltersPicker/FiltersCard/FilterButton.js +8 -0
- package/admin/src/components/FiltersPicker/FiltersCard/FilterInput.js +35 -0
- package/admin/src/components/FiltersPicker/FiltersCard/InputWrapper.js +10 -0
- package/admin/src/components/FiltersPicker/FiltersCard/SizeInput.js +57 -0
- package/admin/src/components/FiltersPicker/FiltersCard/Wrapper.js +8 -0
- package/admin/src/components/FiltersPicker/FiltersCard/index.js +97 -0
- package/admin/src/components/FiltersPicker/FiltersCard/init.js +21 -0
- package/admin/src/components/FiltersPicker/FiltersCard/reducer.js +47 -0
- package/admin/src/components/FiltersPicker/FiltersCard/utils/filtersForm.js +27 -0
- package/admin/src/components/FiltersPicker/index.js +43 -0
- package/admin/src/components/FiltersPicker/utils/formatFilter.js +18 -0
- package/admin/src/components/InfiniteLoadingIndicator/Button.js +20 -0
- package/admin/src/components/InfiniteLoadingIndicator/Container.js +12 -0
- package/admin/src/components/InfiniteLoadingIndicator/Wrapper.js +18 -0
- package/admin/src/components/InfiniteLoadingIndicator/index.js +39 -0
- package/admin/src/components/Input/index.js +7 -0
- package/admin/src/components/InputFileModal/Label.js +47 -0
- package/admin/src/components/InputFileModal/P.js +15 -0
- package/admin/src/components/InputFileModal/index.js +73 -0
- package/admin/src/components/InputMedia/CardControlWrapper.js +12 -0
- package/admin/src/components/InputMedia/CardPreviewWrapper.js +13 -0
- package/admin/src/components/InputMedia/Chevron/Wrapper.js +10 -0
- package/admin/src/components/InputMedia/Chevron/index.js +21 -0
- package/admin/src/components/InputMedia/EmptyInputMedia.js +60 -0
- package/admin/src/components/InputMedia/EmptyText.js +13 -0
- package/admin/src/components/InputMedia/ErrorMessage.js +12 -0
- package/admin/src/components/InputMedia/IconUpload.js +48 -0
- package/admin/src/components/InputMedia/InputFilePreview.js +37 -0
- package/admin/src/components/InputMedia/Name.js +10 -0
- package/admin/src/components/InputMedia/Wrapper.js +11 -0
- package/admin/src/components/InputMedia/index.js +238 -0
- package/admin/src/components/InputUploadURL/Wrapper.js +13 -0
- package/admin/src/components/InputUploadURL/index.js +46 -0
- package/admin/src/components/IntlText/index.js +26 -0
- package/admin/src/components/List/ListCell.js +19 -0
- package/admin/src/components/List/ListRow.js +8 -0
- package/admin/src/components/List/index.js +114 -0
- package/admin/src/components/ListEmpty/Wrapper.js +15 -0
- package/admin/src/components/ListEmpty/index.js +70 -0
- package/admin/src/components/ListEmpty/utils/generateRows.js +12 -0
- package/admin/src/components/ListModal/BaselineAlignmentWrapper.js +12 -0
- package/admin/src/components/ListModal/index.js +66 -0
- package/admin/src/components/ListWrapper/index.js +16 -0
- package/admin/src/components/LoadingIndicator/index.js +44 -0
- package/admin/src/components/ModalHeader/BackButton.js +33 -0
- package/admin/src/components/ModalHeader/Wrapper.js +24 -0
- package/admin/src/components/ModalHeader/index.js +47 -0
- package/admin/src/components/ModalNavWrapper/BaselineAlignment.js +12 -0
- package/admin/src/components/ModalNavWrapper/Hr.js +14 -0
- package/admin/src/components/ModalNavWrapper/Wrapper.js +8 -0
- package/admin/src/components/ModalNavWrapper/index.js +70 -0
- package/admin/src/components/ModalSection/index.js +20 -0
- package/admin/src/components/ModalTab/Count/Wrapper.js +16 -0
- package/admin/src/components/ModalTab/Count/index.js +30 -0
- package/admin/src/components/ModalTab/Wrapper.js +37 -0
- package/admin/src/components/ModalTab/index.js +60 -0
- package/admin/src/components/PlayIcon/Play.js +65 -0
- package/admin/src/components/PlayIcon/Wrapper.js +24 -0
- package/admin/src/components/PlayIcon/index.js +23 -0
- package/admin/src/components/SelectAll/Wrapper.js +19 -0
- package/admin/src/components/SelectAll/index.js +26 -0
- package/admin/src/components/SelectedAssets/DraggableCard.js +149 -0
- package/admin/src/components/SelectedAssets/ListTitleWrapper.js +7 -0
- package/admin/src/components/SelectedAssets/ListWrapper.js +7 -0
- package/admin/src/components/SelectedAssets/SortableList.js +102 -0
- package/admin/src/components/SelectedAssets/Wrapper.js +7 -0
- package/admin/src/components/SelectedAssets/index.js +56 -0
- package/admin/src/components/SortList/Wrapper.js +19 -0
- package/admin/src/components/SortList/index.js +37 -0
- package/admin/src/components/SortListItem/Wrapper.js +28 -0
- package/admin/src/components/SortListItem/index.js +35 -0
- package/admin/src/components/SortPicker/index.js +54 -0
- package/admin/src/components/Tag/Wrapper.js +16 -0
- package/admin/src/components/Tag/index.js +25 -0
- package/admin/src/components/UploadForm/index.js +61 -0
- package/admin/src/components/UploadList/ButtonWrapper.js +7 -0
- package/admin/src/components/UploadList/Container.js +10 -0
- package/admin/src/components/UploadList/CustomModalSection.js +9 -0
- package/admin/src/components/UploadList/ListTitle.js +10 -0
- package/admin/src/components/UploadList/ListTitleWrapper.js +7 -0
- package/admin/src/components/UploadList/RowItem.js +98 -0
- package/admin/src/components/UploadList/index.js +90 -0
- package/admin/src/components/VideoPlayer/Wrapper.js +24 -0
- package/admin/src/components/VideoPlayer/index.js +57 -0
- package/admin/src/components/VideoPreview/CanvasWrapper.js +22 -0
- package/admin/src/components/VideoPreview/Thumbnail.js +12 -0
- package/admin/src/components/VideoPreview/Wrapper.js +15 -0
- package/admin/src/components/VideoPreview/index.js +165 -0
- package/admin/src/components/VideoPreview/reducer.js +33 -0
- package/admin/src/containers/App/index.js +31 -0
- package/admin/src/containers/HomePage/HomePageContent/HomePageList.js +98 -0
- package/admin/src/containers/HomePage/HomePageContent/HomePageSettings.js +103 -0
- package/admin/src/containers/HomePage/HomePageContent/index.js +90 -0
- package/admin/src/containers/HomePage/components.js +18 -0
- package/admin/src/containers/HomePage/index.js +373 -0
- package/admin/src/containers/HomePage/init.js +5 -0
- package/admin/src/containers/HomePage/reducer.js +88 -0
- package/admin/src/containers/HomePage/utils/generateParamsFromQuery.js +14 -0
- package/admin/src/containers/HomePage/utils/generateStringFromParams.js +27 -0
- package/admin/src/containers/HomePage/utils/getHeaderLabel.js +13 -0
- package/admin/src/containers/HomePage/utils/index.js +2 -0
- package/admin/src/containers/Initializer/index.js +44 -0
- package/admin/src/containers/InputModalStepper/HeaderSearch.js +12 -0
- package/admin/src/containers/InputModalStepper/InputModalStepper.js +480 -0
- package/admin/src/containers/InputModalStepper/Search.js +48 -0
- package/admin/src/containers/InputModalStepper/index.js +86 -0
- package/admin/src/containers/InputModalStepper/stepper.js +52 -0
- package/admin/src/containers/InputModalStepperProvider/index.js +564 -0
- package/admin/src/containers/InputModalStepperProvider/init.js +5 -0
- package/admin/src/containers/InputModalStepperProvider/reducer.js +301 -0
- package/admin/src/containers/ModalStepper/index.js +629 -0
- package/admin/src/containers/ModalStepper/init.js +5 -0
- package/admin/src/containers/ModalStepper/reducer.js +132 -0
- package/admin/src/containers/ModalStepper/stepper.js +46 -0
- package/admin/src/containers/SettingsPage/Divider.js +11 -0
- package/admin/src/containers/SettingsPage/SectionTitleWrapper.js +7 -0
- package/admin/src/containers/SettingsPage/Wrapper.js +17 -0
- package/admin/src/containers/SettingsPage/index.js +198 -0
- package/admin/src/containers/SettingsPage/init.js +5 -0
- package/admin/src/containers/SettingsPage/reducer.js +38 -0
- package/admin/src/contexts/AppContext/index.js +5 -0
- package/admin/src/contexts/InputModal/InputModalDataManager.js +5 -0
- package/admin/src/contexts/index.js +2 -0
- package/admin/src/hooks/index.js +2 -0
- package/admin/src/hooks/useAppContext/index.js +6 -0
- package/admin/src/hooks/useModalContext.js +6 -0
- package/admin/src/icons/BrokenFile/index.js +91 -0
- package/admin/src/icons/Cloud/index.js +35 -0
- package/admin/src/icons/DoubleFile/index.js +35 -0
- package/admin/src/icons/Download/index.js +36 -0
- package/admin/src/icons/File/index.js +35 -0
- package/admin/src/index.js +85 -0
- package/admin/src/lifecycles.js +13 -0
- package/admin/src/permissions.js +40 -0
- package/admin/src/pluginId.js +5 -0
- package/admin/src/translations/de.json +81 -0
- package/admin/src/translations/dk.json +81 -0
- package/admin/src/translations/en.json +86 -0
- package/admin/src/translations/es.json +81 -0
- package/admin/src/translations/fr.json +85 -0
- package/admin/src/translations/he.json +81 -0
- package/admin/src/translations/index.js +39 -0
- package/admin/src/translations/it.json +82 -0
- package/admin/src/translations/ja.json +81 -0
- package/admin/src/translations/ms.json +73 -0
- package/admin/src/translations/pl.json +81 -0
- package/admin/src/translations/pt-BR.json +81 -0
- package/admin/src/translations/ru.json +81 -0
- package/admin/src/translations/sk.json +81 -0
- package/admin/src/translations/th.json +81 -0
- package/admin/src/translations/uk.json +78 -0
- package/admin/src/translations/zh-Hans.json +86 -0
- package/admin/src/translations/zh.json +81 -0
- package/admin/src/utils/ItemTypes.js +3 -0
- package/admin/src/utils/canDownloadFile.js +3 -0
- package/admin/src/utils/compactParams.js +13 -0
- package/admin/src/utils/createFileToDownloadName.js +9 -0
- package/admin/src/utils/createMatrix.js +32 -0
- package/admin/src/utils/createNewFilesToDownloadArray.js +57 -0
- package/admin/src/utils/createNewFilesToUploadArray.js +25 -0
- package/admin/src/utils/formatBytes.js +13 -0
- package/admin/src/utils/formatFileForEditing.js +20 -0
- package/admin/src/utils/formatFilters.js +23 -0
- package/admin/src/utils/generatePageFromStart.js +5 -0
- package/admin/src/utils/generateStartFromPage.js +5 -0
- package/admin/src/utils/getFileModelTimestamps.js +15 -0
- package/admin/src/utils/getFilesToDownload.js +5 -0
- package/admin/src/utils/getRequestUrl.js +5 -0
- package/admin/src/utils/getTrad.js +5 -0
- package/admin/src/utils/getType.js +15 -0
- package/admin/src/utils/getYupError.js +17 -0
- package/admin/src/utils/index.js +20 -0
- package/admin/src/utils/unformatBytes.js +16 -0
- package/admin/src/utils/urlYupSchema.js +49 -0
- package/config/config.json +8 -0
- package/config/functions/bootstrap.js +143 -0
- package/config/routes.json +98 -0
- package/config/schema.graphql.js +96 -0
- package/controllers/Upload.js +103 -0
- package/controllers/upload/admin.js +213 -0
- package/controllers/upload/api.js +125 -0
- package/controllers/validation/settings.js +22 -0
- package/controllers/validation/upload.js +27 -0
- package/documentation/1.0.0/overrides/upload-File.json +263 -0
- package/errors.js +48 -0
- package/index.js +7 -0
- package/middlewares/upload/defaults.json +5 -0
- package/middlewares/upload/index.js +35 -0
- package/models/File.js +7 -0
- package/models/File.settings.json +86 -0
- package/package.json +71 -0
- package/services/Upload.js +397 -0
- package/services/image-manipulation.js +167 -0
- package/utils/file.js +23 -0
|
@@ -0,0 +1,629 @@
|
|
|
1
|
+
import React, { useCallback, useEffect, useState, useReducer, useRef } from 'react';
|
|
2
|
+
import axios from 'axios';
|
|
3
|
+
import PropTypes from 'prop-types';
|
|
4
|
+
import { isEqual, isEmpty, get, set } from 'lodash';
|
|
5
|
+
import { Modal, ModalFooter, PopUpWarning, useGlobalContext, request } from 'strapi-helper-plugin';
|
|
6
|
+
import { Button } from '@buffetjs/core';
|
|
7
|
+
import pluginId from '../../pluginId';
|
|
8
|
+
import { getFilesToDownload, getTrad, getYupError, urlSchema } from '../../utils';
|
|
9
|
+
import { useAppContext } from '../../hooks';
|
|
10
|
+
import ModalHeader from '../../components/ModalHeader';
|
|
11
|
+
import stepper from './stepper';
|
|
12
|
+
import init from './init';
|
|
13
|
+
import reducer, { initialState } from './reducer';
|
|
14
|
+
|
|
15
|
+
const ModalStepper = ({
|
|
16
|
+
initialFileToEdit,
|
|
17
|
+
initialStep,
|
|
18
|
+
isOpen,
|
|
19
|
+
onClosed,
|
|
20
|
+
onRemoveFileFromDataToDelete,
|
|
21
|
+
onToggle,
|
|
22
|
+
}) => {
|
|
23
|
+
const { allowedActions } = useAppContext();
|
|
24
|
+
const { emitEvent, formatMessage } = useGlobalContext();
|
|
25
|
+
const [isWarningDeleteOpen, setIsWarningDeleteOpen] = useState(false);
|
|
26
|
+
const [showModalConfirmButtonLoading, setShowModalConfirmButtonLoading] = useState(false);
|
|
27
|
+
const [isFormDisabled, setIsFormDisabled] = useState(false);
|
|
28
|
+
const [formErrors, setFormErrors] = useState(null);
|
|
29
|
+
const [shouldRefetch, setShouldRefetch] = useState(false);
|
|
30
|
+
const [displayNextButton, setDisplayNextButton] = useState(false);
|
|
31
|
+
const [reducerState, dispatch] = useReducer(reducer, initialState, init);
|
|
32
|
+
const { currentStep, fileToEdit, filesToDownload, filesToUpload } = reducerState.toJS();
|
|
33
|
+
const { Component, components, headerBreadcrumbs, next, prev, withBackButton } = stepper[
|
|
34
|
+
currentStep
|
|
35
|
+
];
|
|
36
|
+
const filesToUploadLength = filesToUpload.length;
|
|
37
|
+
const toggleRef = useRef(onToggle);
|
|
38
|
+
const editModalRef = useRef();
|
|
39
|
+
const downloadFilesRef = useRef();
|
|
40
|
+
|
|
41
|
+
useEffect(() => {
|
|
42
|
+
if (currentStep === 'upload') {
|
|
43
|
+
// Close the modal
|
|
44
|
+
if (filesToUploadLength === 0) {
|
|
45
|
+
// Passing true to the onToggle prop will refetch the data when the modal closes
|
|
46
|
+
toggleRef.current(true);
|
|
47
|
+
} else {
|
|
48
|
+
// Download files from url
|
|
49
|
+
downloadFilesRef.current();
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
53
|
+
}, [filesToUploadLength, currentStep]);
|
|
54
|
+
|
|
55
|
+
useEffect(() => {
|
|
56
|
+
if (isOpen) {
|
|
57
|
+
goTo(initialStep);
|
|
58
|
+
|
|
59
|
+
if (initialFileToEdit) {
|
|
60
|
+
dispatch({
|
|
61
|
+
type: 'INIT_FILE_TO_EDIT',
|
|
62
|
+
fileToEdit: initialFileToEdit,
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
// Disabling the rule because we just want to let the ability to open the modal
|
|
67
|
+
// at a specific step then we will let the stepper handle the navigation
|
|
68
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
69
|
+
}, [isOpen]);
|
|
70
|
+
|
|
71
|
+
const addFilesToUpload = ({ target: { value } }) => {
|
|
72
|
+
emitEvent('didSelectFile', { source: 'computer', location: 'upload' });
|
|
73
|
+
|
|
74
|
+
dispatch({
|
|
75
|
+
type: 'ADD_FILES_TO_UPLOAD',
|
|
76
|
+
filesToUpload: value,
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
goTo(next);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
downloadFilesRef.current = async () => {
|
|
83
|
+
const files = getFilesToDownload(filesToUpload);
|
|
84
|
+
|
|
85
|
+
// Emit event when the users download files from url
|
|
86
|
+
if (files.length > 0) {
|
|
87
|
+
emitEvent('didSelectFile', { source: 'url', location: 'upload' });
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
try {
|
|
91
|
+
await Promise.all(
|
|
92
|
+
files.map(file => {
|
|
93
|
+
const { source } = file;
|
|
94
|
+
|
|
95
|
+
return axios
|
|
96
|
+
.get(file.fileURL, {
|
|
97
|
+
responseType: 'blob',
|
|
98
|
+
cancelToken: source.token,
|
|
99
|
+
timeout: 60000,
|
|
100
|
+
})
|
|
101
|
+
.then(({ data }) => {
|
|
102
|
+
const fileName = file.fileInfo.name;
|
|
103
|
+
const createdFile = new File([data], fileName, {
|
|
104
|
+
type: data.type,
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
dispatch({
|
|
108
|
+
type: 'FILE_DOWNLOADED',
|
|
109
|
+
blob: createdFile,
|
|
110
|
+
originalIndex: file.originalIndex,
|
|
111
|
+
fileTempId: file.tempId,
|
|
112
|
+
});
|
|
113
|
+
})
|
|
114
|
+
.catch(err => {
|
|
115
|
+
console.error('fetch file error', err);
|
|
116
|
+
|
|
117
|
+
dispatch({
|
|
118
|
+
type: 'SET_FILE_TO_DOWNLOAD_ERROR',
|
|
119
|
+
originalIndex: file.originalIndex,
|
|
120
|
+
fileTempId: file.tempId,
|
|
121
|
+
});
|
|
122
|
+
});
|
|
123
|
+
})
|
|
124
|
+
);
|
|
125
|
+
} catch (err) {
|
|
126
|
+
// Silent
|
|
127
|
+
}
|
|
128
|
+
};
|
|
129
|
+
|
|
130
|
+
const handleAbortUpload = () => {
|
|
131
|
+
const { abortController } = fileToEdit;
|
|
132
|
+
|
|
133
|
+
abortController.abort();
|
|
134
|
+
|
|
135
|
+
dispatch({
|
|
136
|
+
type: 'ON_ABORT_UPLOAD',
|
|
137
|
+
});
|
|
138
|
+
};
|
|
139
|
+
|
|
140
|
+
const handleCancelFileToUpload = fileOriginalIndex => {
|
|
141
|
+
const fileToCancel = filesToUpload.find(file => file.originalIndex === fileOriginalIndex);
|
|
142
|
+
const { source } = fileToCancel;
|
|
143
|
+
|
|
144
|
+
// Cancel
|
|
145
|
+
if (source) {
|
|
146
|
+
// Cancel dowload file upload with axios
|
|
147
|
+
source.cancel('Operation canceled by the user.');
|
|
148
|
+
} else {
|
|
149
|
+
// Cancel upload with fetch
|
|
150
|
+
fileToCancel.abortController.abort();
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
dispatch({
|
|
154
|
+
type: 'REMOVE_FILE_TO_UPLOAD',
|
|
155
|
+
fileIndex: fileOriginalIndex,
|
|
156
|
+
});
|
|
157
|
+
};
|
|
158
|
+
|
|
159
|
+
const handleChange = ({ target: { name, value } }) => {
|
|
160
|
+
let val = value;
|
|
161
|
+
let type = 'ON_CHANGE';
|
|
162
|
+
|
|
163
|
+
if (name === 'url') {
|
|
164
|
+
setFormErrors(null);
|
|
165
|
+
|
|
166
|
+
val = value.split('\n');
|
|
167
|
+
type = 'ON_CHANGE_URLS_TO_DOWNLOAD';
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
dispatch({
|
|
171
|
+
type,
|
|
172
|
+
keys: name,
|
|
173
|
+
value: val,
|
|
174
|
+
});
|
|
175
|
+
};
|
|
176
|
+
|
|
177
|
+
const handleConfirmDeleteFile = useCallback(async () => {
|
|
178
|
+
const { id } = fileToEdit;
|
|
179
|
+
// Remove the file from the selected data to delete
|
|
180
|
+
onRemoveFileFromDataToDelete(id);
|
|
181
|
+
|
|
182
|
+
// Show a loader in the popup warning
|
|
183
|
+
setShowModalConfirmButtonLoading(true);
|
|
184
|
+
|
|
185
|
+
try {
|
|
186
|
+
await request(`/${pluginId}/files/${id}`, {
|
|
187
|
+
method: 'DELETE',
|
|
188
|
+
});
|
|
189
|
+
|
|
190
|
+
setShouldRefetch(true);
|
|
191
|
+
} catch (err) {
|
|
192
|
+
const errorMessage = get(err, 'response.payload.message', 'An error occured');
|
|
193
|
+
|
|
194
|
+
strapi.notification.toggle({
|
|
195
|
+
type: 'warning',
|
|
196
|
+
message: errorMessage,
|
|
197
|
+
});
|
|
198
|
+
} finally {
|
|
199
|
+
setShowModalConfirmButtonLoading(true);
|
|
200
|
+
toggleModalWarning();
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
// eslint-disable-next-line react-hooks/exhaustive-deps
|
|
204
|
+
}, [fileToEdit]);
|
|
205
|
+
|
|
206
|
+
const handleClickNextButton = async () => {
|
|
207
|
+
try {
|
|
208
|
+
await urlSchema.validate(
|
|
209
|
+
{ filesToDownload: filesToDownload.filter(url => !isEmpty(url)) },
|
|
210
|
+
{ abortEarly: false }
|
|
211
|
+
);
|
|
212
|
+
|
|
213
|
+
setFormErrors(null);
|
|
214
|
+
// Navigate to next step
|
|
215
|
+
dispatch({
|
|
216
|
+
type: 'ADD_URLS_TO_FILES_TO_UPLOAD',
|
|
217
|
+
nextStep: next,
|
|
218
|
+
});
|
|
219
|
+
} catch (err) {
|
|
220
|
+
const formattedErrors = getYupError(err);
|
|
221
|
+
|
|
222
|
+
setFormErrors(formattedErrors.filesToDownload);
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
|
|
226
|
+
const handleClickDeleteFile = async () => {
|
|
227
|
+
toggleModalWarning();
|
|
228
|
+
};
|
|
229
|
+
|
|
230
|
+
const handleClickDeleteFileToUpload = fileIndex => {
|
|
231
|
+
dispatch({
|
|
232
|
+
type: 'REMOVE_FILE_TO_UPLOAD',
|
|
233
|
+
fileIndex,
|
|
234
|
+
});
|
|
235
|
+
|
|
236
|
+
if (currentStep === 'edit-new') {
|
|
237
|
+
dispatch({
|
|
238
|
+
type: 'RESET_FILE_TO_EDIT',
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
goNext();
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
|
|
245
|
+
const handleClose = () => {
|
|
246
|
+
onClosed();
|
|
247
|
+
setIsFormDisabled(false);
|
|
248
|
+
setDisplayNextButton(false);
|
|
249
|
+
setFormErrors(null);
|
|
250
|
+
setShouldRefetch(false);
|
|
251
|
+
|
|
252
|
+
dispatch({
|
|
253
|
+
type: 'RESET_PROPS',
|
|
254
|
+
});
|
|
255
|
+
};
|
|
256
|
+
|
|
257
|
+
const handleCloseModalWarning = async () => {
|
|
258
|
+
setShowModalConfirmButtonLoading(false);
|
|
259
|
+
|
|
260
|
+
onToggle(shouldRefetch);
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
const handleGoToEditNewFile = fileIndex => {
|
|
264
|
+
dispatch({
|
|
265
|
+
type: 'SET_FILE_TO_EDIT',
|
|
266
|
+
fileIndex,
|
|
267
|
+
});
|
|
268
|
+
|
|
269
|
+
goTo('edit-new');
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
const handleGoToAddBrowseFiles = () => {
|
|
273
|
+
dispatch({
|
|
274
|
+
type: 'CLEAN_FILES_ERROR',
|
|
275
|
+
});
|
|
276
|
+
|
|
277
|
+
goBack();
|
|
278
|
+
};
|
|
279
|
+
|
|
280
|
+
const handleSetCropResult = blob => {
|
|
281
|
+
// Emit event : the user cropped a file that is not uploaded
|
|
282
|
+
emitEvent('didCropFile', { duplicatedFile: null, location: 'upload' });
|
|
283
|
+
|
|
284
|
+
dispatch({
|
|
285
|
+
type: 'SET_CROP_RESULT',
|
|
286
|
+
blob,
|
|
287
|
+
});
|
|
288
|
+
};
|
|
289
|
+
|
|
290
|
+
const handleSubmitEditNewFile = e => {
|
|
291
|
+
e.preventDefault();
|
|
292
|
+
|
|
293
|
+
dispatch({
|
|
294
|
+
type: 'ON_SUBMIT_EDIT_NEW_FILE',
|
|
295
|
+
});
|
|
296
|
+
|
|
297
|
+
goNext();
|
|
298
|
+
};
|
|
299
|
+
|
|
300
|
+
const handleSubmitEditExistingFile = async (
|
|
301
|
+
e,
|
|
302
|
+
shouldDuplicateMedia = false,
|
|
303
|
+
file = fileToEdit.file,
|
|
304
|
+
isSubmittingAfterCrop = false
|
|
305
|
+
) => {
|
|
306
|
+
e.preventDefault();
|
|
307
|
+
|
|
308
|
+
if (isSubmittingAfterCrop) {
|
|
309
|
+
emitEvent('didCropFile', { duplicatedFile: shouldDuplicateMedia, location: 'upload' });
|
|
310
|
+
}
|
|
311
|
+
|
|
312
|
+
dispatch({
|
|
313
|
+
type: 'ON_SUBMIT_EDIT_EXISTING_FILE',
|
|
314
|
+
});
|
|
315
|
+
|
|
316
|
+
const headers = {};
|
|
317
|
+
const formData = new FormData();
|
|
318
|
+
|
|
319
|
+
// If the file has been cropped we need to add it to the formData
|
|
320
|
+
// otherwise we just don't send it
|
|
321
|
+
const didCropFile = file instanceof File;
|
|
322
|
+
const { abortController, id, fileInfo } = fileToEdit;
|
|
323
|
+
const requestURL = shouldDuplicateMedia ? `/${pluginId}` : `/${pluginId}?id=${id}`;
|
|
324
|
+
|
|
325
|
+
if (didCropFile) {
|
|
326
|
+
formData.append('files', file);
|
|
327
|
+
}
|
|
328
|
+
|
|
329
|
+
formData.append('fileInfo', JSON.stringify(fileInfo));
|
|
330
|
+
|
|
331
|
+
try {
|
|
332
|
+
await request(
|
|
333
|
+
requestURL,
|
|
334
|
+
{
|
|
335
|
+
method: 'POST',
|
|
336
|
+
headers,
|
|
337
|
+
body: formData,
|
|
338
|
+
signal: abortController.signal,
|
|
339
|
+
},
|
|
340
|
+
false,
|
|
341
|
+
false
|
|
342
|
+
);
|
|
343
|
+
// Close the modal and refetch data
|
|
344
|
+
toggleRef.current(true);
|
|
345
|
+
} catch (err) {
|
|
346
|
+
console.error(err);
|
|
347
|
+
const status = get(err, 'response.status', get(err, 'status', null));
|
|
348
|
+
const statusText = get(err, 'response.statusText', get(err, 'statusText', null));
|
|
349
|
+
let errorMessage = get(
|
|
350
|
+
err,
|
|
351
|
+
['response', 'payload', 'message', '0', 'messages', '0', 'message'],
|
|
352
|
+
get(err, ['response', 'payload', 'message'], statusText)
|
|
353
|
+
);
|
|
354
|
+
|
|
355
|
+
// TODO fix errors globally when the back-end sends readable one
|
|
356
|
+
if (status === 413) {
|
|
357
|
+
errorMessage = formatMessage({ id: 'app.utils.errors.file-too-big.message' });
|
|
358
|
+
}
|
|
359
|
+
|
|
360
|
+
if (status) {
|
|
361
|
+
dispatch({
|
|
362
|
+
type: 'SET_FILE_TO_EDIT_ERROR',
|
|
363
|
+
errorMessage,
|
|
364
|
+
});
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
};
|
|
368
|
+
|
|
369
|
+
const handleReplaceMedia = () => {
|
|
370
|
+
emitEvent('didReplaceMedia', { location: 'upload' });
|
|
371
|
+
editModalRef.current.click();
|
|
372
|
+
};
|
|
373
|
+
|
|
374
|
+
const handleToggle = () => {
|
|
375
|
+
if (filesToUploadLength > 0) {
|
|
376
|
+
// eslint-disable-next-line no-alert
|
|
377
|
+
const confirm = window.confirm(
|
|
378
|
+
formatMessage({ id: getTrad('window.confirm.close-modal.files') })
|
|
379
|
+
);
|
|
380
|
+
|
|
381
|
+
if (!confirm) {
|
|
382
|
+
return;
|
|
383
|
+
}
|
|
384
|
+
}
|
|
385
|
+
|
|
386
|
+
if (!isEqual(initialFileToEdit, fileToEdit) && currentStep === 'edit') {
|
|
387
|
+
// eslint-disable-next-line no-alert
|
|
388
|
+
const confirm = window.confirm(
|
|
389
|
+
formatMessage({ id: getTrad('window.confirm.close-modal.file') })
|
|
390
|
+
);
|
|
391
|
+
|
|
392
|
+
if (!confirm) {
|
|
393
|
+
return;
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
onToggle(shouldRefetch);
|
|
398
|
+
};
|
|
399
|
+
|
|
400
|
+
const handleUploadFiles = async () => {
|
|
401
|
+
dispatch({
|
|
402
|
+
type: 'SET_FILES_UPLOADING_STATE',
|
|
403
|
+
});
|
|
404
|
+
|
|
405
|
+
const requests = filesToUpload.map(
|
|
406
|
+
async ({ file, fileInfo, originalName, originalIndex, abortController }) => {
|
|
407
|
+
const formData = new FormData();
|
|
408
|
+
const headers = {};
|
|
409
|
+
|
|
410
|
+
if (originalName === fileInfo.name) {
|
|
411
|
+
set(fileInfo, 'name', null);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
formData.append('files', file);
|
|
415
|
+
formData.append('fileInfo', JSON.stringify(fileInfo));
|
|
416
|
+
|
|
417
|
+
try {
|
|
418
|
+
await request(
|
|
419
|
+
`/${pluginId}`,
|
|
420
|
+
{
|
|
421
|
+
method: 'POST',
|
|
422
|
+
headers,
|
|
423
|
+
body: formData,
|
|
424
|
+
signal: abortController.signal,
|
|
425
|
+
},
|
|
426
|
+
false,
|
|
427
|
+
false
|
|
428
|
+
);
|
|
429
|
+
|
|
430
|
+
setShouldRefetch(true);
|
|
431
|
+
|
|
432
|
+
dispatch({
|
|
433
|
+
type: 'REMOVE_FILE_TO_UPLOAD',
|
|
434
|
+
fileIndex: originalIndex,
|
|
435
|
+
});
|
|
436
|
+
} catch (err) {
|
|
437
|
+
console.error(err);
|
|
438
|
+
const status = get(err, 'response.status', get(err, 'status', null));
|
|
439
|
+
const statusText = get(err, 'response.statusText', get(err, 'statusText', null));
|
|
440
|
+
let errorMessage = get(
|
|
441
|
+
err,
|
|
442
|
+
['response', 'payload', 'message', '0', 'messages', '0', 'message'],
|
|
443
|
+
get(err, ['response', 'payload', 'message'], statusText)
|
|
444
|
+
);
|
|
445
|
+
|
|
446
|
+
// TODO fix errors globally when the back-end sends readable one
|
|
447
|
+
if (status === 413) {
|
|
448
|
+
errorMessage = formatMessage({ id: 'app.utils.errors.file-too-big.message' });
|
|
449
|
+
}
|
|
450
|
+
|
|
451
|
+
if (status) {
|
|
452
|
+
dispatch({
|
|
453
|
+
type: 'SET_FILE_ERROR',
|
|
454
|
+
fileIndex: originalIndex,
|
|
455
|
+
errorMessage,
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
);
|
|
461
|
+
|
|
462
|
+
await Promise.all(requests);
|
|
463
|
+
};
|
|
464
|
+
|
|
465
|
+
const goBack = () => {
|
|
466
|
+
goTo(prev);
|
|
467
|
+
};
|
|
468
|
+
|
|
469
|
+
const goNext = () => {
|
|
470
|
+
if (next === null) {
|
|
471
|
+
onToggle();
|
|
472
|
+
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
goTo(next);
|
|
477
|
+
};
|
|
478
|
+
|
|
479
|
+
const goTo = to => {
|
|
480
|
+
dispatch({
|
|
481
|
+
type: 'GO_TO',
|
|
482
|
+
to,
|
|
483
|
+
});
|
|
484
|
+
};
|
|
485
|
+
|
|
486
|
+
const toggleModalWarning = () => {
|
|
487
|
+
setIsWarningDeleteOpen(prev => !prev);
|
|
488
|
+
};
|
|
489
|
+
|
|
490
|
+
const shouldDisplayNextButton = currentStep === 'browse' && displayNextButton;
|
|
491
|
+
const isFinishButtonDisabled = filesToUpload.some(file => file.isDownloading || file.isUploading);
|
|
492
|
+
const areButtonsDisabledOnEditExistingFile =
|
|
493
|
+
currentStep === 'edit' && fileToEdit.isUploading === true;
|
|
494
|
+
|
|
495
|
+
return (
|
|
496
|
+
<>
|
|
497
|
+
<Modal isOpen={isOpen} onToggle={handleToggle} onClosed={handleClose}>
|
|
498
|
+
{/* header title */}
|
|
499
|
+
<ModalHeader
|
|
500
|
+
goBack={goBack}
|
|
501
|
+
headerBreadcrumbs={headerBreadcrumbs}
|
|
502
|
+
withBackButton={withBackButton}
|
|
503
|
+
/>
|
|
504
|
+
|
|
505
|
+
{/* body of the modal */}
|
|
506
|
+
{Component && (
|
|
507
|
+
<Component
|
|
508
|
+
{...allowedActions}
|
|
509
|
+
onAbortUpload={handleAbortUpload}
|
|
510
|
+
addFilesToUpload={addFilesToUpload}
|
|
511
|
+
fileToEdit={fileToEdit}
|
|
512
|
+
filesToDownload={filesToDownload}
|
|
513
|
+
filesToUpload={filesToUpload}
|
|
514
|
+
formErrors={formErrors}
|
|
515
|
+
components={components}
|
|
516
|
+
isEditingUploadedFile={currentStep === 'edit'}
|
|
517
|
+
isFormDisabled={isFormDisabled}
|
|
518
|
+
onChange={handleChange}
|
|
519
|
+
onClickCancelUpload={handleCancelFileToUpload}
|
|
520
|
+
onClickDeleteFileToUpload={
|
|
521
|
+
currentStep === 'edit' ? handleClickDeleteFile : handleClickDeleteFileToUpload
|
|
522
|
+
}
|
|
523
|
+
onClickEditNewFile={handleGoToEditNewFile}
|
|
524
|
+
onGoToAddBrowseFiles={handleGoToAddBrowseFiles}
|
|
525
|
+
onSubmitEdit={
|
|
526
|
+
currentStep === 'edit' ? handleSubmitEditExistingFile : handleSubmitEditNewFile
|
|
527
|
+
}
|
|
528
|
+
onToggle={handleToggle}
|
|
529
|
+
toggleDisableForm={setIsFormDisabled}
|
|
530
|
+
ref={currentStep === 'edit' ? editModalRef : null}
|
|
531
|
+
setCropResult={handleSetCropResult}
|
|
532
|
+
setShouldDisplayNextButton={setDisplayNextButton}
|
|
533
|
+
withBackButton={withBackButton}
|
|
534
|
+
/>
|
|
535
|
+
)}
|
|
536
|
+
|
|
537
|
+
<ModalFooter>
|
|
538
|
+
<section>
|
|
539
|
+
<Button type="button" color="cancel" onClick={handleToggle}>
|
|
540
|
+
{formatMessage({ id: 'app.components.Button.cancel' })}
|
|
541
|
+
</Button>
|
|
542
|
+
{shouldDisplayNextButton && (
|
|
543
|
+
<Button
|
|
544
|
+
type="button"
|
|
545
|
+
color="primary"
|
|
546
|
+
onClick={handleClickNextButton}
|
|
547
|
+
disabled={isEmpty(filesToDownload)}
|
|
548
|
+
>
|
|
549
|
+
{formatMessage({ id: getTrad('button.next') })}
|
|
550
|
+
</Button>
|
|
551
|
+
)}
|
|
552
|
+
{currentStep === 'upload' && (
|
|
553
|
+
<Button
|
|
554
|
+
type="button"
|
|
555
|
+
color="success"
|
|
556
|
+
onClick={handleUploadFiles}
|
|
557
|
+
disabled={isFinishButtonDisabled}
|
|
558
|
+
>
|
|
559
|
+
{formatMessage(
|
|
560
|
+
{
|
|
561
|
+
id: getTrad(
|
|
562
|
+
`modal.upload-list.footer.button.${
|
|
563
|
+
filesToUploadLength > 1 ? 'plural' : 'singular'
|
|
564
|
+
}`
|
|
565
|
+
),
|
|
566
|
+
},
|
|
567
|
+
{ number: filesToUploadLength }
|
|
568
|
+
)}
|
|
569
|
+
</Button>
|
|
570
|
+
)}
|
|
571
|
+
{currentStep === 'edit-new' && (
|
|
572
|
+
<Button color="success" type="button" onClick={handleSubmitEditNewFile}>
|
|
573
|
+
{formatMessage({ id: 'form.button.finish' })}
|
|
574
|
+
</Button>
|
|
575
|
+
)}
|
|
576
|
+
{currentStep === 'edit' && (
|
|
577
|
+
<div style={{ margin: 'auto 0' }}>
|
|
578
|
+
<Button
|
|
579
|
+
disabled={isFormDisabled || areButtonsDisabledOnEditExistingFile}
|
|
580
|
+
color="primary"
|
|
581
|
+
onClick={handleReplaceMedia}
|
|
582
|
+
style={{ marginRight: 10 }}
|
|
583
|
+
>
|
|
584
|
+
{formatMessage({ id: getTrad('control-card.replace-media') })}
|
|
585
|
+
</Button>
|
|
586
|
+
|
|
587
|
+
<Button
|
|
588
|
+
disabled={isFormDisabled || areButtonsDisabledOnEditExistingFile}
|
|
589
|
+
color="success"
|
|
590
|
+
type="button"
|
|
591
|
+
onClick={handleSubmitEditExistingFile}
|
|
592
|
+
>
|
|
593
|
+
{formatMessage({ id: 'form.button.finish' })}
|
|
594
|
+
</Button>
|
|
595
|
+
</div>
|
|
596
|
+
)}
|
|
597
|
+
</section>
|
|
598
|
+
</ModalFooter>
|
|
599
|
+
</Modal>
|
|
600
|
+
<PopUpWarning
|
|
601
|
+
onClosed={handleCloseModalWarning}
|
|
602
|
+
isOpen={isWarningDeleteOpen}
|
|
603
|
+
toggleModal={toggleModalWarning}
|
|
604
|
+
popUpWarningType="danger"
|
|
605
|
+
onConfirm={handleConfirmDeleteFile}
|
|
606
|
+
isConfirmButtonLoading={showModalConfirmButtonLoading}
|
|
607
|
+
/>
|
|
608
|
+
</>
|
|
609
|
+
);
|
|
610
|
+
};
|
|
611
|
+
|
|
612
|
+
ModalStepper.defaultProps = {
|
|
613
|
+
initialFileToEdit: null,
|
|
614
|
+
initialStep: 'browse',
|
|
615
|
+
onClosed: () => {},
|
|
616
|
+
onRemoveFileFromDataToDelete: () => {},
|
|
617
|
+
onToggle: () => {},
|
|
618
|
+
};
|
|
619
|
+
|
|
620
|
+
ModalStepper.propTypes = {
|
|
621
|
+
initialFileToEdit: PropTypes.object,
|
|
622
|
+
initialStep: PropTypes.string,
|
|
623
|
+
isOpen: PropTypes.bool.isRequired,
|
|
624
|
+
onClosed: PropTypes.func,
|
|
625
|
+
onRemoveFileFromDataToDelete: PropTypes.func,
|
|
626
|
+
onToggle: PropTypes.func,
|
|
627
|
+
};
|
|
628
|
+
|
|
629
|
+
export default ModalStepper;
|