1
0
Fork 0
mirror of https://github.com/plankanban/planka.git synced 2025-07-27 09:09:46 +02:00
planka/client/src/components/Card/Card.jsx

257 lines
8.1 KiB
React
Raw Normal View History

2019-08-31 04:07:25 +05:00
import React, { useCallback, useRef } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { Button, Icon } from 'semantic-ui-react';
import { Link } from 'react-router-dom';
import { Draggable } from 'react-beautiful-dnd';
import { startTimer, stopTimer } from '../../utils/timer';
2019-08-31 04:07:25 +05:00
import Paths from '../../constants/Paths';
import Tasks from './Tasks';
import NameEdit from './NameEdit';
2019-08-31 04:07:25 +05:00
import ActionsPopup from './ActionsPopup';
import User from '../User';
import Label from '../Label';
import DueDate from '../DueDate';
2019-08-31 04:07:25 +05:00
import Timer from '../Timer';
import styles from './Card.module.scss';
2019-08-31 04:07:25 +05:00
const Card = React.memo(
({
id,
index,
name,
dueDate,
2019-08-31 04:07:25 +05:00
timer,
2020-04-23 03:02:53 +05:00
coverUrl,
2020-05-05 01:30:06 +05:00
boardId,
listId,
2020-05-05 01:30:06 +05:00
projectId,
2019-08-31 04:07:25 +05:00
isPersisted,
notificationsTotal,
users,
labels,
tasks,
2020-05-05 01:30:06 +05:00
allProjectsToLists,
allBoardMemberships,
2019-08-31 04:07:25 +05:00
allLabels,
canEdit,
2019-08-31 04:07:25 +05:00
onUpdate,
2020-05-05 01:30:06 +05:00
onMove,
onTransfer,
2019-08-31 04:07:25 +05:00
onDelete,
onUserAdd,
onUserRemove,
2020-05-05 01:30:06 +05:00
onBoardFetch,
2019-08-31 04:07:25 +05:00
onLabelAdd,
onLabelRemove,
onLabelCreate,
onLabelUpdate,
2023-01-09 12:17:06 +01:00
onLabelMove,
2019-08-31 04:07:25 +05:00
onLabelDelete,
}) => {
const nameEdit = useRef(null);
2019-08-31 04:07:25 +05:00
const handleClick = useCallback(() => {
if (document.activeElement) {
document.activeElement.blur();
}
}, []);
const handleToggleTimerClick = useCallback(
(event) => {
event.preventDefault();
onUpdate({
timer: timer.startedAt ? stopTimer(timer) : startTimer(timer),
});
},
[timer, onUpdate],
);
2019-08-31 04:07:25 +05:00
const handleNameUpdate = useCallback(
2020-03-25 00:15:47 +05:00
(newName) => {
2019-08-31 04:07:25 +05:00
onUpdate({
name: newName,
});
},
[onUpdate],
);
const handleNameEdit = useCallback(() => {
nameEdit.current.open();
2019-08-31 04:07:25 +05:00
}, []);
const contentNode = (
<>
2020-04-23 03:02:53 +05:00
{coverUrl && <img src={coverUrl} alt="" className={styles.cover} />}
<div className={styles.details}>
{labels.length > 0 && (
<span className={styles.labels}>
{labels.map((label) => (
<span
key={label.id}
className={classNames(styles.attachment, styles.attachmentLeft)}
>
<Label name={label.name} color={label.color} size="tiny" />
</span>
))}
</span>
)}
<div className={styles.name}>{name}</div>
{tasks.length > 0 && <Tasks items={tasks} />}
{(dueDate || timer || notificationsTotal > 0) && (
<span className={styles.attachments}>
{notificationsTotal > 0 && (
<span
className={classNames(
styles.attachment,
styles.attachmentLeft,
styles.notification,
)}
>
{notificationsTotal}
</span>
)}
{dueDate && (
<span className={classNames(styles.attachment, styles.attachmentLeft)}>
<DueDate value={dueDate} size="tiny" />
</span>
)}
{timer && (
<span className={classNames(styles.attachment, styles.attachmentLeft)}>
<Timer
as="span"
startedAt={timer.startedAt}
total={timer.total}
size="tiny"
onClick={canEdit ? handleToggleTimerClick : undefined}
/>
2020-04-23 03:02:53 +05:00
</span>
)}
</span>
)}
{users.length > 0 && (
<span className={classNames(styles.attachments, styles.attachmentsRight)}>
{users.map((user) => (
<span
key={user.id}
className={classNames(styles.attachment, styles.attachmentRight)}
>
<User name={user.name} avatarUrl={user.avatarUrl} size="small" />
2020-04-23 03:02:53 +05:00
</span>
))}
</span>
)}
</div>
2019-08-31 04:07:25 +05:00
</>
);
return (
<Draggable draggableId={`card:${id}`} index={index} isDragDisabled={!isPersisted || !canEdit}>
2019-08-31 04:07:25 +05:00
{({ innerRef, draggableProps, dragHandleProps }) => (
// eslint-disable-next-line react/jsx-props-no-spreading
<div {...draggableProps} {...dragHandleProps} ref={innerRef} className={styles.wrapper}>
<NameEdit ref={nameEdit} defaultValue={name} onUpdate={handleNameUpdate}>
2019-08-31 04:07:25 +05:00
<div className={styles.card}>
{isPersisted ? (
<>
<Link
2020-04-23 03:02:53 +05:00
to={Paths.CARDS.replace(':id', id)}
2019-08-31 04:07:25 +05:00
className={styles.content}
onClick={handleClick}
>
{contentNode}
</Link>
{canEdit && (
<ActionsPopup
card={{
dueDate,
timer,
boardId,
listId,
projectId,
}}
projectsToLists={allProjectsToLists}
boardMemberships={allBoardMemberships}
currentUserIds={users.map((user) => user.id)}
labels={allLabels}
currentLabelIds={labels.map((label) => label.id)}
onNameEdit={handleNameEdit}
onUpdate={onUpdate}
onMove={onMove}
onTransfer={onTransfer}
onDelete={onDelete}
onUserAdd={onUserAdd}
onUserRemove={onUserRemove}
onBoardFetch={onBoardFetch}
onLabelAdd={onLabelAdd}
onLabelRemove={onLabelRemove}
onLabelCreate={onLabelCreate}
onLabelUpdate={onLabelUpdate}
2023-01-09 12:17:06 +01:00
onLabelMove={onLabelMove}
onLabelDelete={onLabelDelete}
>
<Button className={classNames(styles.actionsButton, styles.target)}>
<Icon fitted name="pencil" size="small" />
</Button>
</ActionsPopup>
)}
2019-08-31 04:07:25 +05:00
</>
) : (
<span className={styles.content}>{contentNode}</span>
)}
</div>
</NameEdit>
2019-08-31 04:07:25 +05:00
</div>
)}
</Draggable>
);
},
);
Card.propTypes = {
id: PropTypes.string.isRequired,
2019-08-31 04:07:25 +05:00
index: PropTypes.number.isRequired,
name: PropTypes.string.isRequired,
dueDate: PropTypes.instanceOf(Date),
2019-08-31 04:07:25 +05:00
timer: PropTypes.object, // eslint-disable-line react/forbid-prop-types
2020-04-23 03:02:53 +05:00
coverUrl: PropTypes.string,
2020-05-05 01:30:06 +05:00
boardId: PropTypes.string.isRequired,
listId: PropTypes.string.isRequired,
2020-05-05 01:30:06 +05:00
projectId: PropTypes.string.isRequired,
2019-08-31 04:07:25 +05:00
isPersisted: PropTypes.bool.isRequired,
notificationsTotal: PropTypes.number.isRequired,
/* eslint-disable react/forbid-prop-types */
users: PropTypes.array.isRequired,
labels: PropTypes.array.isRequired,
tasks: PropTypes.array.isRequired,
2020-05-05 01:30:06 +05:00
allProjectsToLists: PropTypes.array.isRequired,
allBoardMemberships: PropTypes.array.isRequired,
2019-08-31 04:07:25 +05:00
allLabels: PropTypes.array.isRequired,
/* eslint-enable react/forbid-prop-types */
canEdit: PropTypes.bool.isRequired,
2019-08-31 04:07:25 +05:00
onUpdate: PropTypes.func.isRequired,
2020-05-05 01:30:06 +05:00
onMove: PropTypes.func.isRequired,
onTransfer: PropTypes.func.isRequired,
2019-08-31 04:07:25 +05:00
onDelete: PropTypes.func.isRequired,
onUserAdd: PropTypes.func.isRequired,
onUserRemove: PropTypes.func.isRequired,
2020-05-05 01:30:06 +05:00
onBoardFetch: PropTypes.func.isRequired,
2019-08-31 04:07:25 +05:00
onLabelAdd: PropTypes.func.isRequired,
onLabelRemove: PropTypes.func.isRequired,
onLabelCreate: PropTypes.func.isRequired,
onLabelUpdate: PropTypes.func.isRequired,
2023-01-09 12:17:06 +01:00
onLabelMove: PropTypes.func.isRequired,
2019-08-31 04:07:25 +05:00
onLabelDelete: PropTypes.func.isRequired,
};
Card.defaultProps = {
dueDate: undefined,
2019-08-31 04:07:25 +05:00
timer: undefined,
2020-04-23 03:02:53 +05:00
coverUrl: undefined,
2019-08-31 04:07:25 +05:00
};
export default Card;