Skip to content

Commit

Permalink
Merge branch 'main' into development
Browse files Browse the repository at this point in the history
  • Loading branch information
gabrielborgesdm committed Jun 4, 2024
2 parents 9bf77a9 + 8959a44 commit 4593b45
Show file tree
Hide file tree
Showing 2 changed files with 86 additions and 1 deletion.
84 changes: 84 additions & 0 deletions frontend/src/components/AuthorForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import React, { useState } from "react";
import { AuthorCreate } from "types/author";
import Input from "./Form/Input";
import { SubmitHandler, useForm } from "react-hook-form";
import { yupResolver } from "@hookform/resolvers/yup"
import AuthorSchema from "./Schemas/AuthorSchema";
import Form, { FormMessage } from "./Form/Form";
import ManagementService from "services/managementService";

const managementService = new ManagementService()


const AuthorForm: React.FC = () => {
const messageInitialState = { value: "", isError: false }
const [isLoading, setIsLoading] = useState(false)
const [message, setMessage] = useState<FormMessage>(messageInitialState)
const {
register,
handleSubmit,
reset,

formState: { errors },
} = useForm({ resolver: yupResolver(AuthorSchema) })



const onSubmit: SubmitHandler<AuthorCreate> = async (data: AuthorCreate) => {
setIsLoading(true)
const author = await managementService.createAuthor(data)
console.log(author, author === undefined)
setMessage({
value: author ? "Author created with success" : "It wansn't possible to create the author, try again",
isError: author === undefined
})
setIsLoading(false)
// onReset()
}

const onReset = () => {
setMessage(messageInitialState)
reset(undefined)
}

return (
<Form
onSubmit={handleSubmit(onSubmit)}
onReset={onReset}
title="Author Creation"
description="Enter the author details"
message={message}
isLoading={isLoading}>
<>
<Input
label="Author name"
name="name"
register={register}
errorMessage={errors.name?.message}
/>

<Input
label="Email address"
name="email"
register={register}
errorMessage={errors.email?.message}
/>

<Input
label="Nationality"
name="nationality"
register={register}
errorMessage={errors.nationality?.message}
/>

<Input
label="Birth date"
name="birthDate"
register={register}
errorMessage={errors.birthDate?.message}
/></>
</Form>
)
}

export default AuthorForm
3 changes: 2 additions & 1 deletion frontend/src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,11 @@ import Books from "pages/Books/Books";
import Authors from "pages/Authors/Authors";
import Layout from "components/Layout";

import "./index.css";
import AuthorCreate from "pages/Authors/AuthorCreate";
import BookCreate from "pages/Books/BookCreate";

import "./index.css";

const router = createBrowserRouter([
{
path: "/",
Expand Down

0 comments on commit 4593b45

Please sign in to comment.