/** * Copyright since 2007 PrestaShop SA and Contributors * PrestaShop is an International Registered Trademark & Property of PrestaShop SA * * NOTICE OF LICENSE * * This source file is subject to the Open Software License (OSL 3.0) * that is bundled with this package in the file LICENSE.md. * It is also available through the world-wide-web at this URL: * https://opensource.org/licenses/OSL-3.0 * If you did not receive a copy of the license and are unable to * obtain it through the world-wide-web, please send an email * to license@prestashop.com so we can send you a copy immediately. * * DISCLAIMER * * Do not edit or add to this file if you wish to upgrade PrestaShop to newer * versions in the future. If you wish to customize PrestaShop for your * needs please refer to https://devdocs.prestashop.com/ for more information. * * @author PrestaShop SA and Contributors * @copyright Since 2007 PrestaShop SA and Contributors * @license https://opensource.org/licenses/OSL-3.0 Open Software License (OSL 3.0) */ /* eslint-disable no-param-reassign */ import _ from 'lodash'; import * as types from './mutation-types'; export default { [types.UPDATE_ORDER](state: Record, order: Record): void { state.order = order; }, [types.UPDATE_SORT](state: Record, sort: string): void { state.sort = sort; }, [types.UPDATE_KEYWORDS](state: Record, keywords: Array): void { state.keywords = keywords; }, [types.SET_TOTAL_PAGES](state: Record, totalPages: number): void { state.totalPages = Number(totalPages); }, [types.SET_PAGE_INDEX](state: Record, pageIndex: number): void { state.pageIndex = pageIndex; }, [types.SET_SUPPLIERS](state: Record, suppliers: Array>): void { state.suppliers = suppliers; }, [types.SET_CATEGORIES](state: Record, categories: Record): void { state.categories = categories.data.tree.children; }, [types.SET_MOVEMENTS](state: Record, movements: Record): void { state.movements = movements.data; }, [types.SET_TRANSLATIONS](state: Record, translations: Record): void { translations.data.forEach((t: Record) => { state.translations[t.translation_id] = t.name; }); }, [types.LOADING_STATE](state: Record, isLoading: boolean): void { state.isLoading = isLoading; }, [types.APP_IS_READY](state: Record): void { state.isReady = true; }, [types.SET_EMPLOYEES_LIST](state: Record, employees: Record): void { state.employees = employees.data; }, [types.SET_MOVEMENTS_TYPES](state: Record, movementsTypes: Record): void { state.movementsTypes = movementsTypes.data; }, [types.ADD_PRODUCTS](state: Record, products: Record): void { state.productsToUpdate = []; state.selectedProducts = []; _.forEach(products.data.data, (product) => { product.qty = 0; }); state.editBulkUrl = products.data.info.edit_bulk_url; state.products = products.data.data; }, [types.UPDATE_PRODUCT](state: Record, updatedProduct: Record): void { const index = _.findIndex(state.products, { product_id: updatedProduct.product_id, combination_id: updatedProduct.combination_id, }); const updatedIndex = _.findIndex(state.productsToUpdate, { product_id: updatedProduct.product_id, combination_id: updatedProduct.combination_id, }); updatedProduct.qty = 0; state.products.splice(index, 1, updatedProduct); state.productsToUpdate.splice(updatedIndex, 1); }, [types.UPDATE_PRODUCTS_QTY](state: Record, updatedProducts: Record): void { state.productsToUpdate = []; state.selectedProducts = []; _.forEach(updatedProducts, (product) => { const index = _.findIndex(state.products, { product_id: product.product_id, combination_id: product.combination_id, }); product.qty = 0; state.products.splice(index, 1, product); }); state.hasQty = false; }, [types.UPDATE_PRODUCT_QTY](state: Record, updatedProduct: Record): void { let hasQty = false; const productToUpdate = _.find(state.products, { product_id: updatedProduct.product_id, combination_id: updatedProduct.combination_id, }); _.forEach(state.products, (product) => { productToUpdate.qty = updatedProduct.delta; if (product.qty) { hasQty = true; } }); state.hasQty = hasQty; }, [types.ADD_PRODUCT_TO_UPDATE](state: Record, updatedProduct: Record): void { const index = _.findIndex(state.productsToUpdate, { product_id: updatedProduct.product_id, combination_id: updatedProduct.combination_id, }); if (index !== -1) { state.productsToUpdate.splice(index, 1, updatedProduct); } else { state.productsToUpdate.push(updatedProduct); } }, [types.REMOVE_PRODUCT_TO_UPDATE](state: Record, updatedProduct: Record): void { const index = _.findIndex(state.productsToUpdate, { product_id: updatedProduct.product_id, combination_id: updatedProduct.combination_id, }); state.productsToUpdate.splice(index, 1); }, [types.UPDATE_BULK_EDIT_QTY](state: Record, value: number): void { state.bulkEditQty = value; if (value) { _.forEach(state.selectedProducts, (product: Record) => { const index = _.findIndex(state.productsToUpdate, { product_id: product.product_id, combination_id: product.combination_id, }); product.qty = value; product.delta = state.bulkEditQty; if (index !== -1) { state.productsToUpdate.splice(index, 1, product); } else { state.productsToUpdate.push(product); } }); state.hasQty = true; return; } if (value === 0) { _.forEach(state.selectedProducts, (product) => { product.qty = 0; }); state.hasQty = false; return; } if (value === null) { _.forEach(state.selectedProducts, (product) => { product.qty = 0; }); state.productsToUpdate = []; state.selectedProducts = []; state.hasQty = false; } }, [types.ADD_SELECTED_PRODUCT](state: Record, product: Record): void { const index = _.findIndex(state.selectedProducts, { product_id: product.product_id, combination_id: product.combination_id, }); if (index !== -1) { state.selectedProducts.splice(index, 1, product); } else { state.selectedProducts.push(product); } }, [types.REMOVE_SELECTED_PRODUCT](state: Record, product: Record): void { const index = _.findIndex(state.selectedProducts, { product_id: product.product_id, combination_id: product.combination_id, }); if (index !== -1) { state.selectedProducts[index].qty = 0; } state.selectedProducts.splice(index, 1); }, };