2020-03-30 14:07:24 +02:00
|
|
|
import React, { useState, useEffect } from 'react';
|
2020-03-09 16:49:12 +01:00
|
|
|
import PropTypes from 'prop-types';
|
2020-03-25 17:55:06 +01:00
|
|
|
import { CopyToClipboard } from 'react-copy-to-clipboard';
|
|
|
|
import { get } from 'lodash';
|
2020-03-30 10:57:07 +02:00
|
|
|
import { prefixFileUrlWithBackendUrl } from 'strapi-helper-plugin';
|
2020-03-03 16:40:02 +01:00
|
|
|
|
2020-03-30 10:57:07 +02:00
|
|
|
import { getTrad, formatFileForEditing } from '../../utils';
|
2020-03-25 01:04:49 +01:00
|
|
|
import CardControl from '../CardControl';
|
|
|
|
import CardControlWrapper from './CardControlWrapper';
|
|
|
|
import CardPreviewWrapper from './CardPreviewWrapper';
|
|
|
|
import EmptyInputMedia from './EmptyInputMedia';
|
|
|
|
import EmptyText from './EmptyText';
|
|
|
|
import IconUpload from './IconUpload';
|
|
|
|
import InputFilePreview from './InputFilePreview';
|
2020-03-24 00:11:16 +01:00
|
|
|
import InputModalStepper from '../../containers/InputModalStepper';
|
2020-03-09 16:49:12 +01:00
|
|
|
import Name from './Name';
|
2020-03-23 16:24:26 +01:00
|
|
|
import Wrapper from './Wrapper';
|
2020-03-03 16:40:02 +01:00
|
|
|
|
2020-03-25 01:04:49 +01:00
|
|
|
const InputMedia = ({ label, onChange, name, attribute, value, type }) => {
|
2020-03-25 17:55:06 +01:00
|
|
|
const [modal, setModal] = useState({
|
|
|
|
isOpen: false,
|
|
|
|
step: null,
|
2020-03-30 14:07:24 +02:00
|
|
|
fileToEdit: null,
|
2020-03-25 17:55:06 +01:00
|
|
|
});
|
2020-03-25 01:04:49 +01:00
|
|
|
const [fileToDisplay, setFileToDisplay] = useState(0);
|
2020-03-30 10:57:07 +02:00
|
|
|
const hasNoValue = !!value && Array.isArray(value) && value.length === 0;
|
2020-03-25 17:55:06 +01:00
|
|
|
const currentFile = attribute.multiple ? value[fileToDisplay] : value;
|
|
|
|
const fileURL = get(currentFile, ['url'], null);
|
|
|
|
const prefixedFileURL = fileURL ? prefixFileUrlWithBackendUrl(fileURL) : null;
|
|
|
|
const displaySlidePagination =
|
|
|
|
attribute.multiple && value.length > 1 ? ` (${fileToDisplay + 1}/${value.length})` : '';
|
|
|
|
|
2020-03-30 14:07:24 +02:00
|
|
|
useEffect(() => {
|
|
|
|
console.log(modal);
|
|
|
|
}, [modal]);
|
|
|
|
|
2020-03-09 16:49:12 +01:00
|
|
|
const handleClickToggleModal = () => {
|
2020-03-30 14:07:24 +02:00
|
|
|
setModal(prev => ({ step: 'list', isOpen: !prev.isOpen, fileToEdit: null }));
|
2020-03-09 16:49:12 +01:00
|
|
|
};
|
2020-03-25 01:04:49 +01:00
|
|
|
|
|
|
|
const handleChange = v => {
|
|
|
|
onChange({ target: { name, type, value: v } });
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleFilesNavigation = displayNext => {
|
2020-03-25 15:48:10 +01:00
|
|
|
if (attribute.multiple) {
|
|
|
|
if (displayNext && fileToDisplay === value.length - 1) {
|
|
|
|
setFileToDisplay(0);
|
2020-03-25 01:04:49 +01:00
|
|
|
|
2020-03-25 15:48:10 +01:00
|
|
|
return;
|
|
|
|
}
|
2020-03-25 01:04:49 +01:00
|
|
|
|
2020-03-25 15:48:10 +01:00
|
|
|
if (!displayNext && fileToDisplay === 0) {
|
|
|
|
setFileToDisplay(value.length - 1);
|
|
|
|
} else {
|
|
|
|
setFileToDisplay(prev => (displayNext ? prev + 1 : prev - 1));
|
|
|
|
}
|
2020-03-25 01:04:49 +01:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2020-03-25 17:55:06 +01:00
|
|
|
const handleRemoveFile = () => {
|
|
|
|
const newValue = attribute.multiple
|
|
|
|
? value.filter((file, index) => index !== fileToDisplay)
|
|
|
|
: null;
|
|
|
|
|
|
|
|
if (fileToDisplay === newValue.length) {
|
|
|
|
setFileToDisplay(fileToDisplay > 0 ? fileToDisplay - 1 : fileToDisplay);
|
|
|
|
}
|
2020-03-30 10:57:07 +02:00
|
|
|
|
2020-03-25 17:55:06 +01:00
|
|
|
handleChange(newValue);
|
|
|
|
};
|
|
|
|
|
|
|
|
const handleEditFile = () => {
|
2020-03-30 10:57:07 +02:00
|
|
|
setModal(() => ({ isOpen: true, step: 'edit', fileToEdit: formatFileForEditing(currentFile) }));
|
2020-03-25 17:55:06 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
const handleCopy = () => {
|
|
|
|
strapi.notification.info(getTrad('notification.link-copied'));
|
|
|
|
};
|
2020-03-09 16:49:12 +01:00
|
|
|
|
|
|
|
return (
|
2020-03-24 00:11:16 +01:00
|
|
|
<Wrapper>
|
2020-03-25 01:04:49 +01:00
|
|
|
<Name htmlFor={name}>{`${label}${displaySlidePagination}`}</Name>
|
|
|
|
|
|
|
|
<CardPreviewWrapper>
|
|
|
|
<CardControlWrapper>
|
2020-03-25 15:48:10 +01:00
|
|
|
<CardControl color="#9EA7B8" type="plus" onClick={handleClickToggleModal} />
|
2020-03-25 17:55:06 +01:00
|
|
|
{!hasNoValue && (
|
|
|
|
<>
|
|
|
|
<CardControl color="#9EA7B8" type="pencil" onClick={handleEditFile} />
|
|
|
|
<CopyToClipboard onCopy={handleCopy} text={prefixedFileURL}>
|
|
|
|
<CardControl color="#9EA7B8" type="link" />
|
|
|
|
</CopyToClipboard>
|
|
|
|
<CardControl color="#9EA7B8" type="trash-alt" onClick={handleRemoveFile} />
|
|
|
|
</>
|
|
|
|
)}
|
2020-03-25 01:04:49 +01:00
|
|
|
</CardControlWrapper>
|
|
|
|
{hasNoValue ? (
|
2020-03-25 15:48:10 +01:00
|
|
|
<EmptyInputMedia onClick={handleClickToggleModal}>
|
2020-03-25 01:04:49 +01:00
|
|
|
<IconUpload />
|
|
|
|
<EmptyText id={getTrad('input.placeholder')} />
|
|
|
|
</EmptyInputMedia>
|
|
|
|
) : (
|
|
|
|
<InputFilePreview
|
2020-03-25 13:17:47 +01:00
|
|
|
isSlider={attribute.multiple && value.length > 1}
|
2020-03-25 17:55:06 +01:00
|
|
|
file={currentFile}
|
2020-03-25 01:04:49 +01:00
|
|
|
onClick={handleFilesNavigation}
|
|
|
|
/>
|
|
|
|
)}
|
2020-03-24 00:11:16 +01:00
|
|
|
</CardPreviewWrapper>
|
2020-03-09 16:49:12 +01:00
|
|
|
|
2020-03-25 17:55:06 +01:00
|
|
|
{modal.isOpen && (
|
2020-03-24 00:11:16 +01:00
|
|
|
<InputModalStepper
|
2020-03-25 17:55:06 +01:00
|
|
|
isOpen={modal.isOpen}
|
|
|
|
step={modal.step}
|
|
|
|
fileToEdit={modal.fileToEdit}
|
2020-03-23 16:24:26 +01:00
|
|
|
multiple={attribute.multiple}
|
2020-03-25 01:04:49 +01:00
|
|
|
onInputMediaChange={handleChange}
|
2020-03-25 15:48:10 +01:00
|
|
|
selectedFiles={value}
|
2020-03-09 16:49:12 +01:00
|
|
|
onToggle={handleClickToggleModal}
|
|
|
|
/>
|
|
|
|
)}
|
2020-03-24 00:11:16 +01:00
|
|
|
</Wrapper>
|
2020-03-09 16:49:12 +01:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
InputMedia.propTypes = {
|
|
|
|
attribute: PropTypes.shape({
|
|
|
|
multiple: PropTypes.bool,
|
|
|
|
required: PropTypes.bool,
|
2020-03-23 16:24:26 +01:00
|
|
|
type: PropTypes.string,
|
2020-03-09 16:49:12 +01:00
|
|
|
}).isRequired,
|
2020-03-23 16:24:26 +01:00
|
|
|
label: PropTypes.string,
|
|
|
|
name: PropTypes.string.isRequired,
|
|
|
|
onChange: PropTypes.func.isRequired,
|
2020-03-25 01:04:49 +01:00
|
|
|
type: PropTypes.string.isRequired,
|
2020-03-25 15:48:10 +01:00
|
|
|
value: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
|
2020-03-09 16:49:12 +01:00
|
|
|
};
|
|
|
|
InputMedia.defaultProps = {
|
|
|
|
label: '',
|
2020-03-25 01:04:49 +01:00
|
|
|
value: null,
|
2020-03-03 16:40:02 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export default InputMedia;
|