-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/task 7 formik #6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feature/task-6
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -1,10 +1,29 @@ | ||||||
| import "./UpsertMovieModal.css"; | ||||||
| import React from "react"; | ||||||
| import { useForm } from "react-hook-form"; | ||||||
| import GenresRepo from "../services/GenreRepo"; | ||||||
| import { useDispatch } from "react-redux"; | ||||||
| import { bindActionCreators } from "redux"; | ||||||
| import { actionCreators } from "../store"; | ||||||
| import { useFormik } from "formik"; | ||||||
| import * as Yup from "yup"; | ||||||
|
|
||||||
| const MovieSchema = Yup.object().shape({ | ||||||
| title: Yup.string().required(), | ||||||
| url: Yup.string().required(), | ||||||
| releaseDate: Yup.string().required(), | ||||||
| rating: Yup.number() | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For number you can use
Suggested change
|
||||||
| .required() | ||||||
| .test("is more the 0 less then 10", | ||||||
| "Rating must be from 0 to 10 ", (val)=>{ | ||||||
| return val >= 0 && val <= 10 | ||||||
| }), | ||||||
| runtime: Yup.number() | ||||||
| .required() | ||||||
| .test("more then 0", "runtime should be positive", val=>{ | ||||||
| return val >= 0 | ||||||
| }), | ||||||
| overview: Yup.string().required() | ||||||
| }) | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also you can check data types in the backend side, they use |
||||||
|
|
||||||
|
|
||||||
| export default function UpsertMovieModal({header, movie, handleCloseModal}){ | ||||||
|
|
@@ -14,25 +33,37 @@ export default function UpsertMovieModal({header, movie, handleCloseModal}){ | |||||
| const genreRepo = new GenresRepo(); | ||||||
| const genres = genreRepo.getAllGenres(); | ||||||
|
|
||||||
|
|
||||||
| const { register, handleSubmit } = useForm(); | ||||||
| const onSubmit = (data, e) => { | ||||||
| if (movie === undefined){ | ||||||
| handleCreate(data); | ||||||
| } else { | ||||||
| handleUpdate(movie, data); | ||||||
| } | ||||||
| const defaultFormData = {} | ||||||
|
|
||||||
| handleCloseModal(); | ||||||
| }; | ||||||
| const onError = (errors, e) => console.log(errors, e); | ||||||
| if(movie !== undefined) { | ||||||
| defaultFormData.title = movie.title; | ||||||
| defaultFormData.url = movie.poster_path; | ||||||
| defaultFormData.rating = movie.vote_average; | ||||||
| defaultFormData.runtime = movie.runtime; | ||||||
| defaultFormData.overview = movie.overview; | ||||||
| defaultFormData.releaseDate = movie.release_date; | ||||||
| } | ||||||
|
|
||||||
| const formik = useFormik({ | ||||||
| initialValues: defaultFormData, | ||||||
| validationSchema: MovieSchema, | ||||||
| onSubmit: values => { | ||||||
| console.log(values) | ||||||
| if (movie === undefined){ | ||||||
| handleCreate(values); | ||||||
| } else { | ||||||
| handleUpdate(movie, values); | ||||||
| } | ||||||
| handleCloseModal(); | ||||||
| } | ||||||
| }) | ||||||
|
|
||||||
| const handleUpdate = (movie, formData)=>{ | ||||||
| if(formData.title){ | ||||||
| movie.title = formData.title; | ||||||
| } | ||||||
| if(formData.genre && formData.genre !== "All"){ | ||||||
| movie.genres = [{name: formData.genre}]; | ||||||
| movie.genres = [formData.genre]; | ||||||
| } | ||||||
|
|
||||||
| if(formData.url){ | ||||||
|
|
@@ -50,6 +81,10 @@ export default function UpsertMovieModal({header, movie, handleCloseModal}){ | |||||
| movie.overview = formData.overview; | ||||||
| } | ||||||
|
|
||||||
| if(formData.releaseDate){ | ||||||
| movie.release_date = formData.releaseDate; | ||||||
| } | ||||||
|
|
||||||
| updateMovie(movie) | ||||||
| }; | ||||||
|
|
||||||
|
|
@@ -62,60 +97,108 @@ export default function UpsertMovieModal({header, movie, handleCloseModal}){ | |||||
| newMovie.vote_average = Number(formData.rating); | ||||||
| newMovie.runtime = Number(formData.runtime); | ||||||
| newMovie.overview = formData.overview; | ||||||
| newMovie.release_date = formData.releaseDate; | ||||||
| createMovie(newMovie) | ||||||
| }; | ||||||
|
|
||||||
| const defaultFormData = {} | ||||||
|
|
||||||
| if(movie !== undefined) { | ||||||
| defaultFormData.title = movie.title; | ||||||
| defaultFormData.url = movie.poster_path; | ||||||
| defaultFormData.rating = movie.vote_average; | ||||||
| defaultFormData.runtime = movie.runtime; | ||||||
| defaultFormData.overview = movie.overview; | ||||||
| } | ||||||
|
|
||||||
|
|
||||||
| return ( | ||||||
| <> | ||||||
| <h1>{header}</h1> | ||||||
| <form className="addMovieForm" onSubmit={handleSubmit(onSubmit, onError)}> | ||||||
| <form className="addMovieForm" onSubmit={formik.handleSubmit} onReset={formik.handleReset}> | ||||||
| <div className="col1-2 row1 form-input"> | ||||||
| <label >TITLE</label> | ||||||
| <input placeholder="Best Movie" {...register("title")} type="text" name="title" defaultValue={defaultFormData.title}/> | ||||||
| <label >TITLE</label> | ||||||
| <input | ||||||
| placeholder="Best Movie" | ||||||
| type="text" | ||||||
| name="title" | ||||||
| onChange={formik.handleChange} | ||||||
| value={formik.values.title} | ||||||
| onBlur={formik.handleBlur('title')} | ||||||
| /> | ||||||
| {formik.touched.title && formik.errors.title} | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Create a simple component which will render error according to mockup |
||||||
| </div> | ||||||
|
|
||||||
| <div className="col1-2 row2 form-input"> | ||||||
| <label>MOVIE URL</label> | ||||||
| <input placeholder="http://picture-url" {...register("url")} type="text" name="url" defaultValue={defaultFormData.url} /> | ||||||
| <input | ||||||
| type="text" | ||||||
| placeholder="http://picture-url" | ||||||
| name="url" | ||||||
| onChange={formik.handleChange} | ||||||
| value={formik.values.url} | ||||||
| onBlur={formik.handleBlur('url')} | ||||||
|
|
||||||
| /> | ||||||
| {formik.touched.url && formik.errors.url} | ||||||
| </div> | ||||||
|
|
||||||
| <div className="col1-2 row3 form-input"> | ||||||
| <label>GENRE</label> | ||||||
| <select {...register("genre")} name="genre"> | ||||||
| {genres.map((genre) => | ||||||
| <option key={genre.id}>{genre.name}</option> | ||||||
| )} | ||||||
| <select | ||||||
| name="genre" | ||||||
| onChange={formik.handleChange} | ||||||
| value={formik.values.genre} | ||||||
| onBlur={formik.handleBlur('genre')}> | ||||||
|
|
||||||
| {genres.map((genre) => | ||||||
| <option key={genre.id}>{genre.name}</option> | ||||||
| )} | ||||||
| </select> | ||||||
| {formik.touched.genre && formik.errors.genre} | ||||||
| </div> | ||||||
|
|
||||||
| <div className="col3 row1 form-input"> | ||||||
| <label>RELEASE DATE</label> | ||||||
| <input type="date" {...register("date")} name="releaseDate"/> | ||||||
| <input type="date" onChange={formik.handleChange} | ||||||
| value = {formik.values.releaseDate} | ||||||
| name="releaseDate" | ||||||
| onBlur={formik.handleBlur('releaseDate')} | ||||||
| /> | ||||||
| {formik.touched.releaseDate && formik.errors.releaseDate} | ||||||
|
|
||||||
| </div> | ||||||
|
|
||||||
| <div className="col3 row2 form-input"> | ||||||
| <label>RATING</label> | ||||||
| <input placeholder="7.8" {...register("rating")} type="text" name="rating" defaultValue={defaultFormData.rating} /> | ||||||
| <input | ||||||
| placeholder="7.8" | ||||||
| type="text" | ||||||
| name="rating" | ||||||
| onChange={formik.handleChange} | ||||||
| value={formik.values.rating} | ||||||
| onBlur={formik.handleBlur('rating')} | ||||||
|
|
||||||
| /> | ||||||
| {formik.touched.rating && formik.errors.rating} | ||||||
| </div> | ||||||
|
|
||||||
| <div className="col3 row3 form-input"> | ||||||
| <label>RUNTIME</label> | ||||||
| <input placeholder="120" {...register("runtime")} type="text" name="runtime" defaultValue={defaultFormData.runtime} /> | ||||||
| <input | ||||||
| placeholder="120" | ||||||
| type="text" | ||||||
| name="runtime" | ||||||
| onChange={formik.handleChange} | ||||||
| value={formik.values.runtime} | ||||||
| onBlur={formik.handleBlur('runtime')} | ||||||
|
|
||||||
| /> | ||||||
| {formik.touched.runtime && formik.errors.runtime} | ||||||
| </div> | ||||||
|
|
||||||
| <div className="col1-3 row4 form-input"> | ||||||
| <label>OVERVIEW</label> | ||||||
| <textarea placeholder="Lorem ipsum" {...register("overview")} type="text" name="overview" defaultValue={defaultFormData.overview}/> | ||||||
| <textarea | ||||||
| placeholder="Lorem ipsum" | ||||||
| type="text" | ||||||
| name="overview" | ||||||
| onChange={formik.handleChange} | ||||||
| value={formik.values.overview} | ||||||
| onBlur={formik.handleBlur('overview')} | ||||||
|
|
||||||
| /> | ||||||
| {formik.touched.overview && formik.errors.overview} | ||||||
| </div> | ||||||
|
|
||||||
| <div className="col2-3 row5 form-control"> | ||||||
|
|
@@ -128,4 +211,3 @@ export default function UpsertMovieModal({header, movie, handleCloseModal}){ | |||||
| </> | ||||||
| ); | ||||||
| } | ||||||
|
|
||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It will be logically if it has url validation, because random string couldn't be a url. Yup has api for it