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