-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(frontend): implement resources forms
- Loading branch information
1 parent
34624dd
commit 9bf77a9
Showing
7 changed files
with
299 additions
and
134 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,11 +1,8 @@ | ||
import AuthorForm from "components/AuthorForm"; | ||
import AuthorForm from "pages/Authors/AuthorForm"; | ||
import React from "react"; | ||
|
||
|
||
const AuthorCreate: React.FC = () => { | ||
return ( | ||
<AuthorForm /> | ||
) | ||
} | ||
return <AuthorForm />; | ||
}; | ||
|
||
export default AuthorCreate | ||
export default AuthorCreate; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import React, { useState } from "react"; | ||
import { AuthorFormCreate } from "types/author"; | ||
import Input from "../../components/Form/Input"; | ||
import { SubmitHandler, useForm } from "react-hook-form"; | ||
import { yupResolver } from "@hookform/resolvers/yup"; | ||
import AuthorSchema from "../../components/Schemas/AuthorSchema"; | ||
import Form, { FormMessage } from "../../components/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<AuthorFormCreate> = async (data: AuthorFormCreate) => { | ||
setIsLoading(true); | ||
const author = await managementService.createAuthor(data); | ||
if (author) onReset(); | ||
setMessage({ | ||
value: author ? "Author created with success" : "It wasn't possible to create the author, try again", | ||
isError: author === undefined, | ||
}); | ||
setIsLoading(false); | ||
}; | ||
|
||
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} | ||
> | ||
<> | ||
<div className="sm:col-span-3"> | ||
<Input label="Author name" name="name" register={register} errorMessage={errors.name?.message} /> | ||
</div> | ||
<div className="sm:col-span-3"> | ||
<Input label="Email address" name="email" register={register} errorMessage={errors.email?.message} /> | ||
</div> | ||
<div className="sm:col-span-3"> | ||
<Input | ||
label="Nationality" | ||
name="nationality" | ||
register={register} | ||
errorMessage={errors.nationality?.message} | ||
/> | ||
</div> | ||
<div className="sm:col-span-3"> | ||
<Input label="Birth date" name="birthDate" register={register} errorMessage={errors.birthDate?.message} /> | ||
</div> | ||
</> | ||
</Form> | ||
); | ||
}; | ||
|
||
export default AuthorForm; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import React from "react"; | ||
import BookForm from "./BookForm"; | ||
|
||
const BookCreate: React.FC = () => { | ||
return <BookForm />; | ||
}; | ||
|
||
export default BookCreate; |
Oops, something went wrong.