From a0413df2a3a4ca0e268414d06d4a41ee1f677b67 Mon Sep 17 00:00:00 2001 From: mateuswetah Date: Tue, 28 Jun 2022 16:36:23 -0300 Subject: [PATCH] Basic item creation flow working. #20 , #15 and #17. --- src/pages/CollectionPage.vue | 15 +++++++++++++-- src/pages/LoginPage.vue | 15 +++++++-------- src/store/storeWp.ts | 36 ++++++++++++++++++++++-------------- 3 files changed, 42 insertions(+), 24 deletions(-) diff --git a/src/pages/CollectionPage.vue b/src/pages/CollectionPage.vue index 7ce9b71..75b13fd 100644 --- a/src/pages/CollectionPage.vue +++ b/src/pages/CollectionPage.vue @@ -44,6 +44,7 @@ import { } from '@ionic/vue'; import BaseLayout from '@/components/base/BaseLayout.vue'; import ItemsList from '@/components/lists/ItemsList.vue'; +import { InAppBrowserEvent } from '@awesome-cordova-plugins/in-app-browser'; export default defineComponent({ components: { @@ -116,8 +117,18 @@ export default defineComponent({ icon: documentOutline, data: 'single item', handler: () => { - console.log('Item simples') - wpStore.openAppBrowser('/collections/' + props.id + '/items/new'); + wpStore.openInAppBrowser('/collections/' + props.id + '/items/new'); + wpStore.listenEventInAppBrowser((event: InAppBrowserEvent) => { + if (event && + event.data && + event.data.type === 'item_updated' && + event.data.item && + event.data.item.status !== 'auto-draft' + ) { + wpStore.hideInAppBrowser(); + loadItemsByCollection({}, true); + } + }); }, }, { diff --git a/src/pages/LoginPage.vue b/src/pages/LoginPage.vue index 05d1f41..b373ff9 100644 --- a/src/pages/LoginPage.vue +++ b/src/pages/LoginPage.vue @@ -95,11 +95,8 @@ export default { }, methods: { async openLoginForm() { - await this.wpStore.fetchApplicationAuthorization( - this.siteUrl, - "_self", - "location=no" - ); + this.wpStore.userSiteUrl = this.siteUrl; + await this.wpStore.fetchApplicationAuthorization(this.siteUrl); if (this.wpStore.authorizationURL) { this.wpStore.createInAppBrowser(); this.wpStore.inAppBrowser @@ -108,6 +105,7 @@ export default { } }, async handleBrowserLoadStop(event: InAppBrowserEvent) { + console.log(event) if ( event.url && typeof event.url == "string" && @@ -115,16 +113,17 @@ export default { event.url.split("?").length >= 2 ) { const params = new URLSearchParams(event.url.split("?")[1]); - if (params.get("page") === "tainacan_admin") { + + if ( params.get("page") === "tainacan_admin" ) { const userLogin = params.get("user_login"); let userToken = params.get("password"); - + console.log(userToken) if ( typeof userToken == "string" && userToken.indexOf("#") >= 0 ) userToken = userToken.split("#")[0]; - + console.log(userToken, userLogin) if (!!userLogin && !!userToken) { this.wpStore.inAppBrowser.hide(); diff --git a/src/store/storeWp.ts b/src/store/storeWp.ts index a8f204f..a9b4fe3 100644 --- a/src/store/storeWp.ts +++ b/src/store/storeWp.ts @@ -3,8 +3,10 @@ import axios from "axios"; import { Storage } from "@ionic/storage"; import { InAppBrowser, + InAppBrowserEvent, + InAppBrowserEventType, InAppBrowserObject, - InAppBrowserEvent + InAppBrowserOriginal } from "@awesome-cordova-plugins/in-app-browser/index"; const store = new Storage(); @@ -89,32 +91,38 @@ const useWpStore = defineStore("wp", { this.userLogin = await store.get("userLogin"); this.userToken = await store.get("userToken"); }, - createInAppBrowser() { - let tainacanAdminUrl = this.userSiteUrl + "/wp-admin/admin.php?page=tainacan_admin&mobileAppMode=true"; + createInAppBrowser(url = '',extraParams = 'location=no,fullscreen=no,zoom=no') { + let tainacanAdminUrl = this.userSiteUrl + "/wp-admin/admin.php?page=tainacan_admin&mobileAppMode=true&itemEditionMode=true" + url; if (!this.userIsLoggedIn && this.authorizationURL) tainacanAdminUrl = this.authorizationURL + "?app_name=TainacanMobileApp&success_url=" + tainacanAdminUrl; - const anInAppBrowser = InAppBrowser.create(tainacanAdminUrl); + const anInAppBrowser = InAppBrowser.create(tainacanAdminUrl, '_blank', extraParams); this.inAppBrowser = anInAppBrowser; }, - openAppBrowser(url: string) { - const inAppBrowserScript = ` - window.addEventListener('hashchange', function() { - console.log(window.location.hash); - }); + openInAppBrowser(url: string) { + + if (!this.inAppBrowser || !this.inAppBrowser.executeScript) + this.createInAppBrowser('#' + url, 'hidden=yes,location=no,fullscreen=no,zoom=no'); + else { + const urlRedirectionScript = ` window.history.replaceState( null, null, - '${this.userSiteUrl}/wp-admin/admin.php?mobileAppMode=true&page=tainacan_admin#${url}' + '${this.userSiteUrl}/wp-admin/admin.php?page=tainacan_admin&mobileAppMode=true&itemEditionMode=true#${url}' ); window.history.go(0); `; - if (!this.inAppBrowser || !this.inAppBrowser.executeScript) - this.createInAppBrowser(); - - this.inAppBrowser.executeScript({ code: inAppBrowserScript }); + this.inAppBrowser.executeScript({ code: urlRedirectionScript }); + } this.inAppBrowser.show(); }, + hideInAppBrowser() { + if (this.inAppBrowser && this.inAppBrowser.hide) + this.inAppBrowser.hide(); + }, + listenEventInAppBrowser(event: any) { + this.inAppBrowser.on('message').subscribe(event); + } }, }); export { useWpStore };