forked from deriv-com/deriv-app
-
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.
[FEQ] / Ameerul / FEQ-1719 Implement the rating of order transaction (d…
…eriv-com#14166) * chore: created rating modal * chore: added test cases, added props and change typing
- Loading branch information
1 parent
a7c039c
commit 5459c3f
Showing
9 changed files
with
252 additions
and
10 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
38 changes: 38 additions & 0 deletions
38
packages/p2p-v2/src/components/Modals/RatingModal/RatingModal.scss
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,38 @@ | ||
.p2p-v2-rating-modal { | ||
height: auto; | ||
width: 44rem; | ||
border-radius: 8px; | ||
|
||
@include mobile { | ||
max-width: calc(100vw - 3.2rem); | ||
} | ||
|
||
&__button { | ||
gap: 0.2rem; | ||
padding-left: 0.4rem; | ||
|
||
&--disabled { | ||
// stylelint-disable-next-line declaration-no-important | ||
border-color: #d6dadb !important; | ||
|
||
& .deriv-text > span { | ||
// stylelint-disable-next-line declaration-no-important | ||
color: #999 !important; | ||
} | ||
} | ||
} | ||
|
||
&__stars { | ||
@include mobile { | ||
width: 75%; | ||
display: flex; | ||
justify-content: center; | ||
|
||
& .p2p-v2-star-rating { | ||
& svg { | ||
margin-right: 0.7rem; | ||
} | ||
} | ||
} | ||
} | ||
} |
123 changes: 123 additions & 0 deletions
123
packages/p2p-v2/src/components/Modals/RatingModal/RatingModal.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,123 @@ | ||
import React, { useEffect, useState } from 'react'; | ||
import clsx from 'clsx'; | ||
import { StarRating } from '@/components'; | ||
import { StandaloneThumbsDownRegularIcon, StandaloneThumbsUpRegularIcon } from '@deriv/quill-icons'; | ||
import { Button, Modal, Text, useDevice } from '@deriv-com/ui'; | ||
import './RatingModal.scss'; | ||
|
||
export type TRatingModalProps = { | ||
isBuyOrder: boolean; | ||
isModalOpen: boolean; | ||
isRecommendedPreviously: number | null; | ||
onRequestClose: () => void; | ||
ratingValue: number; | ||
}; | ||
|
||
const RatingModal = ({ | ||
isBuyOrder, | ||
isModalOpen, | ||
isRecommendedPreviously, | ||
onRequestClose, | ||
ratingValue, | ||
}: TRatingModalProps) => { | ||
const [rating, setRating] = useState<number>(ratingValue); | ||
const [isNoSelected, setIsNoSelected] = useState(false); | ||
const [isYesSelected, setIsYesSelected] = useState(false); | ||
|
||
const { isMobile } = useDevice(); | ||
const buttonTextSize = isMobile ? 'sm' : 'xs'; | ||
|
||
const handleSelectYes = () => { | ||
if (isNoSelected) { | ||
setIsNoSelected(false); | ||
} | ||
setIsYesSelected(prevState => !prevState); | ||
}; | ||
|
||
const handleSelectNo = () => { | ||
if (isYesSelected) { | ||
setIsYesSelected(false); | ||
} | ||
setIsNoSelected(prevState => !prevState); | ||
}; | ||
|
||
useEffect(() => { | ||
if (isRecommendedPreviously !== null) { | ||
if (isRecommendedPreviously) { | ||
setIsYesSelected(true); | ||
} else { | ||
setIsNoSelected(true); | ||
} | ||
} | ||
}, []); | ||
|
||
return ( | ||
<Modal ariaHideApp={false} className='p2p-v2-rating-modal' isOpen={isModalOpen} onRequestClose={onRequestClose}> | ||
<Modal.Header hideBorder onRequestClose={onRequestClose}> | ||
<Text size='md' weight='bold'> | ||
How would you rate this transaction? | ||
</Text> | ||
</Modal.Header> | ||
<Modal.Body className='px-0 py-4 lg:px-[2.4rem]'> | ||
<div className='p2p-v2-rating-modal__stars' data-testid='dt_p2p_v2_rating_modal_stars'> | ||
<StarRating allowHover onClick={setRating} ratingValue={rating} starsScale={1.6} /> | ||
</div> | ||
{rating > 0 && ( | ||
<div className='lg:px-0 pt-8 px-[2.4rem]'> | ||
<Text size='sm'>Would you recommend this {`${isBuyOrder ? 'buyer' : 'seller'}`}?</Text> | ||
<div className='mt-6 flex gap-3'> | ||
<Button | ||
className={clsx('p2p-v2-rating-modal__button', { | ||
'p2p-v2-rating-modal__button--disabled': !isYesSelected, | ||
})} | ||
color='black' | ||
icon={ | ||
<StandaloneThumbsUpRegularIcon | ||
fill={isYesSelected ? '#000' : '#999'} | ||
iconSize='sm' | ||
/> | ||
} | ||
onClick={handleSelectYes} | ||
size='sm' | ||
variant='outlined' | ||
> | ||
<Text size={buttonTextSize}>Yes</Text> | ||
</Button> | ||
<Button | ||
className={clsx('p2p-v2-rating-modal__button', { | ||
'p2p-v2-rating-modal__button--disabled': !isNoSelected, | ||
})} | ||
color='black' | ||
icon={ | ||
<StandaloneThumbsDownRegularIcon | ||
fill={isNoSelected ? '#000' : '#999'} | ||
iconSize='sm' | ||
/> | ||
} | ||
onClick={handleSelectNo} | ||
size='sm' | ||
variant='outlined' | ||
> | ||
<Text size={buttonTextSize}>No</Text> | ||
</Button> | ||
</div> | ||
</div> | ||
)} | ||
</Modal.Body> | ||
<Modal.Footer hideBorder> | ||
<Button | ||
className='border-2' | ||
color={rating ? 'primary' : 'black'} | ||
onClick={onRequestClose} | ||
size='lg' | ||
textSize={isMobile ? 'md' : 'sm'} | ||
variant={rating ? 'contained' : 'outlined'} | ||
> | ||
{rating ? 'Done' : 'Skip'} | ||
</Button> | ||
</Modal.Footer> | ||
</Modal> | ||
); | ||
}; | ||
|
||
export default RatingModal; |
74 changes: 74 additions & 0 deletions
74
packages/p2p-v2/src/components/Modals/RatingModal/__tests__/RatingModal.spec.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,74 @@ | ||
import React from 'react'; | ||
import { render, screen } from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import RatingModal, { TRatingModalProps } from '../RatingModal'; | ||
|
||
let mockProps: TRatingModalProps = { | ||
isBuyOrder: true, | ||
isModalOpen: true, | ||
isRecommendedPreviously: null, | ||
onRequestClose: jest.fn(), | ||
ratingValue: 0, | ||
}; | ||
|
||
const disabledClassName = 'p2p-v2-rating-modal__button--disabled'; | ||
|
||
jest.mock('@deriv-com/ui', () => ({ | ||
...jest.requireActual('@deriv-com/ui'), | ||
useDevice: () => ({ isMobile: false }), | ||
})); | ||
|
||
describe('<RatingModal />', () => { | ||
it('should render just the star rating initially', () => { | ||
render(<RatingModal {...mockProps} />); | ||
|
||
expect(screen.getByText('How would you rate this transaction?')).toBeInTheDocument(); | ||
expect(screen.getByTestId('dt_p2p_v2_rating_modal_stars')).toBeInTheDocument(); | ||
expect(screen.queryByText('Would you recommend this buyer?')).not.toBeInTheDocument(); | ||
expect(screen.queryByRole('button', { name: 'Yes' })).not.toBeInTheDocument(); | ||
expect(screen.queryByRole('button', { name: 'No' })).not.toBeInTheDocument(); | ||
expect(screen.queryByRole('button', { name: 'Done' })).not.toBeInTheDocument(); | ||
expect(screen.getByRole('button', { name: 'Skip' })).toBeInTheDocument(); | ||
}); | ||
|
||
it('should show the recommendation buttons if ratingValue is passed ', () => { | ||
mockProps = { ...mockProps, ratingValue: 4 }; | ||
render(<RatingModal {...mockProps} />); | ||
|
||
expect(screen.getByText('Would you recommend this buyer?')).toBeInTheDocument(); | ||
expect(screen.getByRole('button', { name: 'Yes' })).toBeInTheDocument(); | ||
expect(screen.getByRole('button', { name: 'No' })).toBeInTheDocument(); | ||
expect(screen.getByRole('button', { name: 'Done' })).toBeInTheDocument(); | ||
expect(screen.queryByRole('button', { name: 'Skip' })).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('should enable the Yes button when clicked and disabled the No button if isRecommendedPreviously is 0', () => { | ||
mockProps = { ...mockProps, isRecommendedPreviously: 0 }; | ||
|
||
render(<RatingModal {...mockProps} />); | ||
|
||
const noButton = screen.getByRole('button', { name: 'No' }); | ||
expect(noButton).not.toHaveClass(disabledClassName); | ||
|
||
const yesButton = screen.getByRole('button', { name: 'Yes' }); | ||
userEvent.click(yesButton); | ||
|
||
expect(yesButton).not.toHaveClass(disabledClassName); | ||
expect(noButton).toHaveClass(disabledClassName); | ||
}); | ||
|
||
it('should enable the No button when clicked and disabled the Yes button if isRecommendedPreviously is 1', () => { | ||
mockProps = { ...mockProps, isRecommendedPreviously: 1 }; | ||
|
||
render(<RatingModal {...mockProps} />); | ||
|
||
const yesButton = screen.getByRole('button', { name: 'Yes' }); | ||
expect(yesButton).not.toHaveClass(disabledClassName); | ||
|
||
const noButton = screen.getByRole('button', { name: 'No' }); | ||
userEvent.click(noButton); | ||
|
||
expect(noButton).not.toHaveClass(disabledClassName); | ||
expect(yesButton).toHaveClass(disabledClassName); | ||
}); | ||
}); |
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 { default as RatingModal } from './RatingModal'; |
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