Skip to content

Commit

Permalink
Merge pull request #1 from coronasafe/develop
Browse files Browse the repository at this point in the history
merge
  • Loading branch information
SandeepThomas authored Mar 24, 2020
2 parents 4b5c463 + 65d4156 commit 5d4f379
Show file tree
Hide file tree
Showing 2 changed files with 150 additions and 2 deletions.
5 changes: 3 additions & 2 deletions src/Components/Ambulance/DriverDetailsForm.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
import React, { useState } from "react";
import { Button, Card, CardHeader, Grid, CardContent, CardActions, Checkbox } from "@material-ui/core";
import { TextInputField } from '../Common/HelperInputFields';
import { useDispatch } from "react-redux"; import { A } from "hookrouter";
import { useDispatch } from "react-redux";
import { postAmbulance } from "../../Redux/actions";
import { isEmpty, get } from "lodash";


export const DriverDetailsForm = (props:any) => {
const { vehicleInfo } = props;
const dispatch: any = useDispatch();
Expand All @@ -18,7 +20,6 @@ export const DriverDetailsForm = (props:any) => {
const initErr: any = {};
const [form, setForm] = useState(initForm);
const [errors, setErrors] = useState(initErr);

const handleChange = (e: any) => {

const { value, name } = e.target;
Expand Down
147 changes: 147 additions & 0 deletions src/Components/Facility/DoctorCapacityEditForm.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
import React, { useReducer } from 'react'
import { Grid, InputLabel, Card, CardHeader, CardContent, Button } from '@material-ui/core'
import { TextInputField } from '../Common/HelperInputFields';

const initForm: any = {
totalDoctors: "",
currentAvailability: ""
};
const initialState = {
form: { ...initForm },
errors: { ...initForm }
};
const doctorCapacityEditReducer = (state = initialState, action: any) => {
switch (action.type) {
case "set_form": {
return {
...state,
form: action.form
}
}
case "set_error": {
return {
...state,
errors: action.errors
}
}
case "set_reset": {
return {
...state,
form: action.form
}
}
default:
return state
}
}

export const DoctorCapacityEditForm = (props: any) => {
const [state, dispatch] = useReducer(doctorCapacityEditReducer, initialState)

const handleChange = (e: any) => {
let form = { ...state.form }
form[e.target.name] = e.target.value
dispatch({ type: "set_form", form })
}

const validateData = () => {
let errors = { ...initForm }
let invalidForm = false
Object.keys(state.form).map((field) => {
if (field === "totalDoctors" && state.form.totalDoctors == "") {
errors[field] = "Field is required"
invalidForm = true;
} else if (field === "totalDoctors" && isNaN(state.form.totalDoctors)) {
errors[field] = "Please enter a valid number"
invalidForm = true;
}
if (field === "currentAvailability" && state.form.currentAvailability == "") {
errors[field] = "Field is required"
invalidForm = true;
} else if (field === "currentAvailability" && isNaN(state.form.currentAvailability)) {
errors[field] = "Please enter a valid number"
invalidForm = true;
}
if (field === "currentAvailability" && state.form.currentAvailability != ""
&& state.form.currentAvailability > state.form.totalDoctors) {
errors[field] = "Current availability cannot be greater than Total"
invalidForm = true;
}
})
if (invalidForm) {
dispatch({ type: "set_error", errors })
return false
}
dispatch({ type: "set_error", errors })
return true
}

const handleSubmit = (e: any) => {
e.preventDefault();
if (validateData()) {
//api-call
}
}
const handleCancel = (e: any) => {
const form = { ...initForm };
dispatch({ type: "set_reset", form })
};
return <div>
<Grid container alignContent="center" justify="center">
<Grid item xs={12}>
<Card>
<CardHeader title="Edit Doctor Capacity" />
<form onSubmit={e => { handleSubmit(e) }}>
<CardContent>
<InputLabel id="demo-simple-select-outlined-label">Total Number of Doctors</InputLabel>
<TextInputField
name="totalDoctors"
variant="outlined"
margin="dense"
type="number"
// InputLabelProps={{ shrink: !!form.registrationNumber }}
value={state.form.totalDoctors}
onChange={handleChange}
errors={state.errors.totalDoctors}
/>
</CardContent>
<CardContent>
<InputLabel id="demo-simple-select-outlined-label">Doctors Available Currently</InputLabel>
<TextInputField
name="currentAvailability"
variant="outlined"
margin="dense"
type="number"
// InputLabelProps={{ shrink: !!form.registrationNumber }}
value={state.form.currentAvailability}
onChange={handleChange}
errors={state.errors.currentAvailability}
/>
</CardContent>
<CardContent>
<Grid container justify="center" spacing={5} >
<Grid item>
<Button
color="primary"
variant="contained"
type="submit"
style={{ marginLeft: 'auto' }}
onClick={(e) => handleSubmit(e)}
>Update</Button>
</Grid>
<Grid item>
<Button
color="primary"
variant="contained"
style={{ marginLeft: 'auto' }}
onClick={handleCancel}
>Cancel</Button>
</Grid>
</Grid>
</CardContent>
</form>
</Card>
</Grid>
</Grid>
</div>
}

0 comments on commit 5d4f379

Please sign in to comment.