Reverte opcionalidade de subcategoria no portal
This commit is contained in:
parent
6a04ef4843
commit
9f85cbaba5
3 changed files with 7 additions and 31 deletions
|
|
@ -1,6 +1,6 @@
|
|||
"use client"
|
||||
|
||||
import { useCallback, useMemo, useState } from "react"
|
||||
import { useMemo, useState } from "react"
|
||||
import { useRouter } from "next/navigation"
|
||||
import { useMutation } from "convex/react"
|
||||
import { toast } from "sonner"
|
||||
|
|
@ -45,20 +45,9 @@ export function PortalTicketForm() {
|
|||
const [subcategoryId, setSubcategoryId] = useState<string | null>(null)
|
||||
const [attachments, setAttachments] = useState<Array<{ storageId: string; name: string; size?: number; type?: string }>>([])
|
||||
const [isSubmitting, setIsSubmitting] = useState(false)
|
||||
const [hasSubcategoryOptions, setHasSubcategoryOptions] = useState(false)
|
||||
|
||||
const isFormValid = useMemo(() => {
|
||||
return Boolean(
|
||||
subject.trim() &&
|
||||
description.trim() &&
|
||||
categoryId &&
|
||||
(hasSubcategoryOptions ? subcategoryId : true)
|
||||
)
|
||||
}, [subject, description, categoryId, subcategoryId, hasSubcategoryOptions])
|
||||
|
||||
const handleSecondaryOptionsChange = useCallback((hasOptions: boolean) => {
|
||||
setHasSubcategoryOptions((prev) => (prev === hasOptions ? prev : hasOptions))
|
||||
}, [])
|
||||
return Boolean(subject.trim() && description.trim() && categoryId && subcategoryId)
|
||||
}, [subject, description, categoryId, subcategoryId])
|
||||
|
||||
async function handleSubmit(event: React.FormEvent) {
|
||||
event.preventDefault()
|
||||
|
|
@ -86,7 +75,7 @@ export function PortalTicketForm() {
|
|||
queueId: undefined,
|
||||
requesterId: viewerId,
|
||||
categoryId: categoryId as Id<"ticketCategories">,
|
||||
subcategoryId: subcategoryId ? (subcategoryId as Id<"ticketSubcategories">) : undefined,
|
||||
subcategoryId: subcategoryId as Id<"ticketSubcategories">,
|
||||
})
|
||||
|
||||
if (plainDescription.length > 0) {
|
||||
|
|
@ -169,10 +158,9 @@ export function PortalTicketForm() {
|
|||
subcategoryId={subcategoryId}
|
||||
onCategoryChange={setCategoryId}
|
||||
onSubcategoryChange={setSubcategoryId}
|
||||
onSecondaryOptionsChange={handleSecondaryOptionsChange}
|
||||
layout="stacked"
|
||||
categoryLabel="Categoria *"
|
||||
subcategoryLabel={hasSubcategoryOptions ? "Subcategoria *" : "Subcategoria (opcional)"}
|
||||
subcategoryLabel="Subcategoria *"
|
||||
secondaryEmptyLabel="Selecione uma categoria"
|
||||
/>
|
||||
<div className="space-y-1">
|
||||
|
|
|
|||
|
|
@ -18,8 +18,7 @@ interface CategorySelectProps {
|
|||
categoryId: string | null
|
||||
subcategoryId: string | null
|
||||
onCategoryChange: (categoryId: string) => void
|
||||
onSubcategoryChange: (subcategoryId: string | null) => void
|
||||
onSecondaryOptionsChange?: (hasSecondary: boolean) => void
|
||||
onSubcategoryChange: (subcategoryId: string) => void
|
||||
autoSelectFirst?: boolean
|
||||
disabled?: boolean
|
||||
categoryLabel?: string
|
||||
|
|
@ -40,7 +39,6 @@ export function CategorySelectFields({
|
|||
subcategoryId,
|
||||
onCategoryChange,
|
||||
onSubcategoryChange,
|
||||
onSecondaryOptionsChange,
|
||||
autoSelectFirst = true,
|
||||
disabled = false,
|
||||
categoryLabel = "Primária",
|
||||
|
|
@ -80,16 +78,6 @@ export function CategorySelectFields({
|
|||
}
|
||||
}, [categoryId, secondaryOptions, subcategoryId, onSubcategoryChange])
|
||||
|
||||
useEffect(() => {
|
||||
onSecondaryOptionsChange?.(secondaryOptions.length > 0)
|
||||
}, [secondaryOptions, onSecondaryOptionsChange])
|
||||
|
||||
useEffect(() => {
|
||||
if (secondaryOptions.length === 0 && subcategoryId) {
|
||||
onSubcategoryChange(null)
|
||||
}
|
||||
}, [secondaryOptions, subcategoryId, onSubcategoryChange])
|
||||
|
||||
const containerClass = layout === "stacked" ? "flex flex-col gap-3" : "grid gap-3 sm:grid-cols-2"
|
||||
|
||||
return (
|
||||
|
|
|
|||
|
|
@ -112,7 +112,7 @@ export function NewTicketDialog() {
|
|||
}
|
||||
}
|
||||
|
||||
const handleSubcategoryChange = (value: string | null) => {
|
||||
const handleSubcategoryChange = (value: string) => {
|
||||
const previous = form.getValues("subcategoryId") ?? ""
|
||||
const next = value ?? ""
|
||||
form.setValue("subcategoryId", next, {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue