@@ -2,14 +2,22 @@ import { getAuditLogs, getAuditLogsCount } from "api/api"
2
2
import { getErrorMessage } from "api/errors"
3
3
import { AuditLog } from "api/typesGenerated"
4
4
import { displayError } from "components/GlobalSnackbar/utils"
5
- import { assign , createMachine } from "xstate"
5
+ import { getPaginationData } from "components/PaginationWidget/utils"
6
+ import {
7
+ PaginationContext ,
8
+ PaginationMachineRef ,
9
+ paginationMachine ,
10
+ } from "xServices/pagination/paginationXService"
11
+ import { assign , createMachine , spawn , send } from "xstate"
12
+
13
+ const auditPaginationId = "auditPagination"
6
14
7
15
interface AuditContext {
8
16
auditLogs ?: AuditLog [ ]
9
17
count ?: number
10
- page : number
11
- limit : number
12
18
filter : string
19
+ paginationContext : PaginationContext
20
+ paginationRef ?: PaginationMachineRef
13
21
}
14
22
15
23
export const auditMachine = createMachine (
@@ -29,22 +37,20 @@ export const auditMachine = createMachine(
29
37
} ,
30
38
events : { } as
31
39
| {
32
- type : "NEXT"
33
- }
34
- | {
35
- type : "PREVIOUS"
36
- }
37
- | {
38
- type : "GO_TO_PAGE"
39
- page : number
40
+ type : "UPDATE_PAGE"
41
+ page : string
40
42
}
41
43
| {
42
44
type : "FILTER"
43
45
filter : string
44
46
} ,
45
47
} ,
46
- initial : "loading " ,
48
+ initial : "startPagination " ,
47
49
states : {
50
+ startPagination : {
51
+ entry : "assignPaginationRef" ,
52
+ always : "loading" ,
53
+ } ,
48
54
loading : {
49
55
// Right now, XState doesn't a good job with state + context typing so
50
56
// this forces the AuditPageView to showing the loading state when the
@@ -65,21 +71,12 @@ export const auditMachine = createMachine(
65
71
} ,
66
72
success : {
67
73
on : {
68
- NEXT : {
69
- actions : [ "assignNextPage" , "onPageChange" ] ,
70
- target : "loading" ,
71
- } ,
72
- PREVIOUS : {
73
- actions : [ "assignPreviousPage" , "onPageChange" ] ,
74
- target : "loading" ,
75
- } ,
76
- GO_TO_PAGE : {
77
- actions : [ "assignPage" , "onPageChange" ] ,
74
+ UPDATE_PAGE : {
75
+ actions : [ "updateURL" ] ,
78
76
target : "loading" ,
79
77
} ,
80
78
FILTER : {
81
- actions : [ "assignFilter" ] ,
82
- target : "loading" ,
79
+ actions : [ "assignFilter" , "sendResetPage" ] ,
83
80
} ,
84
81
} ,
85
82
} ,
@@ -97,14 +94,12 @@ export const auditMachine = createMachine(
97
94
auditLogs : ( _ , event ) => event . data . auditLogs ,
98
95
count : ( _ , event ) => event . data . count ,
99
96
} ) ,
100
- assignNextPage : assign ( {
101
- page : ( { page } ) => page + 1 ,
102
- } ) ,
103
- assignPreviousPage : assign ( {
104
- page : ( { page } ) => page - 1 ,
105
- } ) ,
106
- assignPage : assign ( {
107
- page : ( _ , { page } ) => page ,
97
+ assignPaginationRef : assign ( {
98
+ paginationRef : ( context ) =>
99
+ spawn (
100
+ paginationMachine . withContext ( context . paginationContext ) ,
101
+ auditPaginationId ,
102
+ ) ,
108
103
} ) ,
109
104
assignFilter : assign ( {
110
105
filter : ( _ , { filter } ) => filter ,
@@ -116,24 +111,29 @@ export const auditMachine = createMachine(
116
111
)
117
112
displayError ( message )
118
113
} ,
114
+ sendResetPage : send ( { type : "RESET_PAGE" } , { to : auditPaginationId } ) ,
119
115
} ,
120
116
services : {
121
- loadAuditLogsAndCount : async ( { page, limit, filter } , _ ) => {
122
- const [ auditLogs , count ] = await Promise . all ( [
123
- getAuditLogs ( {
124
- // The page in the API starts at 0
125
- offset : ( page - 1 ) * limit ,
126
- limit,
127
- q : filter ,
128
- } ) . then ( ( data ) => data . audit_logs ) ,
129
- getAuditLogsCount ( {
130
- q : filter ,
131
- } ) . then ( ( data ) => data . count ) ,
132
- ] )
117
+ loadAuditLogsAndCount : async ( context ) => {
118
+ if ( context . paginationRef ) {
119
+ const { offset, limit } = getPaginationData ( context . paginationRef )
120
+ const [ auditLogs , count ] = await Promise . all ( [
121
+ getAuditLogs ( {
122
+ offset,
123
+ limit,
124
+ q : context . filter ,
125
+ } ) . then ( ( data ) => data . audit_logs ) ,
126
+ getAuditLogsCount ( {
127
+ q : context . filter ,
128
+ } ) . then ( ( data ) => data . count ) ,
129
+ ] )
133
130
134
- return {
135
- auditLogs,
136
- count,
131
+ return {
132
+ auditLogs,
133
+ count,
134
+ }
135
+ } else {
136
+ throw new Error ( "Cannot get audit logs without pagination data" )
137
137
}
138
138
} ,
139
139
} ,
0 commit comments