Handle back/forward page in advanced search
authorChocobozzz <me@florianbigard.com>
Mon, 23 Jul 2018 09:12:03 +0000 (11:12 +0200)
committerChocobozzz <me@florianbigard.com>
Tue, 24 Jul 2018 12:04:05 +0000 (14:04 +0200)
client/src/app/search/advanced-search.model.ts
client/src/app/search/search.component.ts
client/src/app/search/search.service.ts

index a0f3331755f2f9941590cdfb630c57f21885f382..aad4367881f8c0f37771114e270cf666822978a5 100644 (file)
@@ -32,14 +32,14 @@ export class AdvancedSearch {
   }) {
     if (!options) return
 
-    this.startDate = options.startDate
-    this.endDate = options.endDate
-    this.nsfw = options.nsfw
-    this.categoryOneOf = options.categoryOneOf
-    this.licenceOneOf = options.licenceOneOf
-    this.languageOneOf = options.languageOneOf
-    this.tagsOneOf = options.tagsOneOf
-    this.tagsAllOf = options.tagsAllOf
+    this.startDate = options.startDate || undefined
+    this.endDate = options.endDate || undefined
+    this.nsfw = options.nsfw || undefined
+    this.categoryOneOf = options.categoryOneOf || undefined
+    this.licenceOneOf = options.licenceOneOf || undefined
+    this.languageOneOf = options.languageOneOf || undefined
+    this.tagsOneOf = options.tagsOneOf || undefined
+    this.tagsAllOf = options.tagsAllOf || undefined
     this.durationMin = parseInt(options.durationMin, 10)
     this.durationMax = parseInt(options.durationMax, 10)
 
index 09028fec5e0f75c8cdbc1835b0d367bdfe7c7081..8860b9268cc40d54769b2a5fb5ebaea51a3cea3b 100644 (file)
@@ -27,6 +27,7 @@ export class SearchComponent implements OnInit, OnDestroy {
 
   private subActivatedRoute: Subscription
   private currentSearch: string
+  private isInitialLoad = true
 
   constructor (
     private i18n: I18n,
@@ -39,23 +40,28 @@ export class SearchComponent implements OnInit, OnDestroy {
   ) { }
 
   ngOnInit () {
-    this.advancedSearch = new AdvancedSearch(this.route.snapshot.queryParams)
-    if (this.advancedSearch.containsValues()) this.isSearchFilterCollapsed = false
-
     this.subActivatedRoute = this.route.queryParams.subscribe(
       queryParams => {
         const querySearch = queryParams['search']
 
         if (!querySearch) return this.redirectService.redirectToHomepage()
-        if (querySearch === this.currentSearch) return
 
         // Search updated, reset filters
-        if (this.currentSearch) this.advancedSearch.reset()
+        if (this.currentSearch !== querySearch) {
+          this.resetPagination()
+          this.advancedSearch.reset()
+
+          this.currentSearch = querySearch
+          this.updateTitle()
+        }
+
+        this.advancedSearch = new AdvancedSearch(queryParams)
 
-        this.currentSearch = querySearch
-        this.updateTitle()
+        // Don't hide filters if we have some of them AND the user just came on the webpage
+        this.isSearchFilterCollapsed = this.isInitialLoad === false || !this.advancedSearch.containsValues()
+        this.isInitialLoad = false
 
-        this.reload()
+        this.search()
       },
 
       err => this.notificationsService.error('Error', err.text)
@@ -89,20 +95,16 @@ export class SearchComponent implements OnInit, OnDestroy {
   }
 
   onFiltered () {
-    this.updateUrlFromAdvancedSearch()
-    // Hide the filters
-    this.isSearchFilterCollapsed = true
+    this.resetPagination()
 
-    this.reload()
+    this.updateUrlFromAdvancedSearch()
   }
 
-  private reload () {
+  private resetPagination () {
     this.pagination.currentPage = 1
     this.pagination.totalItems = null
 
     this.videos = []
-
-    this.search()
   }
 
   private updateTitle () {
index c6106afd659f2c57fc7295eb8c7b8bd449b1d755..b46cb97f41f903bfba0695a90878c0c0bfaac2e9 100644 (file)
@@ -44,7 +44,7 @@ export class SearchService {
       const value = advancedSearchObject[name]
       if (!value) continue
 
-      if (Array.isArray(value)) {
+      if (Array.isArray(value) && value.length !== 0) {
         for (const v of value) params = params.append(name, v)
       } else {
         params = params.append(name, value)