Advertisement
a_kozhukhar

Products.vue

Dec 23rd, 2020
1,112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <template>
  2. <div class="row">
  3.   <div class="special-filter-wrap col-md-3 col-lg-3 col-lg-offset-0">
  4.     <SelectCar :view="'short'" :page="'products'" :preSelected="selectedCar2" v-if="!carinfo.fuel && !selectedCar.fuel" />
  5.   </div>
  6.  
  7.   <div class="col-sm-12 col-md-9 col-lg-9" :class="carinfo.fuel || selectedCar.fuel ? ['col-md-12','col-lg-12'] : ''">
  8.     <div class="row">
  9.       <div class="col-xs-12 col-sm-12 ">
  10.         <CarTitle :titleValue="titleVal" :filterProducts="filterProducts" :selectedProduct="selectedProduct" :filterSuppliers="filterSuppliers" :selectedSuppliers="selectedSuppliers" />
  11.       </div>
  12.     </div>
  13.  
  14.     <grid-loader :loading="loading"></grid-loader>
  15.  
  16.     <div v-if="!loading">
  17.       <ProductRow :productGroup="productGroup" v-for="(productGroup, index) in productGroups" :key="index" />
  18.  
  19.       <h1 v-if="!loading && productGroups.length == 0">{{ $t('no_required_products') }}</h1>
  20.  
  21.       <div class="pagination-wrapper flex-row valign-wrapper" v-show="pageCount > 1">
  22.         <paginate :page-count="pageCount" v-model="page" :prev-text="'Prev'" :next-text="'Next'" :container-class="'pagination'">
  23.         </paginate>
  24.       </div>
  25.     </div>
  26.   </div>
  27. </div>
  28. </template>
  29.  
  30. <script>
  31. import SelectCar from "@/components/SelectCar"
  32. import ProductRow from "@/components/ProductRow"
  33. import CarTitle from "@/components/CarTitle"
  34. import {
  35.   mapGetters
  36. } from "vuex"
  37. import loaderMixin from "@/mixins/loader.mixin"
  38. import Paginate from 'vuejs-paginate'
  39. import carinfo from "../store/carinfo"
  40.  
  41. export default {
  42.   name: "products",
  43.   mixins: [loaderMixin],
  44.   data: () => ({
  45.     productGroups: [],
  46.     loading: false,
  47.     pageSize: 5,
  48.     page: 1,
  49.     pageCount: 1,
  50.     filterProducts: [],
  51.     filterSuppliers: [],
  52.     titleVal: '',
  53.     selectedProduct: '',
  54.     selectedSuppliers: [],
  55.     selectedCar: {},
  56.     selectedCar2: {
  57.       manufacturer: "",
  58.       model: "",
  59.       engine: "",
  60.       year: "",
  61.       fuel: ""
  62.     },
  63.     needToUpdate: true
  64.   }),
  65.  
  66.   async created() {
  67.     this.page = parseInt(this.$route.query.page) || 1
  68.     if (!this.$route.query.hasOwnProperty('suppliers') || this.$route.query.suppliers == null) {
  69.       const routeQuery = {
  70.         ...this.$route.query
  71.       }
  72.       const queryPush = Object.assign(routeQuery, {
  73.         suppliers: this.$route.params.brand
  74.       })
  75.       this.$router.push({
  76.         query: queryPush
  77.       })
  78.     }
  79.     await this.fetchProducts()
  80.   },
  81.  
  82.   watch: {
  83.     page: async function(newValue) {
  84.       // this.selectedCar = {}
  85.       const routeQuery = {
  86.         ...this.$route.query
  87.       }
  88.       const queryPush = Object.assign(routeQuery, {
  89.         page: newValue
  90.       })
  91.       this.$router.push({
  92.         query: queryPush
  93.       })
  94.       await this.fetchProducts()
  95.     }
  96.   },
  97.  
  98.   methods: {
  99.     async fetchProducts() {
  100.       this.loading = true
  101.  
  102.       /* категории */
  103.       let splittedSlug = this.$route.path.split("/").filter((el) => el != "")
  104.       splittedSlug.splice(0, 1) // убираем лишние элементы, полученные из URL
  105.  
  106.       this.man = await this.$store.dispatch("fetchSelectData")
  107.       let pos = 0,
  108.         carInfo = []
  109.       splittedSlug.forEach(value => !this.man.includes(value) ? pos++ : pos += 0)
  110.       console.log(pos)
  111.       if (splittedSlug.length > 3) splittedSlug = splittedSlug.splice(0, 3)
  112.  
  113.       let carInfo = this.$route.path.split("/").filter((el) => el != "" && !splittedSlug.includes(el))
  114.       carInfo.splice(0, (this.$route.params.brand) ? 2 : 1)
  115.  
  116.       if (carInfo.length) {
  117.         if (this.man.includes(carInfo[0])) {
  118.           this.man = await this.$store.dispatch("fetchSelectData", {
  119.             manufacturer: carInfo[0]
  120.           });
  121.           this.carinfo.manufacturer = carInfo[0]
  122.           this.selectedCar.manufacturer = carInfo[0]
  123.         } else this.carinfo.manufacturer = ""
  124.  
  125.         if (this.man.includes(carInfo[1])) {
  126.           this.man = await this.$store.dispatch("fetchSelectData", {
  127.             manufacturer: carInfo[0],
  128.             model: carInfo[1]
  129.           });
  130.           this.carinfo.model = carInfo[1]
  131.           this.selectedCar.model = carInfo[1]
  132.         } else this.carinfo.model = ""
  133.  
  134.         if (this.man.includes(carInfo[2])) {
  135.           this.man = await this.$store.dispatch("fetchSelectData", {
  136.             manufacturer: carInfo[0],
  137.             model: carInfo[1],
  138.             engine: carInfo[2]
  139.           });
  140.           this.carinfo.engine = carInfo[2]
  141.           this.selectedCar.engine = carInfo[2]
  142.         } else this.carinfo.engine = ""
  143.  
  144.         if (this.man.includes(parseInt(carInfo[3]))) {
  145.           this.man = await this.$store.dispatch("fetchSelectData", {
  146.             manufacturer: carInfo[0],
  147.             model: carInfo[1],
  148.             engine: carInfo[2],
  149.             year: carInfo[3]
  150.           });
  151.           this.carinfo.year = carInfo[3]
  152.           this.selectedCar.year = carInfo[3]
  153.         } else this.carinfo.year = ""
  154.         if (this.man.includes(carInfo[4])) {
  155.           this.man = await this.$store.dispatch("fetchSelectData", {
  156.             manufacturer: carInfo[0],
  157.             model: carInfo[1],
  158.             engine: carInfo[2],
  159.             year: carInfo[3],
  160.             fuel: carInfo[4]
  161.           });
  162.           this.carinfo.fuel = carInfo[4]
  163.           this.selectedCar.fuel = carInfo[4]
  164.         } else this.carinfo.fuel = ""
  165.         if (Object.keys(this.selectedCar).length == 5 && this.needToUpdate === true) {
  166.           await this.$store.dispatch("removeCarInfo")
  167.           console.log(await this.$store.dispatch("updateCarInfo", {
  168.             ...this.selectedCar
  169.           }))
  170.           this.needToUpdate = false
  171.         }
  172.         this.selectedCar2 = this.selectedCar
  173.         console.log(this.selectedCar2)
  174.         console.log(Object.keys(this.selectedCar).length == 5 ? 'Stored' : 'Temp', JSON.stringify(this.selectedCar))
  175.       }
  176.       const params = new URLSearchParams()
  177.       splittedSlug.reverse().forEach(value => {
  178.         value = decodeURIComponent(value)
  179.         if (value != this.$route.query.supplier && value != this.$route.query.suppliers && value != this.$route.params.brand) params.append("categoryChain", value)
  180.       });
  181.  
  182.       /* пагинация */
  183.       params.append("page", this.page)
  184.       params.append("pageSize", this.pageSize)
  185.  
  186.       /* фильтры */
  187.       this.selectedProduct = this.$route.query.product || ""
  188.       if (!!this.selectedProduct) params.append("product", this.selectedProduct)
  189.       const suppliers = this.$route.query.suppliers || []
  190.       if (suppliers.length > 0) {
  191.         if (Array.isArray(suppliers)) {
  192.           this.$route.query.suppliers.forEach(value => params.append("suppliers", value))
  193.           this.selectedSuppliers = suppliers
  194.         } else {
  195.           params.append("suppliers", suppliers)
  196.           this.selectedSuppliers.push(suppliers)
  197.         }
  198.       }
  199.  
  200.       /* если есть строка поиска */
  201.       if (this.$route.query.searchString) {
  202.         params.append("searchString", this.$route.query.searchString)
  203.         params.append("partialMatch", true)
  204.       }
  205.  
  206.       /* выбранный авто */
  207.       if (this.carinfo.manufacturer) {
  208.         params.append("manufacturer", this.carinfo.manufacturer)
  209.         if (this.carinfo.model != "" && this.carinfo.model) params.append("model", this.carinfo.model)
  210.         if (this.carinfo.engine != "" && this.carinfo.engine) params.append("engine", this.carinfo.engine)
  211.         if (this.carinfo.year != "" && this.carinfo.year) params.append("year", this.carinfo.year)
  212.         if (this.carinfo.fuel != "" && this.carinfo.fuel) params.append("fuel", this.carinfo.fuel)
  213.       }
  214.       if (this.carinfo.refinement) {
  215.         params.append("version", this.carinfo.refinement.version)
  216.         params.append("powerHP", this.carinfo.refinement.power)
  217.         params.append("gear", this.carinfo.refinement.gear)
  218.         params.append("volume", this.carinfo.refinement.volume)
  219.         params.append("body", this.carinfo.refinement.body)
  220.       }
  221.       console.log('API Query:', decodeURIComponent(params));
  222.       const result = await this.$store.dispatch("fetchProducts", params)
  223.       this.productGroups = result.productGroups
  224.       this.pageCount = result.pageCount
  225.       this.filterProducts = result.products
  226.       this.filterSuppliers = result.suppliers
  227.       this.filterSuppliers.push(this.$route.params.brand)
  228.       this.filterSuppliers = [...new Set(this.filterSuppliers)]
  229.       try {
  230.         this.titleVal = this.productGroups[0].products[0].category.name
  231.       } catch (e) {}
  232.       // console.log('--- + ---', Object.keys(JSON.parse(JSON.stringify(this.selectedCar))).length, Object.keys(this.carinfo).length)
  233.       this.loading = false
  234.     }
  235.   },
  236.  
  237.   computed: {
  238.     ...mapGetters(["carinfo"]),
  239.     classObject: function() {
  240.       return {
  241.         "col-md-12": this.carinfo.manufacturer,
  242.         "col-lg-12": this.carinfo.manufacturer,
  243.       };
  244.     }
  245.   },
  246.  
  247.   mounted() {
  248.     window.Event.$on("updateProducts", () => {
  249.       this.fetchProducts()
  250.     })
  251.   },
  252.  
  253.   components: {
  254.     SelectCar,
  255.     ProductRow,
  256.     CarTitle,
  257.     Paginate
  258.   }
  259. }
  260. </script>
  261.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement