/**
 * mSupply Mobile
 * Sustainable Solutions (NZ) Ltd. 2019
 */

import { UIDatabase } from '../database';

export const PATIENT_ACTIONS = {
  FIELD_VALIDITY: 'Patient/fieldValidity',
  FIELD_UPDATE: 'Patient/fieldUpdate',
};

const setFieldValidity = (field, newValidity) => ({
  type: PATIENT_ACTIONS.FIELD_VALIDITY,
  payload: { field, validity: newValidity },
});

const setFieldUpdate = (field, newValue) => ({
  type: PATIENT_ACTIONS.FIELD_UPDATE,
  payload: { field, value: newValue },
});

const patientUpdate = () => (_, getState) => {
  const { patient } = getState();
  const {
    currentPatient,
    firstName,
    lastName,
    code,
    dateOfBirth,
    email,
    phone,
    addressOne,
    addressTwo,
    country,
  } = patient;

  UIDatabase.write(() => {
    UIDatabase.update(
      'Name',
      ...currentPatient,
      firstName,
      lastName,
      code,
      dateOfBirth,
      email,
      phone,
      addressOne,
      addressTwo,
      country
    );
  });
};

export const PatientActions = { patientUpdate, setFieldUpdate, setFieldValidity };

@@ -218,17 +218,382 @@
 pdate',%0A
-%7D
+  PATIENT_EDIT: 'Patient/patientEdit',%0A  PATIENT_CREATION: 'Patient/patientCreation',%0A  COMPLETE: 'Patient/complete',%0A%7D;%0A%0Aconst closeModal = () =%3E (%7B type: PATIENT_ACTIONS.COMPLETE %7D);%0A%0Aconst createPatient = () =%3E (%7B type: PATIENT_ACTIONS.PATIENT_CREATION %7D);%0A%0Aconst editPatient = patient =%3E (%7B%0A  type: PATIENT_ACTIONS.PATIENT_EDIT,%0A  payload: %7B%0A    patient,%0A  %7D,%0A%7D)
 ;%0A%0Aconst
@@ -887,17 +887,24 @@
  () =%3E (
-_
+dispatch
 , getSta
@@ -1168,22 +1168,17 @@
 ate(
-%0A      
 'Name',
+ %7B
 %0A   
@@ -1342,21 +1342,23 @@
 ntry
+,
 %0A    
+%7D
 );%0A  %7D);
 %0A%7D;%0A
@@ -1353,16 +1353,43 @@
 );%0A  %7D);
+%0A%0A  dispatch(closeModal());
 %0A%7D;%0A%0Aexp
@@ -1416,16 +1416,35 @@
 ions = %7B
+%0A  createPatient,%0A 
  patient
@@ -1446,24 +1446,26 @@
 tientUpdate,
+%0A 
  setFieldUpd
@@ -1468,16 +1468,18 @@
 dUpdate,
+%0A 
  setFiel
@@ -1487,12 +1487,42 @@
 Validity
- 
+,%0A  editPatient,%0A  closeModal,%0A
 %7D;%0A

Add editpatient/createpatient/close modal actions