Skip to content

Commit d3e6971

Browse files
committed
allow to filter reddit card by language
1 parent 283cb7a commit d3e6971

2 files changed

Lines changed: 58 additions & 5 deletions

File tree

src/config/index.tsx

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,6 +179,7 @@ export const GLOBAL_TAG = {
179179
devtoValues: ['programming'],
180180
hashnodeValues: ['programming'],
181181
mediumValues: ['programming'],
182+
redditValues: ['programming'],
182183
freecodecampValues: ['programming'],
183184
}
184185
export const MY_LANGUAGES_TAG = {
@@ -188,6 +189,7 @@ export const MY_LANGUAGES_TAG = {
188189
devtoValues: ['myLangs'],
189190
hashnodeValues: ['myLangs'],
190191
mediumValues: ['myLangs'],
192+
redditValues: ['myLangs'],
191193
freecodecampValues: ['myLangs'],
192194
}
193195
export const MAX_ITEMS_PER_CARD = 50

src/features/cards/components/redditCard/RedditCard.tsx

Lines changed: 56 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,37 @@ import { Article, CardPropsType } from 'src/types'
55
import { useUserPreferences } from 'src/stores/preferences'
66
import { getCardTagsValue } from 'src/utils/DataEnhancement'
77
import ArticleItem from './ArticleItem'
8+
import { useState, useEffect } from 'react'
9+
import { Tag } from 'src/features/remoteConfig'
10+
import { GLOBAL_TAG, MY_LANGUAGES_TAG } from 'src/config'
11+
import SelectableCard from 'src/components/SelectableCard'
12+
import { trackCardLanguageSelect } from 'src/lib/analytics'
13+
14+
const REDDIT_MENU_LANGUAGE_ID = 'REDDIT_MENU_LANGUAGE_ID'
815

916
export function RedditCard({ withAds, meta }: CardPropsType) {
10-
const { userSelectedTags } = useUserPreferences()
17+
const { userSelectedTags, cardsSettings, setCardSettings } = useUserPreferences()
18+
const [selectedTag, setSelectedTag] = useState<Tag>()
19+
20+
useEffect(() => {
21+
if (selectedTag) {
22+
setCardSettings(meta.value, { language: selectedTag.label })
23+
}
24+
}, [selectedTag, meta, setCardSettings])
25+
26+
const getQueryTags = () => {
27+
if (!selectedTag) {
28+
return []
29+
}
1130

12-
const tags = getCardTagsValue(userSelectedTags, 'redditValues')
31+
if (selectedTag.value === MY_LANGUAGES_TAG.redditValues[0]) {
32+
return getCardTagsValue(userSelectedTags, 'redditValues') || []
33+
}
34+
return selectedTag.redditValues || []
35+
}
1336

14-
const results = useGetRedditArticles({ tags })
37+
console.log('getQueryTags()', getQueryTags())
38+
const results = useGetRedditArticles({ tags: getQueryTags() })
1539

1640
const getIsLoading = () => results.some((result) => result.isLoading)
1741

@@ -25,11 +49,38 @@ export function RedditCard({ withAds, meta }: CardPropsType) {
2549
}
2650

2751
const renderItem = (item: Article, index: number) => (
28-
<ArticleItem item={item} key={`re-${index}`} index={index} analyticsTag={meta.analyticsTag} />
52+
<ArticleItem
53+
item={item}
54+
key={`md-${index}`}
55+
index={index}
56+
selectedTag={selectedTag}
57+
analyticsTag={meta.analyticsTag}
58+
/>
2959
)
3060

61+
const HeaderTitle = () => {
62+
return (
63+
<div style={{ display: 'inline-block', margin: 0, padding: 0 }}>
64+
<span> {meta.label} </span>
65+
<SelectableCard
66+
isLanguage={true}
67+
tagId={REDDIT_MENU_LANGUAGE_ID}
68+
selectedTag={selectedTag}
69+
setSelectedTag={setSelectedTag}
70+
fallbackTag={GLOBAL_TAG}
71+
cardSettings={cardsSettings?.reddit?.language}
72+
trackEvent={(tag: Tag) => trackCardLanguageSelect(meta.analyticsTag, tag.value)}
73+
data={userSelectedTags.map((tag) => ({
74+
label: tag.label,
75+
value: tag.value,
76+
}))}
77+
/>
78+
</div>
79+
)
80+
}
81+
3182
return (
32-
<Card card={meta}>
83+
<Card card={meta} titleComponent={<HeaderTitle />}>
3384
<ListComponent
3485
items={getData()}
3586
isLoading={getIsLoading()}

0 commit comments

Comments
 (0)