Skip to content

Commit

Permalink
Merge pull request #1581 from qiqqq/feature/offline-orders-confirmation
Browse files Browse the repository at this point in the history
Issue #1430 Offline orders confirmation
  • Loading branch information
pkarw authored Aug 14, 2018
2 parents 3017d29 + 2c77dd1 commit 710ef47
Show file tree
Hide file tree
Showing 21 changed files with 641 additions and 312 deletions.
1 change: 1 addition & 0 deletions config/default.json
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@
"payment_methods_mapping": {
},
"offline_orders": {
"automatic_transmission_enabled": false,
"notification" : {
"enabled": true,
"title" : "Order waiting!",
Expand Down
40 changes: 40 additions & 0 deletions core/api/offline-order/cancelOrder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/**
* Functionality for cancelling orders placed offline
*
* #### Methods
* - **`cancelOrder()`** removes not transmitted orders from Local Storage
*
* Part of [Offline order API Module](https://github.com/DivanteLtd/vue-storefront/tree/master/doc/api-modules)
*/
import * as localForage from 'localforage'
import config from 'config'

import EventBus from '@vue-storefront/core/plugins/event-bus'
import UniversalStorage from '@vue-storefront/store/lib/storage'
import { currentStoreView } from '@vue-storefront/store/lib/multistore'

export const cancelOrder = {
methods: {
cancelOrder () {
const storeView = currentStoreView()
const dbNamePrefix = storeView.storeCode ? storeView.storeCode + '-' : ''

const ordersCollection = new UniversalStorage(localForage.createInstance({
name: dbNamePrefix + 'shop',
storeName: 'orders',
driver: localForage[config.localForage.defaultDrivers['orders']]
}))

ordersCollection.iterate((order, id, iterationNumber) => {
if (!order.transmited) {
ordersCollection.removeItem(id)
}
}).catch(err => {
console.log(err)
console.log('Not transmitted orders was deleted')
})

EventBus.$emit('modal-hide', 'modal-order-confirmation')
}
}
}
21 changes: 21 additions & 0 deletions core/api/offline-order/confirmOrder.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
/**
* Functionality for confirming orders placed offline
*
* #### Methods
* - **`confirmOrder()`** emits event to send orders placed offline to server
*
* Part of [Offline order API Module](https://github.com/DivanteLtd/vue-storefront/tree/master/doc/api-modules)
*/
import EventBus from '@vue-storefront/core/plugins/event-bus'
import config from 'config'

export const confirmOrder = {
methods: {
confirmOrder () {
EventBus.$emit('order/PROCESS_QUEUE', { config: config })
EventBus.$emit('sync/PROCESS_QUEUE', { config: config })
this.$store.dispatch('cart/load')
EventBus.$emit('modal-hide', 'modal-order-confirmation')
}
}
}
42 changes: 42 additions & 0 deletions core/api/offline-order/helpers/onNetworkStatusChange.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as localForage from 'localforage'
import config from 'config'

import EventBus from '@vue-storefront/core/plugins/event-bus'

import UniversalStorage from '@vue-storefront/store/lib/storage'
import { currentStoreView } from '@vue-storefront/store/lib/multistore'

export function onNetworkStatusChange (store) {
console.log('Are we online: ' + navigator.onLine)

if (typeof navigator !== 'undefined' && navigator.onLine) {
if (config.orders.offline_orders.automatic_transmission_enabled || store.getters['checkout/isThankYouPage']) {
EventBus.$emit('order/PROCESS_QUEUE', { config: config }) // process checkout queue
EventBus.$emit('sync/PROCESS_QUEUE', { config: config }) // process checkout queue
// store.dispatch('cart/serverPull', { forceClientState: false })
store.dispatch('cart/load')
} else {
const ordersToConfirm = []
const storeView = currentStoreView()
const dbNamePrefix = storeView.storeCode ? storeView.storeCode + '-' : ''

const ordersCollection = new UniversalStorage(localForage.createInstance({
name: dbNamePrefix + 'shop',
storeName: 'orders',
driver: localForage[config.localForage.defaultDrivers['orders']]
}))

ordersCollection.iterate((order, id, iterationNumber) => {
if (!order.transmited) {
ordersToConfirm.push(order)
}
}).catch(err => {
console.log(err)
})

if (ordersToConfirm.length > 0) {
EventBus.$emit('offline-order-confirmation', ordersToConfirm)
}
}
}
}
7 changes: 7 additions & 0 deletions core/api/offline-order/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { confirmOrder } from './confirmOrder'
import { cancelOrder } from './cancelOrder'

export {
confirmOrder,
cancelOrder
}
Loading

0 comments on commit 710ef47

Please sign in to comment.