import { useState } from 'react' import { Card, Stack, TextInput, Textarea, Select, Group, Button, Switch, NumberInput, Collapse, Title, } from '@pikku/mantine/core' import { useDisclosure } from '@mantine/hooks' import { notifications } from '@mantine/notifications' import { m } from '@/i18n/messages' import { useQueryClient } from '@tanstack/react-query' import { Plus } from 'lucide-react' import { usePikkuMutation } from '@perauset/functions-sdk/pikku/api.gen' type Category = | 'housekeeping' | 'kitchen' | 'maintenance' | 'operations' | 'retreat' | 'transport' const CATEGORIES: { value: Category; label: string }[] = [ { value: 'housekeeping', label: 'Housekeeping' }, { value: 'kitchen', label: 'Kitchen' }, { value: 'maintenance', label: 'Maintenance' }, { value: 'operations', label: 'Operations' }, { value: 'retreat', label: 'Retreat' }, { value: 'transport', label: 'Transport' }, ] export function CreateTemplateForm() { const queryClient = useQueryClient() const [opened, { toggle }] = useDisclosure(false) const [title, setTitle] = useState('') const [description, setDescription] = useState('') const [category, setCategory] = useState(null) const [isClaimable, setIsClaimable] = useState(false) const [defaultLocation, setDefaultLocation] = useState('') const [estimatedMinutes, setEstimatedMinutes] = useState('') const [recurrenceCron, setRecurrenceCron] = useState('') const resetForm = () => { setTitle('') setDescription('') setCategory(null) setIsClaimable(false) setDefaultLocation('') setEstimatedMinutes('') setRecurrenceCron('') } const mutation = usePikkuMutation('createTaskTemplate', { onSuccess: () => { notifications.show({ title: m.templates_created(), message: `"${title}" template has been created.`, color: 'teal', }) resetForm() toggle() queryClient.invalidateQueries({ predicate: (q) => typeof q.queryKey[0] === 'string' && (q.queryKey[0] as string).toLowerCase().includes('template'), }) }, onError: (err: Error) => { notifications.show({ title: m.common_error(), message: err.message ?? 'Something went wrong', color: 'red', }) }, }) const handleSubmit = (e: React.FormEvent) => { e.preventDefault() if (!title.trim() || !category) { notifications.show({ title: m.common_validation(), message: m.templates_title_category_required(), color: 'orange', }) return } const body = { title: title.trim(), category, ...(description.trim() ? { description: description.trim() } : {}), ...(isClaimable ? { isClaimable: true } : {}), ...(defaultLocation.trim() ? { defaultLocation: defaultLocation.trim() } : {}), ...(typeof estimatedMinutes === 'number' && estimatedMinutes > 0 ? { estimatedMinutes } : {}), ...(recurrenceCron.trim() ? { recurrenceCron: recurrenceCron.trim() } : {}), } mutation.mutate(body) } return ( <> {m.templates_new()}
setTitle(e.currentTarget.value)} required />