|
| 1 | +import { useState, useEffect } from 'react' |
| 2 | +import CardComponent from 'src/components/CardComponent' |
| 3 | +import { ListComponent } from 'src/components/List' |
| 4 | +import { useGetArticles } from '../api/getArticles' |
| 5 | +import { ArticleType, CardPropsType } from 'src/types' |
| 6 | +import { useUserPreferences } from 'src/stores/preferences' |
| 7 | +import { getCardTagsValue } from 'src/utils/DataEnhancement' |
| 8 | +import ArticleItem from './ArticleItem' |
| 9 | +import { Tag } from 'src/features/remoteConfig' |
| 10 | +import { GLOBAL_TAG, MY_LANGUAGES_TAG } from 'src/Constants' |
| 11 | +import { trackCardLanguageSelect } from 'src/lib/analytics' |
| 12 | +import SelectableCard from 'src/components/SelectableCard' |
| 13 | + |
| 14 | +const FCC_MENU_LANGUAGE_ID = 'FCC_MENU_LANGUAGE_ID' |
| 15 | + |
| 16 | +export function FreecodecampCard(props: CardPropsType) { |
| 17 | + const { label, icon, withAds } = props |
| 18 | + const { userSelectedTags, cardsSettings, setCardSettings, listingMode } = useUserPreferences() |
| 19 | + const [selectedTag, setSelectedTag] = useState<Tag>() |
| 20 | + |
| 21 | + useEffect(() => { |
| 22 | + if (selectedTag) { |
| 23 | + setCardSettings(label.toLowerCase(), { language: selectedTag.label }) |
| 24 | + } |
| 25 | + }, [selectedTag]) |
| 26 | + |
| 27 | + const getQueryTags = () => { |
| 28 | + if (!selectedTag) { |
| 29 | + return [] |
| 30 | + } |
| 31 | + |
| 32 | + if (selectedTag.value === MY_LANGUAGES_TAG.freecodecampValues[0]) { |
| 33 | + return getCardTagsValue(userSelectedTags, 'freecodecampValues') |
| 34 | + } |
| 35 | + return selectedTag.freecodecampValues |
| 36 | + } |
| 37 | + |
| 38 | + const results = useGetArticles({ tags: getQueryTags() }) |
| 39 | + |
| 40 | + const getIsLoading = () => results.some((result) => result.isLoading) |
| 41 | + |
| 42 | + const getData = () => { |
| 43 | + return results |
| 44 | + .reduce((acc: ArticleType[], curr) => { |
| 45 | + if (!curr.data) return acc |
| 46 | + return [...acc, ...curr.data] |
| 47 | + }, []) |
| 48 | + .sort((a, b) => b.reactions - a.reactions) |
| 49 | + } |
| 50 | + |
| 51 | + const renderItem = (item: ArticleType, index: number) => ( |
| 52 | + <ArticleItem |
| 53 | + item={item} |
| 54 | + key={`fcc-${index}`} |
| 55 | + index={index} |
| 56 | + selectedTag={selectedTag} |
| 57 | + listingMode={listingMode} |
| 58 | + /> |
| 59 | + ) |
| 60 | + |
| 61 | + const HeaderTitle = () => { |
| 62 | + return ( |
| 63 | + <div style={{ display: 'inline-block', margin: 0, padding: 0 }}> |
| 64 | + <span> {label} </span> |
| 65 | + <SelectableCard |
| 66 | + isLanguage={true} |
| 67 | + tagId={FCC_MENU_LANGUAGE_ID} |
| 68 | + selectedTag={selectedTag} |
| 69 | + setSelectedTag={setSelectedTag} |
| 70 | + fallbackTag={GLOBAL_TAG} |
| 71 | + cardSettings={cardsSettings?.devto?.language} |
| 72 | + trackEvent={(tag: Tag) => trackCardLanguageSelect('Devto', tag.value)} |
| 73 | + data={userSelectedTags.map((tag) => ({ |
| 74 | + label: tag.label, |
| 75 | + value: tag.value, |
| 76 | + }))} |
| 77 | + /> |
| 78 | + </div> |
| 79 | + ) |
| 80 | + } |
| 81 | + |
| 82 | + return ( |
| 83 | + <CardComponent |
| 84 | + icon={<span className="blockHeaderIcon">{icon}</span>} |
| 85 | + link="https://freecodecamp.com/news" |
| 86 | + title={<HeaderTitle />}> |
| 87 | + <ListComponent |
| 88 | + items={getData()} |
| 89 | + isLoading={getIsLoading()} |
| 90 | + renderItem={renderItem} |
| 91 | + withAds={withAds} |
| 92 | + /> |
| 93 | + </CardComponent> |
| 94 | + ) |
| 95 | +} |
0 commit comments