Skip to content
This repository has been archived by the owner on Jan 9, 2023. It is now read-only.

Commit

Permalink
feat(init): cleans up update patient code
Browse files Browse the repository at this point in the history
  • Loading branch information
jackcmeyer committed Nov 21, 2019
1 parent c4ced2c commit d6ab622
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 23 deletions.
47 changes: 29 additions & 18 deletions src/containers/ViewPatient.tsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
import React, { useState, useEffect } from 'react'
import { useDispatch, useSelector } from 'react-redux'
import { withRouter, RouteComponentProps } from 'react-router-dom'
import { withRouter, useHistory, useParams } from 'react-router-dom'
import { Button, Alert } from '@hospitalrun/components'
import { fetchPatient, updatePatient } from '../slices/patient-slice'
import { RootState } from '../store/store'
import Patient from '../model/Patient'
import PatientForm from '../components/PatientForm'

interface Props extends RouteComponentProps {
patient: Patient
}

const ViewPatient = (props: Props) => {
const [currentPatient, setCurrentPatient] = useState(new Patient('', '', '', ''))
const { match } = props
const { id } = match.params as any
const ViewPatient = () => {
const dispatch = useDispatch()
const { patient, isLoading, isUpdatedSuccessfully } = useSelector(
(state: RootState) => state.patient,
)
const [isEditable, setIsEditable] = useState(false)
const { patient, isLoading, isUpdated } = useSelector((state: RootState) => state.patient)
const [currentPatientDetails, setCurrentPatientDetails] = useState({
firstName: '',
lastName: '',
})
const history = useHistory()
const { id } = useParams()

const onSaveButtonClick = async () => {
currentPatient.id = patient.id
currentPatient.rev = patient.rev
dispatch(updatePatient(currentPatient))
dispatch(
updatePatient(
new Patient(
patient.id,
patient.rev,
currentPatientDetails.firstName,
currentPatientDetails.lastName,
),
),
)
setIsEditable(false)
}

const onCancelButtonClick = () => {
const { history } = props
history.push(`/patients`)
}

Expand All @@ -36,12 +43,16 @@ const ViewPatient = (props: Props) => {
}

const onFieldChange = (key: string, value: string) => {
;(currentPatient as any)[key] = value
setCurrentPatient(currentPatient)
setCurrentPatientDetails({
...currentPatientDetails,
[key]: value,
})
}

useEffect(() => {
dispatch(fetchPatient(id))
if (id) {
dispatch(fetchPatient(id))
}
}, [dispatch, id])

if (isLoading) {
Expand All @@ -51,7 +62,7 @@ const ViewPatient = (props: Props) => {
return (
<div className="container">
<Button onClick={onEditButtonClick}>Edit</Button>
{isUpdated && (
{isUpdatedSuccessfully && (
<Alert
color="success"
title="Successfully Updated"
Expand Down
8 changes: 3 additions & 5 deletions src/slices/patient-slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@ import { AppThunk } from '../store/store'

interface PatientState {
isLoading: boolean
isUpdated: boolean
isCreated: boolean
isUpdatedSuccessfully: boolean
patient: Patient
}

const initialState: PatientState = {
isLoading: false,
isUpdated: false,
isCreated: false,
isUpdatedSuccessfully: false,
patient: new Patient('', '', '', ''),
}

Expand All @@ -32,7 +30,7 @@ const patientSlice = createSlice({
},
updateStart: startLoading,
updatePatientSuccess(state, { payload }: PayloadAction<Patient>) {
state.isUpdated = true
state.isUpdatedSuccessfully = true
state.isLoading = false
state.patient = payload
},
Expand Down

0 comments on commit d6ab622

Please sign in to comment.