From af22ce043a3749db36deb52646cf2530a6662b10 Mon Sep 17 00:00:00 2001 From: Jack Meyer Date: Fri, 7 Feb 2020 17:16:05 -0600 Subject: [PATCH] feat(toolbar): basic button toolbar --- src/HospitalRun.tsx | 120 ++++++++++--------- src/page-header/ButtonToolBar.tsx | 9 ++ src/page-header/button-bar-context.tsx | 36 ++++++ src/patients/list/Patients.tsx | 19 ++- src/scheduling/appointments/Appointments.tsx | 20 +++- 5 files changed, 144 insertions(+), 60 deletions(-) create mode 100644 src/page-header/ButtonToolBar.tsx create mode 100644 src/page-header/button-bar-context.tsx diff --git a/src/HospitalRun.tsx b/src/HospitalRun.tsx index 2ccf0bccbf..0cd8e7799a 100644 --- a/src/HospitalRun.tsx +++ b/src/HospitalRun.tsx @@ -5,6 +5,8 @@ import { Toaster } from '@hospitalrun/components' import Appointments from 'scheduling/appointments/Appointments' import NewAppointment from 'scheduling/appointments/new/NewAppointment' import ViewAppointment from 'scheduling/appointments/view/ViewAppointment' +import { ButtonBarProvider } from 'page-header/button-bar-context' +import ButtonToolBar from 'page-header/ButtonToolBar' import Sidebar from './components/Sidebar' import Permissions from './model/Permissions' import Dashboard from './dashboard/Dashboard' @@ -19,68 +21,72 @@ import PrivateRoute from './components/PrivateRoute' const HospitalRun = () => { const { title } = useSelector((state: RootState) => state.title) const { permissions } = useSelector((state: RootState) => state.user) + return (
-
-
-
-

{title}

-
-
- - - - - - - - - - -
- -
-
+ +
+
+
+

{title}

+ +
+
+ + + + + + + + + + +
+ +
+
+
) diff --git a/src/page-header/ButtonToolBar.tsx b/src/page-header/ButtonToolBar.tsx new file mode 100644 index 0000000000..701ade5e85 --- /dev/null +++ b/src/page-header/ButtonToolBar.tsx @@ -0,0 +1,9 @@ +import React from 'react' +import { useButtons } from './button-bar-context' + +const ButtonToolBar = () => { + const buttons = useButtons() + return <>{buttons.map((button) => button)} +} + +export default ButtonToolBar diff --git a/src/page-header/button-bar-context.tsx b/src/page-header/button-bar-context.tsx new file mode 100644 index 0000000000..dcb076f584 --- /dev/null +++ b/src/page-header/button-bar-context.tsx @@ -0,0 +1,36 @@ +import React, { useState } from 'react' + +type Props = { + children: React.ReactNode +} + +type ButtonUpdater = (buttons: React.ReactNode[]) => void + +const ButtonBarStateContext = React.createContext([]) +const ButtonBarUpdateContext = React.createContext(() => {}) + +function ButtonBarProvider(props: Props) { + const { children } = props + const [state, setState] = useState([]) + return ( + + {children} + + ) +} +function useButtons() { + const context = React.useContext(ButtonBarStateContext) + if (context === undefined) { + throw new Error('useCountState must be used within a CountProvider') + } + return context +} +function useButtonToolbarSetter() { + const context = React.useContext(ButtonBarUpdateContext) + if (context === undefined) { + throw new Error('useCountDispatch must be used within a CountProvider') + } + return context +} + +export { ButtonBarProvider, useButtons, useButtonToolbarSetter } diff --git a/src/patients/list/Patients.tsx b/src/patients/list/Patients.tsx index 307727e6ab..2ee672b656 100644 --- a/src/patients/list/Patients.tsx +++ b/src/patients/list/Patients.tsx @@ -3,6 +3,7 @@ import { useSelector, useDispatch } from 'react-redux' import { useHistory } from 'react-router' import { useTranslation } from 'react-i18next' import { Spinner, TextInput, Button, List, ListItem, Container, Row } from '@hospitalrun/components' +import { useButtonToolbarSetter } from 'page-header/button-bar-context' import { RootState } from '../../store' import { fetchPatients, searchPatients } from '../patients-slice' import useTitle from '../../page-header/useTitle' @@ -14,11 +15,27 @@ const Patients = () => { const dispatch = useDispatch() const { patients, isLoading } = useSelector((state: RootState) => state.patients) + const setButtonToolBar = useButtonToolbarSetter() + setButtonToolBar([ + , + ]) + const [searchText, setSearchText] = useState('') useEffect(() => { dispatch(fetchPatients()) - }, [dispatch]) + + return () => { + setButtonToolBar([]) + } + }, [dispatch, setButtonToolBar]) if (isLoading) { return diff --git a/src/scheduling/appointments/Appointments.tsx b/src/scheduling/appointments/Appointments.tsx index 7676b1beb9..e45b105bd6 100644 --- a/src/scheduling/appointments/Appointments.tsx +++ b/src/scheduling/appointments/Appointments.tsx @@ -1,11 +1,12 @@ import React, { useEffect, useState } from 'react' -import { Calendar } from '@hospitalrun/components' +import { Calendar, Button } from '@hospitalrun/components' import useTitle from 'page-header/useTitle' import { useTranslation } from 'react-i18next' import { useSelector, useDispatch } from 'react-redux' import { RootState } from 'store' import { useHistory } from 'react-router' import PatientRepository from 'clients/db/PatientRepository' +import { useButtonToolbarSetter } from 'page-header/button-bar-context' import { fetchAppointments } from './appointments-slice' interface Event { @@ -23,10 +24,25 @@ const Appointments = () => { const dispatch = useDispatch() const { appointments } = useSelector((state: RootState) => state.appointments) const [events, setEvents] = useState([]) + const setButtonToolBar = useButtonToolbarSetter() + setButtonToolBar([ + , + ]) useEffect(() => { dispatch(fetchAppointments()) - }, [dispatch]) + + return () => { + setButtonToolBar([]) + } + }, [dispatch, setButtonToolBar]) useEffect(() => { const getAppointments = async () => {