Skip to content

Commit 7d1e8d1

Browse files
committed
refactor: extract duplicate functions to utils
1 parent 411e662 commit 7d1e8d1

File tree

3 files changed

+33
-34
lines changed

3 files changed

+33
-34
lines changed

adminforth/spa/src/components/Filters.vue

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -153,14 +153,13 @@
153153
import { watch, computed, ref, reactive } from 'vue';
154154
import { useI18n } from 'vue-i18n';
155155
import CustomDateRangePicker from '@/components/CustomDateRangePicker.vue';
156-
import { callAdminForthApi, loadMoreForeignOptions, searchForeignOptions } from '@/utils';
156+
import { callAdminForthApi, loadMoreForeignOptions, searchForeignOptions, createSearchInputHandlers } from '@/utils';
157157
import { useRouter } from 'vue-router';
158158
import CustomRangePicker from "@/components/CustomRangePicker.vue";
159159
import { useFiltersStore } from '@/stores/filters';
160160
import { getCustomComponent } from '@/utils';
161161
import Input from '@/afcl/Input.vue';
162162
import Select from '@/afcl/Select.vue';
163-
import debounce from 'debounce';
164163
import Spinner from '@/afcl/Spinner.vue';
165164
166165
const filtersStore = useFiltersStore();
@@ -259,21 +258,11 @@ const onFilterInput = computed(() => {
259258
});
260259
261260
const onSearchInput = computed(() => {
262-
if (!props.columns) return {};
263-
264-
const result = props.columns.reduce((acc, c) => {
265-
if (c.foreignResource && c.foreignResource.searchableFields) {
266-
return {
267-
...acc,
268-
[c.name]: debounce((searchTerm) => {
269-
searchOptions(c.name, searchTerm);
270-
}, c.filterOptions?.debounceTimeMs || 300),
271-
};
272-
}
273-
return acc;
274-
}, {});
275-
276-
return result;
261+
return createSearchInputHandlers(
262+
props.columns,
263+
searchOptions,
264+
(column) => column.filterOptions?.debounceTimeMs || 300
265+
);
277266
});
278267
279268
function setFilterItem({ column, operator, value }) {

adminforth/spa/src/components/ResourceForm.vue

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -63,15 +63,14 @@
6363

6464
<script setup lang="ts">
6565
66-
import { applyRegexValidation, callAdminForthApi, loadMoreForeignOptions, searchForeignOptions} from '@/utils';
66+
import { applyRegexValidation, callAdminForthApi, loadMoreForeignOptions, searchForeignOptions, createSearchInputHandlers} from '@/utils';
6767
import { computedAsync } from '@vueuse/core';
6868
import { computed, onMounted, reactive, ref, watch, provide } from 'vue';
6969
import { useRouter, useRoute } from 'vue-router';
7070
import { useCoreStore } from "@/stores/core";
7171
import GroupsTable from '@/components/GroupsTable.vue';
7272
import { useI18n } from 'vue-i18n';
7373
import { type AdminForthResourceCommon } from '@/types/Common';
74-
import debounce from 'debounce';
7574
7675
const { t } = useI18n();
7776
@@ -356,21 +355,10 @@ const getOtherColumns = () => {
356355
const otherColumns = getOtherColumns();
357356
358357
const onSearchInput = computed(() => {
359-
if (!props.resource.columns) return {};
360-
361-
const result = props.resource.columns.reduce((acc, c) => {
362-
if (c.foreignResource && c.foreignResource.searchableFields) {
363-
return {
364-
...acc,
365-
[c.name]: debounce((searchTerm: string) => {
366-
searchOptions(c.name, searchTerm);
367-
}, 300),
368-
};
369-
}
370-
return acc;
371-
}, {} as Record<string, (searchTerm: string) => void>);
372-
373-
return result;
358+
return createSearchInputHandlers(
359+
props.resource.columns,
360+
searchOptions
361+
);
374362
});
375363
376364
provide('columnLoadingState', columnLoadingState);

adminforth/spa/src/utils.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import { useUserStore } from './stores/user';
77
import { Dropdown } from 'flowbite';
88
import adminforth from './adminforth';
99
import sanitizeHtml from 'sanitize-html'
10+
import debounce from 'debounce';
1011

1112
const LS_LANG_KEY = `afLanguage`;
1213

@@ -393,4 +394,25 @@ export async function searchForeignOptions({
393394
} finally {
394395
state.loading = false;
395396
}
397+
}
398+
399+
export function createSearchInputHandlers(
400+
columns: any[],
401+
searchFunction: (columnName: string, searchTerm: string) => void,
402+
getDebounceMs?: (column: any) => number
403+
) {
404+
if (!columns) return {};
405+
406+
return columns.reduce((acc, c) => {
407+
if (c.foreignResource && c.foreignResource.searchableFields) {
408+
const debounceMs = getDebounceMs ? getDebounceMs(c) : 300;
409+
return {
410+
...acc,
411+
[c.name]: debounce((searchTerm: string) => {
412+
searchFunction(c.name, searchTerm);
413+
}, debounceMs),
414+
};
415+
}
416+
return acc;
417+
}, {} as Record<string, (searchTerm: string) => void>);
396418
}

0 commit comments

Comments
 (0)