REST API PATCH Запрос с формой - PullRequest
0 голосов
/ 02 мая 2020

Я использую formik для отправки формы в React, но когда я хочу 'PATCH', что-то formik выдвигает весь объект вместо измененного поля. Вот мой код:

editProfile. js

import React, { useEffect } from 'react';
import { makeStyles, Grid, Typography, TextField, Button } from '@material-ui/core';
import { Formik, Form } from 'formik';
import * as Yup from 'yup';
import { useDispatch, useSelector } from 'react-redux';
import { updateUser } from '../actions';
import { useHistory } from 'react-router-dom';
import NavBar from './navBar';
const useStyles = makeStyles((theme) => ({
    
}));


const Validation = Yup.object().shape({
    firstName: Yup
        .string()
        .min(2, 'Name is too short!')
        .max(15, 'Name is too long!')
        .notRequired(),

    secondName: Yup
        .string()
        .min(2, 'Name is too short!')
        .max(15, 'Name is too long!')
        .notRequired(),

    lastName: Yup
        .string()
        .min(2, 'Name is too short!')
        .max(15, 'Name is too long!')
        .notRequired(),

    password: Yup
        .string()
        .min(6, 'Password too short')
        .max(20, 'Password too long')
        .notRequired(),
    age: Yup
        .number()
        .positive('Age must not be negative')
        .notRequired(),
    bloodGroup: Yup
        .string()
        .max(3, 'Blood group can\'t greater than 3')
        .notRequired(),
    weight: Yup
        .number()
        .positive('Weight must be a positive number')
        .notRequired(),
    bmi: Yup
        .number()
        .positive()
        .notRequired(),
    leftEye: Yup
        .number()
        .max(6)
        .positive()
        .notRequired(),
    rightEye: Yup
        .number()
        .max(6)
        .positive()
        .notRequired(),
    doctorName: Yup
        .string()
        .max(15, 'Name is too long!')
        .notRequired(),
    doctorEmail: Yup
        .string()
        .email()
        .notRequired(),
    doctorPhone: Yup
        .number()
        .notRequired(),
    specialist: Yup
        .string()
        .notRequired(),
    address: Yup
        .string()
        .max(100, 'Too long address')
        .notRequired(),
    lastVisit: Yup
        .date()
        .notRequired(),
    nextVisit: Yup
        .date()
        .notRequired(),

})


