import pick from 'lodash/pick'; import React, { useCallback } from 'react'; import PropTypes from 'prop-types'; import { useTranslation } from 'react-i18next'; import { Button } from 'semantic-ui-react'; import { Popup } from '../../lib/custom-ui'; import { useSteps } from '../../hooks'; import AddStep from './AddStep'; import EditStep from './EditStep'; import Item from './Item'; import styles from './LabelsStep.module.css'; const StepTypes = { ADD: 'ADD', EDIT: 'EDIT', }; const LabelsStep = React.memo( ({ items, currentIds, title, onSelect, onDeselect, onCreate, onUpdate, onDelete, onBack }) => { const [t] = useTranslation(); const [step, openStep, handleBack] = useSteps(); const handleAddClick = useCallback(() => { openStep(StepTypes.ADD); }, [openStep]); const handleEdit = useCallback( id => { openStep(StepTypes.EDIT, { id, }); }, [openStep], ); const handleSelect = useCallback( id => { onSelect(id); }, [onSelect], ); const handleDeselect = useCallback( id => { onDeselect(id); }, [onDeselect], ); const handleUpdate = useCallback( (id, data) => { onUpdate(id, data); }, [onUpdate], ); const handleDelete = useCallback( id => { onDelete(id); }, [onDelete], ); if (step) { switch (step.type) { case StepTypes.ADD: return ; case StepTypes.EDIT: { const currentItem = items.find(item => item.id === step.params.id); if (currentItem) { return ( handleUpdate(currentItem.id, data)} onDelete={() => handleDelete(currentItem.id)} onBack={handleBack} /> ); } openStep(null); break; } default: } } return ( <> {t(title)} {items.map(item => ( handleSelect(item.id)} onDeselect={() => handleDeselect(item.id)} onEdit={() => handleEdit(item.id)} /> ))}