-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Implemented: functionality to product store card on facility details page (#10)
- Loading branch information
Showing
11 changed files
with
463 additions
and
85 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,199 @@ | ||
<template> | ||
<ion-content> | ||
<ion-list> | ||
<ion-list-header>{{ getProductStore(currentProductStore.productStoreId).storeName }}</ion-list-header> | ||
<ion-item button @click="makePrimary()"> | ||
{{ translate("Primary") }} | ||
<ion-icon slot="end" :color="primaryMember.facilityGroupId === currentProductStore.productStoreId ? 'warning' : ''" :icon="primaryMember.facilityGroupId === currentProductStore.productStoreId ? star : starOutline" /> | ||
</ion-item> | ||
<ion-item button lines="none" @click="removeStoreFromFacility()"> | ||
{{ translate("Unlink") }} | ||
<ion-icon slot="end" :icon="removeCircleOutline" /> | ||
</ion-item> | ||
</ion-list> | ||
</ion-content> | ||
</template> | ||
|
||
<script lang="ts"> | ||
import { | ||
IonContent, | ||
IonIcon, | ||
IonItem, | ||
IonList, | ||
IonListHeader, | ||
popoverController | ||
} from "@ionic/vue"; | ||
import { defineComponent } from "vue"; | ||
import { removeCircleOutline, star, starOutline } from "ionicons/icons"; | ||
import { translate } from "@hotwax/dxp-components"; | ||
import { mapGetters, useStore } from "vuex"; | ||
import { FacilityService } from "@/services/FacilityService"; | ||
import { DateTime } from "luxon"; | ||
import { hasError } from "@/adapter"; | ||
import { showToast } from "@/utils"; | ||
import logger from "@/logger"; | ||
export default defineComponent({ | ||
name: "ProductStorePopover", | ||
components: { | ||
IonContent, | ||
IonIcon, | ||
IonItem, | ||
IonList, | ||
IonListHeader | ||
}, | ||
props: ['currentProductStore', 'facilityId', 'primaryMember'], | ||
computed: { | ||
...mapGetters({ | ||
facilityProductStores: 'facility/getFacilityProductStores', | ||
getProductStore: 'util/getProductStore', | ||
productStores: 'util/getProductStores' | ||
}) | ||
}, | ||
methods: { | ||
async removeStoreFromFacility() { | ||
try { | ||
const resp = await FacilityService.updateProductStoreFacility({ | ||
facilityId: this.facilityId, | ||
productStoreId: this.currentProductStore.productStoreId, | ||
fromDate: this.currentProductStore.fromDate, | ||
thruDate: DateTime.now().toMillis() | ||
}) | ||
if(!hasError(resp)) { | ||
showToast(translate('Store unlinked successfully.')) | ||
// Removing store from primary Member group if primary. | ||
if(this.currentProductStore.productStoreId === this.primaryMember.facilityGroupId){ | ||
await this.revokePrimaryStatusFromStore() | ||
} | ||
// refetching product stores with updated roles | ||
await this.store.dispatch('facility/getFacilityProductStores', { facilityId: this.facilityId }) | ||
} else { | ||
throw resp.data | ||
} | ||
} catch(err) { | ||
logger.error(err) | ||
showToast(translate('Store unlink failed.')) | ||
} | ||
popoverController.dismiss() | ||
}, | ||
async makePrimary() { | ||
const productStoreId = this.currentProductStore.productStoreId | ||
if(this.primaryMember.facilityGroupId === productStoreId) { | ||
this.revokePrimaryStatusFromStore() | ||
popoverController.dismiss() | ||
return; | ||
} | ||
let resp; | ||
let facilityGroupId; | ||
// Fetching the facility group corresponding to the selected product store. | ||
// There should be one facility group where facilityGroupId equals to productStoreId in order | ||
// to manage primary product store of a facility. | ||
facilityGroupId = await this.fetchFacilityGroup(productStoreId) | ||
// Create one facility group corresponding to the selected product store if not exists. | ||
if(!facilityGroupId) { | ||
facilityGroupId = await this.createFacilityGroup(productStoreId) | ||
} | ||
if(facilityGroupId) { | ||
try { | ||
resp = await FacilityService.addFacilityToGroup({ | ||
facilityId: this.facilityId, | ||
facilityGroupId: facilityGroupId | ||
}) | ||
if(!hasError(resp)) { | ||
// Remove old primary store | ||
if(this.primaryMember.facilityGroupId) { | ||
await this.revokePrimaryStatusFromStore() | ||
} | ||
} else { | ||
throw resp.data | ||
} | ||
} catch(err) { | ||
showToast(translate("Failed to make product store primary.")) | ||
logger.error(err) | ||
} | ||
} else { | ||
showToast(translate("Failed to make product store primary.")) | ||
} | ||
popoverController.dismiss() | ||
}, | ||
async fetchFacilityGroup(productStoreId: string) { | ||
let facilityGroupId; | ||
let resp; | ||
try { | ||
resp = await FacilityService.fetchFacilityGroup({ | ||
inputFields: { | ||
facilityGroupId: productStoreId | ||
}, | ||
entityName: 'FacilityGroup', | ||
viewSize: 100 | ||
}) | ||
if(!hasError(resp)) { | ||
facilityGroupId = resp.data.docs[0].facilityGroupId | ||
} else { | ||
throw resp.data | ||
} | ||
} catch(err) { | ||
logger.error(err) | ||
} | ||
return facilityGroupId | ||
}, | ||
async createFacilityGroup(productStoreId: string) { | ||
let facilityGroupId; | ||
try { | ||
const resp = await FacilityService.createFacilityGroup({ | ||
facilityGroupId: productStoreId, | ||
facilityGroupName: this.getProductStore(productStoreId).storeName, | ||
facilityGroupTypeId: 'FEATURING' | ||
}) | ||
if(!hasError(resp)) { | ||
facilityGroupId = resp.data.facilityGroupId | ||
} else { | ||
throw resp.data | ||
} | ||
} catch(err) { | ||
logger.error(err) | ||
} | ||
return facilityGroupId | ||
}, | ||
async revokePrimaryStatusFromStore() { | ||
let resp; | ||
try { | ||
resp = await FacilityService.updateFacilityToGroup({ | ||
"facilityId": this.facilityId, | ||
"facilityGroupId": this.primaryMember.facilityGroupId, | ||
"fromDate": this.primaryMember.fromDate, | ||
"thruDate": DateTime.now().toMillis() | ||
}) | ||
if(hasError(resp)) { | ||
throw resp.data | ||
} | ||
} catch (err) { | ||
logger.error(err) | ||
} | ||
} | ||
}, | ||
setup() { | ||
const store = useStore(); | ||
return { | ||
removeCircleOutline, | ||
star, | ||
starOutline, | ||
store, | ||
translate | ||
}; | ||
} | ||
}); | ||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.