@autobusal/admin-agents 1.0.0

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.
package/Browse.tsx ADDED
@@ -0,0 +1,104 @@
1
+ import { TFunction } from 'i18next';
2
+ import { useNavigate, useSearchParams } from 'react-router-dom';
3
+ import { Meta, ContactInfo, Avatar, Table, Row } from '@autobusal/common';
4
+ import { usePage } from '@autobusal/hooks';
5
+ import { DisplayStatus, getParams, success } from '@autobusal/utilities';
6
+ import { GetAgents, DeleteAgent } from './services';
7
+
8
+ interface Props {
9
+ url: string
10
+ t: TFunction<'common'>
11
+ }
12
+
13
+ const Browse = ({ url, t }: Props): JSX.Element => {
14
+ const [ searchParams ] = useSearchParams();
15
+
16
+ usePage('agents');
17
+
18
+ const { data, isLoading, isFetching, refetch } = GetAgents(getParams(searchParams, ['page', 'sort', 'order', 'q']));
19
+
20
+ const { mutate: Delete } = DeleteAgent();
21
+
22
+ const navigate = useNavigate();
23
+
24
+ const onDelete = (id: number): void => {
25
+ Delete(id, {
26
+ onSuccess: () => {
27
+ success(t('admin_agents.manage.messages.deleted', { ns: 'common' }));
28
+
29
+ refetch();
30
+ }
31
+ });
32
+ };
33
+
34
+ const onLoginAs = (id: number): void => {
35
+ if (window.confirm(t('admin_agents.browse.confirm', { ns: 'common' }))) {
36
+ const find = data?.data.find(item => item.id === id);
37
+ navigate(`/account/login_as/${ find?.user.id }`);
38
+ }
39
+ };
40
+
41
+ const rows = data?.data.map(item => (
42
+ <Row
43
+ key={ item.id }
44
+ id={ item.id }
45
+ data={ [
46
+ <Avatar key={ item.id } display={ item } />,
47
+ item.nipt,
48
+ item.user.email,
49
+ <ContactInfo key={ item.id } item={ item } t={ t } />,
50
+ DisplayStatus(item.user.status, t)
51
+ ] }
52
+ />
53
+ ));
54
+
55
+ return (
56
+ <Meta title={ t('admin_agents.browse.title', { ns: 'common' }) }>
57
+ <h1>{ t('admin_agents.browse.title', { ns: 'common' }) }</h1>
58
+
59
+ <Table
60
+ url={ url }
61
+ columns={ [{
62
+ name: t('admin_agents.browse.company', { ns: 'common' }),
63
+ width: 19,
64
+ slug: 'company',
65
+ type: 'az'
66
+ }, {
67
+ name: t('admin_agents.browse.nipt', { ns: 'common' }),
68
+ width: 11,
69
+ slug: 'nipt',
70
+ type: 'az'
71
+ }, {
72
+ name: t('admin_agents.browse.email', { ns: 'common' }),
73
+ width: 28
74
+ }, {
75
+ name: t('admin_agents.browse.info', { ns: 'common' }),
76
+ slug: 'funds',
77
+ width: 20
78
+ }, {
79
+ name: t('admin_agents.browse.status', { ns: 'common' }),
80
+ width: 12
81
+ }] }
82
+ rows={ rows }
83
+ pages={ data }
84
+ sorting={ {
85
+ slug: 'company',
86
+ order: 'asc'
87
+ } }
88
+ search={ searchParams.get('q') }
89
+ loading={ isLoading }
90
+ fetching={ isFetching }
91
+ t={ t }
92
+ actions={ [
93
+ 'create', 'search', 'view', 'delete', 'login_as'
94
+ ] }
95
+ handlers={ {
96
+ delete: onDelete,
97
+ login_as: onLoginAs
98
+ } }
99
+ />
100
+ </Meta>
101
+ );
102
+ };
103
+
104
+ export default Browse;
package/Manage.tsx ADDED
@@ -0,0 +1,255 @@
1
+ import { TFunction } from 'i18next';
2
+ import { useParams, useNavigate, useSearchParams } from 'react-router-dom';
3
+ import { Meta, BackWithTitle, Viewer, IpLogs, ApiDetails } from '@autobusal/common';
4
+ import { usePage, useSubscriptions, useOperators } from '@autobusal/hooks';
5
+ import { GetStatus, getAllowed, success } from '@autobusal/utilities';
6
+ import { AgentData } from '@autobusal/providers/types/agents';
7
+ import { GetAgent, PostAgent, DeleteAgent } 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 [ searchParams ] = useSearchParams();
18
+
19
+ const id = Number(params.id);
20
+
21
+ usePage('agents');
22
+
23
+ const navigate = useNavigate();
24
+
25
+ const subscriptions = useSubscriptions('agent');
26
+ const operators = useOperators();
27
+
28
+ const { data } = GetAgent(id);
29
+
30
+ const { mutate: Update, isPending: isPendingSave } = PostAgent(id);
31
+
32
+ const { mutate: Delete, isPending: isPendingDelete } = DeleteAgent();
33
+
34
+ const onSave = (data: AgentData): void => {
35
+ Update(data, {
36
+ onSuccess: () => {
37
+ success(t('admin_agents.manage.messages.saved', { ns: 'common' }));
38
+
39
+ navigate(url);
40
+ }
41
+ });
42
+ };
43
+
44
+ const onDelete = (): void => {
45
+ Delete(id, {
46
+ onSuccess: () => {
47
+ success(t('admin_agents.manage.messages.deleted', { ns: 'common' }));
48
+
49
+ navigate(url);
50
+ }
51
+ });
52
+ };
53
+
54
+ const title = id > 0 ? t('admin_agents.manage.title.update', { company: data?.company, ns: 'common' }) : t('admin_agents.manage.title.new', { ns: 'common' });
55
+
56
+ return (
57
+ <Meta title={ title }>
58
+ <BackWithTitle title={ title } to={ url } t={ t } />
59
+
60
+ <Viewer
61
+ id={ id }
62
+ data={ [{
63
+ label: t('admin_agents.manage.company', { ns: 'common' }),
64
+ name: 'company',
65
+ type: 'text',
66
+ value: data?.company,
67
+ rules: 'required|min_length:4|max_length:255'
68
+ }, {
69
+ label: t('admin_agents.manage.nipt', { ns: 'common' }),
70
+ name: 'nipt',
71
+ type: 'text',
72
+ value: data?.nipt,
73
+ rules: 'required|min_length:1|max_length:50'
74
+ }, {}, {
75
+ label: t('admin_agents.manage.first_name', { ns: 'common' }),
76
+ name: 'first_name',
77
+ type: 'text',
78
+ value: data?.first_name,
79
+ rules: 'required|min_length:3|max_length:75'
80
+ }, {
81
+ label: t('admin_agents.manage.last_name', { ns: 'common' }),
82
+ name: 'last_name',
83
+ type: 'text',
84
+ value: data?.last_name,
85
+ rules: 'required|min_length:3|max_length:75'
86
+ }, {}, {
87
+ label: t('admin_agents.manage.address', { ns: 'common' }),
88
+ name: 'address',
89
+ type: 'textarea',
90
+ value: data?.address,
91
+ rules: 'required|min_length:5'
92
+ }, {
93
+ label: t('admin_agents.manage.city', { ns: 'common' }),
94
+ name: 'city',
95
+ type: 'text',
96
+ value: data?.city,
97
+ rules: 'required|min_length:2|max_length:40'
98
+ }, {}, {
99
+ label: t('admin_agents.manage.mobile', { ns: 'common' }),
100
+ name: 'mobile',
101
+ type: 'text',
102
+ value: data?.mobile,
103
+ rules: 'required|min_length:10|max_length:20'
104
+ }, {
105
+ label: t('admin_agents.manage.mobile_2', { ns: 'common' }),
106
+ name: 'mobile2',
107
+ type: 'text',
108
+ value: data?.mobile2
109
+ }, {}, {
110
+ label: t('admin_agents.manage.phone', { ns: 'common' }),
111
+ name: 'phone',
112
+ type: 'text',
113
+ value: data?.phone
114
+ }, {
115
+ label: t('admin_agents.manage.fax', { ns: 'common' }),
116
+ name: 'fax',
117
+ type: 'text',
118
+ value: data?.fax
119
+ }, {}, {
120
+ label: t('admin_agents.manage.email', { ns: 'common' }),
121
+ name: 'email',
122
+ type: 'email'
123
+ }, {
124
+ label: t('admin_agents.manage.password', { ns: 'common' }),
125
+ name: 'password',
126
+ type: 'password'
127
+ }, {}, {
128
+ label: t('admin_agents.manage.description', { ns: 'common' }),
129
+ name: 'description',
130
+ type: 'textarea',
131
+ value: data?.description
132
+ }, {
133
+ label: t('admin_agents.manage.website', { ns: 'common' }),
134
+ name: 'website',
135
+ type: 'text',
136
+ value: data?.website
137
+ }, {
138
+ label: t('admin_agents.manage.logo', { ns: 'common' }),
139
+ name: 'logo',
140
+ type: 'file',
141
+ value: data?.logo_url
142
+ }, {
143
+ label: t('admin_agents.manage.license', { ns: 'common' }),
144
+ name: 'license',
145
+ type: 'text',
146
+ value: data?.license,
147
+ rules: 'required|min_length:3|max_length:50'
148
+ }, {
149
+ label: t('admin_agents.manage.subscription', { ns: 'common' }),
150
+ name: 'subscription_id',
151
+ type: 'select',
152
+ value: data?.user.subscription?.subscription.id,
153
+ values: subscriptions,
154
+ rules: 'required',
155
+ notice: t('admin_agents.manage.subscription_notice', { ns: 'common' })
156
+ }, {
157
+ label: t('admin_agents.manage.operators', { ns: 'common' }),
158
+ name: 'operators',
159
+ type: 'checkbox-list-all',
160
+ selected: data?.operators,
161
+ values: operators
162
+ }, {
163
+ label: t('admin_agents.manage.departure', { ns: 'common' }),
164
+ name: 'departure',
165
+ type: 'text',
166
+ value: data?.user.comissions?.departure_edit
167
+ }, {
168
+ label: t('admin_agents.manage.departure_self', { ns: 'common' }),
169
+ name: 'departure_self',
170
+ type: 'text',
171
+ value: data?.user.comissions?.departure_self_edit
172
+ }, {}, {
173
+ label: t('admin_agents.manage.return', { ns: 'common' }),
174
+ name: 'return',
175
+ type: 'text',
176
+ value: data?.user.comissions?.return_edit
177
+ }, {
178
+ label: t('admin_agents.manage.return_self', { ns: 'common' }),
179
+ name: 'return_self',
180
+ type: 'text',
181
+ value: data?.user.comissions?.return_self_edit
182
+ }, {}, {
183
+ label: t('admin_agents.manage.unique', { ns: 'common' }),
184
+ name: 'unique',
185
+ type: 'select',
186
+ value: data?.user.comissions?.unique,
187
+ values: getAllowed(t)
188
+ }, {
189
+ label: t('admin_agents.manage.api', { ns: 'common' }),
190
+ name: 'api',
191
+ type: 'select',
192
+ value: data?.user.api?.api,
193
+ values: getAllowed(t)
194
+ }, {
195
+ label: t('admin_agents.manage.bkt', { ns: 'common' }),
196
+ name: 'bkt',
197
+ type: 'select',
198
+ value: data?.user.api?.bkt,
199
+ values: getAllowed(t)
200
+ }, {
201
+ label: t('admin_agents.manage.devpos', { ns: 'common' }),
202
+ name: 'devpos',
203
+ type: 'select',
204
+ value: data?.user.api?.devpos,
205
+ values: getAllowed(t)
206
+ }, {
207
+ label: t('admin_agents.manage.external', { ns: 'common' }),
208
+ name: 'external',
209
+ type: 'select',
210
+ value: data?.user.comissions?.external,
211
+ values: getAllowed(t)
212
+ }, {
213
+ label: t('admin_agents.manage.devpos_settings', { ns: 'common' }),
214
+ name: 'devpos_settings',
215
+ type: 'component',
216
+ value: (
217
+ <ApiDetails id={ data?.user.id } type="devpos" t={ t } />
218
+ )
219
+ }, {}, {
220
+ label: t('admin_agents.manage.status', { ns: 'common' }),
221
+ name: 'status',
222
+ type: 'select',
223
+ value: data?.user.status,
224
+ values: GetStatus(t)
225
+ }, {
226
+ label: t('admin_agents.manage.display_email', { ns: 'common' }),
227
+ name: 'display_email',
228
+ type: 'display',
229
+ value: (data?.user.email ?? '-')
230
+ }, {}, {
231
+ label: t('admin_agents.manage.ip_logs', { ns: 'common' }),
232
+ name: 'ip_logs',
233
+ type: 'component',
234
+ value: (
235
+ <IpLogs
236
+ id={ id }
237
+ page={ Number(searchParams.get('page')) }
238
+ type="agents"
239
+ t={ t }
240
+ />
241
+ )
242
+ }] }
243
+ pending={ isPendingSave || isPendingDelete }
244
+ t={ t }
245
+ actions={ [
246
+ 'save', 'delete'
247
+ ]}
248
+ onSave={ onSave }
249
+ onDelete={ onDelete }
250
+ />
251
+ </Meta>
252
+ );
253
+ };
254
+
255
+ export default Manage;
package/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ import Browse from './Browse';
2
+ import Manage from './Manage';
3
+
4
+ export {
5
+ Browse,
6
+ Manage
7
+ };
package/package.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "name": "@autobusal/admin-agents",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "main": "index.ts"
6
+ }
package/services.ts ADDED
@@ -0,0 +1,88 @@
1
+ import { UseMutationResult, UseQueryResult, UseSuspenseQueryResult, useMutation, useQuery, useSuspenseQuery } from '@tanstack/react-query';
2
+ import { apiClient } from '@autobusal/providers';
3
+ import { PaginationData } from '@autobusal/providers/types/pagination';
4
+ import { AgentData } from '@autobusal/providers/types/agents';
5
+ import { TableParamsData } from '@autobusal/providers/types/other';
6
+
7
+ export const GetAgents = (params: TableParamsData): UseQueryResult<PaginationData<AgentData>> => (
8
+ useQuery({
9
+ queryKey: ['admin-agents', params],
10
+ queryFn: async () => (
11
+ await apiClient
12
+ .get('/api/agents/admin/browse', { params })
13
+ .then(response => (
14
+ response.data
15
+ ))
16
+ )
17
+ })
18
+ );
19
+
20
+ export const GetAgent = (id: number): UseSuspenseQueryResult<AgentData> => (
21
+ useSuspenseQuery({
22
+ queryKey: ['admin-agents-manage', { id }],
23
+ queryFn: async () => {
24
+ if (id === 0) {
25
+ return null;
26
+ }
27
+
28
+ return await apiClient
29
+ .get('/api/agents/admin/get', {
30
+ params: { id }
31
+ })
32
+ .then(response => (
33
+ response.data
34
+ ))
35
+ }
36
+ })
37
+ );
38
+
39
+ export const PostAgent = (id: number): UseMutationResult<void, Error, AgentData, unknown> => (
40
+ useMutation({
41
+ mutationKey: ['admin-agents-save', { id }],
42
+ mutationFn: async (data: AgentData) => {
43
+ const formData = new FormData();
44
+
45
+ if (id > 0) {
46
+ formData.append('id', String(id));
47
+ }
48
+
49
+ Object.keys(data).map(name => {
50
+ if (name === 'logo') {
51
+ if (data.logo !== undefined) {
52
+ formData.append('logo', data.logo[0] ?? '');
53
+ }
54
+ } else if (name === 'operators') {
55
+ formData.append('operators', JSON.stringify(data.operators));
56
+ } else {
57
+ const value = String(data[name as keyof typeof data]);
58
+ formData.append(name, value);
59
+ }
60
+ });
61
+
62
+ return await apiClient
63
+ .post('/api/agents/admin/update', formData, {
64
+ headers: {
65
+ 'Content-Type': 'multipart/form-data'
66
+ }
67
+ })
68
+ .then(response => (
69
+ response.data
70
+ ));
71
+ }
72
+ })
73
+ );
74
+
75
+ export const DeleteAgent = (): UseMutationResult<void, Error, number, unknown> => (
76
+ useMutation({
77
+ mutationKey: ['admin-agents-delete'],
78
+ mutationFn: async (id: number) => (
79
+ await apiClient
80
+ .delete('/api/agents/admin/delete', {
81
+ params: { id }
82
+ })
83
+ .then(response => (
84
+ response.data
85
+ ))
86
+ )
87
+ })
88
+ );