-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathArticleDetails.tsx
More file actions
141 lines (133 loc) · 4.86 KB
/
Copy pathArticleDetails.tsx
File metadata and controls
141 lines (133 loc) · 4.86 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { memo } from 'react';
import { classNames } from 'shared/lib/classNames/classNames';
import { useTranslation } from 'react-i18next';
import {
DynamicModuleLoader,
ReducersList,
} from 'shared/lib/components/DynamicModuleLoader/DynamicModuleLoader';
import { useAppDispatch } from 'shared/lib/hooks/useAppDispatch/useAppDispatch';
import { useSelector } from 'react-redux';
import { Text, TextAlign, TextSize, TextTheme } from 'shared/ui/Text/Text';
import { Skeleton } from 'shared/ui/Skeleton/Skeleton';
import { Avatar } from 'shared/ui/Avatar/Avatar';
import EyeIcon from 'shared/assets/icons/eye.svg';
import CalendarIcon from 'shared/assets/icons/calendar.svg';
import { Icon } from 'shared/ui/Icon/Icon';
import { useInitialEffect } from 'shared/lib/hooks/useInitialEffect/useInitialEffect';
import { HStack, VStack } from 'shared/ui/Stack';
import { ArticleBlock, ArticleBlockType } from '../../model/types/article';
import { fetchArticleById } from '../../model/services/fetchArticleById/fetchArticleById';
import cls from './ArticleDetails.module.scss';
import { articleDetailsReducer } from '../../model/slice/articleDetailsSlice';
import {
getArticleDetailsData,
getArticleDetailsError,
getArticleDetailsIsLoading,
} from '../../model/selectors/articleDetails';
import { ArticleCodeBlockComponent } from '../ArticleCodeBlockComponent/ArticleCodeBlockComponent';
import { ArticleTextBlockComponent } from '../ArticleTextBlockComponent/ArticleTextBlockComponent';
import { ArticleImageBlockComponent } from '../ArticleImageBlockComponent/ArticleImageBlockComponent';
interface ArticleDetailsProps {
id?: string;
className?: string;
}
const reducers: ReducersList = {
articleDetails: articleDetailsReducer,
};
const renderBlock = (block: ArticleBlock) => {
switch (block.type) {
case ArticleBlockType.CODE:
return (
<ArticleCodeBlockComponent
key={block.id}
className={cls.block}
block={block}
/>
);
case ArticleBlockType.IMAGE:
return (
<ArticleImageBlockComponent
key={block.id}
block={block}
className={cls.block}
/>
);
case ArticleBlockType.TEXT:
return (
<ArticleTextBlockComponent
key={block.id}
className={cls.block}
block={block}
/>
);
default:
return null;
}
};
export const ArticleDetails = memo(({ className, id }: ArticleDetailsProps) => {
const { t } = useTranslation('article-details');
const dispatch = useAppDispatch();
const error = useSelector(getArticleDetailsError);
const article = useSelector(getArticleDetailsData);
const isLoading = useSelector(getArticleDetailsIsLoading);
useInitialEffect(() => {
dispatch(fetchArticleById(id));
});
let content;
if (isLoading) {
content = (
<VStack gap="16" align="center" max>
<Skeleton width={200} height={200} border="50%" />
<Skeleton width={200} height={32} />
<Skeleton width={200} height={24} />
<Skeleton width="100%" height={200} />
<Skeleton width="100%" height={200} />
</VStack>
);
} else if (error) {
content = (
<HStack justify="center" max>
<Text
align={TextAlign.CENTER}
theme={TextTheme.ERROR}
text={t('An error occurred while loading the article')}
/>
</HStack>
);
} else {
content = (
<>
<HStack justify="center" max>
<Avatar size={200} src={article?.img} />
</HStack>
<VStack gap="16" max>
<Text
text={article?.subtitle}
title={article?.title}
size={TextSize.L}
/>
<HStack gap="8">
<Icon Svg={EyeIcon} />
<Text text={String(article?.views)} />
</HStack>
<HStack gap="8">
<Icon Svg={CalendarIcon} />
<Text text={article?.createdAt} />
</HStack>
</VStack>
{article?.blocks.map(renderBlock)}
</>
);
}
return (
<DynamicModuleLoader reducers={reducers}>
<VStack
max
gap="16"
className={classNames(cls.ArticleDetails, {}, [className])}
>
{content}
</VStack>
</DynamicModuleLoader>
);
});