Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#187376 Bugfix for GTM tracking #283

Merged
merged 1 commit into from
Apr 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion src/modules/icmaa-external-checkout/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,14 @@ export const IcmaaExternalCheckoutModule: StorefrontModule = function ({ router,
EventBus.$on('session-after-nonauthorized', async () => {
const customerToken = Vue.$cookies.get('vsf_token_customer')
const quoteToken = Vue.$cookies.get('vsf_token_quote')
const lastOrderToken = Vue.$cookies.get('vsf_token_lastorder')

if (!store.getters['user/isLoggedIn'] && (customerToken || quoteToken)) {
if (!store.getters['user/isLoggedIn'] && (customerToken || quoteToken || lastOrderToken)) {
if (customerToken) {
Logger.info('Customer token found in cookie – try to login:', 'external-checkout', customerToken)()
store.dispatch('user/startSessionWithToken', customerToken).then(() => {
Vue.$cookies.remove('vsf_token_customer', undefined, getCookieHostname())
Vue.$cookies.remove('vsf_token_lastorder', undefined, getCookieHostname())
})
}

Expand All @@ -41,6 +43,13 @@ export const IcmaaExternalCheckoutModule: StorefrontModule = function ({ router,
Vue.$cookies.remove('vsf_token_quote', undefined, getCookieHostname())
})
}

if (!customerToken && lastOrderToken) {
Logger.info('Last-order token found in cookie – try to load last order:', 'external-checkout', lastOrderToken)()
store.dispatch('user/loadLastOrderToHistory', { token: lastOrderToken }).then(() => {
Vue.$cookies.remove('vsf_token_lastorder', undefined, getCookieHostname())
})
}
}
})

Expand Down
14 changes: 12 additions & 2 deletions src/modules/icmaa-external-checkout/pages/ExternalSuccess.vue
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,11 @@ export default {
* logged in when the beforeMount event hook is called. Otherwise the `checkout-success-last-order-loaded`
* event won't ever be fired on first request because `isLoggedIn` is false.
*/
isLoggedIn (isLoggedIn) {
this.onLogin(isLoggedIn)
isLoggedIn (value) {
this.onLogin(value)
},
lastOrder () {
this.guestOrder()
}
},
methods: {
Expand All @@ -69,6 +72,13 @@ export default {
if (value || this.isLoggedIn) {
await this.$store.dispatch('user/refreshOrdersHistory', { resolvedFromCache: false })
this.$bus.$emit('checkout-success-last-order-loaded', this.lastOrder)
} else if (!value && !this.isLoggedIn) {
await this.$store.dispatch('user/loadLastOrderFromCache')
}
},
async guestOrder () {
if (!this.isLoggedIn && this.lastOrder) {
this.$bus.$emit('checkout-success-last-order-loaded', this.lastOrder)
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions src/modules/icmaa-user/data-resolver/UserService.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { processLocalizedURLAddress } from '@vue-storefront/core/helpers'
import { TaskQueue } from '@vue-storefront/core/lib/sync'
import Task from '@vue-storefront/core/lib/sync/types/Task'
import config from 'config'
import getApiEndpointUrl from '@vue-storefront/core/helpers/getApiEndpointUrl'

const headers = {
'Accept': 'application/json, text/plain, */*',
'Content-Type': 'application/json'
}

const getLastOrder = async (token: string): Promise<Task> =>
TaskQueue.execute({
url: processLocalizedURLAddress(
getApiEndpointUrl(config.users, 'last_order').replace('{{token}}', token)
),
payload: {
method: 'GET',
mode: 'cors',
headers
}
})

export const UserService: any = {
getLastOrder
}
25 changes: 25 additions & 0 deletions src/modules/icmaa-user/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import RootState from '@vue-storefront/core/types/RootState'
import UserState from '../types/UserState'
import { UserProfile } from '@vue-storefront/core/modules/user/types/UserProfile'
import { UserService } from '@vue-storefront/core/data-resolver'
import { UserService as IcmaaUserService } from '../data-resolver/UserService'
import * as types from './mutation-types'
import * as userTypes from '@vue-storefront/core/modules/user/store/mutation-types'
import { SearchQuery } from 'storefront-query-builder'
Expand Down Expand Up @@ -95,6 +96,30 @@ const actions: ActionTree<UserState, RootState> = {

return resp
},
async loadLastOrderToHistory ({ commit, dispatch }, { token }) {
const resp = await IcmaaUserService.getLastOrder(token)

if (resp.code === 200) {
const order = await dispatch('loadOrderProducts', { order: resp.result, history: [ resp.result ] })

commit(userTypes.USER_ORDERS_HISTORY_LOADED, { items: [ order ] })
EventBus.$emit('user-after-loaded-orders', resp.result)
}

return resp
},
async loadLastOrderFromCache ({ dispatch }) {
let resolvedFromCache = false
const ordersHistory = await dispatch('loadOrdersFromCache')
if (ordersHistory) {
Logger.log('Current user order history served from cache', 'user')()
resolvedFromCache = true
}

if (!resolvedFromCache) {
Promise.resolve(null)
}
},
async loadOrderProducts ({ dispatch }, { order, history }) {
const index = history.findIndex(o => o.id === order.id)
if (history[index] && history[index].products) {
Expand Down