const EditProfile = () => {
    const classes = useStyles();
    const dispatch = useDispatch();
    const user = useSelector(state => state.auth.user);
    const isSignedIn = useSelector(state => state.auth.isSignedIn);
    const history = useHistory();
    useEffect(() => {
        if (!isSignedIn) {
            history.push('/')
        }
    }, [isSignedIn, history])
    if (!user) {
        return null;
    } else {
        return (
            <React.Fragment>
                <NavBar />
                <Grid
                    container
                    direction='column'
                    justify='flex-start'
                    alignItems='flex-start'
                    className={classes.gridHome}
                >
                    <Grid item xs={12} className={classes.gridContent}>
                        <Typography className={classes.title}>Medico</Typography>
                    </Grid>
                    <Grid item xs={12} className={classes.gridContent}>
                        <Typography className={classes.signUp}>Welcome {user.user.firstName ? user.user.firstName : null}!</Typography>
                    </Grid>

                    <Formik

                        initialValues={{
                            firstName: '',
                            secondName: '',
                            lastName: '',
                            password: '',
                            age: '',
                            bloodGroup: '',
                            weight: '',
                            leftEye: 1,
                            rightEye: 1,
                            bmi: 1,
                            doctorName: '',
                            doctorPhone: 0,
                            doctorEmail: '',
                            address: '',
                            specialist: '',
                            lastVisit: new Date(),
                            nextVisit: new Date(),
                        }}
                        validationSchema={Validation}
                        onSubmit={(values) => {
                            dispatch(updateUser(values))
                        }}
                    >
                        {({ errors, handleChange, touched }) =>
                            <Form style={{ width: '100%' }}>
                                <Grid container direction='row'>
                                    <Grid item xs={12} className={classes.name}>
                                        <Typography className={classes.signUp}>Name</Typography>
                                    </Grid>
                                    <Grid item md={4} xs={12} className={classes.inputGrid}>
                                        <TextField
                                            className={classes.inputFields}
                                            error={errors.firstName && touched.firstName}
                                            name='firstName'
                                            autoComplete='on'
                                            shrink='true'
                                            variant='standard'
                                            onChange={handleChange}
                                            id='firstName'
                                            label='First Name'
                                            helperText={
                                                errors.firstName && touched.firstName
                                                    ? errors.firstName : null
                                            }
                                        />
                                    </Grid>
                                    <Grid item md={4} xs={12} className={classes.inputGrid}>
                                        <TextField
                                            className={classes.inputFields}
                                            error={errors.secondName && touched.secondName}
                                            name='secondName'
                                            autoComplete='on'
                                            variant='standard'
                                            onChange={handleChange}
                                            id='secondName'
                                            label='Middle Name'
                                            helperText={
                                                errors.secondName && touched.secondName
                                                    ? errors.secondName : null
                                            }
                                        />
                                    </Grid>
                                    <Grid item md={4} xs={12} className={classes.inputGrid}>
                                        <TextField
                                            className={classes.inputFields}
                                            error={errors.lastName && touched.lastName}
                                            name='lastName'
                                            autoComplete='on'
                                            variant='standard'
                                            onChange={handleChange}
                                            id='lastName'
                                            label='Last Name'
                                            helperText={
                                                errors.lastName && touched.lastName
                                                    ? errors.lastName : null
                                            }
                                        />
                                    </Grid>

                                    <Grid item xs={12} className={classes.name}>
                                        <Typography className={classes.signUp}>Password</Typography>
                                    </Grid>
                                    <Grid item md={6} xs={12} className={classes.inputGrid}>
                                        <TextField
                                            className={classes.inputFields}
                                            error={errors.password && touched.password}
                                            name='password'
                                            type='password'
                                            variant='standard'
                                            onChange={handleChange}
                                            id='password'
                                            label='Password'
                                            helperText={
                                                errors.password && touched.password
                                                    ? errors.password : null
                                            }
                                        />
                                    </Grid>
                                    <Grid item xs={12} className={classes.name}>
                                        <Typography className={classes.signUp}>Personal Information</Typography>
                                    </Grid>
                                    <Grid item md={2} xs={6} className={classes.inputGrid} >
                                        <TextField
                                            className={classes.inputFields}
                                            error={errors.age && touched.age}
                                            name='age'
                                            variant='standard'
                                            onChange={handleChange}
                                            id='age'
                                            label='Age'
                                            helperText={
                                                errors.age && touched.age
                                                    ? errors.age : null
                                            }
                                        />
                                    </Grid>
                                    <Grid item md={2} xs={6} className={classes.inputGrid} >
                                        <TextField
                                            className={classes.inputFields}
                                            error={errors.bloodGroup && touched.bloodGroup}
                                            name='bloodGroup'
                                            variant='standard'
                                            onChange={handleChange}
                                            id='bloodGroup'
                                            label='Blood Group'
                                            helperText={
                                                errors.bloodGroup && touched.bloodGroup
                                                    ? errors.bloodGroup : null
                                            }
                                        />
                                    </Grid>
                                    <Grid item md={2} xs={6} className={classes.inputGrid} >
                                        <TextField
                                            className={classes.inputFields}
                                            error={errors.weight && touched.weight}
                                            name='weight'
                                            variant='standard'
                                            onChange={handleChange}
                                            id='weight'
                                            label='Weight(in Kg)'
                                            helperText={
                                                errors.weight && touched.weight
                                                    ? errors.weight : null
                                            }
                                        />
                                    </Grid>
                                    <Grid item md={2} xs={6} className={classes.inputGrid} >
                                        <TextField
                                            className={classes.inputFields}
                                            error={errors.bmi && touched.bmi}
                                            name='bmi'
                                            variant='standard'
                                            onChange={handleChange}
                                            id='bmi'
                                            label='BMI'
                                            helperText={
                                                errors.bmi && touched.bmi
                                                    ? errors.bmi : null
                                            }
                                        />
                                    </Grid>
                                    <Grid item md={2} xs={6} className={classes.inputGrid} >
                                        <TextField
                                            className={classes.inputFields}
                                            error={errors.leftEye && touched.leftEye}
                                            name='leftEye'
                                            variant='standard'
                                            onChange={handleChange}
                                            id='leftEye'
                                            label='Left Eye'
                                            helperText={
                                                errors.leftEye && touched.leftEye
                                                    ? errors.leftEye : null
                                            }
                                        />
                                    </Grid>
                                    <Grid item md={2} xs={6} className={classes.inputGrid} >
                                        <TextField
                                            className={classes.inputFields}
                                            error={errors.rightEye && touched.rightEye}
                                            name='rightEye'
                                            variant='standard'
                                            onChange={handleChange}
                                            id='rightEye'
                                            label='Right Eye'
                                            helperText={
                                                errors.rightEye && touched.rightEye
                                                    ? errors.rightEye : null
                                            }
                                        />
                                    </Grid>
                                    <Grid item xs={6} container justify='center' alignItems='center' className={classes.loginGrid}>
                                        <Button
                                            className={classes.button}
                                            type='submit'
                                            variant='contained'
                                        >Submit</Button>
                                    </Grid>
                                </Grid>
                            </Form>
                        }
                    </Formik>
                </Grid>
            </React.Fragment >
        );
    }


}

export default EditProfile;

Предположим, я просто хочу указать pu sh 'secondName' для 'PATCH', вместо того, чтобы formik отправляет все поля, упомянутые в 'initialValues'. Как управлять отправкой formik только то поле, которое было изменено. PS: я удалил некоторые поля, чтобы сократить фрагмент.

...