import { useQuery, useQueryClient } from '@tanstack/react-query' import { useEffect, useState } from 'react' import { useParams } from 'react-router-dom' import TagLink from '@/components/TagLink' import Label from '@/components/common/Label' 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 } from '@/lib/utils' import type { FC, FormEvent } from 'react' import type { Deerjikist, Platform } from '@/types' export default (() => { const { id } = useParams () const tagId = String (id ?? '') const tagKey = tagsKeys.deerjikists (tagId) const { data: qData, isLoading: loading } = useQuery ({ queryKey: tagKey, queryFn: () => fetchDeerjikistsByTag (tagId) }) const tag = qData?.tag const deerjikists = qData?.deerjikists ?? [] const [data, setData] = useState<(Omit & { platform: Platform | null })[]> ([]) const [disabled, setDisabled] = useState (true) const qc = useQueryClient () const handleSubmit = async (e: FormEvent) => { e.preventDefault () try { setDisabled (true) setData (await apiPut (`/tags/${ id }/deerjikists`, data)) qc.invalidateQueries ({ queryKey: tagsKeys.root }) toast ({ description: '更新しました.' }) } catch { toast ({ title: '更新失敗', description: '入力内容を確認してください.' }) } finally { setDisabled (false) } } useEffect (() => { if (!(tag)) { setDisabled (true) return } setData (deerjikists) setDisabled (false) }, [deerjikists]) return ( {(loading || !(tag)) ? 'Loading...' : (
{data.map ((datum, i) => (
{/* プラットフォーム */}
{/* コード */}
setData (prev => { const rtn = [...prev] rtn[i] = { ...rtn[i], code: e.target.value } return rtn })}/>
))}
)}
) }) satisfies FC