import { useCallback } from "react"; import { Controller, SubmitHandler, useForm } from "react-hook-form"; import { createTask, getTags, useQuery } from "wasp/client/operations"; import { Tag } from "wasp/entities"; import { Button } from "../../shared/components/Button"; import { Input } from "../../shared/components/Input"; import { CreateTagDialog } from "../../tags/components/CreateTagDialog"; import { TagLabel } from "../../tags/components/TagLabel"; interface CreateTaskFormValues { description: string; tagIds: string[]; } export function CreateTaskForm() { const { data: tags } = useQuery(getTags); const { handleSubmit, getValues, setValue, watch, control, reset } = useForm({ defaultValues: { description: "", tagIds: [], }, }); const onSubmit: SubmitHandler = async (data, event) => { event?.stopPropagation(); try { await createTask(data); } catch (err: unknown) { window.alert(`Error while creating task: ${String(err)}`); } finally { reset(); } }; const toggleTag = useCallback( function toggleTag(id: Tag["id"]) { const tagIds = getValues("tagIds"); if (tagIds.includes(id)) { setValue( "tagIds", tagIds.filter((tagId) => tagId !== id), ); } else { setValue("tagIds", [...tagIds, id]); } }, [getValues, setValue], ); const tagIds = watch("tagIds"); return (

Create a new task

( )} />
Select tags
{tags && tags.length > 0 && (
    {tags.map((tag) => (
  • ))}
)}
); }