@autobusal/account-transactions 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.
@@ -0,0 +1,95 @@
1
+ import { useSearchParams } from 'react-router-dom';
2
+ import { TFunction } from 'i18next';
3
+ import { Meta, Table, Row } from '@autobusal/common';
4
+ import { usePage } from '@autobusal/hooks';
5
+ import { getParams } from '@autobusal/utilities';
6
+ import Information from '../Information';
7
+ import { GetTransactions } from '../services';
8
+
9
+ interface Props {
10
+ url: string
11
+ t: TFunction<'common'>
12
+ }
13
+
14
+ const Admin = ({ url, t }: Props): JSX.Element => {
15
+ const [ searchParams ] = useSearchParams();
16
+
17
+ usePage('transactions');
18
+
19
+ const { data, isLoading, isFetching } = GetTransactions(getParams(searchParams, ['page', 'sort', 'order', 'q', 'id']));
20
+
21
+ const rows = data?.data.map(item => (
22
+ <Row
23
+ key={ item.id }
24
+ id={ item.id }
25
+ data={ [
26
+ <Information key={ item.id } item={ item } t={ t } />,
27
+ item.from?.name ?? t('data.user_types.system', { ns: 'common' }),
28
+ item.to?.name ?? t('data.user_types.system', { ns: 'common' }),
29
+ t(`data.transaction_types.${ item.type }`, { ns: 'common' }),
30
+ item.amount_display,
31
+ t(`data.transaction_status.${ item.status }`, { ns: 'common' }),
32
+ item.updated_at
33
+ ] }
34
+ />
35
+ ));
36
+
37
+ return (
38
+ <Meta title={ t('transactions.browse.title', { ns: 'common' }) }>
39
+ <h1>{ t('transactions.browse.title', { ns: 'common' }) }</h1>
40
+
41
+ <Table
42
+ url={ url }
43
+ columns={ [{
44
+ name: t('transactions.browse.information', { ns: 'common' }),
45
+ width: 21,
46
+ slug: 'public',
47
+ type: 'az'
48
+ }, {
49
+ name: t('transactions.browse.from', { ns: 'common' }),
50
+ width: 15,
51
+ slug: 'from',
52
+ type: 'az'
53
+ }, {
54
+ name: t('transactions.browse.to', { ns: 'common' }),
55
+ width: 15,
56
+ slug: 'to',
57
+ type:'az',
58
+ }, {
59
+ name: t('transactions.browse.type', { ns: 'common' }),
60
+ width: 10,
61
+ slug: 'type',
62
+ type: 'az'
63
+ }, {
64
+ name: t('transactions.browse.amount', { ns: 'common' }),
65
+ width: 9,
66
+ slug: 'amount'
67
+ }, {
68
+ name: t('transactions.browse.status', { ns: 'common' }),
69
+ width: 8,
70
+ slug: 'status',
71
+ type: 'az'
72
+ }, {
73
+ name: t('transactions.browse.date', { ns: 'common' }),
74
+ slug: 'updated_at',
75
+ width: 12
76
+ }] }
77
+ rows={ rows }
78
+ pages={ data }
79
+ sorting={ {
80
+ slug: 'updated_at',
81
+ order: 'desc'
82
+ } }
83
+ search={ searchParams.get('q') }
84
+ loading={ isLoading }
85
+ fetching={ isFetching }
86
+ t={ t }
87
+ actions={ [
88
+ 'search', 'update'
89
+ ] }
90
+ />
91
+ </Meta>
92
+ );
93
+ };
94
+
95
+ export default Admin;
@@ -0,0 +1,19 @@
1
+ import { TFunction } from 'i18next';
2
+ import Admin from './Admin';
3
+ import Simple from './Simple';
4
+
5
+ interface Props {
6
+ type?: string
7
+ url: string
8
+ t: TFunction<'common'>
9
+ }
10
+
11
+ const Browse = ({ type, url, t }: Props): JSX.Element => {
12
+ if (type === 'admin' || type === 'financer') {
13
+ return <Admin url={ url } t={ t } />;
14
+ }
15
+
16
+ return <Simple url={ url } t={ t } />;
17
+ };
18
+
19
+ export default Browse;
@@ -0,0 +1,89 @@
1
+ import { useSearchParams } from 'react-router-dom';
2
+ import { TFunction } from 'i18next';
3
+ import { Meta, Table, Row } from '@autobusal/common';
4
+ import { usePage } from '@autobusal/hooks';
5
+ import { getParams } from '@autobusal/utilities';
6
+ import Information from '../Information';
7
+ import { GetTransactions } from '../services';
8
+
9
+ interface Props {
10
+ url: string
11
+ t: TFunction<'common'>
12
+ }
13
+
14
+ const Simple = ({ url, t }: Props): JSX.Element => {
15
+ const [ searchParams ] = useSearchParams();
16
+
17
+ usePage('transactions');
18
+
19
+ const { data, isLoading, isFetching } = GetTransactions(getParams(searchParams, ['page', 'sort', 'order', 'q']));
20
+
21
+ const rows = data?.data.map(item => (
22
+ <Row
23
+ key={ item.id }
24
+ id={ item.id }
25
+ data={ [
26
+ <Information key={ item.id } item={ item } t={ t } />,
27
+ item.from?.name ?? t('data.user_types.system', { ns: 'common' }),
28
+ t(`data.transaction_types.${ item.type }`, { ns: 'common' }),
29
+ item.amount_display,
30
+ t(`data.transaction_status.${ item.status }`, { ns: 'common' }),
31
+ item.updated_at
32
+ ] }
33
+ />
34
+ ));
35
+
36
+ return (
37
+ <Meta title={ t('transactions.browse.title', { ns: 'common' }) }>
38
+ <h1>{ t('transactions.browse.title', { ns: 'common' }) }</h1>
39
+
40
+ <Table
41
+ url={ url }
42
+ columns={ [{
43
+ name: t('transactions.browse.information', { ns: 'common' }),
44
+ width: 30,
45
+ slug: 'public',
46
+ type: 'az'
47
+ }, {
48
+ name: t('transactions.browse.from', { ns: 'common' }),
49
+ width: 15,
50
+ slug: 'from',
51
+ type: 'az'
52
+ }, {
53
+ name: t('transactions.browse.type', { ns: 'common' }),
54
+ width: 10,
55
+ slug: 'type',
56
+ type: 'az'
57
+ }, {
58
+ name: t('transactions.browse.amount', { ns: 'common' }),
59
+ width: 10,
60
+ slug: 'amount'
61
+ }, {
62
+ name: t('transactions.browse.status', { ns: 'common' }),
63
+ width: 13,
64
+ slug: 'status',
65
+ type: 'az'
66
+ }, {
67
+ name: t('transactions.browse.date', { ns: 'common' }),
68
+ slug: 'updated_at',
69
+ width: 12
70
+ }] }
71
+ rows={ rows }
72
+ pages={ data }
73
+ sorting={ {
74
+ slug: 'updated_at',
75
+ order: 'desc'
76
+ } }
77
+ search={ searchParams.get('q') }
78
+ loading={ isLoading }
79
+ fetching={ isFetching }
80
+ t={ t }
81
+ actions={ [
82
+ 'search', 'view'
83
+ ] }
84
+ />
85
+ </Meta>
86
+ );
87
+ };
88
+
89
+ export default Simple;
@@ -0,0 +1,22 @@
1
+ import { TFunction } from 'i18next';
2
+ import { getText } from './utilities';
3
+ import { TransactionData } from '@autobusal/providers/types/account';
4
+
5
+ interface Props {
6
+ item: TransactionData
7
+ t: TFunction<'common'>
8
+ }
9
+
10
+ const Information = ({ item, t }: Props): JSX.Element => (
11
+ <>
12
+ { getText(item.public_display, t) }
13
+
14
+ { item.private && (
15
+ <div className="row-data">
16
+ <strong>{ t('transactions.browse.private', { ns: 'common' }) }</strong>: { item.private }
17
+ </div>
18
+ ) }
19
+ </>
20
+ );
21
+
22
+ export default Information;
@@ -0,0 +1,113 @@
1
+ import { TFunction } from 'i18next';
2
+ import { useNavigate, useParams, Navigate } 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 { getText, getStatuses } from '../utilities';
7
+ import { TransactionData } from '@autobusal/providers/types/account';
8
+ import { GetTransaction, PostTransaction } from '../services';
9
+
10
+ interface Props {
11
+ url: string
12
+ t: TFunction<'common'>
13
+ }
14
+
15
+ const Admin = ({ url, t }: Props): JSX.Element => {
16
+ const params = useParams();
17
+
18
+ const id = Number(params.id);
19
+
20
+ usePage('transactions');
21
+
22
+ const navigate = useNavigate();
23
+
24
+ const { data } = GetTransaction(id);
25
+
26
+ const { mutate: Update, isPending } = PostTransaction(id);
27
+
28
+ if (id === 0) {
29
+ return <Navigate to={ url } />;
30
+ }
31
+
32
+ const onSave = (data: TransactionData): void => {
33
+ Update(data, {
34
+ onSuccess: () => {
35
+ success(t('transactions.manage.messages.saved', { ns: 'common' }));
36
+
37
+ navigate(url);
38
+ }
39
+ });
40
+ };
41
+
42
+ const title = t('transactions.manage.title', { id: data?.id, ns: 'common' });
43
+
44
+ return (
45
+ <Meta title={ title }>
46
+ <BackWithTitle title={ title } to={ url } t={ t } />
47
+
48
+ <Viewer
49
+ id={ id }
50
+ data={ [{
51
+ label: t('transactions.manage.information', { ns: 'common' }),
52
+ type: 'display',
53
+ name: 'public',
54
+ value: getText((data?.public_display ?? []), t)
55
+ }, {}, {
56
+ label: t('transactions.manage.from', { ns: 'common' }),
57
+ type: 'display',
58
+ name: 'from',
59
+ value: data?.from?.name ?? t('data.user_types.system', { ns: 'common' }),
60
+ }, {
61
+ label: t('transactions.manage.to', { ns: 'common' }),
62
+ type: 'display',
63
+ name: 'to',
64
+ value: data?.to?.name ?? t('data.user_types.system', { ns: 'common' }),
65
+ }, {}, {
66
+ label: t('transactions.manage.amount', { ns: 'common' }),
67
+ type: 'display',
68
+ name: 'amount',
69
+ value: data?.amount_display
70
+ }, {
71
+ label: t('transactions.manage.paid', { ns: 'common' }),
72
+ type: 'display',
73
+ name: 'paid_date',
74
+ value: data?.paid_date ?? '-'
75
+ }, {}, {
76
+ label: t('transactions.manage.type', { ns: 'common' }),
77
+ type: 'display',
78
+ name: 'type',
79
+ value: t(`data.transaction_types.${ data?.type }`, { ns: 'common' })
80
+ }, {
81
+ label: t('transactions.manage.status', { ns: 'common' }),
82
+ name: 'status',
83
+ type: 'select',
84
+ value: data?.status,
85
+ values: getStatuses(t)
86
+ }, {
87
+ label: t('transactions.manage.date', { ns: 'common' }),
88
+ type: 'display',
89
+ name: 'updated_at',
90
+ value: data?.updated_at
91
+ }, {}, {
92
+ label: t('transactions.manage.private', { ns: 'common' }),
93
+ type: 'display',
94
+ name: 'private',
95
+ value: data?.private
96
+ }, {}, {
97
+ label: t('transactions.manage.comment', { ns: 'common' }),
98
+ name: 'comment',
99
+ type: 'textarea',
100
+ value: data?.comment
101
+ }] }
102
+ pending={ isPending }
103
+ t={ t }
104
+ actions={ [
105
+ 'update'
106
+ ]}
107
+ onSave={ onSave }
108
+ />
109
+ </Meta>
110
+ );
111
+ };
112
+
113
+ export default Admin;
@@ -0,0 +1,19 @@
1
+ import { TFunction } from 'i18next';
2
+ import Admin from './Admin';
3
+ import Simple from './Simple';
4
+
5
+ interface Props {
6
+ type?: string
7
+ url: string
8
+ t: TFunction<'common'>
9
+ }
10
+
11
+ const Manage = ({ type, url, t }: Props): JSX.Element => {
12
+ if (type === 'admin' || type === 'financer') {
13
+ return <Admin url={ url } t={ t } />;
14
+ }
15
+
16
+ return <Simple url={ url } t={ t } />
17
+ };
18
+
19
+ export default Manage;
@@ -0,0 +1,90 @@
1
+ import { TFunction } from 'i18next';
2
+ import { useNavigate, useParams, Navigate } 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 { getText } from '../utilities';
7
+ import { TransactionData } from '@autobusal/providers/types/account';
8
+ import { GetTransaction, PostTransaction } from '../services';
9
+
10
+ interface Props {
11
+ url: string
12
+ t: TFunction<'common'>
13
+ }
14
+
15
+ const Simple = ({ url, t }: Props): JSX.Element => {
16
+ const params = useParams();
17
+
18
+ const id = Number(params.id);
19
+
20
+ usePage('transactions');
21
+
22
+ const navigate = useNavigate();
23
+
24
+ const { data } = GetTransaction(id);
25
+
26
+ const { mutate: Update, isPending } = PostTransaction(id);
27
+
28
+ if (id === 0) {
29
+ return <Navigate to={ url } />;
30
+ }
31
+
32
+ const onSave = (data: TransactionData): void => {
33
+ Update(data, {
34
+ onSuccess: () => {
35
+ success(t('transactions.manage.messages.saved', { ns: 'common' }));
36
+
37
+ navigate(url);
38
+ }
39
+ });
40
+ };
41
+
42
+ const title = t('transactions.manage.title', { id: data?.id, ns: 'common' });
43
+
44
+ return (
45
+ <Meta title={ title }>
46
+ <BackWithTitle title={ title } to={ url } t={ t } />
47
+
48
+ <Viewer
49
+ id={ id }
50
+ data={ [{
51
+ label: t('transactions.manage.information', { ns: 'common' }),
52
+ type: 'display',
53
+ name: 'public',
54
+ value: getText((data?.public_display ?? []), t)
55
+ }, {
56
+ label: t('transactions.manage.from', { ns: 'common' }),
57
+ type: 'display',
58
+ name: 'from',
59
+ value: data?.from?.name ?? t('data.user_types.system', { ns: 'common' }),
60
+ }, {}, {
61
+ label: t('transactions.manage.amount', { ns: 'common' }),
62
+ type: 'display',
63
+ name: 'amount',
64
+ value: data?.amount_display
65
+ }, {
66
+ label: t('transactions.manage.paid', { ns: 'common' }),
67
+ type: 'display',
68
+ name: 'paid_date',
69
+ value: data?.paid_date ?? '-'
70
+ }, {}, {
71
+ label: t('transactions.manage.type', { ns: 'common' }),
72
+ type: 'display',
73
+ name: 'type',
74
+ value: t(`data.transaction_types.${ data?.type }`)
75
+ }, {
76
+ label: t('transactions.manage.status', { ns: 'common' }),
77
+ name: 'status',
78
+ type: 'display',
79
+ value: t(`data.transaction_status.${ data?.status }`)
80
+ }] }
81
+ pending={ isPending }
82
+ t={ t }
83
+ actions={ [] }
84
+ onSave={ onSave }
85
+ />
86
+ </Meta>
87
+ );
88
+ };
89
+
90
+ export default Simple;
package/index.ts ADDED
@@ -0,0 +1,7 @@
1
+ import Browse from './Browse/Browse';
2
+ import Manage from './Manage/Manage';
3
+
4
+ export {
5
+ Browse,
6
+ Manage
7
+ };
package/package.json ADDED
@@ -0,0 +1,6 @@
1
+ {
2
+ "name": "@autobusal/account-transactions",
3
+ "version": "1.0.0",
4
+ "type": "module",
5
+ "main": "index.ts"
6
+ }
package/services.ts ADDED
@@ -0,0 +1,49 @@
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 { TransactionData } from '@autobusal/providers/types/account';
5
+ import { TableParamsData } from '@autobusal/providers/types/other';
6
+
7
+ export const GetTransactions = (params: TableParamsData): UseQueryResult<PaginationData<TransactionData>> => (
8
+ useQuery({
9
+ queryKey: ['account-transactions', params],
10
+ queryFn: async () => (
11
+ await apiClient
12
+ .get('/api/transactions/account/browse', { params })
13
+ .then(response => (
14
+ response.data
15
+ ))
16
+ )
17
+ })
18
+ );
19
+
20
+ export const GetTransaction = (id: number): UseSuspenseQueryResult<TransactionData> => (
21
+ useSuspenseQuery({
22
+ queryKey: ['account-transactions-manage', { id }],
23
+ queryFn: async () => (
24
+ await apiClient
25
+ .get('/api/transactions/account/get', {
26
+ params: { id }
27
+ })
28
+ .then(response => (
29
+ response.data
30
+ ))
31
+ )
32
+ })
33
+ );
34
+
35
+ export const PostTransaction = (id: number): UseMutationResult<void, Error, TransactionData, unknown> => (
36
+ useMutation({
37
+ mutationKey: ['account-transactions-save', { id }],
38
+ mutationFn: async (data: TransactionData) => (
39
+ await apiClient
40
+ .post('/api/transactions/account/update', {
41
+ ...data,
42
+ id
43
+ })
44
+ .then(response => (
45
+ response.data
46
+ ))
47
+ )
48
+ })
49
+ );
package/utilities.ts ADDED
@@ -0,0 +1,20 @@
1
+ import { TFunction } from 'i18next';
2
+ import { DropdownData } from '@autobusal/providers/types/other';
3
+
4
+ export const getText = (information: string[], t: TFunction<'common'>): string => {
5
+ if (information.length > 1) {
6
+ return t(`data.transactions.${ information[0] }`, { id: information[1], ns: 'common' });
7
+ }
8
+
9
+ return information[0];
10
+ };
11
+
12
+ export const getStatuses = (t: TFunction<'common'>): DropdownData[] => ([
13
+ {
14
+ name: t('data.transaction_status.paid', { ns: 'common' }),
15
+ id: 'paid'
16
+ }, {
17
+ name: t('data.transaction_status.credit', { ns: 'common' }),
18
+ id: 'credit'
19
+ }
20
+ ]);