Skip to content

Commit 90ba7df

Browse files
committed
feat: add clearCardSettingsLanguages action and update UserTags component to handle 'all' tag click
1 parent fbead10 commit 90ba7df

File tree

3 files changed

+61
-19
lines changed

3 files changed

+61
-19
lines changed

src/components/Elements/UserTags/UserTags.tsx

Lines changed: 50 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,65 @@
1-
import { useCallback } from 'react'
1+
import { useCallback, useMemo } from 'react'
2+
import { FaGlobe } from 'react-icons/fa'
23
import { TiPlus } from 'react-icons/ti'
34
import { Link } from 'react-router-dom'
45
import { useUserPreferences } from 'src/stores/preferences'
56
import { useShallow } from 'zustand/shallow'
67

78
export const UserTags = () => {
8-
const { cards, userSelectedTags, cardsSettings, setCardSettings } = useUserPreferences(
9-
useShallow((state) => ({
10-
cards: state.cards,
11-
userSelectedTags: state.userSelectedTags,
12-
cardsSettings: state.cardsSettings,
13-
setCardSettings: state.setCardSettings,
14-
}))
15-
)
9+
const { cards, userSelectedTags, cardsSettings, setCardSettings, clearCardSettingsLanguages } =
10+
useUserPreferences(
11+
useShallow((state) => ({
12+
cards: state.cards,
13+
userSelectedTags: state.userSelectedTags,
14+
cardsSettings: state.cardsSettings,
15+
setCardSettings: state.setCardSettings,
16+
clearCardSettingsLanguages: state.clearCardSettingsLanguages,
17+
}))
18+
)
1619

17-
const onTagClicked = useCallback((tagValue: string) => {
18-
cards.forEach((card) => {
19-
setCardSettings(card.name, {
20-
...cardsSettings[card.id],
21-
language: tagValue,
20+
const onTagClicked = useCallback(
21+
(tagValue: string) => {
22+
if (tagValue === 'all') {
23+
clearCardSettingsLanguages()
24+
return
25+
}
26+
27+
cards.forEach((card) => {
28+
setCardSettings(card.name, {
29+
...cardsSettings[card.id],
30+
language: tagValue,
31+
})
2232
})
23-
})
24-
}, [])
33+
},
34+
[cards, cardsSettings, setCardSettings]
35+
)
36+
37+
const tagsList = useMemo(() => {
38+
const tags = userSelectedTags.map((tag) => ({
39+
label: tag.label,
40+
value: tag.value,
41+
icon: undefined,
42+
}))
43+
44+
if (tags.length === 0) {
45+
return tags
46+
}
47+
48+
return [
49+
{
50+
label: 'All',
51+
value: 'all',
52+
icon: <FaGlobe className="tagIcon" />,
53+
},
54+
...tags,
55+
]
56+
}, [userSelectedTags])
2557

2658
return (
2759
<div className="tags">
28-
{userSelectedTags.map((tag, index) => (
60+
{tagsList.map((tag, index) => (
2961
<button key={index} className="tag tagHoverable" onClick={() => onTagClicked(tag.value)}>
30-
{tag.label}
62+
{tag.icon} {tag.label}
3163
</button>
3264
))}
3365
<Link to="/settings/topics" className="tag tagHoverable" aria-label="Open settings">

src/stores/preferences.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ type UserPreferencesStoreActions = {
5151
unfollowTag: (tag: Tag) => void
5252
setMaxVisibleCards: (maxVisibleCards: number) => void
5353
setCardSettings: (card: string, settings: CardSettingsType) => void
54+
clearCardSettingsLanguages: () => void
5455
setOccupation: (occupation: string | null) => void
5556
markOnboardingAsCompleted: () => void
5657
setUserCustomCards: (cards: SupportedCardType[]) => void
@@ -106,6 +107,15 @@ export const useUserPreferences = create(
106107
[card]: { ...state.cardsSettings[card], ...settings },
107108
},
108109
})),
110+
clearCardSettingsLanguages: () =>
111+
set((state) => ({
112+
cardsSettings: Object.fromEntries(
113+
Object.entries(state.cardsSettings).map(([card, settings]) => [
114+
card,
115+
{ ...settings, language: undefined },
116+
])
117+
),
118+
})),
109119
markOnboardingAsCompleted: () =>
110120
set(() => ({
111121
onboardingCompleted: true,

src/types/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,7 @@ export type BaseItemPropsType<
143143
}
144144

145145
export type CardSettingsType = {
146-
language: string
146+
language?: string
147147
sortBy: string
148148
dateRange?: string
149149
}

0 commit comments

Comments
 (0)