Add file attachments

This commit is contained in:
Maksim Eltyshev
2020-04-21 05:44:12 +05:00
parent 202abacaec
commit 6a68ec9c1e
103 changed files with 1847 additions and 305 deletions
@@ -0,0 +1,45 @@
import React, { useCallback } from 'react';
import PropTypes from 'prop-types';
import Item from './Item';
const Attachments = React.memo(({ items, onUpdate, onDelete }) => {
const handleUpdate = useCallback(
(id, data) => {
onUpdate(id, data);
},
[onUpdate],
);
const handleDelete = useCallback(
(id) => {
onDelete(id);
},
[onDelete],
);
return (
<>
{items.map((item) => (
<Item
key={item.id}
name={item.name}
url={item.url}
thumbnailUrl={item.thumbnailUrl}
createdAt={item.createdAt}
isPersisted={item.isPersisted}
onUpdate={(data) => handleUpdate(item.id, data)}
onDelete={() => handleDelete(item.id)}
/>
))}
</>
);
});
Attachments.propTypes = {
items: PropTypes.array.isRequired, // eslint-disable-line react/forbid-prop-types
onUpdate: PropTypes.func.isRequired,
onDelete: PropTypes.func.isRequired,
};
export default Attachments;
+107
View File
@@ -0,0 +1,107 @@
import dequal from 'dequal';
import React, { useCallback, useEffect, useRef } from 'react';
import PropTypes from 'prop-types';
import { useTranslation } from 'react-i18next';
import { Button, Form } from 'semantic-ui-react';
import { withPopup } from '../../../lib/popup';
import { Input, Popup } from '../../../lib/custom-ui';
import { useForm, useSteps } from '../../../hooks';
import DeleteStep from '../../DeleteStep';
import styles from './EditPopup.module.css';
const StepTypes = {
DELETE: 'DELETE',
};
const EditStep = React.memo(({ defaultData, onUpdate, onDelete, onClose }) => {
const [t] = useTranslation();
const [data, handleFieldChange] = useForm(() => ({
name: '',
...defaultData,
}));
const [step, openStep, handleBack] = useSteps();
const nameField = useRef(null);
const handleSubmit = useCallback(() => {
const cleanData = {
...data,
name: data.name.trim(),
};
if (!cleanData.name) {
nameField.current.select();
return;
}
if (!dequal(cleanData, defaultData)) {
onUpdate(cleanData);
}
onClose();
}, [defaultData, onUpdate, onClose, data]);
const handleDeleteClick = useCallback(() => {
openStep(StepTypes.DELETE);
}, [openStep]);
useEffect(() => {
nameField.current.select();
}, []);
if (step && step.type === StepTypes.DELETE) {
return (
<DeleteStep
title={t('common.deleteAttachment', {
context: 'title',
})}
content={t('common.areYouSureYouWantToDeleteThisAttachment')}
buttonContent={t('action.deleteAttachment')}
onConfirm={onDelete}
onBack={handleBack}
/>
);
}
return (
<>
<Popup.Header>
{t('common.editAttachment', {
context: 'title',
})}
</Popup.Header>
<Popup.Content>
<Form onSubmit={handleSubmit}>
<div className={styles.text}>{t('common.title')}</div>
<Input
fluid
ref={nameField}
name="name"
value={data.name}
className={styles.field}
onChange={handleFieldChange}
/>
<Button positive content={t('action.save')} />
</Form>
<Button
content={t('action.delete')}
className={styles.deleteButton}
onClick={handleDeleteClick}
/>
</Popup.Content>
</>
);
});
EditStep.propTypes = {
defaultData: PropTypes.object.isRequired, // eslint-disable-line react/forbid-prop-types
onUpdate: PropTypes.func.isRequired,
onDelete: PropTypes.func.isRequired,
onClose: PropTypes.func.isRequired,
};
export default withPopup(EditStep);
@@ -0,0 +1,17 @@
.deleteButton {
bottom: 12px;
box-shadow: 0 1px 0 #cbcccc;
position: absolute;
right: 9px;
}
.field {
margin-bottom: 8px;
}
.text {
color: #444444;
font-size: 12px;
font-weight: bold;
padding-bottom: 6px;
}
@@ -0,0 +1,85 @@
import React, { useCallback } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { useTranslation } from 'react-i18next';
import { Button, Icon, Loader } from 'semantic-ui-react';
import EditPopup from './EditPopup';
import styles from './Item.module.css';
const Item = React.memo(
({ name, url, thumbnailUrl, createdAt, isPersisted, onUpdate, onDelete }) => {
const [t] = useTranslation();
const handleClick = useCallback(() => {
window.open(url, '_blank');
}, [url]);
if (!isPersisted) {
return (
<div className={classNames(styles.wrapper, styles.wrapperSubmitting)}>
<Loader inverted />
</div>
);
}
const filename = url.split('/').pop();
const extension = filename.slice((Math.max(0, filename.lastIndexOf('.')) || Infinity) + 1);
return (
/* eslint-disable jsx-a11y/click-events-have-key-events,
jsx-a11y/no-static-element-interactions */
<div className={styles.wrapper} onClick={handleClick}>
{/* eslint-enable jsx-a11y/click-events-have-key-events,
jsx-a11y/no-static-element-interactions */}
<div
className={styles.thumbnail}
style={{
backgroundImage: thumbnailUrl && `url(${thumbnailUrl}`,
}}
>
{!thumbnailUrl && <span className={styles.extension}>{extension || '-'}</span>}
</div>
<div className={styles.details}>
<span className={styles.name}>{name}</span>
<span className={styles.options}>
{t('format:longDateTime', {
postProcess: 'formatDate',
value: createdAt,
})}
</span>
</div>
<EditPopup
defaultData={{
name,
}}
onUpdate={onUpdate}
onDelete={onDelete}
>
<Button className={classNames(styles.button, styles.target)}>
<Icon fitted name="pencil" size="small" />
</Button>
</EditPopup>
</div>
);
},
);
Item.propTypes = {
name: PropTypes.string.isRequired,
url: PropTypes.string,
thumbnailUrl: PropTypes.string,
createdAt: PropTypes.instanceOf(Date),
isPersisted: PropTypes.bool.isRequired,
onUpdate: PropTypes.func.isRequired,
onDelete: PropTypes.func.isRequired,
};
Item.defaultProps = {
url: undefined,
thumbnailUrl: undefined,
createdAt: undefined,
};
export default Item;
@@ -0,0 +1,90 @@
.button {
background: transparent !important;
box-shadow: none !important;
line-height: 28px !important;
margin: 0 !important;
min-height: auto !important;
opacity: 0;
padding: 0 !important;
position: absolute;
right: 2px;
top: 2px;
width: 28px;
}
.button:hover {
background-color: rgba(9, 30, 66, 0.08) !important;
}
.details {
box-sizing: border-box;
padding: 8px 32px 8px 128px;
margin: 0;
min-height: 80px;
z-index: 0;
}
.extension {
color: #5e6c84;
display: block;
font-size: 18px;
font-weight: 700;
height: 100%;
line-height: 80px;
text-align: center;
text-decoration: none;
text-transform: uppercase;
width: 100%;
}
.name {
color: #17394d;
font-size: 14px;
font-weight: 700;
line-height: 20px;
word-wrap: break-word;
}
.options {
display: block;
color: #6b808c;
line-height: 20px;
margin-bottom: 8px;
}
.thumbnail {
border-radius: 3px;
background-color: rgba(9, 30, 66, 0.04);
background-position: center;
background-repeat: no-repeat;
background-size: cover;
border-radius: 3px;
height: 80px;
left: 0;
margin-top: -40px;
position: absolute;
text-align: center;
text-decoration: none;
top: 50%;
width: 112px;
z-index: 1;
}
.wrapper {
cursor: pointer;
margin-bottom: 8px;
min-height: 80px;
position: relative;
}
.wrapper:hover .details {
background-color: rgba(9, 30, 66, 0.04);
}
.wrapper:hover .target {
opacity: 1;
}
.wrapperSubmitting {
background-color: rgba(9, 30, 66, 0.04);
}
@@ -0,0 +1,3 @@
import Attachments from './Attachments';
export default Attachments;