@@ -2,41 +2,25 @@ import Component from '@glimmer/component';
22import { inject as service } from '@ember/service' ;
33import { tracked } from '@glimmer/tracking' ;
44import { action } from '@ember/object' ;
5- import { none } from '@ ember/object/computed ' ;
5+ import { task } from 'ember-concurrency ' ;
66import copyToClipboard from '@fleetbase/ember-core/utils/copy-to-clipboard' ;
77
8+ function filterParams ( obj ) {
9+ // eslint-disable-next-line no-unused-vars
10+ return Object . fromEntries ( Object . entries ( obj ) . filter ( ( [ _ , value ] ) => value !== null && value !== undefined ) ) ;
11+ }
12+
813export default class WebhookAttemptsComponent extends Component {
914 @service store ;
1015 @service intl ;
1116 @service hostRouter ;
12-
13- /**
14- * The current viewing webhook status
15- *
16- * @var {String}
17- */
17+ @service notifications ;
18+ @service urlSearchParams ;
1819 @tracked attemptStatus = null ;
19-
20- /**
21- * The webhook request logs for this endpoint.
22- *
23- * @var {String}
24- */
2520 @tracked webhookRequestLogs = [ ] ;
26-
27- /**
28- * The loading state for webhook request logs.
29- *
30- * @var {Boolean}
31- */
32- @tracked isLoading = false ;
33-
34- /**
35- * If not attempt status is set
36- *
37- * @var {Boolean}
38- */
39- @none ( 'attemptStatus' ) noAttemptStatus ;
21+ @tracked webhook ;
22+ @tracked page = 1 ;
23+ @tracked date = null ;
4024
4125 /**
4226 * All columns applicable for orders
@@ -89,29 +73,78 @@ export default class WebhookAttemptsComponent extends Component {
8973 * Creates an instance of WebhookAttemptsComponent.
9074 * @memberof WebhookAttemptsComponent
9175 */
92- constructor ( ) {
76+ constructor ( owner , { webhook } ) {
9377 super ( ...arguments ) ;
94- this . loadWebhookRequestLogs ( ) ;
78+ this . webhook = webhook ;
79+ this . restoreParams ( ) ;
80+ this . getWebhookRequestLogs . perform ( ) ;
81+ }
82+
83+ restoreParams ( ) {
84+ if ( this . urlSearchParams . has ( 'page' ) ) {
85+ this . page = new Number ( this . urlSearchParams . get ( 'page' ) ) ;
86+ }
87+
88+ if ( this . urlSearchParams . has ( 'status' ) ) {
89+ this . status = this . urlSearchParams . get ( 'status' ) ;
90+ }
91+
92+ if ( this . urlSearchParams . has ( 'date' ) ) {
93+ this . status = this . urlSearchParams . get ( 'date' ) ;
94+ }
95+ }
96+
97+ /**
98+ * Load webhook request logs.
99+ *
100+ * @param {Object } [params={}]
101+ * @param {Object } [options={}]
102+ * @memberof WebhookAttemptsComponent
103+ */
104+ @task * getWebhookRequestLogs ( params = { } , options = { } ) {
105+ params = filterParams ( { ...params , created_at : this . date , status : this . attemptStatus , page : this . page } ) ;
106+
107+ try {
108+ this . webhookRequestLogs = yield this . store . query ( 'webhook-request-log' , { limit : 12 , sort : '-created_at' , webhook_uuid : this . webhook . id , page : this . page , ...params } , options ) ;
109+ } catch ( error ) {
110+ this . notifications . serverError ( error ) ;
111+ }
95112 }
96113
97114 /**
98- * Load webhook request logs for this webhook
115+ * Filter webhook attempt logs by date.
99116 *
117+ * @param {Object } { formattedDate }
100118 * @memberof WebhookAttemptsComponent
101119 */
102- @action loadWebhookRequestLogs ( params = { } , options = { } ) {
103- const { webhook } = this . args ;
104-
105- this . isLoading = true ;
106-
107- return this . store
108- . query ( 'webhook-request-log' , { limit : - 1 , webhook_uuid : webhook . id , ...params } , options )
109- . then ( ( webhookRequestLogs ) => {
110- this . webhookRequestLogs = webhookRequestLogs ;
111- } )
112- . finally ( ( ) => {
113- this . isLoading = false ;
114- } ) ;
120+ @action filterByDate ( { formattedDate } ) {
121+ this . date = formattedDate ;
122+ this . urlSearchParams . addParamToCurrentUrl ( 'date' , formattedDate ) ;
123+ this . getWebhookRequestLogs . perform ( ) ;
124+ }
125+
126+ /**
127+ * Clear date filter.
128+ *
129+ * @memberof WebhookAttemptsComponent
130+ */
131+ @action clearDate ( ) {
132+ this . date = null ;
133+ this . urlSearchParams . removeParamFromCurrentUrl ( 'date' ) ;
134+ this . getWebhookRequestLogs . perform ( ) ;
135+ }
136+
137+ /**
138+ * Handle page change.
139+ *
140+ * @param {number } [page=1]
141+ * @memberof WebhookAttemptsComponent
142+ * @void
143+ */
144+ @action changePage ( page = 1 ) {
145+ this . page = page ;
146+ this . urlSearchParams . addParamToCurrentUrl ( 'page' , page ) ;
147+ this . getWebhookRequestLogs . perform ( ) ;
115148 }
116149
117150 /**
@@ -136,10 +169,12 @@ export default class WebhookAttemptsComponent extends Component {
136169 status = typeof status === 'string' ? status : null ;
137170
138171 this . attemptStatus = status ;
139-
140- if ( status ) {
141- this . loadWebhookRequestLogs ( { status } ) ;
172+ if ( status === null ) {
173+ this . urlSearchParams . removeParamFromCurrentUrl ( 'status' ) ;
174+ } else {
175+ this . urlSearchParams . addParamToCurrentUrl ( 'status' , status ) ;
142176 }
177+ this . getWebhookRequestLogs . perform ( ) ;
143178 }
144179
145180 /**
0 commit comments