Skip to content

Commit

Permalink
feat: use sorter
Browse files Browse the repository at this point in the history
  • Loading branch information
mp3000mp committed Oct 6, 2024
1 parent 2dea6f3 commit b25f5e3
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 12 deletions.
62 changes: 62 additions & 0 deletions frontend/src/composables/useFilter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import {computed, ref} from 'vue'
import type {Ref} from 'vue'

export enum FilterConfigTypeEnum {
STRING = 'string',
// DATE = 'date', // todo
// SELECT = 'select', // todo
CUSTOM = 'custom'
}
type FilterConfig = {
property: string
type: FilterConfigTypeEnum
propFunc?: (a: any) => any // todo T
customFunc?: (a: any) => boolean // todo T
}
type Filter = FilterConfig & {
value: any // todo type ?
}

function propFilter<T>(filter: FilterConfig): (item: any) => bool {

Check warning on line 20 in frontend/src/composables/useFilter.ts

View workflow job for this annotation

GitHub Actions / lint-and-tests

'T' is defined but never used
let filterFunc: (prop: any) => bool
switch (item.type) {

Check failure on line 22 in frontend/src/composables/useFilter.ts

View workflow job for this annotation

GitHub Actions / lint-and-tests

src/views/users/__tests__/UsersView.vue.test.ts > UsersView.vue > sorts users

ReferenceError: item is not defined ❯ propFilter src/composables/useFilter.ts:22:3 ❯ src/composables/useFilter.ts:54:20 ❯ ReactiveEffect.fn src/composables/useFilter.ts:53:19 ❯ ReactiveEffect.run node_modules/@vue/reactivity/dist/reactivity.cjs.js:182:19 ❯ ComputedRefImpl.get value [as value] node_modules/@vue/reactivity/dist/reactivity.cjs.js:1143:33 ❯ unref node_modules/@vue/reactivity/dist/reactivity.cjs.js:1022:29 ❯ Object.get node_modules/@vue/reactivity/dist/reactivity.cjs.js:1028:35 ❯ Proxy._sfc_render src/views/users/UsersView.vue:50:12 ❯ renderComponentRoot node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:812:16

Check failure on line 22 in frontend/src/composables/useFilter.ts

View workflow job for this annotation

GitHub Actions / lint-and-tests

src/views/users/__tests__/UsersView.vue.test.ts > UsersView.vue > filters users

ReferenceError: item is not defined ❯ propFilter src/composables/useFilter.ts:22:3 ❯ src/composables/useFilter.ts:54:20 ❯ ReactiveEffect.fn src/composables/useFilter.ts:53:19 ❯ ReactiveEffect.run node_modules/@vue/reactivity/dist/reactivity.cjs.js:182:19 ❯ ComputedRefImpl.get value [as value] node_modules/@vue/reactivity/dist/reactivity.cjs.js:1143:33 ❯ unref node_modules/@vue/reactivity/dist/reactivity.cjs.js:1022:29 ❯ Object.get node_modules/@vue/reactivity/dist/reactivity.cjs.js:1028:35 ❯ Proxy._sfc_render src/views/users/UsersView.vue:50:12 ❯ renderComponentRoot node_modules/@vue/runtime-core/dist/runtime-core.cjs.js:812:16
case FilterConfigTypeEnum.DATE:
// todo
break;
case FilterConfigTypeEnum.SELECT:
// todo
break;
case FilterConfigTypeEnum.STRING:
filterFunc = (prop: string) => {
if (filter.filterValue.length < 1) {
return true
}
return String(prop).toLowerCase().includes(filter.filterValue.toLowerCase())
}
break;
}

return (item: any): bool => {
if (FilterConfigTypeEnum.CUSTOM === filter.type) {
return filter.customFunc(item)
}

return filterFunc(filter.propFunc(item))
}
}

