Skip to content

Commit cbd9c08

Browse files
th-351: Sidebar for drivers (#358)
* th-351: * sidebar * th-351: * active tab * th-351: * close shift button to sidebar * th-351: * small changes * th-351: * small changes * th-351: * small changes
1 parent 6185994 commit cbd9c08

File tree

10 files changed

+96
-64
lines changed

10 files changed

+96
-64
lines changed

frontend/src/libs/components/router/router.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ const Router = (): JSX.Element => {
9292
<Route
9393
path={AppRoute.AVAILABLE_TRUCKS}
9494
element={
95-
<PageLayout isSidebarHidden>
95+
<PageLayout>
9696
<AvailableTrucks />
9797
</PageLayout>
9898
}
@@ -101,7 +101,7 @@ const Router = (): JSX.Element => {
101101
<Route
102102
path={AppRoute.ORDERS}
103103
element={
104-
<PageLayout isSidebarHidden>
104+
<PageLayout>
105105
<Orders />
106106
</PageLayout>
107107
}

frontend/src/libs/types/sidebar.type.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
import {
2-
type SidebarTabsName,
3-
type SidebarTabsPath,
4-
} from '~/libs/enums/enums.js';
1+
import { type AppRoute, type SidebarTabsName } from '~/libs/enums/enums.js';
52
import { type IconName } from '~/libs/enums/icon-name.enum.js';
63
import { type ValueOf } from '~/libs/types/types.js';
74

85
type TabsType = {
96
name: ValueOf<typeof SidebarTabsName>;
10-
path: ValueOf<typeof SidebarTabsPath>;
7+
path: ValueOf<typeof AppRoute>;
118
icon: ValueOf<typeof IconName>;
129
};
1310

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { Button } from '~/libs/components/components.js';
2+
import { IconName } from '~/libs/enums/icon-name.enum.js';
3+
import { getValidClassNames } from '~/libs/helpers/helpers.js';
4+
import {
5+
useAppDispatch,
6+
useAppSelector,
7+
useCallback,
8+
} from '~/libs/hooks/hooks.js';
9+
import { manufacturerKeyToReadableName } from '~/packages/trucks/libs/maps/manufacturer-key-to-readable-name.map.js';
10+
import { actions as driverActions } from '~/slices/driver/driver.js';
11+
import { ShiftStatus } from '~/slices/driver/libs/enums/enums.js';
12+
import {
13+
selectActiveTruck,
14+
selectShiftStatus,
15+
} from '~/slices/driver/selectors.js';
16+
17+
import styles from './styles.module.scss';
18+
19+
const EndShiftButton: React.FC = () => {
20+
const dispatch = useAppDispatch();
21+
const shiftStatus = useAppSelector(selectShiftStatus);
22+
const truck = useAppSelector(selectActiveTruck);
23+
24+
const isActive = shiftStatus === ShiftStatus.ACTIVE;
25+
26+
const handleClick = useCallback(() => {
27+
void dispatch(driverActions.endShift());
28+
}, [dispatch]);
29+
30+
const manufacturerName =
31+
truck && manufacturerKeyToReadableName[truck.manufacturer];
32+
33+
return (
34+
<>
35+
{isActive && (
36+
<div className={styles.block}>
37+
<p className={getValidClassNames(styles.truckInfo, 'textMd')}>
38+
{manufacturerName} {truck?.licensePlateNumber}
39+
</p>
40+
<Button
41+
label={'End shift'}
42+
frontIcon={IconName.TRUCK}
43+
onClick={handleClick}
44+
className={styles.button}
45+
/>
46+
</div>
47+
)}
48+
</>
49+
);
50+
};
51+
52+
export { EndShiftButton };
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
@import "src/assets/css/vars.scss";
2+
3+
.block {
4+
margin-top: auto;
5+
}
6+
7+
.button {
8+
width: 100%;
9+
}
10+
11+
.truckInfo {
12+
margin: 10px 0;
13+
color: $grey;
14+
text-align: center;
15+
}

frontend/src/pages/dashboard/components/sidebar/libs/helpers.ts

Lines changed: 0 additions & 7 deletions
This file was deleted.
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
import { AppRoute } from '~/libs/enums/app-route.enum';
2-
31
const checkActiveTab = (path: string, tab: string): boolean => {
4-
return path === `${AppRoute.DASHBOARD}/${tab}`;
2+
return path === tab;
53
};
64

75
export { checkActiveTab };

frontend/src/pages/dashboard/components/sidebar/libs/tabs.ts

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,38 @@
1-
import {
2-
IconName,
3-
SidebarTabsName,
4-
SidebarTabsPath,
5-
} from '~/libs/enums/enums.js';
1+
import { AppRoute, IconName, SidebarTabsName } from '~/libs/enums/enums.js';
62
import { type TabsType } from '~/libs/types/types.js';
73

84
const BUSINESS_TABS: TabsType[] = [
95
{
106
name: SidebarTabsName.ORDERS,
11-
path: SidebarTabsPath.ORDERS,
7+
path: AppRoute.DASHBOARD_ORDERS,
128
icon: IconName.LIST,
139
},
1410
{
1511
name: SidebarTabsName.TRUCKS,
16-
path: SidebarTabsPath.TRUCKS,
12+
path: AppRoute.DASHBOARD_TRUCKS,
1713
icon: IconName.TRUCK,
1814
},
1915
{
2016
name: SidebarTabsName.DRIVERS,
21-
path: SidebarTabsPath.DRIVERS,
17+
path: AppRoute.DASHBOARD_DRIVERS,
2218
icon: IconName.USERS,
2319
},
2420
];
2521

2622
const DRIVER_TABS: TabsType[] = [
2723
{
2824
name: SidebarTabsName.ORDERS,
29-
path: SidebarTabsPath.ORDERS,
25+
path: AppRoute.ORDERS,
3026
icon: IconName.LIST,
3127
},
3228
{
3329
name: SidebarTabsName.TRUCK,
34-
path: SidebarTabsPath.CHOOSE_TRUCK,
30+
path: AppRoute.AVAILABLE_TRUCKS,
3531
icon: IconName.TRUCK,
3632
},
3733
{
3834
name: SidebarTabsName.PROFILE,
39-
path: SidebarTabsPath.PROFILE,
35+
path: AppRoute.EDIT_PROFILE,
4036
icon: IconName.USER_PEN,
4137
},
4238
];

frontend/src/pages/dashboard/components/sidebar/sidebar.tsx

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
import { Button } from '~/libs/components/components.js';
2-
import { AppRoute } from '~/libs/enums/enums.js';
2+
import { type AppRoute } from '~/libs/enums/enums.js';
33
import { getValidClassNames } from '~/libs/helpers/helpers.js';
44
import { useCallback, useLocation, useNavigate } from '~/libs/hooks/hooks.js';
5-
import { type TabName, type TabsType } from '~/libs/types/types.js';
5+
import { type TabsType, type ValueOf } from '~/libs/types/types.js';
66
import { UserGroupKey } from '~/packages/users/libs/enums/enums.js';
77
import { useAuthUser } from '~/slices/auth/auth.js';
88

9-
import { checkActiveTab } from './libs/helpers.js';
9+
import { EndShiftButton } from './components/end-shift-button/end-shift-button.js';
10+
import { checkActiveTab } from './libs/helpers/check-active-tab.helper.js';
1011
import { BUSINESS_TABS, DRIVER_TABS } from './libs/tabs.js';
1112
import styles from './styles.module.scss';
1213

@@ -19,15 +20,15 @@ const Sidebar: React.FC<Properties> = ({ isCollapsed = false }: Properties) => {
1920
const location = useLocation();
2021
const user = useAuthUser();
2122

23+
const isDriver = user?.group.key === UserGroupKey.DRIVER;
24+
2225
const getTabs = useCallback((): TabsType[] => {
23-
return user?.group.key === UserGroupKey.BUSINESS
24-
? BUSINESS_TABS
25-
: DRIVER_TABS;
26-
}, [user?.group.key]);
26+
return isDriver ? DRIVER_TABS : BUSINESS_TABS;
27+
}, [isDriver]);
2728

2829
const handleTabClick = useCallback(
29-
(tabName: TabName) => () => {
30-
navigate(`${AppRoute.DASHBOARD}/${tabName}`);
30+
(tabName: ValueOf<typeof AppRoute>) => () => {
31+
navigate(tabName);
3132
},
3233
[navigate],
3334
);
@@ -52,7 +53,7 @@ const Sidebar: React.FC<Properties> = ({ isCollapsed = false }: Properties) => {
5253
)}
5354
frontIcon={tab.icon}
5455
variant="text"
55-
onClick={handleTabClick(tab.name)}
56+
onClick={handleTabClick(tab.path)}
5657
>
5758
<span className={'visually-hidden'}>{tab.name}</span>
5859
</Button>
@@ -68,6 +69,7 @@ const Sidebar: React.FC<Properties> = ({ isCollapsed = false }: Properties) => {
6869
)}
6970
>
7071
<ul className={styles.list}>{renderTabs()}</ul>
72+
{isDriver && <EndShiftButton />}
7173
</div>
7274
);
7375
};

