Skip to content

Commit b2c409d

Browse files
committed
fix: correct filters for multiple plugin instances
1 parent 0b3f5e2 commit b2c409d

File tree

1 file changed

+83
-9
lines changed

1 file changed

+83
-9
lines changed

custom/InlineList.vue

Lines changed: 83 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
<Filters
55
v-if="filterableColumns.length"
66
:columns="filterableColumns"
7-
v-model:filters="filters"
87
:columnsMinMax="columnsMinMax"
98
:show="filtersShow"
109
@hide="filtersShow = false"
10+
:filtersStore="filtersStore"
1111
/>
1212
</Teleport>
1313

@@ -81,15 +81,15 @@
8181
returnTo: $route.fullPath,
8282
},
8383
}"
84-
class="flex items-center py-1 px-3 text-sm font-medium text-gray-900 focus:outline-none bg-white rounded border border-gray-300 hover:bg-gray-100 hover:text-lightPrimary focus:z-10 focus:ring-4 focus:ring-gray-100 dark:focus:ring-gray-700 dark:bg-gray-800 dark:text-gray-400 dark:border-gray-600 dark:hover:text-white dark:hover:bg-gray-700 rounded-default gap-1"
84+
class="flex h-[34px] af-button-shadow items-center py-1 px-3 text-sm font-medium text-gray-900 focus:outline-none bg-white rounded border border-gray-300 hover:bg-gray-100 hover:text-lightPrimary focus:z-10 focus:ring-4 focus:ring-gray-100 dark:focus:ring-gray-700 dark:bg-gray-800 dark:text-gray-400 dark:border-gray-600 dark:hover:text-white dark:hover:bg-gray-700 rounded-default gap-1"
8585
>
8686
<IconPlusOutline class="w-4 h-4"/>
8787
{{$t('Create')}}
8888
</RouterLink>
8989

9090
<button
9191
v-if="listResource?.options?.allowedActions?.filter"
92-
class="flex gap-1 items-center py-1 px-3 text-sm font-medium text-gray-900 focus:outline-none bg-white rounded border border-gray-300 hover:bg-gray-100 hover:text-lightPrimary focus:z-10 focus:ring-4 focus:ring-gray-100 dark:focus:ring-gray-700 dark:bg-gray-800 dark:text-gray-400 dark:border-gray-600 dark:hover:text-white dark:hover:bg-gray-700 rounded-default"
92+
class="flex gap-1 h-[34px] af-button-shadow items-center py-1 px-3 text-sm font-medium text-gray-900 focus:outline-none bg-white rounded border border-gray-300 hover:bg-gray-100 hover:text-lightPrimary focus:z-10 focus:ring-4 focus:ring-gray-100 dark:focus:ring-gray-700 dark:bg-gray-800 dark:text-gray-400 dark:border-gray-600 dark:hover:text-white dark:hover:bg-gray-700 rounded-default"
9393
@click="()=>{filtersShow = !filtersShow}"
9494
>
9595
<IconFilterOutline class="w-4 h-4"/>
@@ -122,18 +122,20 @@
122122
@update:checkboxes="checkboxes = $event"
123123
@update:records="getList"
124124

125+
:page="page"
125126
:sort="sort"
126127
:pageSize="pageSize"
127128
:totalRows="totalRows"
128129
:checkboxes="checkboxes"
130+
:isVirtualScrollEnabled="false"
129131
:customActionsInjection="listResource?.options?.pageInjections?.list?.customActionIcons"
130132
/>
131133
</div>
132134

133135
</td>
134136
</template>
135137

