From 2fa02472eed849cd2be460b599ee0c662477636a Mon Sep 17 00:00:00 2001 From: Bogdan Date: Tue, 29 Apr 2025 03:12:33 +0300 Subject: [PATCH] Fixed auto tagging tag specification --- .../src/Components/Form/FormInputGroup.tsx | 4 +- .../Components/Form/Tag/SeriesTagInput.tsx | 54 +++++++++++++------ 2 files changed, 41 insertions(+), 17 deletions(-) diff --git a/frontend/src/Components/Form/FormInputGroup.tsx b/frontend/src/Components/Form/FormInputGroup.tsx index f2529b73a..0d443ee64 100644 --- a/frontend/src/Components/Form/FormInputGroup.tsx +++ b/frontend/src/Components/Form/FormInputGroup.tsx @@ -139,11 +139,11 @@ type PickProps = C extends 'text' ? // eslint-disable-next-line @typescript-eslint/no-explicit-any EnhancedSelectInputProps : C extends 'seriesTag' - ? SeriesTagInputProps + ? SeriesTagInputProps : C extends 'seriesTypeSelect' ? SeriesTypeSelectInputProps : C extends 'tag' - ? SeriesTagInputProps + ? SeriesTagInputProps : C extends 'tagSelect' ? TagSelectInputProps : C extends 'text' diff --git a/frontend/src/Components/Form/Tag/SeriesTagInput.tsx b/frontend/src/Components/Form/Tag/SeriesTagInput.tsx index 1035c0304..df2b603cb 100644 --- a/frontend/src/Components/Form/Tag/SeriesTagInput.tsx +++ b/frontend/src/Components/Form/Tag/SeriesTagInput.tsx @@ -1,4 +1,4 @@ -import React, { useCallback } from 'react'; +import React, { useCallback, useMemo } from 'react'; import { useDispatch, useSelector } from 'react-redux'; import { createSelector } from 'reselect'; import { addTag } from 'Store/Actions/tagActions'; @@ -12,10 +12,10 @@ interface SeriesTag extends TagBase { name: string; } -export interface SeriesTagInputProps { +export interface SeriesTagInputProps { name: string; - value: number[]; - onChange: (change: InputChanged) => void; + value: V; + onChange: (change: InputChanged) => void; } const VALID_TAG_REGEX = new RegExp('[^-_a-z0-9]', 'i'); @@ -59,28 +59,48 @@ function createSeriesTagsSelector(tags: number[]) { }); } -export default function SeriesTagInput({ +export default function SeriesTagInput({ name, value, onChange, -}: SeriesTagInputProps) { +}: SeriesTagInputProps) { const dispatch = useDispatch(); + const isArray = Array.isArray(value); + + const arrayValue = useMemo(() => { + if (isArray) { + return value as number[]; + } + + return value === 0 ? [] : [value as number]; + }, [isArray, value]); const { tags, tagList, allTags } = useSelector( - createSeriesTagsSelector(value) + createSeriesTagsSelector(arrayValue) ); const handleTagCreated = useCallback( (tag: SeriesTag) => { - onChange({ name, value: [...value, tag.id] }); + if (isArray) { + onChange({ name, value: [...value, tag.id] as V }); + } else { + onChange({ + name, + value: tag.id as V, + }); + } }, - [name, value, onChange] + [name, value, isArray, onChange] ); const handleTagAdd = useCallback( (newTag: SeriesTag) => { if (newTag.id) { - onChange({ name, value: [...value, newTag.id] }); + if (isArray) { + onChange({ name, value: [...value, newTag.id] as V }); + } else { + onChange({ name, value: newTag.id as V }); + } return; } @@ -96,17 +116,21 @@ export default function SeriesTagInput({ ); } }, - [name, value, allTags, handleTagCreated, onChange, dispatch] + [name, value, isArray, allTags, handleTagCreated, onChange, dispatch] ); const handleTagDelete = useCallback( ({ index }: { index: number }) => { - const newValue = value.slice(); - newValue.splice(index, 1); + if (isArray) { + const newValue = value.slice(); + newValue.splice(index, 1); - onChange({ name, value: newValue }); + onChange({ name, value: newValue as V }); + } else { + onChange({ name, value: 0 as V }); + } }, - [name, value, onChange] + [name, value, isArray, onChange] ); return (