mirror of
https://github.com/baserow/baserow.git
synced 2026-12-04 00:08:25 +00:00
* fix: keep published page data when a refresh is refused * Review feedback
169 lines
4.4 KiB
JavaScript
169 lines
4.4 KiB
JavaScript
import _ from 'lodash'
|
|
import DataSourceService from '@baserow/modules/builder/services/dataSource'
|
|
import PublishedBuilderService from '@baserow/modules/builder/services/publishedBuilder'
|
|
import { handleDispatchError } from '@baserow/modules/builder/utils/error'
|
|
|
|
const state = () => ({})
|
|
|
|
const fetchTimeoutPerPage = {}
|
|
|
|
const mutations = {
|
|
SET_CONTENT(state, { page, dataSourceId, value }) {
|
|
if (!page.contents) {
|
|
// In Vue 3, direct assignment is reactive
|
|
page.contents = {}
|
|
}
|
|
|
|
if (!_.isEqual(page.contents[dataSourceId], value)) {
|
|
// Here we need to change the reference of the dataSourceContents object to
|
|
// trigger computed values that use it in some situation (before the key exists
|
|
// for instance)
|
|
page.contents = {
|
|
...page.contents,
|
|
[dataSourceId]: value,
|
|
}
|
|
}
|
|
},
|
|
CLEAR_CONTENTS(state, { page }) {
|
|
page.contents = {}
|
|
},
|
|
// Clear only the contents for the specified data source.
|
|
CLEAR_CONTENT(state, { page, dataSourceId }) {
|
|
const contents = Object.assign({}, page.contents)
|
|
delete contents[dataSourceId]
|
|
page.contents = contents
|
|
},
|
|
SET_LOADING(state, { page, value }) {
|
|
page._.dataSourceContentLoading = value
|
|
},
|
|
}
|
|
|
|
const actions = {
|
|
/**
|
|
* Fetch the content for every data sources of the given page.
|
|
*/
|
|
async fetchPageDataSourceContent(
|
|
{ commit, rootGetters },
|
|
{ page, data: queryData, mode }
|
|
) {
|
|
const { $registry, $i18n, $client, $config } = this
|
|
commit('SET_LOADING', { page, value: true })
|
|
|
|
let service = DataSourceService
|
|
if (['preview', 'public'].includes(mode)) {
|
|
service = PublishedBuilderService
|
|
}
|
|
|
|
const failedDataSources = []
|
|
try {
|
|
const { data } = await service($client).dispatchAll(
|
|
page.id,
|
|
queryData,
|
|
rootGetters['publicBuilder/getPreviewBuilderId']
|
|
)
|
|
|
|
Object.entries(data).forEach(([dataSourceIdStr, dataContent]) => {
|
|
const dataSourceId = parseInt(dataSourceIdStr, 10)
|
|
if (dataContent._error) {
|
|
failedDataSources.push({
|
|
id: dataSourceId,
|
|
error: { error: dataContent._error, detail: dataContent.detail },
|
|
})
|
|
commit('SET_CONTENT', { page, dataSourceId, value: null })
|
|
} else {
|
|
commit('SET_CONTENT', { page, dataSourceId, value: dataContent })
|
|
}
|
|
})
|
|
} catch (e) {
|
|
commit('CLEAR_CONTENTS', { page })
|
|
throw e
|
|
} finally {
|
|
commit('SET_LOADING', { page, value: false })
|
|
}
|
|
return failedDataSources
|
|
},
|
|
|
|
async fetchPageDataSourceContentById(
|
|
{ commit, rootGetters },
|
|
{ page, dataSourceId, dispatchContext, mode, replace = false }
|
|
) {
|
|
const { $registry, $i18n, $client, $config } = this
|
|
commit('SET_LOADING', { page, value: true })
|
|
|
|
let service = DataSourceService
|
|
if (['preview', 'public'].includes(mode)) {
|
|
service = PublishedBuilderService
|
|
}
|
|
|
|
try {
|
|
const { data } = await service($client).dispatch(
|
|
dataSourceId,
|
|
dispatchContext,
|
|
{ range: null },
|
|
null,
|
|
rootGetters['publicBuilder/getPreviewBuilderId']
|
|
)
|
|
|
|
if (replace) {
|
|
commit('CLEAR_CONTENT', { page, dataSourceId })
|
|
}
|
|
|
|
commit('SET_CONTENT', {
|
|
page,
|
|
dataSourceId,
|
|
value: data,
|
|
})
|
|
} catch (error) {
|
|
commit('CLEAR_CONTENT', { page, dataSourceId })
|
|
throw error
|
|
} finally {
|
|
commit('SET_LOADING', { page, value: false })
|
|
}
|
|
},
|
|
|
|
debouncedFetchPageDataSourceContent(
|
|
{ dispatch },
|
|
{ page, data: queryData, mode }
|
|
) {
|
|
if (fetchTimeoutPerPage[page.id]) {
|
|
clearTimeout(fetchTimeoutPerPage[page.id])
|
|
}
|
|
fetchTimeoutPerPage[page.id] = setTimeout(async () => {
|
|
try {
|
|
await dispatch('fetchPageDataSourceContent', {
|
|
page,
|
|
data: queryData,
|
|
mode,
|
|
})
|
|
} catch (error) {
|
|
handleDispatchError(error, this.app)
|
|
}
|
|
}, 500)
|
|
},
|
|
|
|
clearDataSourceContent({ commit }, { page, dataSourceId }) {
|
|
commit('SET_CONTENT', { page, dataSourceId, value: null })
|
|
},
|
|
|
|
clearDataSourceContents({ commit }, { page }) {
|
|
commit('CLEAR_CONTENTS', { page })
|
|
},
|
|
}
|
|
|
|
const getters = {
|
|
getDataSourceContents: (state) => (page) => {
|
|
return page.contents || {}
|
|
},
|
|
getLoading: (state) => (page) => {
|
|
return page._.dataSourceContentLoading
|
|
},
|
|
}
|
|
|
|
export default {
|
|
namespaced: true,
|
|
state,
|
|
getters,
|
|
actions,
|
|
mutations,
|
|
}
|