ice.fo.utils
Version:
276 lines (223 loc) • 7.14 kB
JavaScript
import dayjs from 'dayjs'
import { getValue } from 'ice.fo.utils/GetterUtils'
import { DEVICE_RENDER_CHANNEL, ROUTE_NAMES } from 'ice.fo.utils/constants/enums'
import { EditorSignal } from 'ice.fo.utils/WindowUtils'
export function checkIsBuilder ({ route }) {
return !!route.query.__editorId && [
ROUTE_NAMES.BuilderEditablePage,
ROUTE_NAMES.ContentEditablePage,
ROUTE_NAMES.LandingPageBuilder,
ROUTE_NAMES.ImagePageBuilder,
ROUTE_NAMES.GuideFrame,
'ice-preview',
].includes(route.name)
}
export function checkExposureRules ({ rule, $nuxt }) {
if (!rule) {
return true
}
const exposureType = getValue(rule.exposureType)
// [Exposure] Check visible by Locales
const currentLocale = $nuxt.$i18n && $nuxt.$i18n.locale
const displayLocales = rule && (rule.locale || []).map(i => i.value)
if (currentLocale && displayLocales && displayLocales.length && !displayLocales.includes(currentLocale)) {
return false
}
// [Exposure] Check visible by Device Mode
const displayChannel = rule && (rule.channel || []).map(i => i.value)
if (displayChannel && displayChannel.length) {
const visibleForMobile = displayChannel.includes(DEVICE_RENDER_CHANNEL.MOBILE) && $nuxt.$$isMobile
const visibleForDesktop = displayChannel.includes(DEVICE_RENDER_CHANNEL.DESKTOP) && $nuxt.$$isDesktop
const visibleForApp = displayChannel.includes(DEVICE_RENDER_CHANNEL.APP) && $nuxt.$$isApp
if (!visibleForMobile && !visibleForDesktop && !visibleForApp) {
return false
}
}
// [Exposure] Check visible by User Signed Session
const visibleForSigned = rule.signed
const isLoggedIn = $nuxt.$isSigned
if (visibleForSigned && !isLoggedIn) {
return false
}
// [Exposure] Check visible by Period
const useByPeriod = rule.exposurePeriod
const periodFrom = rule.startDate && dayjs(rule.startDate, 'YYYYMMDDHHmmss')
const periodTo = rule.endDate && dayjs(rule.endDate, 'YYYYMMDDHHmmss')
if (useByPeriod && periodFrom && periodTo) {
const now = dayjs()
if (periodFrom.isAfter(now) || periodTo.isBefore(now)) {
return false
}
}
// [Exposure] Check visible by Schedule
if (exposureType == 'schedule') {
const schedules = rule.schedule || []
const visibleForSchedule = schedules.some((schedule) => {
const days = (schedule.days || []).map(i => getValue(i))
const startTime = schedule.startTime && dayjs(schedule.startTime, 'HH:mm:ss')
const endTime = schedule.endTime && dayjs(schedule.endTime, 'HH:mm:ss')
const now = dayjs()
const currentWeekday = now.weekday()
if (!days.some(x => x == currentWeekday)) {
return false
}
if (startTime && now.isBefore(startTime)) {
return false
}
if (endTime && now.isAfter(endTime)) {
return false
}
return true
})
if (!visibleForSchedule) {
return false
}
}
// [Exposure] Check visible by Target
const targeting = rule.targeting || []
const visibleByTarget = targeting.every((i) => {
const field = getValue(i, '', 'field')
const method = getValue(i, '', 'method')
const value = getValue(i, '', 'value')
const isNot = i.isNot
const contextValues = {
...$nuxt.$store.state.session.customerTargetValues,
}
const targetValue = getExposureTargetValue({ values: contextValues, field, $nuxt })
let result
switch (method) {
case 'above':
result = targetValue >= value
break
case 'below':
result = targetValue <= value
break
case 'equals':
result = targetValue == value
break
case 'contains':
break
case 'startsWith':
break
case 'endsWith':
break
case 'fromto':
break
case 'isEmpty':
break
case 'el':
break
}
return isNot ? !result : result
})
if (!visibleByTarget) {
return false
}
// [Exposure] Check complete
return true
}
export function makeSvcAPI (target, tid = 'api') {
if (typeof target == 'string') {
return target
}
target = target.item || target
if (tid == 'api') {
return `/svc/${getValue(target.category)}/${target.apiId}`
}
}
export async function updateInstanceProps ({ $axios, instanceId, props }) {
const { data: { item: instance } } = await $axios.request({
url: '/adm/foInstance/read',
params: {
id: instanceId,
},
})
const propId = instance.props.id
const propClass = instance.props.typeId
await $axios.request({
url: `/adm/${propClass}/update`,
method: 'post',
data: {
_enableFormType: 'create',
...props,
_typeId: propClass,
id: propId,
},
})
}
export async function addLastChildrenFoInstance ({ $axios, parentId, componentId, props }) {
const [{ data: newData }, { data: parentData }] = await Promise.all([
$axios.request({
url: '/adm/foInstance/save',
method: 'post',
data: {
component: componentId,
props,
layout: null,
},
}),
$axios.request({
url: '/adm/foInstance/read',
params: {
id: parentId,
},
}),
])
const newInstance = newData.item
const parentInstance = parentData.item
const newInstanceId = newInstance.id
const parentChildrenIds = (parentInstance.instances || []).map(i => i.id)
// add/push new instance to last children
parentChildrenIds.push(newInstanceId)
await $axios.request({
url: '/adm/foInstance/save',
method: 'post',
data: {
instanceId: parentId,
instances: parentChildrenIds.join(','),
},
})
return newInstance
}
export async function removeChildrenFoInstance ({ $axios, parentId, instanceId }) {
const { data: parentData } = await $axios.request({
url: '/adm/foInstance/read',
params: {
id: parentId,
},
})
const parentInstance = parentData.item
const parentChildrenIds = (parentInstance.instances || []).map(i => i.id)
// Remove select instance from parent
const filteredInstances = parentChildrenIds.filter(id => id != instanceId)
await $axios.request({
url: '/adm/foInstance/save',
method: 'post',
data: {
instanceId: parentId,
instances: filteredInstances.join(','),
},
})
}
function getExposureTargetValue ({ values, field, $nuxt }) {
if (field == 'lastWeekProductRead') {
const lastWeekProductRead = values.lastWeekProductRead || {}
const productId = $nuxt.$route.params.id || $nuxt.$route.query.productId
const key = Object.keys(lastWeekProductRead).find(i => i.endsWith('::' + productId))
return lastWeekProductRead[key] || 0
}
return values[field]
}
export function getElementInstanceId (node) {
// This expects InstanceRenderer.vue to set attribute [bd-instance-id]
if (node.data && node.data.attrs) {
return node.data.attrs['bd-instance-id'] || node.data.props.instanceId
}
if (node instanceof HTMLElement) {
return node.getAttribute('id')
}
return null
}
export function refreshBuilderPage () {
EditorSignal.emit('updatedPage', {})
}