import { uniq } from 'lodash'; import React, { useCallback, useMemo, useState } from 'react'; import { useSelector } from 'react-redux'; import { Tag } from 'App/State/TagsAppState'; import Artist from 'Artist/Artist'; import Form from 'Components/Form/Form'; import FormGroup from 'Components/Form/FormGroup'; import FormInputGroup from 'Components/Form/FormInputGroup'; import FormLabel from 'Components/Form/FormLabel'; import Label from 'Components/Label'; import Button from 'Components/Link/Button'; import ModalBody from 'Components/Modal/ModalBody'; import ModalContent from 'Components/Modal/ModalContent'; import ModalFooter from 'Components/Modal/ModalFooter'; import ModalHeader from 'Components/Modal/ModalHeader'; import { inputTypes, kinds, sizes } from 'Helpers/Props'; import createAllArtistSelector from 'Store/Selectors/createAllArtistSelector'; import createTagsSelector from 'Store/Selectors/createTagsSelector'; import translate from 'Utilities/String/translate'; import styles from './TagsModalContent.css'; interface TagsModalContentProps { artistIds: number[]; onApplyTagsPress: (tags: number[], applyTags: string) => void; onModalClose: () => void; } function TagsModalContent(props: TagsModalContentProps) { const { artistIds, onModalClose, onApplyTagsPress } = props; const allArtists: Artist[] = useSelector(createAllArtistSelector()); const tagList: Tag[] = useSelector(createTagsSelector()); const [tags, setTags] = useState([]); const [applyTags, setApplyTags] = useState('add'); const artistTags = useMemo(() => { const tags = artistIds.reduce((acc: number[], id) => { const a = allArtists.find((a) => a.id === id); if (a) { acc.push(...a.tags); } return acc; }, []); return uniq(tags); }, [artistIds, allArtists]); const onTagsChange = useCallback( ({ value }: { value: number[] }) => { setTags(value); }, [setTags] ); const onApplyTagsChange = useCallback( ({ value }: { value: string }) => { setApplyTags(value); }, [setApplyTags] ); const onApplyPress = useCallback(() => { onApplyTagsPress(tags, applyTags); }, [tags, applyTags, onApplyTagsPress]); const applyTagsOptions = [ { key: 'add', value: translate('Add'), }, { key: 'remove', value: translate('Remove'), }, { key: 'replace', value: translate('Replace'), }, ]; return ( {translate('Tags')}
{translate('Tags')} {translate('ApplyTags')} {translate('Result')}
{artistTags.map((id) => { const tag = tagList.find((t) => t.id === id); if (!tag) { return null; } const removeTag = (applyTags === 'remove' && tags.indexOf(id) > -1) || (applyTags === 'replace' && tags.indexOf(id) === -1); return ( ); })} {(applyTags === 'add' || applyTags === 'replace') && tags.map((id) => { const tag = tagList.find((t) => t.id === id); if (!tag) { return null; } if (artistTags.indexOf(id) > -1) { return null; } return ( ); })}
); } export default TagsModalContent;