Skip to content

Commit

Permalink
fix: Fix styles, refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
meltyshev committed Aug 12, 2024
1 parent c594e8b commit 12f05ad
Show file tree
Hide file tree
Showing 20 changed files with 233 additions and 206 deletions.
21 changes: 4 additions & 17 deletions client/src/components/Card/Card.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ const Card = React.memo(
index,
name,
dueDate,
dueCompleted,
isDueDateCompleted,
stopwatch,
coverUrl,
boardId,
Expand Down Expand Up @@ -82,15 +82,6 @@ const Card = React.memo(
[onUpdate],
);

const handleDueDateCompletionUpdate = useCallback(
(dueDateCompleted) => {
onUpdate({
dueDateCompleted,
});
},
[onUpdate],
);

const handleNameEdit = useCallback(() => {
nameEdit.current.open();
}, []);
Expand Down Expand Up @@ -130,12 +121,7 @@ const Card = React.memo(
)}
{dueDate && (
<span className={classNames(styles.attachment, styles.attachmentLeft)}>
<DueDate
value={dueDate}
completed={dueCompleted}
size="tiny"
onUpdateCompletion={handleDueDateCompletionUpdate}
/>
<DueDate value={dueDate} isCompleted={isDueDateCompleted} size="tiny" />
</span>
)}
{stopwatch && (
Expand Down Expand Up @@ -236,7 +222,7 @@ Card.propTypes = {
index: PropTypes.number.isRequired,
name: PropTypes.string.isRequired,
dueDate: PropTypes.instanceOf(Date),
dueCompleted: PropTypes.bool.isRequired,
isDueDateCompleted: PropTypes.bool,
stopwatch: PropTypes.object, // eslint-disable-line react/forbid-prop-types
coverUrl: PropTypes.string,
boardId: PropTypes.string.isRequired,
Expand Down Expand Up @@ -271,6 +257,7 @@ Card.propTypes = {

Card.defaultProps = {
dueDate: undefined,
isDueDateCompleted: undefined,
stopwatch: undefined,
coverUrl: undefined,
};
Expand Down
46 changes: 25 additions & 21 deletions client/src/components/CardModal/CardModal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React, { useCallback, useRef, useState } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { useTranslation } from 'react-i18next';
import { Button, Grid, Icon, Modal } from 'semantic-ui-react';
import { Button, Checkbox, Grid, Icon, Modal } from 'semantic-ui-react';
import { usePopup } from '../../lib/popup';
import { Markdown } from '../../lib/custom-ui';

Expand Down Expand Up @@ -32,7 +32,7 @@ const CardModal = React.memo(
name,
description,
dueDate,
dueCompleted,
isDueDateCompleted,
stopwatch,
isSubscribed,
isActivitiesFetching,
Expand Down Expand Up @@ -119,6 +119,12 @@ const CardModal = React.memo(
[onUpdate],
);

const handleDueDateCompletionChange = useCallback(() => {
onUpdate({
isDueDateCompleted: !isDueDateCompleted,
});
}, [isDueDateCompleted, onUpdate]);

const handleStopwatchUpdate = useCallback(
(newStopwatch) => {
onUpdate({
Expand Down Expand Up @@ -172,15 +178,6 @@ const CardModal = React.memo(
onClose();
}, [onClose]);

const handleDueDateCompletion = useCallback(
(completion) => {
onUpdate({
dueCompleted: completion,
});
},
[onUpdate],
);

const AttachmentAddPopup = usePopup(AttachmentAddStep);
const BoardMembershipsPopup = usePopup(BoardMembershipsStep);
const LabelsPopup = usePopup(LabelsStep);
Expand Down Expand Up @@ -310,17 +307,24 @@ const CardModal = React.memo(
context: 'title',
})}
</div>
<span className={styles.attachment}>
<span className={classNames(styles.attachment, styles.attachmentDueDate)}>
{canEdit ? (
<DueDateEditPopup defaultValue={dueDate} onUpdate={handleDueDateUpdate}>
<DueDate
value={dueDate}
completed={dueCompleted}
onUpdateCompletion={handleDueDateCompletion}
<>
<Checkbox
checked={isDueDateCompleted}
disabled={!canEdit}
onChange={handleDueDateCompletionChange}
/>
</DueDateEditPopup>
<DueDateEditPopup defaultValue={dueDate} onUpdate={handleDueDateUpdate}>
<DueDate
withStatusIcon
value={dueDate}
isCompleted={isDueDateCompleted}
/>
</DueDateEditPopup>
</>
) : (
<DueDate value={dueDate} completed={dueCompleted} />
<DueDate withStatusIcon value={dueDate} isCompleted={isDueDateCompleted} />
)}
</span>
</div>
Expand Down Expand Up @@ -576,7 +580,7 @@ CardModal.propTypes = {
name: PropTypes.string.isRequired,
description: PropTypes.string,
dueDate: PropTypes.instanceOf(Date),
dueCompleted: PropTypes.bool,
isDueDateCompleted: PropTypes.bool,
stopwatch: PropTypes.object, // eslint-disable-line react/forbid-prop-types
isSubscribed: PropTypes.bool.isRequired,
isActivitiesFetching: PropTypes.bool.isRequired,
Expand Down Expand Up @@ -631,7 +635,7 @@ CardModal.propTypes = {
CardModal.defaultProps = {
description: undefined,
dueDate: undefined,
dueCompleted: false,
isDueDateCompleted: false,
stopwatch: undefined,
};

Expand Down
6 changes: 6 additions & 0 deletions client/src/components/CardModal/CardModal.module.scss
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@
max-width: 100%;
}

.attachmentDueDate {
align-items: center;
display: flex;
gap: 4px;
}

.attachments {
display: inline-block;
margin: 0 8px 8px 0;
Expand Down
184 changes: 108 additions & 76 deletions client/src/components/DueDate/DueDate.jsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import upperFirst from 'lodash/upperFirst';
import React, { useCallback } from 'react';
import React, { useEffect, useRef } from 'react';
import PropTypes from 'prop-types';
import classNames from 'classnames';
import { useTranslation } from 'react-i18next';
import { Checkbox } from 'semantic-ui-react';
import { Icon } from 'semantic-ui-react';
import { useForceUpdate } from '../../lib/hooks';

import getDateFormat from '../../utils/get-date-format';

Expand All @@ -15,6 +16,12 @@ const SIZES = {
MEDIUM: 'medium',
};

const STATUSES = {
DUE_SOON: 'dueSoon',
OVERDUE: 'overdue',
COMPLETED: 'completed',
};

const LONG_DATE_FORMAT_BY_SIZE = {
tiny: 'longDate',
small: 'longDate',
Expand All @@ -27,96 +34,121 @@ const FULL_DATE_FORMAT_BY_SIZE = {
medium: 'fullDateTime',
};

const getDueClass = (value) => {
const now = new Date();
const tomorrow = new Date(now).setDate(now.getDate() + 1);
const STATUS_ICON_PROPS_BY_STATUS = {
[STATUSES.DUE_SOON]: {
name: 'hourglass half',
color: 'orange',
},
[STATUSES.OVERDUE]: {
name: 'hourglass end',
color: 'red',
},
[STATUSES.COMPLETED]: {
name: 'checkmark',
color: 'green',
},
};

const getStatus = (dateTime, isCompleted) => {
if (isCompleted) {
return STATUSES.COMPLETED;
}

const secondsLeft = Math.floor((dateTime.getTime() - new Date().getTime()) / 1000);

if (secondsLeft <= 0) {
return STATUSES.OVERDUE;
}

if (secondsLeft <= 24 * 60 * 60) {
return STATUSES.DUE_SOON;
}

if (now > value) return styles.overdue;
if (tomorrow > value) return styles.soon;
return null;
};

const DueDate = React.memo(
({ value, completed, size, isDisabled, onClick, onUpdateCompletion }) => {
const [t] = useTranslation();

const dateFormat = getDateFormat(
value,
LONG_DATE_FORMAT_BY_SIZE[size],
FULL_DATE_FORMAT_BY_SIZE[size],
);

const classes = [
styles.wrapper,
styles[`wrapper${upperFirst(size)}`],
onClick && styles.wrapperHoverable,
completed ? styles.completed : getDueClass(value),
];

const handleToggleChange = useCallback(
(event) => {
event.preventDefault();
event.stopPropagation();
if (!isDisabled) onUpdateCompletion(!completed);
},
[onUpdateCompletion, completed, isDisabled],
);

return onClick ? (
<div className={styles.wrapperGroup}>
<button
type="button"
aria-label="Toggle completion"
className={classNames(...classes, styles.wrapperCheckbox)}
onClick={handleToggleChange}
>
<Checkbox
className={styles.checkbox}
checked={completed}
disabled={isDisabled}
onChange={handleToggleChange}
/>
</button>
<button
type="button"
disabled={isDisabled}
className={classNames(...classes, styles.wrapperButton)}
onClick={onClick}
>
<span>
{t(`format:${dateFormat}`, {
value,
postProcess: 'formatDate',
})}
</span>
</button>
</div>
) : (
<span className={classNames(...classes)}>
{t(`format:${dateFormat}`, {
value,
postProcess: 'formatDate',
})}
</span>
);
},
);
const DueDate = React.memo(({ value, size, isCompleted, isDisabled, withStatusIcon, onClick }) => {
const [t] = useTranslation();
const forceUpdate = useForceUpdate();

const statusRef = useRef(null);
statusRef.current = getStatus(value, isCompleted);

const intervalRef = useRef(null);

const dateFormat = getDateFormat(
value,
LONG_DATE_FORMAT_BY_SIZE[size],
FULL_DATE_FORMAT_BY_SIZE[size],
);

useEffect(() => {
if ([null, STATUSES.DUE_SOON].includes(statusRef.current)) {
intervalRef.current = setInterval(() => {
const status = getStatus(value, isCompleted);

if (status !== statusRef.current) {
forceUpdate();
}

if (status === STATUSES.OVERDUE) {
clearInterval(intervalRef.current);
}
}, 1000);
}

return () => {
if (intervalRef.current) {
clearInterval(intervalRef.current);
}
};
}, [value, isCompleted, forceUpdate]);

const contentNode = (
<span
className={classNames(
styles.wrapper,
styles[`wrapper${upperFirst(size)}`],
!withStatusIcon && statusRef.current && styles[`wrapper${upperFirst(statusRef.current)}`],
onClick && styles.wrapperHoverable,
)}
>
{t(`format:${dateFormat}`, {
value,
postProcess: 'formatDate',
})}
{withStatusIcon && statusRef.current && (
// eslint-disable-next-line react/jsx-props-no-spreading
<Icon {...STATUS_ICON_PROPS_BY_STATUS[statusRef.current]} className={styles.statusIcon} />
)}
</span>
);

return onClick ? (
<button type="button" disabled={isDisabled} className={styles.button} onClick={onClick}>
{contentNode}
</button>
) : (
contentNode
);
});

DueDate.propTypes = {
value: PropTypes.instanceOf(Date).isRequired,
size: PropTypes.oneOf(Object.values(SIZES)),
isCompleted: PropTypes.bool.isRequired,
isDisabled: PropTypes.bool,
completed: PropTypes.bool,
withStatusIcon: PropTypes.bool,
onClick: PropTypes.func,
onUpdateCompletion: PropTypes.func,
onCompletionToggle: PropTypes.func,
};

DueDate.defaultProps = {
size: SIZES.MEDIUM,
isDisabled: false,
completed: false,
withStatusIcon: false,
onClick: undefined,
onUpdateCompletion: undefined,
onCompletionToggle: undefined,
};

export default DueDate;
Loading

0 comments on commit 12f05ad

Please sign in to comment.