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 list filters not clearing #1006

Merged
Merged
Show file tree
Hide file tree
Changes from 6 commits
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
26 changes: 14 additions & 12 deletions packages/plugins/materials/src/meta/block/src/BlockGroupFilters.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
<div class="block-filters-item-value">
<tiny-checkbox-group
v-if="!filter.usingSelect"
v-model="state.checkGroup[filter.id]"
v-model="state.filterValues[filter.id]"
type="checkbox"
@change="getFilters($event, filter.id)"
@change="handleValueChange"
>
<tiny-checkbox
v-for="item in selectOptions[filter.id]"
Expand All @@ -18,11 +18,11 @@
</tiny-checkbox-group>
<tiny-select
v-else
v-model="state.checkGroup[filter.id]"
v-model="state.filterValues[filter.id]"
size="mini"
multiple
is-drop-inherit-width
@change="getFilters($event, filter.id)"
@change="handleValueChange"
>
<tiny-option
v-for="item in selectOptions[filter.id]"
Expand Down Expand Up @@ -54,15 +54,19 @@ export default {
}
},
setup(props, { emit }) {
const filters = {}
const state = reactive({
checkGroup: props.filters.reduce(
const initFilterValues = () => {
return props.filters.reduce(
(result, filter) => ({
...result,
[filter.id]: []
}),
{}
)
}

const state = reactive({
// filterValues 是一个对象。key 为 filter id,value 为选中的值,是一个数组
filterValues: initFilterValues()
})

// 不同的filter,值所在的字段可能是id或者name。这里把实际的值都映射到value字段
Expand All @@ -79,16 +83,14 @@ export default {
)
})

const getFilters = (checked, id) => {
filters[id] = checked

emit('search', null, filters)
const handleValueChange = () => {
emit('search', state.filterValues)
}

return {
state,
selectOptions,
getFilters
handleValueChange
}
}
}
Expand Down
134 changes: 71 additions & 63 deletions packages/plugins/materials/src/meta/block/src/BlockGroupPanel.vue
Original file line number Diff line number Diff line change
@@ -1,18 +1,22 @@
<template>
<plugin-setting
v-if="panel.created"
v-show="panel.show"
:title="selectedGroup.groupName"
@cancel="closeGroupPanel"
@save="addBlocks"
>
<plugin-setting v-if="panel.show" :title="validGroup.groupName" @cancel="closeGroupPanel" @save="addBlocks">
<template #content>
<div class="block-add-content">
<div class="block-add-content-title">区块列表</div>
<block-group-filters :filters="state.filters" @search="searchBlocks"></block-group-filters>
<block-group-transfer :blockList="filteredBlocks">
<block-group-filters
v-if="panel.show"
:key="validGroup.groupId"
:filters="state.filters"
@search="searchBlocks"
></block-group-filters>
<block-group-transfer :blockList="state.blockList">
<template #search>
<tiny-search class="transfer-order-search" v-model="state.searchValue" placeholder="请输入关键词">
<tiny-search
class="transfer-order-search"
v-model="state.searchValue"
placeholder="请输入关键词"
@update:modelValue="searchBlocks(state.filterValues)"
>
<template #prefix>
<tiny-icon-search />
</template>
Expand All @@ -24,7 +28,7 @@
</plugin-setting>
</template>
<script>
import { nextTick, reactive, watch, provide, inject, ref, computed } from 'vue'
import { reactive, watch, provide, inject, ref } from 'vue'
import { Search } from '@opentiny/vue'
import { iconSearch } from '@opentiny/vue-icon'
import { PluginSetting } from '@opentiny/tiny-engine-common'
Expand All @@ -37,7 +41,6 @@ import {
getMetaApi,
META_SERVICE
} from '@opentiny/tiny-engine-meta-register'
import { utils } from '@opentiny/tiny-engine-utils'
import BlockGroupTransfer from './BlockGroupTransfer.vue'
import BlockGroupFilters from './BlockGroupFilters.vue'

Expand Down Expand Up @@ -73,16 +76,22 @@ export default {
TinyIconSearch: iconSearch()
},
setup() {
const { isDefaultGroupId, isRefresh, selectedGroup, selectedBlockArray, getGroupList, cancelCheckAll } = useBlock()
const {
isAllGroupId,
isDefaultGroupId,
isRefresh,
selectedGroup,
selectedBlockArray,
getGroupList,
cancelCheckAll
} = useBlock()
const { panel, closePanel } = useGroupPanel()
const { message } = useModal()
const getAppId = () => getMetaApi(META_SERVICE.GlobalService).getBaseInfo().id
const panelState = inject('panelState', {})
const blockUsers = ref([])
provide('blockUsers', blockUsers)

const { escapeRegExp } = utils

const state = reactive({
searchValue: '',
blockList: [],
Expand All @@ -104,24 +113,28 @@ export default {
children: [],
usingSelect: true
}
]
],
filterValues: {}
})

