-
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 rating without saving on server
- Loading branch information
Showing
16 changed files
with
254 additions
and
5 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
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 @@ | ||
export { RatingCard } from './ui/RatingCard/RatingCard'; |
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,16 @@ | ||
import type { ComponentMeta, ComponentStory } from '@storybook/react'; | ||
|
||
import { RatingCard } from './RatingCard'; | ||
|
||
export default { | ||
title: 'shared/Rating', | ||
component: RatingCard, | ||
argTypes: { | ||
backgroundColor: { control: 'color' }, | ||
}, | ||
} as ComponentMeta<typeof RatingCard>; | ||
|
||
const Template: ComponentStory<typeof RatingCard> = (args) => <RatingCard {...args} />; | ||
|
||
export const Normal = Template.bind({}); | ||
Normal.args = {}; |
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,104 @@ | ||
import { memo, useCallback, useState } from 'react'; | ||
import { useTranslation } from 'react-i18next'; | ||
import { BrowserView, MobileView } from 'react-device-detect'; | ||
|
||
import { classNames } from '@/shared/lib/classNames'; | ||
import { Card } from '@/shared/ui/Card'; | ||
import { HStack, VStack } from '@/shared/ui/Stack'; | ||
import { Text } from '@/shared/ui/Text'; | ||
import { StarRating } from '@/shared/ui/StarRating'; | ||
import { Modal } from '@/shared/ui/Modal'; | ||
import { Input } from '@/shared/ui/Input'; | ||
import { Button, ButtonSize, ButtonTheme } from '@/shared/ui/Button'; | ||
import { Drawer } from '@/shared/ui/Drawer'; | ||
|
||
import cls from './RatingCard.module.scss'; | ||
|
||
interface RatingCardProps { | ||
className?: string; | ||
title?: string; | ||
feedbackTitle?: string; | ||
hasFeedback?: boolean; | ||
onCancel?: (starsCount: number) => void; | ||
onAccept?: (starsCount: number, feedback?: string) => void; | ||
} | ||
|
||
export const RatingCard = memo((props: RatingCardProps) => { | ||
const { | ||
className, | ||
title, | ||
feedbackTitle, | ||
hasFeedback, | ||
onCancel, | ||
onAccept, | ||
} = props; | ||
const { t } = useTranslation(); | ||
|
||
const [isModalOpen, setIsModalOpen] = useState(false); | ||
const [starsCount, setStarsCount] = useState(0); | ||
const [feedback, setFeedback] = useState(''); | ||
|
||
const onSelectStars = useCallback((selectedStarsCount: number) => { | ||
setStarsCount(selectedStarsCount); | ||
|
||
if (hasFeedback) { | ||
setIsModalOpen(true); | ||
} else { | ||
onAccept?.(selectedStarsCount); | ||
} | ||
}, [hasFeedback, onAccept]); | ||
|
||
const acceptHandler = useCallback(() => { | ||
setIsModalOpen(false); | ||
onAccept?.(starsCount, feedback); | ||
}, [feedback, starsCount, onAccept]); | ||
|
||
const cancelHandler = useCallback(() => { | ||
setIsModalOpen(false); | ||
onCancel?.(starsCount); | ||
}, [onCancel, starsCount]); | ||
|
||
const modalContent = ( | ||
<> | ||
<Text title={feedbackTitle} /> | ||
|
||
<Input value={feedback} onChange={setFeedback} placeholder={t('Ваш отзыв')} /> | ||
</> | ||
); | ||
|
||
return ( | ||
<Card className={classNames(cls.ratingCard, {}, [className])}> | ||
<VStack align="center" gap="8"> | ||
<Text title={title} /> | ||
|
||
<StarRating size={40} onSelect={onSelectStars} /> | ||
</VStack> | ||
|
||
<BrowserView> | ||
<Modal isOpen={isModalOpen} lazy onClose={cancelHandler}> | ||
<VStack gap="32" max> | ||
{modalContent} | ||
|
||
<HStack gap="16" max justify="end"> | ||
<Button onClick={acceptHandler}>{t('Отправить')}</Button> | ||
|
||
<Button theme={ButtonTheme.OUTLINE_RED} onClick={cancelHandler}>{t('Отмена')}</Button> | ||
</HStack> | ||
</VStack> | ||
</Modal> | ||
</BrowserView> | ||
|
||
<MobileView> | ||
<Drawer isOpen={isModalOpen} lazy onClose={cancelHandler}> | ||
<VStack gap="32" max> | ||
{modalContent} | ||
|
||
<Button onClick={acceptHandler} size={ButtonSize.L} fullWidth>{t('Отправить')}</Button> | ||
</VStack> | ||
</Drawer> | ||
</MobileView> | ||
</Card> | ||
); | ||
}); | ||
|
||
RatingCard.displayName = 'RatingCard'; |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 |
---|---|---|
|
@@ -81,3 +81,7 @@ | |
.disabled { | ||
opacity: 0.5; | ||
} | ||
|
||
.fullWidth { | ||
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
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 @@ | ||
export { StarRating } from './ui/StarRating'; |
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,15 @@ | ||
.starIcon { | ||
cursor: pointer; | ||
} | ||
|
||
.selected { | ||
cursor: auto; | ||
} | ||
|
||
.normal { | ||
fill: none; | ||
} | ||
|
||
.hovered { | ||
fill: var(--primary-color); | ||
} |
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,16 @@ | ||
import type { ComponentMeta, ComponentStory } from '@storybook/react'; | ||
|
||
import { StarRating } from './StarRating'; | ||
|
||
export default { | ||
title: 'shared/StarRating', | ||
component: StarRating, | ||
argTypes: { | ||
backgroundColor: { control: 'color' }, | ||
}, | ||
} as ComponentMeta<typeof StarRating>; | ||
|
||
const Template: ComponentStory<typeof StarRating> = (args) => <StarRating {...args} />; | ||
|
||
export const Normal = Template.bind({}); | ||
Normal.args = {}; |
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,74 @@ | ||
import { memo, useState } from 'react'; | ||
|
||
import { classNames } from '@/shared/lib/classNames'; | ||
import StarIcon from '@/shared/assets/icons/star-20-20.svg'; | ||
|
||
import cls from './StarRating.module.scss'; | ||
import { Icon } from '../../Icon'; | ||
|
||
interface StarRatingProps { | ||
className?: string; | ||
onSelect?: (starsCount: number) => void; | ||
size?: number; | ||
selectedStars?: number; | ||
} | ||
|
||
const stars = [1, 2, 3, 4, 5]; | ||
|
||
export const StarRating = memo((props: StarRatingProps) => { | ||
const { | ||
className, | ||
onSelect, | ||
size = 30, | ||
selectedStars = 0, | ||
} = props; | ||
|
||
const [currentStarsCount, setCurrentStarsCount] = useState(selectedStars); | ||
const [isSelected, setIsSelected] = useState(Boolean(selectedStars)); | ||
|
||
const onHover = (starsCount: number) => () => { | ||
if (!isSelected) { | ||
setCurrentStarsCount(starsCount); | ||
} | ||
}; | ||
|
||
const onLeave = () => { | ||
if (!isSelected) { | ||
setCurrentStarsCount(0); | ||
} | ||
}; | ||
|
||
const onClick = (starsCount: number) => () => { | ||
if (!isSelected) { | ||
onSelect?.(starsCount); | ||
setCurrentStarsCount(starsCount); | ||
setIsSelected(true); | ||
} | ||
}; | ||
|
||
return ( | ||
<div className={classNames(cls.starRating, {}, [className])}> | ||
{stars.map((starNumber) => ( | ||
<Icon | ||
Svg={StarIcon} | ||
key={starNumber} | ||
className={classNames( | ||
cls.starIcon, | ||
{ | ||
[cls.hovered]: currentStarsCount >= starNumber, | ||
[cls.selected]: isSelected, | ||
}, | ||
[cls.normal], | ||
)} | ||
width={size} | ||
height={size} | ||
onMouseLeave={onLeave} | ||
onMouseEnter={onHover(starNumber)} | ||
onClick={onClick(starNumber)} | ||
/> | ||
))} | ||
</div> | ||
); | ||
}); | ||
|
||
StarRating.displayName = 'StarRating'; |