import React, { useCallback, useEffect, useRef, useState } from 'react'; import PropTypes from 'prop-types'; import classNames from 'classnames'; import { useTranslation } from 'react-i18next'; import { Draggable, Droppable } from 'react-beautiful-dnd'; import { Button, Icon } from 'semantic-ui-react'; import { usePopup } from '../../lib/popup'; import DroppableTypes from '../../constants/DroppableTypes'; import CardContainer from '../../containers/CardContainer'; import NameEdit from './NameEdit'; import CardAdd from './CardAdd'; import ActionsStep from './ActionsStep'; import { ReactComponent as PlusMathIcon } from '../../assets/images/plus-math-icon.svg'; import styles from './List.module.scss'; const List = React.memo( // eslint-disable-next-line prettier/prettier ({ id, index, name, isPersisted, isCollapsed, cardIds, isFiltered, filteredCardIds, canEdit, onUpdate, onDelete, onCardCreate }) => { const [t] = useTranslation(); const [isAddCardOpened, setIsAddCardOpened] = useState(false); const nameEdit = useRef(null); const listWrapper = useRef(null); const handleToggleCollapseClick = useCallback(() => { if (isPersisted && canEdit) { onUpdate({ isCollapsed: !isCollapsed, }); } }, [isPersisted, canEdit, onUpdate, isCollapsed]); const handleHeaderNameClick = useCallback(() => { if (isPersisted && canEdit) { nameEdit.current.open(); } }, [isPersisted, canEdit]); const handleNameUpdate = useCallback( (newName) => { onUpdate({ name: newName, }); }, [onUpdate], ); const handleAddCardClick = useCallback(() => { setIsAddCardOpened(true); }, []); const handleAddCardClose = useCallback(() => { setIsAddCardOpened(false); }, []); const handleNameEdit = useCallback(() => { nameEdit.current.open(); }, []); const handleCardAdd = useCallback(() => { setIsAddCardOpened(true); }, []); useEffect(() => { if (isAddCardOpened) { listWrapper.current.scrollTop = listWrapper.current.scrollHeight; } }, [filteredCardIds, isAddCardOpened]); const ActionsPopup = usePopup(ActionsStep); const cardsNode = ( {({ innerRef, droppableProps, placeholder }) => ( // eslint-disable-next-line react/jsx-props-no-spreading
{filteredCardIds.map((cardId, cardIndex) => ( ))} {placeholder} {canEdit && ( )}
)}
); const cardsCountText = () => { return ( [ isFiltered ? `${filteredCardIds.length} ${t('common.of')} ${cardIds.length} ` : `${cardIds.length} `, ] + [cardIds.length !== 1 ? t('common.cards') : t('common.card')] ); }; if (isCollapsed) { return ( {({ innerRef, draggableProps, dragHandleProps }) => (
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */}
{name}
{cardsCountText()}
)}
); } return ( {({ innerRef, draggableProps, dragHandleProps }) => (
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */}
{/* eslint-disable-next-line jsx-a11y/click-events-have-key-events, jsx-a11y/no-static-element-interactions */}
{name}
{isPersisted && canEdit && ( )}
{cardsCountText()}
{cardsNode}
{!isAddCardOpened && canEdit && ( )}
)}
); }, ); List.propTypes = { id: PropTypes.string.isRequired, index: PropTypes.number.isRequired, name: PropTypes.string.isRequired, isCollapsed: PropTypes.bool.isRequired, isPersisted: PropTypes.bool.isRequired, cardIds: PropTypes.array.isRequired, // eslint-disable-line react/forbid-prop-types isFiltered: PropTypes.bool.isRequired, filteredCardIds: PropTypes.array.isRequired, // eslint-disable-line react/forbid-prop-types canEdit: PropTypes.bool.isRequired, onUpdate: PropTypes.func.isRequired, onDelete: PropTypes.func.isRequired, onCardCreate: PropTypes.func.isRequired, }; export default List;