@autobusal/operator-routes 1.0.6 → 1.0.7

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,112 @@
1
+ import { TFunction } from 'i18next';
2
+ import { useParams, Navigate, useNavigate } from 'react-router-dom';
3
+ import { BsPlusCircleFill } from 'react-icons/bs';
4
+ import { Meta, BackWithTitle, Button, Table, Row } from '@autobusal/common';
5
+ import { usePage } from '@autobusal/hooks';
6
+ import { success } from '@autobusal/utilities';
7
+ import { GetAlternates, PostAdd, DeleteAlternate } from '../../../modules/OperatorRoutes/Alternates/services';
8
+
9
+ interface Props {
10
+ url: string
11
+ backUrl: string
12
+ t: TFunction<'common'>
13
+ }
14
+
15
+ const Browse = ({ url, backUrl, t }: Props): JSX.Element => {
16
+ const params = useParams();
17
+
18
+ const id = Number(params.id);
19
+
20
+ url = url.replace('[id]', String(id));
21
+ backUrl = backUrl.replace('[id]', String(id));
22
+
23
+ const navigate = useNavigate();
24
+
25
+ usePage('routes');
26
+
27
+ const { data, isLoading, isFetching, refetch } = GetAlternates(id);
28
+
29
+ const { mutate: Add, isPending } = PostAdd(id);
30
+
31
+ const { mutate: Delete } = DeleteAlternate(id);
32
+
33
+ if (id === 0) {
34
+ return <Navigate to={ backUrl } />;
35
+ }
36
+
37
+ const onNew = (): void => {
38
+ if (!window.confirm(t('alternates.browse.confirm', { ns: 'common' }))) {
39
+ return;
40
+ }
41
+
42
+ Add({}, {
43
+ onSuccess: (data) => {
44
+ success(t('alternates.browse.messages.saved', { ns: 'common' }));
45
+
46
+ navigate(`${ url }/manage/${ data.id }`);
47
+ }
48
+ });
49
+ };
50
+
51
+ const onDelete = (name: number): void => {
52
+ Delete(name, {
53
+ onSuccess: () => {
54
+ success(t('alternates.browse.messages.deleted', { ns: 'common' }));
55
+
56
+ refetch();
57
+ }
58
+ });
59
+ };
60
+
61
+ const rows = data?.alternates.map(item => (
62
+ <Row
63
+ key={ item.id }
64
+ id={ item.id }
65
+ data={ [
66
+ `${ item.date_from } - ${ item.date_to }`
67
+ ] }
68
+ />
69
+ ));
70
+
71
+ const title = t('alternates.browse.title', { name: data?.name, ns: 'common' });
72
+
73
+ return (
74
+ <Meta title={ title }>
75
+ <BackWithTitle title={ title } to={ backUrl } t={ t } />
76
+
77
+ <Table
78
+ url={ url }
79
+ columns={ [{
80
+ name: t('alternates.browse.from', { ns: 'common' }),
81
+ width: 75
82
+ }] }
83
+ rows={ rows }
84
+ loading={ isLoading }
85
+ fetching={ isFetching }
86
+ t={ t }
87
+ actions={ [
88
+ 'view', 'delete', 'extra'
89
+ ] }
90
+ handlers={ {
91
+ delete: onDelete
92
+ } }
93
+ extra={
94
+ <Button
95
+ type="button"
96
+ loading={ isPending }
97
+ text={
98
+ <>
99
+ <BsPlusCircleFill />
100
+ { t('table.actions.new', { ns: 'common' }) }
101
+ </>
102
+ }
103
+ noMargin
104
+ onClick={ onNew }
105
+ />
106
+ }
107
+ />
108
+ </Meta>
109
+ );
110
+ };
111
+
112
+ export default Browse;
@@ -0,0 +1,67 @@
1
+ import { UseMutationResult, UseQueryResult, useMutation, useQuery } from '@tanstack/react-query';
2
+ import { apiClient } from '@autobusal/providers';
3
+ import { RouteData, AlternateData } from '@autobusal/providers/types/routes';
4
+ import { ScheduleForm } from '../Prices/types';
5
+
6
+ export const GetAlternates = (id: number): UseQueryResult<RouteData> => (
7
+ useQuery({
8
+ queryKey: ['operator-alternates', { id }],
9
+ queryFn: async () => (
10
+ await apiClient
11
+ .get('/api/alternates/operator/browse', {
12
+ params: { id }
13
+ })
14
+ .then(response => (
15
+ response.data
16
+ ))
17
+ )
18
+ })
19
+ );
20
+
21
+ export const PostAdd = (id: number): UseMutationResult<AlternateData> => (
22
+ useMutation({
23
+ mutationKey: ['operator-alternates-add', { id }],
24
+ mutationFn: async () => (
25
+ await apiClient
26
+ .post('/api/alternates/operator/add', { id })
27
+ .then(response => (
28
+ response.data
29
+ ))
30
+ )
31
+ })
32
+ );
33
+
34
+ export const PostSchedule = (id: number, name: string): UseMutationResult<void, Error, ScheduleForm, unknown> => (
35
+ useMutation({
36
+ mutationKey: ['operator-alternates-schedule', { id, name }],
37
+ mutationFn: async (data: ScheduleForm) => (
38
+ await apiClient
39
+ .post('/api/alternates/operator/schedule', {
40
+ ...data,
41
+ id,
42
+ name
43
+ })
44
+ .then(response => (
45
+ response.data
46
+ ))
47
+ )
48
+ })
49
+ );
50
+
51
+ export const DeleteAlternate = (id: number): UseMutationResult<void, Error, number, unknown> => (
52
+ useMutation({
53
+ mutationKey: ['operator-alternates-delete'],
54
+ mutationFn: async (name: number) => (
55
+ await apiClient
56
+ .delete('/api/alternates/operator/delete', {
57
+ params: {
58
+ id,
59
+ name
60
+ }
61
+ })
62
+ .then(response => (
63
+ response.data
64
+ ))
65
+ )
66
+ })
67
+ );
@@ -5,7 +5,7 @@ import { Calendar, Button } from '@autobusal/common';
5
5
  import { Display, success } from '@autobusal/utilities';
