HEX
Server: Microsoft-IIS/8.5
System: Windows NT YDAWBH120 6.3 build 9600 (Windows Server 2012 R2 Standard Edition) AMD64
User: tentjecom_web (0)
PHP: 7.4.14
Disabled: NONE
Upload Files
File: D:/HostingSpaces/SBogers10/shop.komma.nl/resources/js/store/modules/checkout.js
import Vuex from "vuex";
import Vue from "vue";
import {CheckoutService} from "../../services/checkoutService";
import {requestThrottleState, requestThrottleGetters, requestThrottleMutations} from '../composables/requestThrottle'
import ErrorHandler from "../../errorHandler";

Vue.use(Vuex);

let checkoutService = new CheckoutService();

const checkoutState = {
    ...requestThrottleState,
    editDetailsRoute: '#',
    cartRoute: '#',
    confirmRoute: '#',
    startPaymentRoute: '#',
    continueShoppingRoute: '#',
    cartItems: [],
    vatTotalsByScenario: [],
    subtotalExVatFormatted: '',
    subtotalIncVatFormatted: '',
    shippingCosts: [],
    shippingAddress: null,
    invoiceAddress: null,
    shippingCostsVatAmountFormatted: '',
    totalFormatted: '',
    totalItemsCount: 0,
    selectedShippingAddressIso3: null,
    discountPriceMutations: null,
    checkoutUser: null
}

const checkoutMutations = {
    ...requestThrottleMutations,
    storeLoadedCheckoutInformation(state, checkoutInformationData) {
        if (checkoutInformationData.hasOwnProperty('startPaymentRoute')) state.startPaymentRoute = checkoutInformationData.startPaymentRoute;
        if (checkoutInformationData.hasOwnProperty('cartRoute')) state.cartRoute = checkoutInformationData.cartRoute;
        if (checkoutInformationData.hasOwnProperty('continueShoppingRoute')) state.continueShoppingRoute = checkoutInformationData.continueShoppingRoute;
        if (checkoutInformationData.hasOwnProperty('editDetailsRoute')) state.editDetailsRoute = checkoutInformationData.editDetailsRoute;
        if (checkoutInformationData.hasOwnProperty('startCheckoutRoute')) state.startCheckoutRoute = checkoutInformationData.startCheckoutRoute;
        if (checkoutInformationData.hasOwnProperty('items')) state.cartItems = checkoutInformationData.items;
        if (checkoutInformationData.hasOwnProperty('subtotalExFormatted')) state.subtotalExVatFormatted = checkoutInformationData.subtotalExFormatted;
        if (checkoutInformationData.hasOwnProperty('subtotalIncFormatted')) state.subtotalIncVatFormatted = checkoutInformationData.subtotalIncFormatted;
        if (checkoutInformationData.hasOwnProperty('totalFormatted')) state.totalFormatted = checkoutInformationData.totalFormatted;
        if (checkoutInformationData.hasOwnProperty('totalItemsCount')) state.totalItemsCount = checkoutInformationData.totalItemsCount;
        if (checkoutInformationData.hasOwnProperty('discountPriceMutations')) Vue.set(state, 'discountPriceMutations', checkoutInformationData.discountPriceMutations);
        if (checkoutInformationData.hasOwnProperty('shippingAddress')) Vue.set(state, 'shippingAddress', checkoutInformationData.shippingAddress);
        if (checkoutInformationData.hasOwnProperty('invoiceAddress')) Vue.set(state, 'invoiceAddress', checkoutInformationData.invoiceAddress);
        if (checkoutInformationData.hasOwnProperty('vatRateTotals')) Vue.set(state, 'vatRateTotals', checkoutInformationData.vatRateTotals);
        if (checkoutInformationData.hasOwnProperty('shippingCosts')) Vue.set(state, 'shippingCosts', checkoutInformationData.shippingCosts);

        const requestKeyIndex = state.requests.indexOf('loadCheckoutInformation');
        if (requestKeyIndex !== -1) state.requests.splice(requestKeyIndex, 1);
    },
    storeLoadedCheckoutUser(state, checkoutUser) {
        Vue.set(state, 'checkoutUser', checkoutUser);
    },
    storeUpdatedShippingAddress(state, shippingAddress) {
        Vue.set(state, 'shippingAddress', shippingAddress);
    },
}

