Explorar o código

Рефакторинг

Book Pauk %!s(int64=2) %!d(string=hai) anos
pai
achega
6eae46eaa1

+ 5 - 3
client/components/Search/AuthorList/AuthorList.vue

@@ -462,7 +462,7 @@ class AuthorList extends BaseList {
             let result;
 
             if (this.abCacheEnabled) {
-                const key = `${authorId}-${this.list.inpxHash}`;
+                const key = `author-${authorId}-${this.list.inpxHash}`;
                 const data = await authorBooksStorage.getData(key);
                 if (data) {
                     result = JSON.parse(data);
@@ -811,7 +811,9 @@ class AuthorList extends BaseList {
 
     async refresh() {
         //параметры запроса
-        const newQuery = _.cloneDeep(this.search);
+        let newQuery = _.cloneDeep(this.search);
+        newQuery = newQuery.setDefaults(newQuery);
+        delete newQuery.setDefaults;
         newQuery.offset = (newQuery.page - 1)*newQuery.limit;
 
         if (_.isEqual(newQuery, this.prevQuery))
@@ -824,7 +826,7 @@ class AuthorList extends BaseList {
             const author = this.cachedAuthors[authorSearch];
 
             if (author) {
-                const key = `${author.id}-${this.list.inpxHash}`;
+                const key = `author-${author.id}-${this.list.inpxHash}`;
                 let data = await authorBooksStorage.getData(key);
 
                 if (data) {

+ 33 - 36
client/components/Search/Search.vue

@@ -97,13 +97,6 @@
                         </q-tooltip>
                     </q-input>
 
-                    <!--div class="q-mx-xs" />
-                    <DivBtn class="text-white q-mt-xs bg-grey-13" :size="30" :icon-size="24" icon="la la-broom" round @click="setDefaults">
-                        <q-tooltip :delay="1500" anchor="bottom middle" content-style="font-size: 80%" max-width="400px">
-                            Сбросить поиск
-                        </q-tooltip>
-                    </DivBtn-->
-
                     <div class="q-mx-xs" />
                     <div class="row items-center q-mt-xs">
                         <div v-show="list.queryFound > 0">
@@ -296,13 +289,17 @@ class Search {
 
     //search fields
     search = {
-        author: '',
-        series: '',
-        title: '',
-        genre: '',
-        lang: '',
-        page: 1,
-        limit: 50,
+        setDefaults(search) {
+            return Object.assign({}, search, {
+                author: search.author || '',
+                series: search.series || '',
+                title: search.title || '',
+                genre: search.genre || '',
+                lang: search.lang || '',
+                page: search.page || 1,
+                limit: search.limit || 50,
+            });
+        },
     };
 
     //settings
@@ -346,6 +343,9 @@ class Search {
         this.commit = this.$store.commit;
         this.api = this.$root.api;
 
+        this.search = this.search.setDefaults(this.search);
+        this.search.lang = this.langDefault;
+
         this.loadSettings();
     }
 
@@ -367,7 +367,6 @@ class Search {
             if (!this.$root.isMobileDevice)
                 this.$refs.authorInput.focus();
 
-            this.setDefaults();
             this.updateListFromRoute(this.$route);
             this.updateSearchFromRouteQuery(this.$route);
 
@@ -699,16 +698,6 @@ class Search {
         this.commit('setSettings', {[name]: _.cloneDeep(newValue)});
     }
 
-    setDefaults() {
-        this.search = Object.assign({}, this.search, {
-            author: '',
-            series: '',
-            title: '',
-            genre: '',
-            lang: this.langDefault,
-        });
-    }
-
     highlightPageScroller(query) {
         const q = _.cloneDeep(query);
         delete q.limit;
@@ -728,31 +717,39 @@ class Search {
     updateSearchFromRouteQuery(to) {
         if (this.list.liberamaReady)
             this.sendCurrentUrl();
-            
+
         if (this.routeUpdating)
             return;
 
         const query = to.query;
 
-        this.search = Object.assign({}, this.search, {
-            author: query.author || '',
-            series: query.series || '',
-            title: query.title || '',
-            genre: query.genre || '',
-            lang: (typeof(query.lang) == 'string' ? query.lang : this.langDefault),
-            page: parseInt(query.page, 10) || 1,
-            limit: parseInt(query.limit, 10) || this.search.limit,
-        });
+        this.search = this.search.setDefaults(
+            Object.assign({}, this.search, {
+                author: query.author,
+                series: query.series,
+                title: query.title,
+                genre: query.genre,
+                lang: (typeof(query.lang) == 'string' ? query.lang : this.langDefault),
+                page: parseInt(query.page, 10),
+                limit: parseInt(query.limit, 10) || this.search.limit,
+            })
+        );
 
         if (this.search.limit > 1000)
             this.search.limit = 1000;
     }
 
     updateRouteQueryFromSearch() {
+        if (!this.ready)
+            return;
+
         this.routeUpdating = true;
         try {
             const oldQuery = this.$route.query;
-            const query = _.pickBy(this.search);
+            const cloned = _.cloneDeep(this.search);
+            delete cloned.setDefaults;
+
+            const query = _.pickBy(cloned);
 
             if (this.search.lang == this.langDefault) {
                 delete query.lang;