2023-04-27 06:48:58 +00:00
|
|
|
<template>
|
2023-11-06 12:03:48 +00:00
|
|
|
<div>
|
|
|
|
<Toasts></Toasts>
|
|
|
|
<PageContent
|
|
|
|
:page="page"
|
|
|
|
:path="path"
|
|
|
|
:params="params"
|
|
|
|
:elements="elements"
|
|
|
|
/>
|
|
|
|
</div>
|
2023-04-27 06:48:58 +00:00
|
|
|
</template>
|
|
|
|
|
|
|
|
<script>
|
2023-08-04 14:29:34 +00:00
|
|
|
import PageContent from '@baserow/modules/builder/components/page/PageContent'
|
2023-04-27 06:48:58 +00:00
|
|
|
import { resolveApplicationRoute } from '@baserow/modules/builder/utils/routing'
|
2023-09-21 14:24:02 +00:00
|
|
|
|
|
|
|
import { DataProviderType } from '@baserow/modules/core/dataProviderTypes'
|
2023-11-06 12:03:48 +00:00
|
|
|
import Toasts from '@baserow/modules/core/components/toasts/Toasts'
|
2023-12-15 09:04:09 +00:00
|
|
|
import ApplicationBuilderFormulaInputGroup from '@baserow/modules/builder/components/ApplicationBuilderFormulaInputGroup'
|
2024-03-01 13:50:13 +00:00
|
|
|
import _ from 'lodash'
|
2023-04-27 06:48:58 +00:00
|
|
|
|
2024-05-02 14:25:42 +00:00
|
|
|
import {
|
|
|
|
getTokenIfEnoughTimeLeft,
|
|
|
|
setToken,
|
|
|
|
userSourceCookieTokenName,
|
|
|
|
} from '@baserow/modules/core/utils/auth'
|
|
|
|
|
2023-04-27 06:48:58 +00:00
|
|
|
export default {
|
2023-11-06 12:03:48 +00:00
|
|
|
components: { PageContent, Toasts },
|
2023-05-11 15:27:17 +00:00
|
|
|
provide() {
|
2023-12-15 09:04:09 +00:00
|
|
|
return {
|
|
|
|
builder: this.builder,
|
|
|
|
page: this.page,
|
|
|
|
mode: this.mode,
|
|
|
|
formulaComponent: ApplicationBuilderFormulaInputGroup,
|
|
|
|
}
|
2023-05-11 15:27:17 +00:00
|
|
|
},
|
2024-05-02 14:25:42 +00:00
|
|
|
async asyncData({ store, params, error, $registry, app, req, route }) {
|
2023-05-11 15:27:17 +00:00
|
|
|
let mode = 'public'
|
2023-08-11 10:02:53 +00:00
|
|
|
const builderId = parseInt(params.builderId, 10)
|
2023-04-27 06:48:58 +00:00
|
|
|
|
2023-08-11 10:02:53 +00:00
|
|
|
// We have a builderId parameter in the path so it's a preview
|
|
|
|
if (builderId) {
|
|
|
|
mode = 'preview'
|
|
|
|
}
|
|
|
|
|
|
|
|
let builder = store.getters['application/getSelected']
|
|
|
|
|
|
|
|
if (!builder || builderId !== builder.id) {
|
2023-04-27 06:48:58 +00:00
|
|
|
try {
|
|
|
|
if (builderId) {
|
|
|
|
// We have the builderId in the params so this is a preview
|
|
|
|
// Must fetch the builder instance by this Id.
|
2023-08-11 10:02:53 +00:00
|
|
|
await store.dispatch('publicBuilder/fetchById', {
|
2023-04-27 06:48:58 +00:00
|
|
|
builderId,
|
|
|
|
})
|
2023-08-11 10:02:53 +00:00
|
|
|
builder = await store.dispatch('application/selectById', builderId)
|
2023-04-27 06:48:58 +00:00
|
|
|
} else {
|
|
|
|
// We don't have the builderId so it's a public page.
|
|
|
|
// Must fetch the builder instance by domain name.
|
2023-08-11 10:02:53 +00:00
|
|
|
const host = process.server ? req.headers.host : window.location.host
|
2023-04-27 06:48:58 +00:00
|
|
|
const domain = new URL(`http://${host}`).hostname
|
|
|
|
|
2023-08-11 10:02:53 +00:00
|
|
|
const { id: receivedBuilderId } = await store.dispatch(
|
|
|
|
'publicBuilder/fetchByDomain',
|
|
|
|
{
|
|
|
|
domain,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
builder = await store.dispatch(
|
|
|
|
'application/selectById',
|
|
|
|
receivedBuilderId
|
|
|
|
)
|
2023-04-27 06:48:58 +00:00
|
|
|
}
|
|
|
|
} catch (e) {
|
2023-08-11 10:02:53 +00:00
|
|
|
return error({
|
2023-04-27 06:48:58 +00:00
|
|
|
statusCode: 404,
|
2023-08-11 10:02:53 +00:00
|
|
|
message: app.i18n.t('publicPage.siteNotFound'),
|
2023-04-27 06:48:58 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2024-05-02 14:25:42 +00:00
|
|
|
store.dispatch('userSourceUser/setCurrentApplication', {
|
|
|
|
application: builder,
|
|
|
|
})
|
|
|
|
if (
|
|
|
|
(!process.server || req) &&
|
|
|
|
!store.getters['userSourceUser/isAuthenticated'](builder)
|
|
|
|
) {
|
|
|
|
// token can be in the query string (SSO) or in the cookies (previous session)
|
|
|
|
let refreshToken = route.query.token
|
|
|
|
if (refreshToken) {
|
|
|
|
setToken(app, refreshToken, userSourceCookieTokenName, {
|
|
|
|
sameSite: 'Lax',
|
|
|
|
})
|
|
|
|
} else {
|
|
|
|
refreshToken = getTokenIfEnoughTimeLeft(app, userSourceCookieTokenName)
|
|
|
|
}
|
|
|
|
|
|
|
|
if (refreshToken) {
|
|
|
|
try {
|
|
|
|
await store.dispatch('userSourceUser/refreshAuth', {
|
|
|
|
application: builder,
|
|
|
|
token: refreshToken,
|
|
|
|
})
|
|
|
|
} catch (error) {
|
|
|
|
if (error.response?.status === 401) {
|
|
|
|
// We logoff as the token has probably expired
|
|
|
|
await store.dispatch('userSourceUser/logoff', {
|
|
|
|
application: builder,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
throw error
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-04-27 06:48:58 +00:00
|
|
|
|
2023-08-11 10:02:53 +00:00
|
|
|
const found = resolveApplicationRoute(builder.pages, params.pathMatch)
|
2023-04-27 06:48:58 +00:00
|
|
|
|
|
|
|
// Handle 404
|
|
|
|
if (!found) {
|
2023-08-11 10:02:53 +00:00
|
|
|
return error({
|
2023-04-27 06:48:58 +00:00
|
|
|
statusCode: 404,
|
2023-08-11 10:02:53 +00:00
|
|
|
message: app.i18n.t('publicPage.pageNotFound'),
|
2023-04-27 06:48:58 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2023-08-11 10:02:53 +00:00
|
|
|
const [pageFound, path, pageParamsValue] = found
|
2023-04-27 06:48:58 +00:00
|
|
|
|
2023-08-11 10:02:53 +00:00
|
|
|
const page = await store.getters['page/getById'](builder, pageFound.id)
|
2023-08-04 14:29:34 +00:00
|
|
|
|
2023-08-11 10:02:53 +00:00
|
|
|
await Promise.all([
|
|
|
|
store.dispatch('dataSource/fetchPublished', {
|
|
|
|
page,
|
|
|
|
}),
|
|
|
|
store.dispatch('element/fetchPublished', { page }),
|
2023-11-06 12:03:48 +00:00
|
|
|
store.dispatch('workflowAction/fetchPublished', { page }),
|
2023-08-11 10:02:53 +00:00
|
|
|
])
|
2023-08-04 14:29:34 +00:00
|
|
|
|
2023-09-21 14:24:02 +00:00
|
|
|
await DataProviderType.initAll($registry.getAll('builderDataProvider'), {
|
|
|
|
builder,
|
|
|
|
page,
|
|
|
|
pageParamsValue,
|
|
|
|
mode,
|
|
|
|
})
|
2023-08-04 14:29:34 +00:00
|
|
|
|
2023-08-11 10:02:53 +00:00
|
|
|
// And finally select the page to display it
|
|
|
|
await store.dispatch('page/selectById', {
|
|
|
|
builder,
|
|
|
|
pageId: pageFound.id,
|
|
|
|
})
|
2023-08-04 14:29:34 +00:00
|
|
|
|
2023-04-27 06:48:58 +00:00
|
|
|
return {
|
|
|
|
builder,
|
|
|
|
page,
|
|
|
|
path,
|
|
|
|
params,
|
2023-05-11 15:27:17 +00:00
|
|
|
mode,
|
2023-04-27 06:48:58 +00:00
|
|
|
}
|
|
|
|
},
|
2023-06-27 09:29:02 +00:00
|
|
|
head() {
|
|
|
|
return {
|
|
|
|
titleTemplate: '',
|
|
|
|
title: this.page.name,
|
|
|
|
bodyAttrs: {
|
|
|
|
class: 'public-page',
|
|
|
|
},
|
2024-04-23 12:12:46 +00:00
|
|
|
link: this.faviconLink,
|
2023-06-27 09:29:02 +00:00
|
|
|
}
|
|
|
|
},
|
2023-08-04 14:29:34 +00:00
|
|
|
computed: {
|
2023-08-11 10:02:53 +00:00
|
|
|
elements() {
|
|
|
|
return this.$store.getters['element/getRootElements'](this.page)
|
|
|
|
},
|
2023-09-21 14:24:02 +00:00
|
|
|
applicationContext() {
|
|
|
|
return {
|
|
|
|
builder: this.builder,
|
|
|
|
page: this.page,
|
|
|
|
pageParamsValue: this.params,
|
|
|
|
mode: this.mode,
|
|
|
|
}
|
2023-08-11 10:02:53 +00:00
|
|
|
},
|
2023-10-31 16:29:20 +00:00
|
|
|
dispatchContext() {
|
2024-04-03 13:40:20 +00:00
|
|
|
return DataProviderType.getAllDataSourceDispatchContext(
|
2023-09-21 14:24:02 +00:00
|
|
|
this.$registry.getAll('builderDataProvider'),
|
|
|
|
this.applicationContext
|
|
|
|
)
|
2023-08-11 10:02:53 +00:00
|
|
|
},
|
2024-03-26 15:46:43 +00:00
|
|
|
isAuthenticated() {
|
2024-05-02 14:25:42 +00:00
|
|
|
return this.$store.getters['userSourceUser/isAuthenticated'](this.builder)
|
2024-03-26 15:46:43 +00:00
|
|
|
},
|
2024-04-23 12:12:46 +00:00
|
|
|
faviconLink() {
|
|
|
|
if (this.builder.favicon_file?.url) {
|
|
|
|
return [
|
|
|
|
{
|
|
|
|
rel: 'icon',
|
|
|
|
type: this.builder.favicon_file.mime_type,
|
|
|
|
href: this.builder.favicon_file.url,
|
|
|
|
sizes: '128x128',
|
|
|
|
hid: true,
|
|
|
|
},
|
|
|
|
]
|
|
|
|
} else {
|
|
|
|
return []
|
|
|
|
}
|
|
|
|
},
|
2023-08-11 10:02:53 +00:00
|
|
|
},
|
|
|
|
watch: {
|
2023-10-31 16:29:20 +00:00
|
|
|
dispatchContext: {
|
2023-08-11 10:02:53 +00:00
|
|
|
deep: true,
|
|
|
|
/**
|
2024-02-28 20:20:30 +00:00
|
|
|
* Update data source content on dispatch context changes
|
2023-08-11 10:02:53 +00:00
|
|
|
*/
|
2024-03-01 13:50:13 +00:00
|
|
|
handler(newDispatchContext, oldDispatchContext) {
|
|
|
|
if (!_.isEqual(newDispatchContext, oldDispatchContext)) {
|
|
|
|
this.$store.dispatch(
|
|
|
|
'dataSourceContent/debouncedFetchPageDataSourceContent',
|
|
|
|
{
|
|
|
|
page: this.page,
|
|
|
|
data: newDispatchContext,
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
2023-08-11 10:02:53 +00:00
|
|
|
},
|
|
|
|
},
|
2024-03-26 15:46:43 +00:00
|
|
|
isAuthenticated() {
|
|
|
|
// When the user login or logout, we need to refetch the elements and actions
|
|
|
|
// as they might have changed
|
|
|
|
this.$store.dispatch('element/fetchPublished', { page: this.page })
|
|
|
|
this.$store.dispatch('workflowAction/fetchPublished', { page: this.page })
|
|
|
|
},
|
2023-08-04 14:29:34 +00:00
|
|
|
},
|
2023-04-27 06:48:58 +00:00
|
|
|
}
|
|
|
|
</script>
|