frontend/src/pages/dashboard/components/sidebar/styles.module.scss

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
@import "src/assets/css/vars.scss";
22

33
.container {
4+
display: flex;
5+
flex-direction: column;
46
width: 210px;
57
height: calc(100% - 20px);
68
margin: 10px;
Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,18 @@
1-
import { Button } from '~/libs/components/components.js';
21
import { AppRoute } from '~/libs/enums/enums.js';
3-
import {
4-
useAppDispatch,
5-
useAppSelector,
6-
useCallback,
7-
useEffect,
8-
useNavigate,
9-
} from '~/libs/hooks/hooks.js';
10-
import { actions as driverActions } from '~/slices/driver/driver.js';
2+
import { useAppSelector, useEffect, useNavigate } from '~/libs/hooks/hooks.js';
113
import { ShiftStatus } from '~/slices/driver/libs/enums/enums.js';
12-
import {
13-
selectActiveTruck,
14-
selectShiftStatus,
15-
} from '~/slices/driver/selectors.js';
4+
import { selectShiftStatus } from '~/slices/driver/selectors.js';
165

176
const Orders: React.FC = () => {
18-
const dispatch = useAppDispatch();
19-
const truck = useAppSelector(selectActiveTruck);
207
const shiftStatus = useAppSelector(selectShiftStatus);
218
const navigate = useNavigate();
229
useEffect(() => {
2310
if (shiftStatus === ShiftStatus.DISABLED) {
2411
navigate(AppRoute.AVAILABLE_TRUCKS);
2512
}
2613
}, [shiftStatus, navigate]);
27-
const handleClick = useCallback(() => {
28-
void dispatch(driverActions.endShift());
29-
}, [dispatch]);
3014

31-
return (
32-
<div>
33-
<p>
34-
Your chosen truck: {truck?.manufacturer} ({truck?.id}) Test
35-
</p>
36-
<Button label={'End shift'} frontIcon={'truck'} onClick={handleClick} />
37-
</div>
38-
);
15+
return <div>Orders page</div>;
3916
};
4017

4118
export { Orders };

0 commit comments

Comments
 (0)