const filteredBlocks = computed(() => {
if (!state.searchValue) {
return state.blockList
}
const validGroup = ref({ ...selectedGroup.value })

const searchValue = state.searchValue.trim()
const pattern = new RegExp(escapeRegExp(searchValue), 'i')
watch(
() => selectedGroup.value.groupId,
(groupId) => {
if (groupId && !isAllGroupId(groupId) && !isDefaultGroupId(groupId)) {
validGroup.value = { ...selectedGroup.value }
}
}
)

return state.blockList.filter((block) => {
return pattern.test(block?.name_cn) || pattern.test(block?.label) || pattern.test(block?.description)
})
})
const clearSearchParams = () => {
state.searchValue = ''
state.filterValues = {}
}

const addBlocks = () => {
const groupId = selectedGroup.value.groupId
const groupId = validGroup.value.groupId
fetchGroupBlocksById({ groupId })
.then((data) => {
const resData =
Expand Down Expand Up @@ -153,7 +166,7 @@ export default {
useMaterial().updateCanvasDependencies(addedBlocks)

isRefresh.value = true
state.searchValue = ''
clearSearchParams()
selectedBlockArray.value.length = 0
useResource().fetchResource({ isInit: false }) // 添加区块分组,不需要重新init页面或者区块。
useNotify({
Expand All @@ -176,7 +189,7 @@ export default {
}

const closeGroupPanel = () => {
state.searchValue = ''
clearSearchParams()
gene9831 marked this conversation as resolved.
Show resolved Hide resolved
selectedBlockArray.value.length = 0
panelState.isBlockGroupPanel = false
closePanel()
Expand All @@ -191,32 +204,29 @@ export default {
return blocks.filter((block) => !isInBlockGroup(block) && !isSelectedBlock(block))
}

const searchBlocks = (value, filters) => {
nextTick(() => {
const params = {
groupId: selectedGroup.value.groupId,
label_contains: state.searchValue,
tag: filters?.tag,
publicType: filters?.publicType,
author: filters?.author
}
fetchAvailableBlocks(params)
.then((data) => {
state.blockList = selectedBlockFilter(data)
})
.catch((error) => {
message({
message: `区块搜索失败: ${error.message || error}`,
status: 'error'
})
const searchBlocks = (filters) => {
state.filterValues = filters

const params = {
groupId: validGroup.value.groupId,
label_contains: state.searchValue.trim(),
tag: filters?.tag,
publicType: filters?.publicType,
author: filters?.author
}
fetchAvailableBlocks(params)
.then((data) => {
state.blockList = selectedBlockFilter(data)
})
.catch((error) => {
message({
message: `区块搜索失败: ${error.message || error}`,
status: 'error'
})
})
})
gene9831 marked this conversation as resolved.
Show resolved Hide resolved
}

const fetchBlocks = () => {
const groupId = selectedGroup.value.groupId
if (!groupId || isDefaultGroupId(groupId)) return

const fetchBlocks = (groupId) => {
fetchAvailableBlocks({ groupId })
.then((data) => {
initGruopBlockMap(getGroupList())
Expand All @@ -231,10 +241,6 @@ export default {
}

const getFilters = () => {
const groupId = selectedGroup.value.groupId
if (!groupId || isDefaultGroupId(groupId)) {
return
}
Promise.allSettled([fetchTenants(), fetchUsers(), fetchTags()]).then((results) => {
state.filters[0].children = [
{
Expand All @@ -259,19 +265,21 @@ export default {
})
}

watch([() => panel.show, () => selectedGroup.value.groupId], (values) => {
if (values[0]) {
panelState.isBlockGroupPanel = true
fetchBlocks()
getFilters()
watch([() => panel.show, () => validGroup.value.groupId], ([show, groupId]) => {
if (!show) {
return
}

panelState.isBlockGroupPanel = true
clearSearchParams()
fetchBlocks(groupId)
getFilters()
gene9831 marked this conversation as resolved.
Show resolved Hide resolved
})

return {
selectedGroup,
validGroup,
state,
panel,
filteredBlocks,
closeGroupPanel,
addBlocks,
searchBlocks
Expand Down
Loading