-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add article rating feature, add rtk mutation post request
- Loading branch information
Showing
16 changed files
with
236 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export { RatingCard } from './ui/RatingCard'; | ||
export type { Rating } from './model/types/types'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
export interface Rating { | ||
rate: number; | ||
feedback?: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
import { rtkApi } from '@/shared/api/rtkApi'; | ||
import { Rating } from '@/entities/Rating'; | ||
|
||
interface GetArticleRatingArg { | ||
userId: string | ||
articleId: string | ||
} | ||
|
||
interface RateArticleArg { | ||
userId: string | ||
articleId: string | ||
rate: number; | ||
feedback?: string | ||
} | ||
|
||
const articleRatingAPI = rtkApi.injectEndpoints({ | ||
endpoints: (build) => ({ | ||
getArticleRating: build.query<Rating[], GetArticleRatingArg>({ | ||
query: ({ userId, articleId }) => ({ | ||
url: '/article-ratings', | ||
params: { | ||
userId, | ||
articleId, | ||
}, | ||
|
||
}), | ||
}), | ||
rateArticle: build.mutation<void, RateArticleArg>({ | ||
query: (arg) => ({ | ||
url: '/article-ratings', | ||
method: 'POST', | ||
body: arg, | ||
}), | ||
}), | ||
}), | ||
}); | ||
|
||
export const useGetArticleRating = articleRatingAPI.useGetArticleRatingQuery; | ||
export const useRateArticle = articleRatingAPI.useRateArticleMutation; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { ArticleRatingAsync as ArticleRating } from './ui/ArticleRating/ArticleRating.async'; |
11 changes: 11 additions & 0 deletions
11
src/features/articleRating/ui/ArticleRating/ArticleRating.async.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { lazy, Suspense } from 'react'; | ||
import { ArticleRatingProps } from './ArticleRating'; | ||
import { Skeleton } from '@/shared/ui/Skeleton/Skeleton'; | ||
|
||
const ArticleRatingLazy = lazy(() => import('./ArticleRating')); | ||
|
||
export const ArticleRatingAsync = (props: ArticleRatingProps) => ( | ||
<Suspense fallback={<Skeleton width="100%" height={140} />}> | ||
<ArticleRatingLazy {...props} /> | ||
</Suspense> | ||
); |
17 changes: 17 additions & 0 deletions
17
src/features/articleRating/ui/ArticleRating/ArticleRating.stories.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
import React from 'react'; | ||
import { ComponentStory, ComponentMeta } from '@storybook/react'; | ||
|
||
import ArticleRating from './ArticleRating'; | ||
|
||
export default { | ||
title: 'shared/ArticleRating', | ||
component: ArticleRating, | ||
argTypes: { | ||
backgroundColor: { control: 'color' }, | ||
}, | ||
} as ComponentMeta<typeof ArticleRating>; | ||
|
||
const Template: ComponentStory<typeof ArticleRating> = (args) => <ArticleRating {...args} />; | ||
|
||
export const Normal = Template.bind({}); | ||
Normal.args = {}; |
65 changes: 65 additions & 0 deletions
65
src/features/articleRating/ui/ArticleRating/ArticleRating.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { useTranslation } from 'react-i18next'; | ||
import { memo, useCallback } from 'react'; | ||
import { useSelector } from 'react-redux'; | ||
import { RatingCard } from '@/entities/Rating'; | ||
import { useGetArticleRating, useRateArticle } from '../../api/articleRatingAPI'; | ||
import { getUserAuthData } from '@/entities/User'; | ||
import { Skeleton } from '@/shared/ui/Skeleton/Skeleton'; | ||
|
||
export interface ArticleRatingProps { | ||
className?: string; | ||
articleId: string; | ||
} | ||
|
||
const ArticleRating = memo((props: ArticleRatingProps) => { | ||
const { className, articleId } = props; | ||
const { t } = useTranslation(); | ||
const userData = useSelector(getUserAuthData); | ||
const { data, isLoading } = useGetArticleRating({ | ||
articleId, | ||
userId: userData?.id ?? '', | ||
}); | ||
|
||
const [rateArticleMutation] = useRateArticle(); | ||
|
||
const rating = data?.[0]; | ||
|
||
const handleRateArticle = useCallback((starsCount: number, feedback?: string) => { | ||
try { | ||
rateArticleMutation({ | ||
userId: userData?.id ?? '', | ||
articleId, | ||
rate: starsCount, | ||
feedback, | ||
}); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
}, [articleId, rateArticleMutation, userData?.id]); | ||
|
||
const onCancel = useCallback((starsCount: number) => { | ||
handleRateArticle(starsCount); | ||
}, [handleRateArticle]); | ||
|
||
const onAccept = useCallback((starsCount: number, feedback?: string) => { | ||
handleRateArticle(starsCount, feedback); | ||
}, [handleRateArticle]); | ||
|
||
if (isLoading) { | ||
return <Skeleton width="100%" height={120} border="8px" />; | ||
} | ||
|
||
return ( | ||
<RatingCard | ||
className={className} | ||
title={t('Rate the article')} | ||
feedbackTitle={t('Leave your feedback on the article, it will help improve the quality')} | ||
hasFeedback | ||
rate={rating?.rate} | ||
onAccept={onAccept} | ||
onCancel={onCancel} | ||
/> | ||
); | ||
}); | ||
|
||
export default ArticleRating; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10,3 +10,7 @@ | |
.outlined { | ||
border: 1px solid var(--primary-color); | ||
} | ||
|
||
.max { | ||
width: 100%; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters