2020-04-21 05:04:34 +05:00
|
|
|
import React, { useCallback } from 'react';
|
|
|
|
import PropTypes from 'prop-types';
|
2020-04-29 23:55:16 +05:00
|
|
|
import { useTranslation } from 'react-i18next';
|
|
|
|
import { Button } from 'semantic-ui-react';
|
|
|
|
import { useToggle } from '../../../lib/hooks';
|
2020-04-21 05:04:34 +05:00
|
|
|
|
|
|
|
import Item from './Item';
|
|
|
|
|
2020-04-29 23:55:16 +05:00
|
|
|
import styles from './Attachments.module.css';
|
|
|
|
|
2020-04-23 03:02:53 +05:00
|
|
|
const Attachments = React.memo(({ items, onUpdate, onDelete, onCoverUpdate }) => {
|
2020-04-29 23:55:16 +05:00
|
|
|
const [t] = useTranslation();
|
|
|
|
const [isOpened, toggleOpened] = useToggle();
|
|
|
|
|
|
|
|
const handleToggleClick = useCallback(() => {
|
|
|
|
toggleOpened();
|
|
|
|
}, [toggleOpened]);
|
|
|
|
|
2020-04-23 03:02:53 +05:00
|
|
|
const handleCoverSelect = useCallback(
|
|
|
|
(id) => {
|
|
|
|
onCoverUpdate(id);
|
|
|
|
},
|
|
|
|
[onCoverUpdate],
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleCoverDeselect = useCallback(() => {
|
|
|
|
onCoverUpdate(null);
|
|
|
|
}, [onCoverUpdate]);
|
|
|
|
|
2020-04-21 05:04:34 +05:00
|
|
|
const handleUpdate = useCallback(
|
|
|
|
(id, data) => {
|
|
|
|
onUpdate(id, data);
|
|
|
|
},
|
|
|
|
[onUpdate],
|
|
|
|
);
|
|
|
|
|
|
|
|
const handleDelete = useCallback(
|
|
|
|
(id) => {
|
|
|
|
onDelete(id);
|
|
|
|
},
|
|
|
|
[onDelete],
|
|
|
|
);
|
|
|
|
|
2020-04-29 23:55:16 +05:00
|
|
|
const visibleItems = isOpened ? items : items.slice(0, 4);
|
|
|
|
|
2020-04-21 05:04:34 +05:00
|
|
|
return (
|
|
|
|
<>
|
2020-04-29 23:55:16 +05:00
|
|
|
{visibleItems.map((item) => (
|
2020-04-21 05:04:34 +05:00
|
|
|
<Item
|
|
|
|
key={item.id}
|
|
|
|
name={item.name}
|
|
|
|
url={item.url}
|
2020-04-23 03:02:53 +05:00
|
|
|
coverUrl={item.coverUrl}
|
2020-04-21 05:04:34 +05:00
|
|
|
createdAt={item.createdAt}
|
2020-04-23 03:02:53 +05:00
|
|
|
isCover={item.isCover}
|
2020-04-21 05:04:34 +05:00
|
|
|
isPersisted={item.isPersisted}
|
2020-04-23 03:02:53 +05:00
|
|
|
onCoverSelect={() => handleCoverSelect(item.id)}
|
|
|
|
onCoverDeselect={handleCoverDeselect}
|
2020-04-21 05:04:34 +05:00
|
|
|
onUpdate={(data) => handleUpdate(item.id, data)}
|
|
|
|
onDelete={() => handleDelete(item.id)}
|
|
|
|
/>
|
|
|
|
))}
|
2020-04-29 23:55:16 +05:00
|
|
|
{items.length > 4 && (
|
|
|
|
<Button
|
|
|
|
fluid
|
|
|
|
content={
|
|
|
|
isOpened
|
|
|
|
? t('action.showFewerAttachments')
|
|
|
|
: t('action.showAllAttachments', {
|
|
|
|
hidden: items.length - visibleItems.length,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
className={styles.toggleButton}
|
|
|
|
onClick={handleToggleClick}
|
|
|
|
/>
|
|
|
|
)}
|
2020-04-21 05:04:34 +05:00
|
|
|
</>
|
|
|
|
);
|
|
|
|
});
|
|
|
|
|
|
|
|
Attachments.propTypes = {
|
|
|
|
items: PropTypes.array.isRequired, // eslint-disable-line react/forbid-prop-types
|
|
|
|
onUpdate: PropTypes.func.isRequired,
|
|
|
|
onDelete: PropTypes.func.isRequired,
|
2020-04-23 03:02:53 +05:00
|
|
|
onCoverUpdate: PropTypes.func.isRequired,
|
2020-04-21 05:04:34 +05:00
|
|
|
};
|
|
|
|
|
|
|
|
export default Attachments;
|