@autobusal/operator-routes 1.0.0 → 1.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,82 @@
1
+ import { TFunction } from 'i18next';
2
+ import { useParams, Navigate, useNavigate } from 'react-router-dom';
3
+ import { Meta, BackWithTitle, Viewer } from '@autobusal/common';
4
+ import { usePage } from '@autobusal/hooks';
5
+ import { success } from '@autobusal/utilities';
6
+ import { getSchedules } from './utilities';
7
+ import { AvailableData } from '@autobusal/providers/types/routes';
8
+ import { GetSchedule, PostSchedule } from './services';
9
+
10
+ interface Props {
11
+ url: string
12
+ t: TFunction<'common'>
13
+ }
14
+
15
+ const Manage = ({ url, t }: Props): JSX.Element => {
16
+ const params = useParams();
17
+
18
+ const id = Number(params.id);
19
+
20
+ url = url.replace('[id]', String(id));
21
+
22
+ usePage('routes');
23
+
24
+ const navigate = useNavigate();
25
+
26
+ const { data } = GetSchedule(id);
27
+
28
+ const { mutate: Update, isPending } = PostSchedule(id);
29
+
30
+ if (id === 0) {
31
+ return <Navigate to={ url } />;
32
+ }
33
+
34
+ const onSave = (data: AvailableData): void => {
35
+ Update(data, {
36
+ onSuccess: () => {
37
+ success(t('schedules_manage.message.saved', { ns: 'common' }));
38
+
39
+ navigate(url);
40
+ }
41
+ });
42
+ };
43
+
44
+ const title = t('schedules_manage.title', { name: data?.name, ns: 'common' });
45
+
46
+ return (
47
+ <Meta title={ title }>
48
+ <BackWithTitle title={ title } to={ url } t={ t } />
49
+
50
+ <Viewer
51
+ id={ id }
52
+ data={ [{
53
+ label: t('schedules_manage.start_date', { ns: 'common' }),
54
+ name: 'start_date',
55
+ type: 'picker',
56
+ value: data?.available.start_date,
57
+ rules: 'required'
58
+ }, {
59
+ label: t('schedules_manage.end_date', { ns: 'common' }),
60
+ name: 'end_date',
61
+ type: 'picker',
62
+ value: data?.available.end_date,
63
+ rules: 'required'
64
+ }, {}, {
65
+ label: t('schedules_manage.schedule', { ns: 'common' }),
66
+ name: 'schedule',
67
+ type: 'checkbox-list-all',
68
+ values: getSchedules(t),
69
+ selected: data?.available.schedule ?? []
70
+ }] }
71
+ pending={ isPending }
72
+ t={ t }
73
+ actions={ [
74
+ 'update'
75
+ ]}
76
+ onSave={ onSave }
77
+ />
78
+ </Meta>
79
+ );
80
+ };
81
+
82
+ export default Manage;
@@ -0,0 +1,39 @@
1
+ import { UseMutationResult, UseSuspenseQueryResult, useMutation, useSuspenseQuery } from '@tanstack/react-query';
2
+ import { apiClient } from '@autobusal/providers';
3
+ import { RouteData, AvailableData } from '@autobusal/providers/types/routes';
4
+
5
+ export const GetSchedule = (id: number): UseSuspenseQueryResult<RouteData> => (
6
+ useSuspenseQuery({
7
+ queryKey: ['operator-schedule-manage', { id }],
8
+ queryFn: async () => {
9
+ if (id === 0) {
10
+ return null;
11
+ }
12
+
13
+ return await apiClient
14
+ .get('/api/schedules/operator/get', {
15
+ params: { id }
16
+ })
17
+ .then(response => (
18
+ response.data
19
+ ))
20
+ }
21
+ })
22
+ );
23
+
24
+ export const PostSchedule = (id: number): UseMutationResult<void, Error, AvailableData, unknown> => (
25
+ useMutation({
26
+ mutationKey: ['operator-schedule-save', { id }],
27
+ mutationFn: async (data: AvailableData) => (
28
+ await apiClient
29
+ .post('/api/schedules/operator/update', {
30
+ ...data,
31
+ id,
32
+ schedule: JSON.stringify(data.schedule)
33
+ })
34
+ .then(response => (
35
+ response.data
36
+ ))
37
+ )
38
+ })
39
+ );
@@ -0,0 +1,27 @@
1
+ import { TFunction } from 'i18next';
2
+ import { DropdownData } from '@autobusal/providers/types/other';
3
+
4
+ export const getSchedules = (t: TFunction<'common'>): DropdownData[] => ([
5
+ {
6
+ name: t('data.days.mon', { ns: 'common' }),
7
+ id: 1
8
+ }, {
9
+ name: t('data.days.tue', { ns: 'common' }),
10
+ id: 2
11
+ }, {
12
+ name: t('data.days.wed', { ns: 'common' }),
13
+ id: 3
14
+ }, {
15
+ name: t('data.days.thu', { ns: 'common' }),
16
+ id: 4
17
+ }, {
18
+ name: t('data.days.fri', { ns: 'common' }),
19
+ id: 5
20
+ }, {
21
+ name: t('data.days.sat', { ns: 'common' }),
22
+ id: 6
23
+ }, {
24
+ name: t('data.days.sun', { ns: 'common' }),
25
+ id: 7
26
+ }
27
+ ]);
package/index.ts CHANGED
@@ -1,7 +1,9 @@
1
1
  import Browse from './Browse';
2
2
  import Manage from './Manage';
3
+ import SchedulesManage from './Schedule/Manage';
3
4
 
4
5
  export {
5
6
  Browse,
6
- Manage
7
+ Manage,
8
+ SchedulesManage
7
9
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autobusal/operator-routes",
3
- "version": "1.0.0",
3
+ "version": "1.0.2",
4
4
  "type": "module",
5
5
  "main": "index.ts"
6
6
  }