Skip to content

Commit 336b666

Browse files
feat: file upload new data model
1 parent dd76184 commit 336b666

File tree

2 files changed

+26
-30
lines changed

2 files changed

+26
-30
lines changed

src/components/FileUpload/UploadConfirmModal.jsx

Lines changed: 15 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,17 @@ import messages from './messages';
99
import { useUploadConfirmModalHooks } from './hooks';
1010

1111
const UploadConfirmModal = ({
12-
open, files, closeHandler, uploadHandler,
12+
open, file, closeHandler, uploadHandler,
1313
}) => {
1414
const { formatMessage } = useIntl();
1515

1616
const {
17-
errors,
17+
shouldShowError,
1818
exitHandler,
1919
confirmUploadClickHandler,
2020
onFileDescriptionChange,
2121
} = useUploadConfirmModalHooks({
22-
files,
22+
file,
2323
closeHandler,
2424
uploadHandler,
2525
});
@@ -30,6 +30,7 @@ const UploadConfirmModal = ({
3030
title={formatMessage(messages.uploadFileModalTitle)}
3131
hasCloseButton={false}
3232
onClose={exitHandler}
33+
isBlocking
3334
>
3435
<ModalDialog.Header>
3536
<ModalDialog.Title>
@@ -39,28 +40,26 @@ const UploadConfirmModal = ({
3940

4041
<ModalDialog.Body>
4142
<div>
42-
{files.map((file, i) => (
43-
// note: we only support one file
44-
// eslint-disable-next-line react/no-array-index-key
45-
<Form.Group key={i}>
43+
{file && (
44+
<Form.Group>
4645
<FormLabel>
4746
<strong>
4847
{formatMessage(messages.uploadFileDescriptionFieldLabel)}
4948
</strong>
5049
<span className="file-name-ellipsis">{file.name}</span>
5150
</FormLabel>
5251
<Form.Control
53-
isInvalid={errors[i]}
54-
name={`file-${i}-description`}
52+
isInvalid={shouldShowError}
53+
name="file-description"
5554
onChange={onFileDescriptionChange}
5655
/>
57-
{errors[i] && (
56+
{shouldShowError && (
5857
<Form.Control.Feedback type="invalid">
59-
{errors[i] && formatMessage(messages.fileDescriptionMissingError)}
58+
formatMessage(messages.fileDescriptionMissingError)
6059
</Form.Control.Feedback>
6160
)}
6261
</Form.Group>
63-
))}
62+
)}
6463
</div>
6564
</ModalDialog.Body>
6665
<ModalDialog.Footer>
@@ -79,18 +78,15 @@ const UploadConfirmModal = ({
7978

8079
UploadConfirmModal.defaultProps = {
8180
open: false,
82-
files: [],
8381
closeHandler: () => {},
8482
uploadHandler: () => {},
8583
};
8684
UploadConfirmModal.propTypes = {
8785
open: PropTypes.bool,
88-
files: PropTypes.arrayOf(
89-
PropTypes.shape({
90-
name: PropTypes.string,
91-
description: PropTypes.string,
92-
}),
93-
),
86+
file: PropTypes.shape({
87+
name: PropTypes.string,
88+
description: PropTypes.string,
89+
}).isRequired,
9490
closeHandler: PropTypes.func,
9591
uploadHandler: PropTypes.func,
9692
};

src/components/FileUpload/hooks.js

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,28 @@ import { useCallback } from 'react';
33
import { StrictDict, useKeyedState } from '@edx/react-unit-test-utils';
44

55
export const stateKeys = StrictDict({
6-
errors: 'errors',
6+
shouldShowError: 'shouldShowError',
77
isModalOpen: 'isModalOpen',
88
uploadArgs: 'uploadArgs',
99
description: 'description',
1010
});
1111
export const useUploadConfirmModalHooks = ({
12-
files, closeHandler, uploadHandler,
12+
file, closeHandler, uploadHandler,
1313
}) => {
14-
const [description, setDescription] = useKeyedState(stateKeys.description, null);
15-
const [errors, setErrors] = useKeyedState(stateKeys.errors, []);
14+
const [description, setDescription] = useKeyedState(stateKeys.description, '');
15+
const [shouldShowError, setShouldShowError] = useKeyedState(stateKeys.shouldShowError, false);
1616

1717
const confirmUploadClickHandler = () => {
18-
const errorList = files.map((file) => (!file.description));
19-
setErrors(errorList);
20-
if (errorList.some((error) => error)) {
21-
return;
18+
if (description !== '') {
19+
uploadHandler(file, description);
20+
} else {
21+
setShouldShowError(true);
2222
}
23-
uploadHandler();
2423
};
2524

2625
const exitHandler = () => {
27-
setErrors([]);
26+
setShouldShowError(false);
27+
setDescription('');
2828
closeHandler();
2929
};
3030

@@ -33,7 +33,7 @@ export const useUploadConfirmModalHooks = ({
3333
const onFileDescriptionChange = (event) => setDescription(event.target.value);
3434

3535
return {
36-
errors,
36+
shouldShowError,
3737
confirmUploadClickHandler,
3838
exitHandler,
3939
onFileDescriptionChange,

0 commit comments

Comments
 (0)