Skip to content

Commit

Permalink
refactor: reorganize code #172
Browse files Browse the repository at this point in the history
  • Loading branch information
wazolab committed Mar 12, 2024
1 parent 36f88e8 commit a082cc7
Showing 1 changed file with 23 additions and 21 deletions.
44 changes: 23 additions & 21 deletions components/PoisList/PoisTable.vue
Original file line number Diff line number Diff line change
Expand Up @@ -31,11 +31,11 @@ const siteStore = useSiteStore()
const { $propertyTranslations } = useNuxtApp()
const { contribMode, isContribEligible, getContributorFields } = useContrib()
const search = ref('')
const cachedKey = computed(() => `pois-${props.category.id}`)
// Fetch POIs by Cache or API
const pois = ref()
const loadingState = ref(false)
const cachedKey = computed(() => `pois-${props.category.id}`)
const { data: cachedPois } = useNuxtData(cachedKey.value)
if (cachedPois.value) {
Expand Down Expand Up @@ -85,36 +85,22 @@ pois.value.features = pois.value.features.map((f: ApiPoi) => {
})
// Transform headers data to be compliant with Vuetify's standards
const headers = computed((): Array<DataTableHeader> => {
const headers = computed(() => {
// Basic Fields
const headers: Array<DataTableHeader> = fields.value.map(f => ({
const h: DataTableHeader[] = fields.value.map(f => ({
filterable: true,
key: `properties.${f.field}`,
sortable: true,
title: $propertyTranslations.p(
f.field,
PropertyTranslationsContextEnum.List,
),
sort: (a: string, b: string) => {
const first = valueToString(a)
const second = valueToString(b)
if (!first && second)
return -1
if (first && !second)
return 1
if (!first && !second)
return 0
return first.localeCompare(second, locale.value, { sensitivity: 'base' })
},
sort: customSort,
}))
// Contrib Field
if (contribMode) {
headers.push({
h.push({
filterable: false,
sortable: false,
key: 'contrib',
Expand All @@ -123,14 +109,14 @@ const headers = computed((): Array<DataTableHeader> => {
}
// Details Field
headers.push({
h.push({
filterable: false,
sortable: false,
key: 'details',
title: 'Actions',
})
return headers
return h
})
function valueToString(item: any) {
Expand All @@ -140,6 +126,22 @@ function valueToString(item: any) {
return item === undefined || typeof item === 'object' ? '' : item
}
function customSort(a: string, b: string) {
const first = valueToString(a)
const second = valueToString(b)
if (!first && second)
return -1
if (first && !second)
return 1
if (!first && !second)
return 0
return first.localeCompare(second, locale.value, { sensitivity: 'base' })
}
function customFilter(value: any, query: string): boolean {
value = valueToString(value)
Expand Down

0 comments on commit a082cc7

Please sign in to comment.