import { useQuery, useQueryClient } from '@tanstack/react-query' import { useEffect, useMemo, useState } from 'react' import { useParams } from 'react-router-dom' import { useLocale } from '@/lib/useLocale' import { useUITranslation } from '@/lib/useUITranslation' import TagLink from '@/components/TagLink' import FieldError from '@/components/common/FieldError' import FormField from '@/components/common/FormField' import PageTitle from '@/components/common/PageTitle' import MainArea from '@/components/layout/MainArea' import { toast } from '@/components/ui/use-toast' import { PLATFORM_NAMES, PLATFORMS } from '@/consts' import { apiPut } from '@/lib/api' import { tagsKeys } from '@/lib/queryKeys' import { fetchDeerjikistsByTag } from '@/lib/tags' import { cn, inputClass } from '@/lib/utils' import { useValidationErrors } from '@/lib/useValidationErrors' import type { FC, FormEvent } from 'react' import type { Deerjikist, Platform } from '@/types' type DeerjikistFormField = 'deerjikists' | `deerjikists${ number }Platform` | `deerjikists${ number }Code` const DeerjikistDetailPage: FC = () => { const { label } = useUITranslation ('shared') const locale = useLocale () const { id } = useParams () const tagId = String (id ?? '') const tagKey = tagsKeys.deerjikists (tagId, locale) const { data: qData, isLoading: loading } = useQuery ({ queryKey: tagKey, queryFn: () => fetchDeerjikistsByTag (tagId, locale) }) const tag = qData?.tag const deerjikists = useMemo (() => qData?.deerjikists ?? [], [qData]) const [data, setData] = useState<(Omit & { platform: Platform | null })[]> ([]) const [disabled, setDisabled] = useState (true) const { baseErrors, fieldErrors, clearValidationErrors, applyValidationError } = useValidationErrors () const qc = useQueryClient () const handleSubmit = async (e: FormEvent) => { e.preventDefault () clearValidationErrors () try { setDisabled (true) setData (await apiPut (`/tags/${ id }/deerjikists`, data, { locale })) qc.invalidateQueries ({ queryKey: tagsKeys.root }) toast ({ description: label ('messages.updated') }) } catch (e) { applyValidationError (e) toast ({ title: label ('messages.updateFailed'), description: label ('messages.checkInputContent') }) } finally { setDisabled (false) } } useEffect (() => { if (!(tag)) { setDisabled (true) return } setData (deerjikists) setDisabled (false) }, [tag, deerjikists]) return ( {(loading || !(tag)) ? label ('common:loading') : (
{data.map ((datum, i) => (
{/* プラットフォーム */} {({ describedBy, invalid }) => ( )} {/* コード */} {({ describedBy, invalid }) => ( setData (prev => { const rtn = [...prev] rtn[i] = { ...rtn[i], code: e.target.value } return rtn })}/>)}
))}
)}
) } export default DeerjikistDetailPage