Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(block-refresh): fix block list not refresh when create update and delete #1056

Open
wants to merge 3 commits into
base: refactor/develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions packages/plugins/block/src/Main.vue
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ import BlockGroupArrange from './BlockGroupArrange.vue'
import CategoryEdit from './CategoryEdit.vue'
import SaveNewBlock from './SaveNewBlock.vue'
import {
setCurrentCategory,
saveBlock,
initEditBlock,
mountedHook,
Expand Down Expand Up @@ -347,13 +348,8 @@ export default {
}

const changeCategory = (val) => {
let params = useBlock().shouldReplaceCategoryWithGroup() ? { groupId: val } : { categoryId: val }

if (!val) {
params = {}
}

updateBlockList(params)
setCurrentCategory(val)
updateBlockList()
}

const editCategory = (category) => {
Expand Down
38 changes: 27 additions & 11 deletions packages/plugins/block/src/js/blockSetting.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
*
*/

import { reactive, readonly, onMounted } from 'vue'
import { ref, reactive, readonly, onMounted } from 'vue'
import { extend } from '@opentiny/vue-renderless/common/object'
import { remove } from '@opentiny/vue-renderless/common/array'
import {
Expand Down Expand Up @@ -53,6 +53,7 @@ import { generateBlock } from '@opentiny/tiny-engine-common/js/vscodeGenerateFil
const { HOST_TYPE } = constants

const STRING_SLOT = ['Slot', 'slot']
const currentCategory = ref('')

// 区块暴露属性和事件的类型
export const META_TYPES = {
Expand Down Expand Up @@ -275,6 +276,10 @@ const state = reactive({
arrayConfig: []
})

export const setCurrentCategory = (categoryId) => {
currentCategory.value = categoryId
}

export const getMaterialHistory = () => state.materialHistory

export const setMaterialHistory = (value) => {
Expand Down Expand Up @@ -360,6 +365,8 @@ export const renameBlockEventName = (name, oldName) => {
delete events[oldName]
}

const getAppId = () => getMetaApi(META_SERVICE.GlobalService).getBaseInfo().id

export const initEditBlock = (block) => {
const currentBlock = useBlock().getCurrentBlock()
// 如果当前点击的区块和画布中的区块是同一区块,则直接获取最新的区块数据
Expand Down Expand Up @@ -391,6 +398,20 @@ export const getBlockBase64 = () => {
})
}

export const updateBlockList = () => {
let params = useBlock().shouldReplaceCategoryWithGroup()
? { groupId: currentCategory.value }
: { categoryId: currentCategory.value }
if (!currentCategory.value) {
params = {}
}
const appId = getAppId()
fetchBlockList({ appId, ...params }).then((data) => {
const blockListDescByUpdateAt = data.sort((a, b) => new Date(b.updated_at) - new Date(a.updated_at))
useBlock().setBlockList(blockListDescByUpdateAt)
})
}

export const delBlock = (closePanel) => () => {
const { getBlockList } = useBlock()
const { message } = useModal()
Expand All @@ -403,6 +424,7 @@ export const delBlock = (closePanel) => () => {
// data:后台删除成功返回的是被删除的数据
remove(getBlockList(), block)
message({ message: '删除区块成功!', status: 'success' })
updateBlockList()
closePanel()
})
.catch((error) => {
Expand Down Expand Up @@ -511,15 +533,15 @@ export const publishBlock = (params) => {
.then(() => {
refreshBlockData(block)
useNotify({ message: '区块发布成功!', type: 'success' })
updateBlockList()
useBlock().isRefresh.value = true
})
.catch((error) => {
useModal().message({ message: error.message, status: 'error' })
})
}
}

const getAppId = () => getMetaApi(META_SERVICE.GlobalService).getBaseInfo().id

const getCategories = () => {
const appId = getAppId()
const fetchData = useBlock().shouldReplaceCategoryWithGroup() ? fetchGroups : fetchCategories
Expand Down Expand Up @@ -633,6 +655,7 @@ const updateBlock = (block = {}) => {
}
// 更新区块分类数据,分类下区块不为空的不能删除
getCategories()
useBlock().isRefresh.value = true
})
.catch((error) => {
useModal().message({ message: error.message, status: 'error' })
Expand Down Expand Up @@ -697,17 +720,10 @@ export const saveBlock = async (block) => {

const actionPromise = block.id ? updateBlock(block) : createBlock(block)
await actionPromise
updateBlockList()
}
}

export const updateBlockList = (params) => {
const appId = getAppId()
fetchBlockList({ appId, ...params }).then((data) => {
const blockListDescByUpdateAt = data.sort((a, b) => new Date(b.updated_at) - new Date(a.updated_at))
useBlock().setBlockList(blockListDescByUpdateAt)
})
}

export const fetchMaterialId = () => {
fetchComponentsMap(getAppId()).then((data) => {
setMaterialHistory(data?.materialHistory)
Expand Down
Loading