From 168a112802cf07331331364f26e7e13afa881f88 Mon Sep 17 00:00:00 2001 From: rabbihossain Date: Fri, 6 Mar 2020 11:52:47 +0600 Subject: [PATCH] feat: making sidebar collapsible this changes make the sidebar collapsible "fix #1858" --- src/HospitalRun.tsx | 8 +++++- src/__tests__/HospitalRun.test.tsx | 15 ++++++++++++ src/__tests__/components/Sidebar.test.tsx | 30 +++++++++++++++-------- src/components/Sidebar.tsx | 27 +++++++++++++++++--- src/components/component-slice.ts | 28 +++++++++++++++++++++ src/index.css | 2 +- src/store/index.ts | 2 ++ 7 files changed, 96 insertions(+), 16 deletions(-) create mode 100644 src/components/component-slice.ts diff --git a/src/HospitalRun.tsx b/src/HospitalRun.tsx index b54011daa6..30cd77acd4 100644 --- a/src/HospitalRun.tsx +++ b/src/HospitalRun.tsx @@ -23,6 +23,7 @@ import PrivateRoute from './components/PrivateRoute' const HospitalRun = () => { const { title } = useSelector((state: RootState) => state.title) const { permissions } = useSelector((state: RootState) => state.user) + const { sidebarCollapsed } = useSelector((state: RootState) => state.components) return (
@@ -31,7 +32,12 @@ const HospitalRun = () => {
-
+

{title}

diff --git a/src/__tests__/HospitalRun.test.tsx b/src/__tests__/HospitalRun.test.tsx index bf7572ae68..17541a3735 100644 --- a/src/__tests__/HospitalRun.test.tsx +++ b/src/__tests__/HospitalRun.test.tsx @@ -34,6 +34,7 @@ describe('HospitalRun', () => { title: 'test', user: { permissions: [Permissions.WritePatients] }, breadcrumbs: { breadcrumbs: [] }, + components: { sidebarCollapsed: false }, }) const wrapper = mount( @@ -62,6 +63,7 @@ describe('HospitalRun', () => { title: 'test', user: { permissions: [] }, breadcrumbs: { breadcrumbs: [] }, + components: { sidebarCollapsed: false }, })} > @@ -94,6 +96,7 @@ describe('HospitalRun', () => { user: { permissions: [Permissions.WritePatients, Permissions.ReadPatients] }, patient: { patient }, breadcrumbs: { breadcrumbs: [] }, + components: { sidebarCollapsed: false }, }) const wrapper = mount( @@ -123,6 +126,7 @@ describe('HospitalRun', () => { title: 'test', user: { permissions: [Permissions.WritePatients] }, breadcrumbs: { breadcrumbs: [] }, + components: { sidebarCollapsed: false }, })} > @@ -141,6 +145,7 @@ describe('HospitalRun', () => { title: 'test', user: { permissions: [Permissions.ReadPatients] }, breadcrumbs: { breadcrumbs: [] }, + components: { sidebarCollapsed: false }, })} > @@ -173,6 +178,7 @@ describe('HospitalRun', () => { user: { permissions: [Permissions.ReadPatients] }, patient: { patient }, breadcrumbs: { breadcrumbs: [] }, + components: { sidebarCollapsed: false }, }) const wrapper = mount( @@ -201,6 +207,7 @@ describe('HospitalRun', () => { title: 'test', user: { permissions: [] }, breadcrumbs: { breadcrumbs: [] }, + components: { sidebarCollapsed: false }, })} > @@ -220,6 +227,7 @@ describe('HospitalRun', () => { user: { permissions: [Permissions.ReadAppointments] }, appointments: { appointments: [] }, breadcrumbs: { breadcrumbs: [] }, + components: { sidebarCollapsed: false }, }) const wrapper = mount( @@ -251,6 +259,7 @@ describe('HospitalRun', () => { title: 'test', user: { permissions: [] }, breadcrumbs: { breadcrumbs: [] }, + components: { sidebarCollapsed: false }, })} > @@ -269,6 +278,7 @@ describe('HospitalRun', () => { title: 'test', user: { permissions: [Permissions.WriteAppointments] }, breadcrumbs: { breadcrumbs: [] }, + components: { sidebarCollapsed: false }, }) const wrapper = mount( @@ -298,6 +308,7 @@ describe('HospitalRun', () => { title: 'test', user: { permissions: [] }, breadcrumbs: { breadcrumbs: [] }, + components: { sidebarCollapsed: false }, })} > @@ -332,6 +343,7 @@ describe('HospitalRun', () => { user: { permissions: [Permissions.WriteAppointments, Permissions.ReadAppointments] }, appointment: { appointment, patient: {} as Patient }, breadcrumbs: { breadcrumbs: [] }, + components: { sidebarCollapsed: false }, }) const wrapper = mount( @@ -364,6 +376,7 @@ describe('HospitalRun', () => { title: 'test', user: { permissions: [Permissions.WriteAppointments] }, breadcrumbs: { breadcrumbs: [] }, + components: { sidebarCollapsed: false }, })} > @@ -382,6 +395,7 @@ describe('HospitalRun', () => { title: 'test', user: { permissions: [Permissions.ReadAppointments] }, breadcrumbs: { breadcrumbs: [] }, + components: { sidebarCollapsed: false }, })} > @@ -403,6 +417,7 @@ describe('HospitalRun', () => { title: 'test', user: { permissions: [Permissions.WritePatients] }, breadcrumbs: { breadcrumbs: [] }, + components: { sidebarCollapsed: false }, })} > diff --git a/src/__tests__/components/Sidebar.test.tsx b/src/__tests__/components/Sidebar.test.tsx index 3c37a9b090..c5136b3a8b 100644 --- a/src/__tests__/components/Sidebar.test.tsx +++ b/src/__tests__/components/Sidebar.test.tsx @@ -6,15 +6,25 @@ import Sidebar from 'components/Sidebar' import { Router } from 'react-router' import { ListItem } from '@hospitalrun/components' import { act } from '@testing-library/react' +import configureMockStore from 'redux-mock-store' +import thunk from 'redux-thunk' +import { Provider } from 'react-redux' + +const mockStore = configureMockStore([thunk]) describe('Sidebar', () => { let history = createMemoryHistory() + const store = mockStore({ + components: { sidebarCollapsed: false }, + }) const setup = (location: string) => { history = createMemoryHistory() history.push(location) return mount( - + + + , ) } @@ -27,7 +37,7 @@ describe('Sidebar', () => { expect( listItems - .at(0) + .at(1) .text() .trim(), ).toEqual('dashboard.label') @@ -38,7 +48,7 @@ describe('Sidebar', () => { const listItems = wrapper.find(ListItem) - expect(listItems.at(0).prop('active')).toBeTruthy() + expect(listItems.at(1).prop('active')).toBeTruthy() }) it('should navigate to / when the dashboard link is clicked', () => { @@ -47,7 +57,7 @@ describe('Sidebar', () => { const listItems = wrapper.find(ListItem) act(() => { - ;(listItems.at(0).prop('onClick') as any)() + ;(listItems.at(1).prop('onClick') as any)() }) expect(history.location.pathname).toEqual('/') @@ -62,7 +72,7 @@ describe('Sidebar', () => { expect( listItems - .at(1) + .at(2) .text() .trim(), ).toEqual('patients.label') @@ -73,7 +83,7 @@ describe('Sidebar', () => { const listItems = wrapper.find(ListItem) - expect(listItems.at(1).prop('active')).toBeTruthy() + expect(listItems.at(2).prop('active')).toBeTruthy() }) it('should navigate to /patients when the patients link is clicked', () => { @@ -82,7 +92,7 @@ describe('Sidebar', () => { const listItems = wrapper.find(ListItem) act(() => { - ;(listItems.at(1).prop('onClick') as any)() + ;(listItems.at(2).prop('onClick') as any)() }) expect(history.location.pathname).toEqual('/patients') @@ -97,7 +107,7 @@ describe('Sidebar', () => { expect( listItems - .at(2) + .at(3) .text() .trim(), ).toEqual('scheduling.label') @@ -108,7 +118,7 @@ describe('Sidebar', () => { const listItems = wrapper.find(ListItem) - expect(listItems.at(2).prop('active')).toBeTruthy() + expect(listItems.at(3).prop('active')).toBeTruthy() }) it('should navigate to /appointments when the scheduling link is clicked', () => { @@ -117,7 +127,7 @@ describe('Sidebar', () => { const listItems = wrapper.find(ListItem) act(() => { - ;(listItems.at(2).prop('onClick') as any)() + ;(listItems.at(3).prop('onClick') as any)() }) expect(history.location.pathname).toEqual('/appointments') diff --git a/src/components/Sidebar.tsx b/src/components/Sidebar.tsx index 8957c07e9e..528357d23d 100644 --- a/src/components/Sidebar.tsx +++ b/src/components/Sidebar.tsx @@ -2,8 +2,14 @@ import React, { CSSProperties } from 'react' import { List, ListItem, Icon } from '@hospitalrun/components' import { useTranslation } from 'react-i18next' import { useLocation, useHistory } from 'react-router' +import { useSelector, useDispatch } from 'react-redux' +import { RootState } from '../store' +import { updateSidebar } from './component-slice' const Sidebar = () => { + const dispatch = useDispatch() + const { sidebarCollapsed } = useSelector((state: RootState) => state.components) + const { t } = useTranslation() const path = useLocation() const history = useHistory() @@ -18,16 +24,29 @@ const Sidebar = () => { } return ( -