export function useFilter<T>(options: FilterConfig[], list: Ref<T>[]) {
const filters: Ref<Filter[]> = ref(options.map((option) => ({...option, value: ''})))

const filteredList = computed(() => {
const rList = [...list.value]
filters.value.forEach((filter) => {
rList.filter(propFilter(filter))
})
return rList
})

return {
filteredList
}
}
8 changes: 6 additions & 2 deletions frontend/src/composables/useSorter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,18 @@ type SortConfig = {
type Sort = Pick<SortConfig, 'property' | 'type' | 'customCompare'>

function propCompare<T>(sort: SortConfig): (a: T, b: T) => number {
let compareFunc = (a: number, b: number): number => a - b
let compareFunc: (a: number, b: number) => number
switch (sort.type) {
case SortConfigTypeEnum.STRING:
compareFunc = (a: string, b: string): number => String(a).localeCompare(String(b))
break
case SortConfigTypeEnum.DATE: // dayjs
compareFunc = (a: Dayjs, b: Dayjs): number => a.diff(b)
break
case SortConfigTypeEnum.NUMBER:
default:
compareFunc = (a: number, b: number): number => a - b
break
}

return (a: T, b: T): number => {
Expand All @@ -48,7 +52,7 @@ function propCompare<T>(sort: SortConfig): (a: T, b: T) => number {
}

export function useSorter<T>(options: SortConfig[], list: Ref<T>[]) {
const sorts = ref(options.map((option) => ({ ...option, priority: 0, asc: true })))
const sorts: Ref<Sort[]> = ref(options.map((option) => ({ ...option, priority: 0, asc: true })))
let maxPriority = 0

function levelSorts(currentPriority: number) {
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/views/tenders/TenderView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -261,7 +261,7 @@ onMounted(async () => {
:average-daily-rate="tender.averageDailyRate"
@show-form="showTenderRowForm(tenderRow)"
/>
<tr class="total" v-if="tenderRowFilterSearch.length < 3">
<tr class="total">
<td colspan="2" class="text-end">Total:</td>
<td>{{ tenderSoldDays }}</td>
<td>{{ tenderFixedRate }}</td>
Expand Down
26 changes: 17 additions & 9 deletions frontend/src/views/users/UsersView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,28 @@ import Mp3000Table from '@/components/Mp3000Table.vue'
import Mp3000TableHeader from '@/components/Mp3000TableHeader.vue'
import UserRow from '@/views/users/UserRow.vue'
import { useSorter, SortConfigTypeEnum } from '@/composables/useSorter'
import {FilterConfigTypeEnum, useFilter} from "@/composables/useFilter";
const userStore = useUserStore()
const isLoading = ref(false)
const users = computed(() => userStore.users)
const filterSearch = ref('')
const filteredUsers = computed(() =>
users.value.filter((user) => {
if (filterSearch.value.length < 1) {
return true
}
return (user.email + user.username).toLowerCase().includes(filterSearch.value.toLowerCase())
})
// const filterSearch = ref('')
// const filteredUsers = computed(() =>
// users.value.filter((user) => {
// if (filterSearch.value.length < 1) {
// return true
// }
// return (user.email + user.username).toLowerCase().includes(filterSearch.value.toLowerCase())
// })
// )
const { filteredList, filter } = useFilter(

Check warning on line 25 in frontend/src/views/users/UsersView.vue

View workflow job for this annotation

GitHub Actions / lint-and-tests

'filter' is assigned a value but never used
[
{ property: 'email', type: FilterConfigTypeEnum.STRING },
],
users
)
const { getAsc, getPriority, sort, sortedList } = useSorter(
[
Expand All @@ -28,7 +35,7 @@ const { getAsc, getPriority, sort, sortedList } = useSorter(
{ property: 'username', type: SortConfigTypeEnum.STRING },
{ property: 'roles', type: SortConfigTypeEnum.STRING }
],
filteredUsers
filteredList
)
onMounted(async () => {
Expand All @@ -42,6 +49,7 @@ onMounted(async () => {
<template>
<div>
<h2>Utilisateurs</h2>
<h3> {{ filteredList.length }}</h3>
<mp3000-table :is-loading="isLoading">
<template v-slot:filters>
<div class="col-auto">
Expand Down

0 comments on commit b25f5e3

Please sign in to comment.