Skip to content

Commit

Permalink
Create form modals
Browse files Browse the repository at this point in the history
  • Loading branch information
EduardoPicolo committed Sep 11, 2022
1 parent 5cbd738 commit 63479fd
Show file tree
Hide file tree
Showing 29 changed files with 925 additions and 528 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"dev": "next dev",
"build": "next build",
"start": "next start",
"lint": "next lint --fix ",
"lint": "next lint --fix",
"type-check": "tsc --noEmit",
"test": "jest",
"prepare": "husky install"
Expand Down
8 changes: 2 additions & 6 deletions src/components/Forms/CategoriaForm/index.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { useForm } from "react-hook-form"
import { SubmitHandler, useForm } from "react-hook-form"
import {
Box,
Button,
Expand All @@ -11,9 +11,7 @@ import {

interface CategoriaFormProps {
defaultValues?: Category | undefined
onSubmit:
| ((data: CategoryPayload) => void)
| ((data: ProblemTypePayload) => void)
onSubmit: SubmitHandler<CategoryPayload> | SubmitHandler<ProblemTypePayload>
}

export const CategoriaForm = ({
Expand All @@ -37,7 +35,6 @@ export const CategoriaForm = ({
<Input
{...register("name", { required: "Campo obrigatório" })}
placeholder="Nome"
variant="flushed"
/>
{errors?.name && (
<FormErrorMessage>{errors?.name?.message}</FormErrorMessage>
Expand All @@ -49,7 +46,6 @@ export const CategoriaForm = ({
<Input
{...register("description", { required: "Campo obrigatório" })}
placeholder="Descrição"
variant="flushed"
/>
{errors?.description && (
<FormErrorMessage>
Expand Down
26 changes: 7 additions & 19 deletions src/components/Forms/ChamadoForm/ChamadoFormWrapper.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,7 @@ import { chamadosDefaultValues } from "@components/Forms/ChamadoForm/helpers"
import { WorkstationModal } from "@components/Modals/WorkstationModal"
import { useRequest } from "@hooks/useRequest"
import { getCities } from "@services/Cidades"
import { request } from "@services/request"
import { getWorkstations, updateWorkstation } from "@services/Workstation"
import { getWorkstations } from "@services/Workstation"
import { getSelectOptions } from "@utils/getSelectOptions"

export interface ChamadoFormProps {
Expand Down Expand Up @@ -157,28 +156,17 @@ export const ChamadoFormWrapper = ({
)

const handleWorkstationEdit = useCallback(
async (data: CreateWorkstationPayload) => {
const payload = {
...data,
phones: (data.phones as unknown as { number: string }[])?.map(
(phone) => phone?.number
)
}

const response = await request<Workstation>(
updateWorkstation(getValues("workstation_id.value"))(payload)
)

if (response.type === "success") {
toast.success(response.value?.message)
(result: Result<ApiResponse<Workstation>>) => {
if (result.type === "success") {
toast.success(result.value?.message)
mutateWorkstations()
} else {
toast.error(response.error?.message)
toast.error(result.error?.message)
}

closeWorkstationModal()
},
[getValues, closeWorkstationModal, mutateWorkstations]
[closeWorkstationModal, mutateWorkstations]
)

return (
Expand Down Expand Up @@ -310,7 +298,7 @@ export const ChamadoFormWrapper = ({
</FormProvider>

<WorkstationModal
defaultValues={workstations?.data?.find(
workstation={workstations?.data?.find(
(station) => station.id === getValues("workstation_id.value")
)}
isOpen={isWorkstationModalOpen}
Expand Down
2 changes: 1 addition & 1 deletion src/components/Forms/ChamadoForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import { ControlledSelect } from "@components/ControlledSelect"
import { EventForm } from "@components/Forms/ChamadoForm/EventForm"
import { EventInfo } from "@components/Forms/ChamadoForm/EventInfo"
import { useDropdownData } from "@components/Forms/ChamadoForm/useDropdowData"
import { Modal } from "@components/Modal/Modal"
import { Modal } from "@components/Modal"
import {
ChamadoPriority,
ChamadoStatus,
Expand Down
49 changes: 0 additions & 49 deletions src/components/Forms/CidadeForm/index.tsx

This file was deleted.

40 changes: 40 additions & 0 deletions src/components/Forms/CityForm/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import { useForm } from "react-hook-form"
import {
Button,
FormControl,
FormErrorMessage,
FormLabel,
Input
} from "@chakra-ui/react"

interface CityFormProps {
defaultValues?: City | undefined
onSubmit: (data: CityPayload) => void
}

export const CityForm = ({ defaultValues, onSubmit }: CityFormProps) => {
const {
register,
handleSubmit,
formState: { errors, isSubmitting }
} = useForm<CityPayload>({
defaultValues
})

return (
<form onSubmit={handleSubmit(onSubmit)}>
<FormControl isInvalid={Boolean(errors?.name)} mb={8}>
<FormLabel>Nome</FormLabel>
<Input
{...register("name", { required: "Campo obrigatório" })}
placeholder="Nome"
/>
<FormErrorMessage>{errors?.name?.message}</FormErrorMessage>
</FormControl>

<Button type="submit" width="100%" isLoading={isSubmitting}>
Registrar
</Button>
</form>
)
}
12 changes: 1 addition & 11 deletions src/components/Forms/UserForm/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ export const UserForm = ({ defaultValues, onSubmit }: UserFormProps) => {
<Input
{...register("name", { required: "Campo obrigatório" })}
placeholder="Nome completo"
variant="flushed"
/>
{errors?.name && (
<FormErrorMessage>{errors?.name?.message}</FormErrorMessage>
Expand All @@ -56,7 +55,6 @@ export const UserForm = ({ defaultValues, onSubmit }: UserFormProps) => {
<Input
{...register("username", { required: "Campo obrigatório" })}
placeholder="Nome de Usuário"
variant="flushed"
/>
{errors?.username && (
<FormErrorMessage>{errors?.username?.message}</FormErrorMessage>
Expand All @@ -66,11 +64,7 @@ export const UserForm = ({ defaultValues, onSubmit }: UserFormProps) => {

<Box>
<FormLabel htmlFor="email">E-mail</FormLabel>
<Input
{...register("email")}
placeholder="Descrição"
variant="flushed"
/>
<Input {...register("email")} placeholder="Descrição" />
{errors?.email && (
<FormErrorMessage>{errors?.email?.message}</FormErrorMessage>
)}
Expand All @@ -86,7 +80,6 @@ export const UserForm = ({ defaultValues, onSubmit }: UserFormProps) => {
isUpdating ? {} : { required: "Campo obrigatório" }
)}
placeholder="Senha"
variant="flushed"
/>
{errors?.password && (
<FormErrorMessage>{errors?.password?.message}</FormErrorMessage>
Expand All @@ -111,7 +104,6 @@ export const UserForm = ({ defaultValues, onSubmit }: UserFormProps) => {
}
)}
placeholder="Confirmar Senha"
variant="flushed"
/>
{errors?.confirmPassword && (
<FormErrorMessage>
Expand All @@ -129,7 +121,6 @@ export const UserForm = ({ defaultValues, onSubmit }: UserFormProps) => {
required: "Campo obrigatório"
})}
defaultValue=""
variant="flushed"
>
<option disabled value="">
Escolha um Acesso
Expand All @@ -148,7 +139,6 @@ export const UserForm = ({ defaultValues, onSubmit }: UserFormProps) => {
<Input
{...register("job_role", { required: "Campo obrigatório" })}
placeholder="Cargo"
variant="flushed"
/>
{errors?.job_role && (
<FormErrorMessage>{errors?.job_role?.message}</FormErrorMessage>
Expand Down
Loading

0 comments on commit 63479fd

Please sign in to comment.