6
6
  import { Container, Dates } from './styles';
7
7
  import { AlternateData } from '@autobusal/providers/types/routes';
8
- import { PostSchedule } from '@pages/Operator/Alternates/services';
8
+ import { PostSchedule } from '../../Alternates/services';
9
9
  import { ScheduleForm } from '../types';
10
10
 
11
11
  interface Props {
@@ -25,18 +25,18 @@ const Schedule = ({ id, data, name, t }: Props): JSX.Element => {
25
25
 
26
26
  const onSubmit = (data: ScheduleForm): void => {
27
27
  Save(data, {
28
- onSuccess: () => success(t('alternates_manage.messages.saved', { ns: 'common' }))
28
+ onSuccess: () => success(t('alternates.manage.messages.saved', { ns: 'common' }))
29
29
  });
30
30
  };
31
31
 
32
32
  return (
33
33
  <Container className="box">
34
- <h2>{ t('alternates_manage.title', { ns: 'common' }) }</h2>
34
+ <h2>{ t('alternates.manage.title', { ns: 'common' }) }</h2>
35
35
 
36
36
  <form onSubmit={ handleSubmit(onSubmit) }>
37
37
  <Dates>
38
38
  <div className="row">
39
- { t('alternates_manage.from', { ns: 'common' }) }
39
+ { t('alternates.manage.from', { ns: 'common' }) }
40
40
 
41
41
  <Calendar
42
42
  type="picker"
@@ -52,7 +52,7 @@ const Schedule = ({ id, data, name, t }: Props): JSX.Element => {
52
52
  </div>
53
53
 
54
54
  <div className="row">
55
- { t('alternates_manage.to', { ns: 'common' }) }
55
+ { t('alternates.manage.to', { ns: 'common' }) }
56
56
 
57
57
  <Calendar
58
58
  type="picker"
package/index.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import Browse from './Browse';
2
2
  import Manage from './Manage';
3
+ import AlternatesBrowse from './Alternates/Browse';
3
4
  import LocationsManage from './Locations/Manage';
4
5
  import PricesManage from './Prices/Manage';
5
6
  import SchedulesManage from './Schedule/Manage';
@@ -10,6 +11,7 @@ import UnavailableManage from './Unavailable/Manage';
10
11
  export {
11
12
  Browse,
12
13
  Manage,
14
+ AlternatesBrowse,
13
15
  LocationsManage,
14
16
  PricesManage,
15
17
  SchedulesManage,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@autobusal/operator-routes",
3
- "version": "1.0.6",
3
+ "version": "1.0.7",
4
4
  "type": "module",
5
5
  "main": "index.ts"
6
6
  }