@autobusal/operator-routes 1.0.3 → 1.0.4

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,72 @@
1
+ import { TFunction } from 'i18next';
2
+ import { useParams, Navigate } from 'react-router-dom';
3
+ import { Meta, BackWithTitle, Table, Row } from '@autobusal/common';
4
+ import { usePage } from '@autobusal/hooks';
5
+ import { GetUnavailableDates, DeleteUnavailable } from './services';
6
+
7
+ interface Props {
8
+ url: string
9
+ t: TFunction<'common'>
10
+ }
11
+
12
+ const Browse = ({ url, t }: Props): JSX.Element => {
13
+ const params = useParams();
14
+
15
+ const id = Number(params.id);
16
+
17
+ url = url.replace('[id]', String(id));
18
+
19
+ usePage('routes');
20
+
21
+ const { data, isLoading, isFetching, refetch } = GetUnavailableDates(id);
22
+
23
+ const { mutate: Delete } = DeleteUnavailable(id);
24
+
25
+ if (id === 0) {
26
+ return <Navigate to={ url } />;
27
+ }
28
+
29
+ const onDelete = (id: number): void => {
30
+ Delete(id, {
31
+ onSuccess: () => refetch()
32
+ });
33
+ };
34
+
35
+ const rows = data?.unavailable.map(item => (
36
+ <Row
37
+ key={ item.id }
38
+ id={ item.id }
39
+ data={ [
40
+ `${ item.start_inactive } - ${ item.end_inactive }`
41
+ ] }
42
+ />
43
+ ));
44
+
45
+ const title = t('unavailable.browse.title', { name: data?.name, ns: 'common' });
46
+
47
+ return (
48
+ <Meta title={ title }>
49
+ <BackWithTitle title={ title } to={ url } t={ t } />
50
+
51
+ <Table
52
+ url={ `/operator/routes/unavailable/${ id }` }
53
+ columns={ [{
54
+ name: t('unavailable.browse.date', { ns: 'common' }),
55
+ width: 90
56
+ }] }
57
+ rows={ rows }
58
+ loading={ isLoading }
59
+ fetching={ isFetching }
60
+ t={ t }
61
+ actions={ [
62
+ 'create', 'view', 'delete'
63
+ ] }
64
+ handlers={ {
65
+ delete: onDelete
66
+ } }
67
+ />
68
+ </Meta>
69
+ );
70
+ };
71
+
72
+ export default Browse;
@@ -0,0 +1,91 @@
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 { UnavailableData } from '@autobusal/providers/types/routes';
7
+ import { GetUnavailable, PostUnavailable, DeleteUnavailable } from './services';
8
+
9
+ interface Props {
10
+ url: string
11
+ t: TFunction<'common'>
12
+ }
13
+
14
+ const Manage = ({ url, t }: Props): JSX.Element => {
15
+ const params = useParams();
16
+
17
+ const id = Number(params.id);
18
+ const routeId = Number(params.route_id);
19
+
20
+ url = url.replace('[routeId]', String(routeId));
21
+
22
+ usePage('routes');
23
+
24
+ const navigate = useNavigate();
25
+
26
+ const { data } = GetUnavailable(routeId, id);
27
+
28
+ const { mutate: Update, isPending: isPendingSave } = PostUnavailable(routeId, id);
29
+
30
+ const { mutate: Delete, isPending: isPendingDelete } = DeleteUnavailable(routeId);
31
+
32
+ if (routeId === 0) {
33
+ return <Navigate to={ url } />;
34
+ }
35
+
36
+ const onSave = (data: UnavailableData): void => {
37
+ Update(data, {
38
+ onSuccess: () => {
39
+ success(t('unavailable.manage.messages.saved', { ns: 'common' }));
40
+
41
+ navigate(url);
42
+ }
43
+ });
44
+ };
45
+
46
+ const onDelete = (): void => {
47
+ Delete(id, {
48
+ onSuccess: () => {
49
+ success(t('unavailable.manage.messages.deleted', { ns: 'common' }));
50
+
51
+ navigate(url);
52
+ }
53
+ });
54
+ };
55
+
56
+ const title = id > 0 ? t('unavailable.manage.title.update', { name: data?.name, ns: 'common' }) : t('unavailable.manage.title.new', { name: data?.name, ns: 'common' });
57
+
58
+ const unavailable = data?.unavailable !== undefined && data.unavailable.length > 0 ? data.unavailable[0] : undefined;
59
+
60
+ return (
61
+ <Meta title={ title }>
62
+ <BackWithTitle title={ title } to={ url } t={ t } />
63
+
64
+ <Viewer
65
+ id={ id }
66
+ data={ [{
67
+ label: t('unavailable.manage.start_inactive', { ns: 'common' }),
68
+ name: 'start_inactive',
69
+ type: 'picker',
70
+ value: unavailable?.start_inactive,
71
+ rules: 'required'
72
+ }, {
73
+ label: t('unavailable.manage.end_inactive', { ns: 'common' }),
74
+ name: 'end_inactive',
75
+ type: 'picker',
76
+ value: unavailable?.end_inactive,
77
+ rules: 'required'
78
+ }] }
79
+ pending={ isPendingSave || isPendingDelete }
80
+ t={ t }
81
+ actions={ [
82
+ 'save', 'delete'
83
+ ]}
84
+ onSave={ onSave }
85
+ onDelete={ onDelete }
86
+ />
87
+ </Meta>
88
+ );
89
+ };
90
+
91
+ export default Manage;
@@ -0,0 +1,72 @@
1
+ import { UseMutationResult, UseQueryResult, UseSuspenseQueryResult, useMutation, useQuery, useSuspenseQuery } from '@tanstack/react-query';
2
+ import { apiClient } from '@autobusal/providers';
3
+ import { RouteData } from '@autobusal/providers/types/routes';
4
+ import { UnavailableData } from '@autobusal/providers/types/routes';
5
+
6
+ export const GetUnavailableDates = (id: number): UseQueryResult<RouteData> => (
7
+ useQuery({
8
+ queryKey: ['operators-unavailable', { id }],
9
+ queryFn: async () => (
10
+ await apiClient
11
+ .get('/api/unavailable/operator/browse', {
12
+ params: { id }
13
+ })
14
+ .then(response => (
15
+ response.data
16
+ ))
17
+ )
18
+ })
19
+ );
20
+
21
+ export const GetUnavailable = (routeId: number, id: number): UseSuspenseQueryResult<RouteData> => (
22
+ useSuspenseQuery({
23
+ queryKey: ['operator-unavailable-manage', { routeId, id }],
24
+ queryFn: async () => (
25
+ await apiClient
26
+ .get('/api/unavailable/operator/get', {
27
+ params: {
28
+ route_id: routeId,
29
+ id
30
+ }
31
+ })
32
+ .then(response => (
33
+ response.data
34
+ ))
35
+ )
36
+ })
37
+ );
38
+
39
+ export const PostUnavailable = (routeId: number, id: number): UseMutationResult<void, Error, UnavailableData, unknown> => (
40
+ useMutation({
41
+ mutationKey: ['unavailable-operator-save', { routeId, id }],
42
+ mutationFn: async (data: UnavailableData) => (
43
+ await apiClient
44
+ .post('/api/unavailable/operator/update', {
45
+ ...data,
46
+ route_id: routeId,
47
+ id
48
+ })
49
+ .then(response => (
50
+ response.data
51
+ ))
52
+ )
53
+ })
54
+ );
55
+
56
+ export const DeleteUnavailable = (routeId: number): UseMutationResult<void, Error, number, unknown> => (
57
+ useMutation({
58
+ mutationKey: ['unavailable-operator-delete', { routeId }],
59
+ mutationFn: async (id: number) => (
60
+ await apiClient
61
+ .delete('/api/unavailable/operator/delete', {
62
+ params: {
63
+ route_id: routeId,
64
+ id
65
+ }
66
+ })
67
+ .then(response => (
68
+ response.data
69
+ ))
70
+ )
71
+ })
72
+ );
package/index.ts CHANGED
@@ -2,10 +2,14 @@ import Browse from './Browse';
2
2
  import Manage from './Manage';
3
3
  import SchedulesManage from './Schedule/Manage';
4
4
  import TransitingManage from './Transiting/Manage';
5
+ import UnavailableBrowse from './Unavailable/Browse';
6
+ import UnavailableManage from './Unavailable/Manage';
5
7
 
6
8
  export {
7
9
  Browse,
8
10
  Manage,
9
11
  SchedulesManage,
10
- TransitingManage
12
+ TransitingManage,
13
+ UnavailableBrowse,
14
+ UnavailableManage
11
15
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autobusal/operator-routes",
3
- "version": "1.0.3",
3
+ "version": "1.0.4",
4
4
  "type": "module",
5
5
  "main": "index.ts"
6
6
  }