File: D:/HostingSpaces/SBogers95/rentman.io/resources/assets/js/createPlan/app.js
import CreatePlan from "./../components/createPlan"
const createPlanEl = document.querySelector('#create-plan-vue')
const productData = createPlanEl.getAttribute('data-product-data')
if(!productData) throw new Error('productData is missing');
createPlanEl.removeAttribute('data-product-data');
const coreProductId = 6
const primaryProductIds = [2, 4]
const productIdsRequiredPrimary = [5]
const equipmentSchedulingId = 2
const productIdsRequiredEquipmentScheduling = [3]
const productIdsRequiredEquipmentSchedulingPro = [8]
const removableProductIds = new Set([coreProductId])
// Prepare products array
let products = Object.values(JSON.parse(productData))
products.forEach(product => {
product.requiresEquipmentScheduling = productIdsRequiredEquipmentScheduling.includes(product.id)
product.requiresEquipmentSchedulingPro = productIdsRequiredEquipmentSchedulingPro.includes(product.id)
product.requiresPrimary = productIdsRequiredPrimary.includes(product.id)
product.levels = product.levels != '' && product.levels != '[]' ? JSON.parse(product.levels) : [];
product.specs = product.translation && product.translation.specs != '' && product.translation.specs != '[]' ? JSON.parse(product.translation.specs) : [];
product.hinted_products = product.hinted_products.split(',')
const hintedProducts = [];
// Validate hinted products exist
product.hinted_products.forEach((productId, index) => {
const hintedProduct = products.find(p => p.id == productId)
if(hintedProduct === undefined) product.hinted_products.splice(index, 1)
else{
hintedProduct.hasParentProduct = true
hintedProduct.parentProduct = product.id
hintedProducts.push(hintedProduct)
removableProductIds.add(parseInt(productId))
}
})
product.hinted_products = hintedProducts
})
const coreProduct = products.find(p => p.id == coreProductId);
if(!coreProduct) throw new Error('Core product (Rentman Platform) / Product with id 6 is missing');
if(!coreProduct.translation) throw new Error('Core product (Rentman Platform) / Product is missing its translation');
coreProduct.translation.specs = coreProduct.translation.specs !== '' ? Object.values(JSON.parse(coreProduct.translation.specs)).map(spec => spec[0]) : []
// Create selectable products, by removing the hinted products and core product (so removableProductIds
const selectableProducts = products.filter(p => !removableProductIds.has(p.id))
Vue.mixin(require('./mixins'));
var vm = new Vue({
el: '#create-plan-vue',
components: {
CreatePlan,
},
data: () => ({
products: products,
selectableProducts: selectableProducts,
coreProduct: coreProduct,
translations: window.rentman.translations,
currencyEuro: true,
powerUsers: 1,
powerUserLimit: 15,
productsInCart: [],
}),
computed: {
hasAddons() {
let hasAddon = false
this.productsInCart.forEach((cartState) => {
const product = this.products.find(p => p.id == cartState.id)
if(product.pricing_product_type == 'addon') hasAddon = true
})
return hasAddon
},
primaryProductSelected() {
let hasPrimaryProduct = false
const selectedProductIds = this.productsInCart.map(p => p.id);
selectedProductIds.forEach(productId => {
let isPrimaryProduct = primaryProductIds.includes(productId)
if(isPrimaryProduct) hasPrimaryProduct = true
})
return hasPrimaryProduct
},
equipmentSchedulingSelected() {
let hasPrimaryProduct = false
const selectedProductIds = this.productsInCart.map(p => p.id);
selectedProductIds.forEach(productId => {
let isPrimaryProduct = equipmentSchedulingId === productId
if(isPrimaryProduct) hasPrimaryProduct = true
})
return hasPrimaryProduct
},
equipmentSchedulingProSelected() {
let hasSchedulingPro = false
const selectedProducts = this.productsInCart
selectedProducts.forEach(product => {
let isSchedulingPro = equipmentSchedulingId === product.id && product.level === 1
if(isSchedulingPro) hasSchedulingPro = true
})
return hasSchedulingPro
}
},
watch: {
productsInCart: {
deep: true,
handler() {
if (!this.equipmentSchedulingProSelected) {
this.productsInCart.find(p => p.id === 8)
? this.productsInCart.find(p => p.id === 8).amount = 0
: null;
}
}
},
}
})