const checkoutActions = {
    loadCheckoutInformation({commit, state}) {
        commit('requesting', 'loadCheckoutInformation')
        let promise = checkoutService.getCheckoutInformation().then(function (response) {
            commit('storeLoadedCheckoutInformation', response.data)
            return response.data;
        });

        promise.catch(ErrorHandler.logFor('loadCheckoutInformation')).finally(() => {
            commit('clearRequest', 'loadCheckoutInformation')
        })

        return promise;
    },
    loadCheckoutUser({commit, state}) {
        commit('requesting', 'loadCheckoutUser')
        let promise = checkoutService.getCheckoutUser().then(function (response) {
            commit('storeLoadedCheckoutUser', response.data.data)
            return response.data.data;
        });

        promise.catch(ErrorHandler.logFor('loadCheckoutUser')).finally(() => {
            commit('clearRequest', 'loadCheckoutUser')
        })

        return promise;
    },
    updateShippingAddress({commit, state, dispatch}, shippingAddress) {
        const {id, street, house_number, postal_code, city, telephone, country_iso3} = shippingAddress;

        let promise = checkoutService.setShippingAddress(id, street, house_number, postal_code, city, telephone, country_iso3).then(function () {
            commit('storeUpdatedShippingAddress', shippingAddress);
            dispatch('loadCheckoutInformation')
            return null;
        });

        promise.catch(ErrorHandler.logFor('storeUpdatedShippingAddress')).finally(() => {
            commit('clearRequest', 'storeUpdatedShippingAddress')
        })

        return promise;
    },
    updateInvoiceAddress({commit, state, dispatch}, invoiceAddress) {
        const {id, street, house_number, postal_code, city, telephone, country_iso3} = invoiceAddress;

        let promise = checkoutService.setInvoiceAddress(id, street, house_number, postal_code, city, telephone, country_iso3).then(function () {
            commit('storeUpdatedInvoiceAddress', invoiceAddress);
            dispatch('loadCheckoutInformation')
        })

        promise.catch(ErrorHandler.logFor('setInvoiceAddress')).finally(() => {
            commit('clearRequest', 'setInvoiceAddress')
        })

        return promise;
    },

    markAsInvoiceAddress({ commit, getters, dispatch }, address) {
        commit('requesting', 'markAsInvoiceAddressForCheckout')
        let promise = checkoutService.markAsInvoiceAddressForCheckout(address).then((response) => {
            dispatch('address/loadInvoiceAddress', null, { root: true });
        })

        promise.catch(ErrorHandler.logFor('markAsInvoiceAddressForCheckout')).finally(() => {
            commit('clearRequest', 'markAsInvoiceAddressForCheckout')
        })

        return promise;
    },


    markAsShippingAddress({ commit, getters, dispatch }, address) {
        commit('requesting', 'markAsShippingAddressForCheckout')
        let promise = checkoutService.markAsShippingAddress(address).then((response) => {
            dispatch('address/loadShippingAddress', null, { root: true });
        });

        promise.catch(ErrorHandler.logFor('markAsShippingAddressForCheckout')).finally(() => {
            commit('clearRequest', 'markAsShippingAddressForCheckout')
        })

        return promise;
    },
}

const checkoutGetters = {
    ...requestThrottleGetters,
    shippingAddressIso3(state) {
        if (state.shippingAddress && state.shippingAddress.hasOwnProperty('country_iso3')) return state.shippingAddress.country_iso3;
        else if (state.shippingCosts && state.shippingCosts.hasOwnProperty('country_iso3')) return state.shippingCosts.country_iso3;
        return null;
    }
}

export default {
    namespaced: true,
    state: checkoutState,
    actions: checkoutActions,
    getters: checkoutGetters,
    mutations: checkoutMutations
}