@@ -28,6 +28,8 @@ const filterStates = {
28
28
29
29
const updateFilterStates = ( key , value ) => {
30
30
filterStates [ key ] = value ;
31
+ const constructedQueryString = formURLQueryString ( filterStates , isDev ) ;
32
+ manipulateURLQueryParams ( constructedQueryString ) ;
31
33
} ;
32
34
33
35
async function getTaskRequests ( query = { } , nextLink ) {
@@ -119,6 +121,45 @@ backDrop.addEventListener('click', () => {
119
121
backDrop . style . display = 'none' ;
120
122
} ) ;
121
123
124
+ function updateUIBasedOnQueryParams ( parsedQueryObj ) {
125
+ const statusFilters = document . querySelectorAll (
126
+ `input[name="status-filter"]` ,
127
+ ) ;
128
+ const requestTypeFilters = document . querySelectorAll (
129
+ `input[name="request-type-filter"]` ,
130
+ ) ;
131
+ const sortOptions = document . querySelectorAll ( '.sort-container' ) ;
132
+
133
+ if ( parsedQueryObj . sort ) {
134
+ sortOptions . forEach ( ( option ) => {
135
+ if ( parsedQueryObj . sort . includes ( Sort [ option . id ] ) ) {
136
+ selectButton ( option ) ;
137
+ updateFilterStates ( 'order' , option . id ) ;
138
+ sortModal . classList . toggle ( 'hidden' ) ;
139
+ }
140
+ } ) ;
141
+ }
142
+
143
+ if ( parsedQueryObj . status ) {
144
+ statusFilters . forEach ( ( filter ) => {
145
+ if ( parsedQueryObj . status . includes ( filter . value . toLowerCase ( ) ) ) {
146
+ filter . checked = true ;
147
+ }
148
+ } ) ;
149
+ }
150
+
151
+ if ( parsedQueryObj [ 'request-type' ] ) {
152
+ requestTypeFilters . forEach ( ( filter ) => {
153
+ if ( parsedQueryObj [ 'request-type' ] . includes ( filter . value ) ) {
154
+ filter . checked = true ;
155
+ }
156
+ } ) ;
157
+ }
158
+
159
+ applyFilterButton . click ( ) ;
160
+ filterModal . classList . toggle ( 'hidden' ) ;
161
+ }
162
+
122
163
function toggleStatusCheckbox ( statusValue ) {
123
164
const element = document . querySelector (
124
165
`#status-filter input[value=${ statusValue } ]` ,
@@ -143,6 +184,18 @@ filterButton.addEventListener('click', (event) => {
143
184
backDrop . style . display = 'flex' ;
144
185
} ) ;
145
186
187
+ function manipulateURLQueryParams ( constructedQueryString ) {
188
+ const currentURLInstance = new URL ( window . location . href ) ;
189
+ currentURLInstance . search = '' ;
190
+ const currentURL = currentURLInstance . href ;
191
+ const newURLWithQueryParams = `${ currentURL } ${ constructedQueryString } ` ;
192
+ window . history . pushState (
193
+ { path : newURLWithQueryParams } ,
194
+ '' ,
195
+ newURLWithQueryParams ,
196
+ ) ;
197
+ }
198
+
146
199
applyFilterButton . addEventListener ( 'click' , async ( ) => {
147
200
filterModal . classList . toggle ( 'hidden' ) ;
148
201
const checkedValuesStatus = getCheckedValues ( 'status-filter' ) ;
@@ -158,9 +211,11 @@ applyFilterButton.addEventListener('click', async () => {
158
211
} ) ;
159
212
clearButton . addEventListener ( 'click' , async function ( ) {
160
213
clearCheckboxes ( 'status-filter' ) ;
214
+ clearCheckboxes ( 'request-type-filter' ) ;
161
215
filterModal . classList . toggle ( 'hidden' ) ;
162
216
changeFilter ( ) ;
163
217
updateFilterStates ( 'status' , '' ) ;
218
+ updateFilterStates ( 'requestType' , '' ) ;
164
219
await renderTaskRequestCards ( filterStates ) ;
165
220
} ) ;
166
221
@@ -183,6 +238,26 @@ function addSortByIcon(name, id, groupName, order) {
183
238
group . appendChild ( containerAsc ) ;
184
239
}
185
240
241
+ function toggleSortModal ( ) {
242
+ sortModal . classList . toggle ( 'hidden' ) ;
243
+ backDrop . style . display = 'none' ;
244
+ }
245
+
246
+ function selectButton ( button ) {
247
+ if ( selectedSortButton === button ) {
248
+ selectedSortButton . classList . remove ( 'selected' ) ;
249
+ selectedSortButton = null ;
250
+ toggleSortModal ( ) ;
251
+ } else {
252
+ if ( selectedSortButton ) {
253
+ selectedSortButton . classList . remove ( 'selected' ) ;
254
+ }
255
+ selectedSortButton = button ;
256
+ selectedSortButton . classList . add ( 'selected' ) ;
257
+ toggleSortModal ( ) ;
258
+ }
259
+ }
260
+
186
261
function sortModalButtons ( ) {
187
262
const assigneeAsc = document . getElementById ( ASSIGNEE_COUNT ) ;
188
263
const assigneeDesc = document . getElementById ( ASSIGNEE_DESC ) ;
@@ -196,26 +271,6 @@ function sortModalButtons() {
196
271
createTimeDesc ,
197
272
] ;
198
273
199
- function toggleSortModal ( ) {
200
- sortModal . classList . toggle ( 'hidden' ) ;
201
- backDrop . style . display = 'none' ;
202
- }
203
-
204
- function selectButton ( button ) {
205
- if ( selectedSortButton === button ) {
206
- selectedSortButton . classList . remove ( 'selected' ) ;
207
- selectedSortButton = null ;
208
- toggleSortModal ( ) ;
209
- } else {
210
- if ( selectedSortButton ) {
211
- selectedSortButton . classList . remove ( 'selected' ) ;
212
- }
213
- selectedSortButton = button ;
214
- selectedSortButton . classList . add ( 'selected' ) ;
215
- toggleSortModal ( ) ;
216
- }
217
- }
218
-
219
274
sortModalButtons . forEach ( ( button ) => {
220
275
if ( button ) {
221
276
button . addEventListener ( 'click' , async ( ) => {
@@ -226,6 +281,7 @@ function sortModalButtons() {
226
281
} ) ;
227
282
}
228
283
} ) ;
284
+
229
285
selectButton ( createTimeAsc ) ;
230
286
toggleSortModal ( ) ;
231
287
}
@@ -410,9 +466,15 @@ async function renderTaskRequestCards(queries = {}, newLink = '') {
410
466
}
411
467
412
468
async function render ( ) {
413
- toggleStatusCheckbox ( Status . PENDING . toUpperCase ( ) ) ;
414
-
415
- await renderTaskRequestCards ( filterStates ) ;
469
+ if ( window . location . search === '' ) {
470
+ toggleStatusCheckbox ( Status . PENDING . toUpperCase ( ) ) ;
471
+ const constructedQueryString = formURLQueryString ( filterStates ) ;
472
+ manipulateURLQueryParams ( constructedQueryString ) ;
473
+ await renderTaskRequestCards ( filterStates ) ;
474
+ } else {
475
+ const parsedQuery = parseQueryParams ( params ) ;
476
+ updateUIBasedOnQueryParams ( parsedQuery ) ;
477
+ }
416
478
addIntersectionObserver ( ) ;
417
479
}
418
480
0 commit comments