147 lines
4.6 KiB
JavaScript
Raw Normal View History

import React, { useState, useEffect } from 'react';
import PropTypes from 'prop-types';
2020-03-25 17:55:06 +01:00
import { CopyToClipboard } from 'react-copy-to-clipboard';
import { get } from 'lodash';
import { prefixFileUrlWithBackendUrl } from 'strapi-helper-plugin';
import { getTrad, formatFileForEditing } from '../../utils';
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';
import InputModalStepper from '../../containers/InputModalStepper';
import Name from './Name';
import Wrapper from './Wrapper';
const InputMedia = ({ label, onChange, name, attribute, value, type }) => {
2020-03-25 17:55:06 +01:00
const [modal, setModal] = useState({
isOpen: false,
step: null,
fileToEdit: null,
2020-03-25 17:55:06 +01:00
});
const [fileToDisplay, setFileToDisplay] = useState(0);
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})` : '';
useEffect(() => {
console.log(modal);
}, [modal]);
const handleClickToggleModal = () => {
setModal(prev => ({ step: 'list', isOpen: !prev.isOpen, fileToEdit: null }));
};
const handleChange = v => {
onChange({ target: { name, type, value: v } });
};
const handleFilesNavigation = displayNext => {
if (attribute.multiple) {
if (displayNext && fileToDisplay === value.length - 1) {
setFileToDisplay(0);
return;
}
if (!displayNext && fileToDisplay === 0) {
setFileToDisplay(value.length - 1);
} else {
setFileToDisplay(prev => (displayNext ? prev + 1 : prev - 1));
}
}
};
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-25 17:55:06 +01:00
handleChange(newValue);
};
const handleEditFile = () => {
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'));
};
return (
<Wrapper>
<Name htmlFor={name}>{`${label}${displaySlidePagination}`}</Name>
<CardPreviewWrapper>
<CardControlWrapper>
<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} />
</>
)}
</CardControlWrapper>
{hasNoValue ? (
<EmptyInputMedia onClick={handleClickToggleModal}>
<IconUpload />
<EmptyText id={getTrad('input.placeholder')} />
</EmptyInputMedia>
) : (
<InputFilePreview
isSlider={attribute.multiple && value.length > 1}
2020-03-25 17:55:06 +01:00
file={currentFile}
onClick={handleFilesNavigation}
/>
)}
</CardPreviewWrapper>
2020-03-25 17:55:06 +01:00
{modal.isOpen && (
<InputModalStepper
2020-03-25 17:55:06 +01:00
isOpen={modal.isOpen}
step={modal.step}
fileToEdit={modal.fileToEdit}
multiple={attribute.multiple}
onInputMediaChange={handleChange}
selectedFiles={value}
onToggle={handleClickToggleModal}
allowedTypes={attribute.allowedTypes}
/>
)}
</Wrapper>
);
};
InputMedia.propTypes = {
attribute: PropTypes.shape({
allowedTypes: PropTypes.arrayOf(PropTypes.string),
multiple: PropTypes.bool,
required: PropTypes.bool,
type: PropTypes.string,
}).isRequired,
label: PropTypes.string,
name: PropTypes.string.isRequired,
onChange: PropTypes.func.isRequired,
type: PropTypes.string.isRequired,
value: PropTypes.oneOfType([PropTypes.object, PropTypes.array]),
};
InputMedia.defaultProps = {
label: '',
value: null,
};
export default InputMedia;