import type { Piece } from "backend/database"; import { PieceId } from "common"; import { FormEventHandler, useId, useRef, useState } from "react"; import { useParams } from "react-router-dom"; import { client } from "../client"; import { useLoading } from "../loading"; import { Button } from "../styled/Button"; import { Input } from "../styled/Input"; export function Piece() { const id = PieceId(useParams().pieceId!); const { isLoading, error, data } = useLoading(() => client.piece.get({ query: { id } })); if (isLoading) { return (
Ładowanie…
); } return (
{error !== null ? ( `Wystąpił błąd: ${error.value}` ) : data[0] === undefined ? ( "Utwór nie istnieje" ) : ( )}
); } namespace PieceForm { export interface Props { readonly data: Piece; } } function PieceForm(props: PieceForm.Props) { const [name, setName] = useState(props.data.name); const [composer, setComposer] = useState(props.data.composer ?? ""); const [lyricist, setLyricist] = useState(props.data.lyricist ?? ""); const [arranger, setArranger] = useState(props.data.arranger ?? ""); const nameId = useId(); const composerId = useId(); const lyricistId = useId(); const arrangerId = useId(); const autoFocusRef = useRef(null); const onSubmit: FormEventHandler = async (e) => { e.preventDefault(); if (props.data === null) { return; } const { error } = await client.piece({ pieceId: props.data.pieceId }).put({ name, composer: composer.length > 0 ? composer : null, lyricist: lyricist.length > 0 ? lyricist : null, arranger: arranger.length > 0 ? arranger : null, }); if (error) { console.error(error.value); return; } autoFocusRef.current?.focus(); } return (
setName(e.target.value)} /> setComposer(e.target.value)} /> setLyricist(e.target.value)} /> setArranger(e.target.value)} />
); }