Skip to content

Commit

Permalink
Merge pull request #232 from ymaheshwari1/app-update
Browse files Browse the repository at this point in the history
Implemented: component to add support to let user know after new app release and update the app
  • Loading branch information
ravilodhi authored Nov 10, 2023
2 parents f514695 + f00d3d4 commit 7eb9e53
Show file tree
Hide file tree
Showing 8 changed files with 129 additions and 23 deletions.
11 changes: 11 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"luxon": "^3.3.0",
"pinia": "2.0.36",
"pinia-plugin-persistedstate": "^3.1.0",
"register-service-worker": "^1.7.2",
"vue": "^3.3.4",
"vue-barcode-reader": "^1.0.3",
"vue-i18n": "^9.2.2"
Expand Down
19 changes: 0 additions & 19 deletions src/components/AppVersionInfo.vue

This file was deleted.

34 changes: 34 additions & 0 deletions src/components/DxpAppVersionInfo.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<template>
<div class="section-header">
<div>
<h1>{{ translate('App') }}</h1>
<p class="overline">{{ translate("Version: ", { appVersion }) }}</p>
</div>
<div class="ion-text-end">
<p class="overline">{{ translate("Built: ", { builtDateTime: getDateTime(appInfo.builtTime) }) }}</p>
<ion-button v-if="pwaState.updateExists" @click="refreshApp()" fill="outline" color="dark" size="small">{{ translate("Update") }}</ion-button>
</div>
</div>
</template>

<script setup lang="ts">
import { IonButton } from '@ionic/vue';
import { DateTime } from 'luxon';
import { appContext, translate } from 'src';
import { computed } from 'vue';
declare var process: any;
const appState = appContext.config.globalProperties.$store
const pwaState = computed(() => appState.getters['user/getPwaState'])
const refreshApp = () => {
appState.dispatch('user/updatePwaState', { registration: pwaState.value.registration, updateExists: false });
if (!pwaState.value.registration || !pwaState.value.registration.waiting) return
pwaState.value.registration.waiting.postMessage({ type: 'SKIP_WAITING' })
}
const appInfo = (process.env.VUE_APP_VERSION_INFO ? JSON.parse(process.env.VUE_APP_VERSION_INFO) : {}) as any;
const appVersion = appInfo.branch ? (appInfo.branch + "-" + appInfo.revision) : appInfo.tag;
const getDateTime = (time: any) => DateTime.fromMillis(time).toLocaleString(DateTime.DATETIME_MED);
</script>
2 changes: 1 addition & 1 deletion src/components/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import '@ionic/vue/css/display.css';

export { default as DxpImage } from './DxpImage.vue';
export { default as DxpUserProfile } from './DxpUserProfile.vue'
export { default as AppVersionInfo } from './AppVersionInfo.vue';
export { default as DxpAppVersionInfo } from './DxpAppVersionInfo.vue';
export { default as LanguageSwitcher } from './LanguageSwitcher.vue';
export { default as DxpMenuFooterNavigation } from './DxpMenuFooterNavigation.vue';
export { default as DxpLogin } from './DxpLogin.vue';
Expand Down
25 changes: 23 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@ declare var process: any;
import { createPinia } from "pinia";
import { useProductIdentificationStore } from "./store/productIdentification";
import { useAuthStore } from "./store/auth";
import { AppVersionInfo, DxpImage, DxpLogin, DxpUserProfile, LanguageSwitcher, DxpMenuFooterNavigation, OmsInstanceNavigator, ProductIdentifier, Scanner, ShopifyImg } from "./components";
import { DxpAppVersionInfo, DxpImage, DxpLogin, DxpUserProfile, LanguageSwitcher, DxpMenuFooterNavigation, OmsInstanceNavigator, ProductIdentifier, Scanner, ShopifyImg } from "./components";
import { goToOms, getProductIdentificationValue } from "./utils";
import { initialiseFirebaseApp } from "./utils/firebase"
import piniaPluginPersistedstate from 'pinia-plugin-persistedstate'
import { createI18n } from 'vue-i18n'
import { useUserStore } from "./store/user";

import "./service-worker"

// TODO: handle cases when the store from app or pinia store are not available
// creating a pinia store for the plugin
const pinia = createPinia();
Expand All @@ -24,6 +26,23 @@ let appContext = {} as any
let productIdentificationContext = {} as any
let notificationContext = {} as any
let userContext = {} as any
let showToast = {} as any

let refreshing = false;

const updateAvailable = ($event: any) => {
const registration = $event.detail;
const updateExists = true;
appContext.config.globalProperties.$store.dispatch('user/updatePwaState', { registration, updateExists });
showToast(translate("New version available, please update the app."));
}

document.addEventListener('swUpdated', updateAvailable, { once: true })
navigator.serviceWorker.addEventListener('controllerchange', () => {
if (refreshing) return
refreshing = true
window.location.reload()
})

// executed on app initialization
export let dxpComponents = {
Expand All @@ -42,7 +61,7 @@ export let dxpComponents = {
app.use(pinia);
app.use(i18n);

app.component('AppVersionInfo', AppVersionInfo)
app.component('DxpAppVersionInfo', DxpAppVersionInfo)
app.component('DxpImage', DxpImage)
app.component('DxpUserProfile', DxpUserProfile)
app.component('LanguageSwitcher', LanguageSwitcher)
Expand All @@ -53,6 +72,8 @@ export let dxpComponents = {
app.component('Scanner', Scanner)
app.component('ShopifyImg', ShopifyImg)

showToast = options.showToast

loginContext.login = options.login
loginContext.logout = options.logout
loginContext.loader = options.loader
Expand Down
36 changes: 36 additions & 0 deletions src/service-worker.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { register } from 'register-service-worker'

declare var process: any;

if (process.env.NODE_ENV === 'production') {
register(`${process.env.BASE_URL}service-worker.js`, {
ready () {
console.log(
'App is being served from cache by a service worker.\n' +
'For more details, visit https://goo.gl/AFskqB'
)
},
registered (registration: any) {
console.log('Service worker has been registered.')
setInterval(() => {
registration.update();
}, 1000 * 60 * 60); // check for service worker update hourly
},
cached () {
console.log('Content has been cached for offline use.')
},
updatefound () {
console.log('New content is downloading.')
},
updated (registration: any) {
console.log('New content is available; please refresh.')
document.dispatchEvent(new CustomEvent('swUpdated', { detail: registration }))
},
offline () {
console.log('No internet connection found. App is running in offline mode.')
},
error (error: any) {
console.error('Error during service worker registration:', error)
}
})
}
24 changes: 23 additions & 1 deletion src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { toastController } from "@ionic/vue";
import { translate } from "src";

const goToOms = (token: string, oms: string) => {
const link = (oms.startsWith('http') ? oms.replace(/api\/?/, "") : `https://${oms}.hotwax.io/`) + `commerce/control/main?token=${token}`

Expand All @@ -24,7 +27,26 @@ const getProductIdentificationValue = (productIdentifier: string, product: any)
return value;
}

const showToast = async (message: string, configButtons?: any) => {
const defaultButtons = [{
text: 'Dismiss',
role: 'cancel'
}]

if (configButtons) defaultButtons.push(...configButtons);

const toast = await toastController
.create({
message: message,
duration: 3000,
position: 'bottom',
buttons: defaultButtons
})
return toast.present();
}

export {
getProductIdentificationValue,
goToOms
goToOms,
showToast
}

0 comments on commit 7eb9e53

Please sign in to comment.