Skip to content

Commit 4f76b4c

Browse files
SkyBird233HouLiXieBuRou
authored andcommitted
chore: refactor category lists
1 parent 6db05f4 commit 4f76b4c

File tree

5 files changed

+50
-28
lines changed

5 files changed

+50
-28
lines changed

components/NewsCategoryList.vue renamed to components/CategoryList.vue

Lines changed: 3 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,18 @@
11
<script lang="ts" setup>
2-
import type { Collections } from '@nuxt/content';
3-
42
const { locale } = useI18n();
53
// const textValue = tm('NewsCategoryList');
64
75
const props = defineProps<{
86
category?: string;
7+
filters?: Array<{ key: string; value: string }>;
98
limit?: number;
109
}>();
1110
12-
const queryNewsCollectionCategory = (
13-
locale: keyof Collections,
14-
category?: string,
15-
limit: number = 0
16-
) => {
17-
const q = queryCollection(locale)
18-
.select('path', 'title', 'date')
19-
.where('path', 'LIKE', '/news%')
20-
.order('date', 'DESC')
21-
.limit(limit);
22-
return category
23-
? () => q.where('categories', 'LIKE', `%"${category}"%`).all() // `["category1","category2"]`
24-
: () => q.all();
25-
};
26-
2711
const { data, error, status } = await useAsyncData(
2812
computed(
29-
() => `${locale.value}:NewsCategoriesList:${props.category}:${props.limit}`
13+
() => `${locale.value}:CategoriesList:${props.category}:${props.limit}`
3014
),
31-
queryNewsCollectionCategory(locale.value, props.category, props.limit)
15+
queryCollectionCategory(props.category, props.limit, props.filters)
3216
);
3317
</script>
3418

pages/index.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const columnBorderlessList = [
2323
<div>
2424
<CategorySecond :title="textValue.title1" />
2525
<article>
26-
<NewsCategoryList :limit="10" />
26+
<CategoryList category="news" :limit="10" />
2727
<div
2828
class="px-[15px] py-[10px] text-right leading-6 font-[12pt] text-link">
2929
<link-standard :link="useTIndex(localLink.news, 1)" />

pages/news/index.vue

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ const textValue = tm('news.index');
44
// const linkValue = tm('allUniversalLink');
55
const newsLimit = 10;
66
useHead({ title: textValue.title });
7+
const filters = (newsCategory) => [{ key: 'categories', value: newsCategory }];
78
</script>
89

910
<template>
@@ -14,9 +15,10 @@ useHead({ title: textValue.title });
1415
:right-text="textValue.all"
1516
right-url="/news/list/advisories"
1617
:show-right-chevron="true" />
17-
<news-category-list
18-
category="advisories"
18+
<category-list
19+
category="news"
1920
:limit="newsLimit"
21+
:filters="filters('advisories')"
2022
class="overflow-y-auto" />
2123
</div>
2224
<div class="">
@@ -26,9 +28,10 @@ useHead({ title: textValue.title });
2628
right-url="/news/list/news"
2729
class="border-l border-l-white"
2830
:show-right-chevron="true" />
29-
<news-category-list
31+
<category-list
3032
category="news"
3133
:limit="newsLimit"
34+
:filters="filters('news')"
3235
class="theme-border-secondary overflow-y-auto border-l-[1px]" />
3336
</div>
3437
<div>
@@ -37,9 +40,10 @@ useHead({ title: textValue.title });
3740
:right-text="textValue.all"
3841
right-url="/news/list/journals"
3942
:show-right-chevron="true" />
40-
<news-category-list
41-
category="journals"
43+
<category-list
44+
category="news"
4245
:limit="newsLimit"
46+
:filters="filters('journals')"
4347
class="overflow-y-auto" />
4448
</div>
4549

@@ -50,9 +54,10 @@ useHead({ title: textValue.title });
5054
right-url="/news/list/minutes"
5155
:show-right-chevron="true"
5256
class="border-l border-l-white" />
53-
<news-category-list
54-
category="minutes"
57+
<category-list
58+
category="news"
5559
:limit="newsLimit"
60+
:filters="filters('minutes')"
5661
class="theme-border-secondary overflow-y-auto border-l-[1px]" />
5762
</div>
5863
</div>

pages/news/list/[category].vue

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,9 @@ useHead({ title: title });
2323
<template>
2424
<div>
2525
<category-second :title="title" class="border-r-solid border-r-white" />
26-
<news-category-list :category="route.params.category" />
26+
<category-list
27+
category="news"
28+
:filters="[{ key: 'categories', value: route.params.category }]" />
2729
</div>
2830
</template>
2931

utils/content.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import type {
2+
CollectionQueryBuilder,
3+
EnCollectionItem,
4+
ZhCollectionItem
5+
} from '@nuxt/content';
6+
7+
export const queryCollectionCategory = (
8+
category?: string,
9+
limit: number = 0,
10+
filters?: Array<{ key: string; value: string }>
11+
) => {
12+
const { locale } = useI18n();
13+
14+
let q = (
15+
queryCollection(locale.value) as CollectionQueryBuilder<
16+
ZhCollectionItem | EnCollectionItem
17+
>
18+
)
19+
.select('path', 'title', 'date')
20+
.where('path', 'LIKE', `/${category}%`)
21+
.order('date', 'DESC')
22+
.limit(limit);
23+
24+
if (filters) {
25+
for (const filter of filters) {
26+
q = q.where(filter.key, 'LIKE', `%"${filter.value}"%`);
27+
}
28+
}
29+
30+
return () => q.all();
31+
};

0 commit comments

Comments
 (0)