Skip to content
This repository was archived by the owner on Nov 20, 2024. It is now read-only.

Commit f5bca73

Browse files
SidStrawDanSnow
authored andcommitted
feat: expanding evaluateCondition to filter featured articles
1 parent 9d3bc9d commit f5bca73

File tree

5 files changed

+35
-10
lines changed

5 files changed

+35
-10
lines changed

packages/karbon/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,4 +218,4 @@
218218
"access": "public"
219219
},
220220
"gitHead": "8df1f4d5837a7e2ddbff6cc79f5fec256c34a394"
221-
}
221+
}

packages/karbon/src/runtime/api/article.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ export const ListArticles = gql`
3131
slug
3232
sid
3333
published_at
34+
featured
3435
plan
3536
cover
3637
seo

packages/karbon/src/runtime/lib/article-filter.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,13 @@ export interface AuthorConditionOption {
2222
value: string
2323
}
2424

25-
export type Condition = DeskConditionOption | TagConditionOption | AuthorConditionOption
25+
export interface FeaturedConditionOption {
26+
type: 'featured'
27+
key?: 'id'
28+
value?: boolean
29+
}
30+
31+
export type Condition = DeskConditionOption | TagConditionOption | AuthorConditionOption | FeaturedConditionOption
2632
export type ConditionInput = string | Condition
2733

2834
export function normalizeCondition(conditionInput?: ConditionInput[]): { identity: string; conditions: Condition[] } {
@@ -34,7 +40,7 @@ export function normalizeCondition(conditionInput?: ConditionInput[]): { identit
3440
type: 'desk' as const,
3541
...value,
3642
}
37-
identityList.push(`${res.type}:${res.key}:${res.value}`)
43+
identityList.push(`${res.type}:${res?.key ?? '-'}:${res?.value ?? true}`)
3844
return res
3945
}
4046

@@ -58,13 +64,19 @@ export function evaluateCondition(article: Article, conditions: Condition[] = []
5864
return true
5965
}
6066

61-
return conditions.some(({ type = 'desk', key, value }) => {
62-
if (type === 'desk') {
63-
return article.desk[key] === value || article.desk?.desk?.[key] === value
64-
} else if (type === 'author') {
65-
return article.authors.some((author) => author.id === value)
66-
} else {
67-
return article.tags.some((tag) => tag[key] === value)
67+
return conditions.every(({ type = 'desk', key, value }) => {
68+
switch (true) {
69+
case type === 'featured':
70+
value ??= true
71+
return article.featured === value
72+
case type === 'desk':
73+
return article.desk[key!] === value || article.desk?.desk?.[key!] === value
74+
case type === 'author':
75+
return article.authors.some((author) => author.id === value)
76+
case type === 'tag':
77+
return article.tags.some((tag) => tag[key!] === value)
78+
default:
79+
return false
6880
}
6981
})
7082
}

packages/karbon/src/runtime/types.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ export interface Article {
201201
published_at?: any | null
202202
slug: string
203203
title: string
204+
featured: boolean
204205
cover?: any | null
205206
seo?: any | null
206207
html?: string | null

packages/playground/pages/index.vue

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ const multipleValues = useField('multiple-value', { type: FieldType.Text, all: t
77
88
const { articles: alphabetArticles } = useFillArticles(10, [{ key: 'slug', value: 'alphabet' }])
99
const { articles: lastArticles } = useFillArticles(9)
10+
const { articles: featureArticles } = useFillArticles(10, [
11+
{ key: 'slug', value: 'test' },
12+
{ type: 'featured' }
13+
])
1014
1115
watch(alphabetArticles, (articles) => {
1216
const slugs = new Set(articles.map((article) => article.slug))
@@ -61,5 +65,12 @@ watch(alphabetArticles, (articles) => {
6165
<ArticleLayout :article="article" />
6266
</div>
6367
</div>
68+
69+
<h2 class="text-2xl mt-4">Feature Articles</h2>
70+
<div class="flex w-screen mt-4">
71+
<div v-for="article in featureArticles" :key="article.id" class="border border-black flex-1 overflow-hidden">
72+
<ArticleLayout :article="article" />
73+
</div>
74+
</div>
6475
</div>
6576
</template>

0 commit comments

Comments
 (0)