136-
<script setup>
138+
<script setup lang="ts">
137139
import { callAdminForthApi } from '@/utils';
138140
import { ref, onMounted, watch, computed, nextTick } from 'vue';
139141
import ResourceListTable from '@/components/ResourceListTable.vue';
@@ -147,9 +149,11 @@ import { showErrorTost, showWarningTost } from '@/composables/useFrontendApi';
147149
import { getIcon, btoa_function } from '@/utils';
148150
import { useI18n } from 'vue-i18n';
149151
import ThreeDotsMenu from '@/components/ThreeDotsMenu.vue';
150-
import { useFiltersStore } from '@/stores/filters';
152+
import type { FilterParams } from '@/types/Common';
153+
import type { AdminforthFilterStoreUnwrapped, sortType } from '@/spa_types/core';
154+
import { useAdminforth } from '@/adminforth';
151155
152-
const filtersStore = useFiltersStore();
156+
const adminforth = useAdminforth();
153157
154158
const listResourceData = ref(null);
155159
@@ -168,7 +172,77 @@ const pageSize = computed(() => listResource.value?.options?.listPageSize || 10)
168172
const rows = ref(null);
169173
const totalRows = ref(0);
170174
171-
const filters = ref([]);
175+
// local filters source for the inline list filter store
176+
const filters = ref<FilterParams[]>([]);
177+
178+
const shouldFilterBeHidden = (fieldName: string): boolean => {
179+
if (listResource.value?.columns) {
180+
const column = listResource.value.columns.find((col: any) => col.name === fieldName);
181+
if (column?.showIn?.filter !== true) {
182+
return true;
183+
}
184+
}
185+
return false;
186+
}
187+
188+
189+
const filtersStore: AdminforthFilterStoreUnwrapped = {
190+
// Expose filters as a getter/setter that directly proxies the local `filters` ref.
191+
// This allows usage patterns like `filtersStore.filters = []` and `filtersStore.filters.push(...)`
192+
// while keeping reactivity and the ref as the single source of truth.
193+
get filters(): FilterParams[] {
194+
return filters.value;
195+
},
196+
set filters(val: FilterParams[]) {
197+
filters.value = val;
198+
},
199+
200+
setSort: (s: sortType) => {
201+
try {
202+
// @ts-ignore - update local `sort` if available
203+
if (typeof sort !== 'undefined' && 'value' in sort) sort.value = s as any;
204+
} catch (e) {
205+
}
206+
},
207+
getSort: (): sortType => {
208+
try {
209+
// @ts-ignore
210+
return (typeof sort !== 'undefined' && 'value' in sort) ? sort.value as sortType : null;
211+
} catch (e) {
212+
return null;
213+
}
214+
},
215+
216+
setFilter: (filter: FilterParams) => {
217+
filters.value.push(filter);
218+
},
219+
220+
setFilters: (f: FilterParams[]) => {
221+
// console.log('Setting filters', f);
222+
filters.value = f || [];
223+
},
224+
225+
getFilters: (): FilterParams[] => {
226+
return filters.value;
227+
},
228+
229+
clearFilter: (fieldName: string): void => {
230+
filters.value = filters.value.filter(f => f.field !== fieldName);
231+
},
232+
233+
clearFilters: (): void => {
234+
filters.value = [];
235+
},
236+
237+
shouldFilterBeHidden: (fieldName: string): boolean => {
238+
return shouldFilterBeHidden(fieldName);
239+
},
240+
241+
// Unwrapped store expects a plain number here, so we expose it as a getter.
242+
get visibleFiltersCount(): number {
243+
return filters.value.filter(f => !shouldFilterBeHidden(f.field)).length;
244+
},
245+
};
172246
const filtersShow = ref(false);
173247
const columnsMinMax = ref(null);
174248
@@ -371,7 +445,7 @@ onMounted( async () => {
371445
})).resource;
372446
373447
if (listResource.value?.options?.allowedActions?.create && listResourceRefColumn.value && !listResourceRefColumn.value.showIn.create) {
374-
showWarningTost(t(`Resource '${listResource.value.resourceId}' column '${listResourceRefColumn.value.name}' should be editable on create page for 'create' action to be enabled`), 10000);
448+
showWarningTost(t(`Resource '${listResource.value.resourceId}' column '${listResourceRefColumn.value.name}' should be editable on create page for 'create' action to be enabled`));
375449
}
376450
377451
columnsMinMax.value = await callAdminForthApi({
@@ -387,7 +461,7 @@ onMounted( async () => {
387461
}
388462
389463
await getList();
390-
filtersStore.setFilters(endFilters.value);
464+
391465
});
392466
393467
function clearCheckboxes() {

0 commit comments

Comments
 (0)