-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
react/ratings: adapt ratings buttons to have new icons, more modern c…
…ode and tests
- Loading branch information
Showing
16 changed files
with
641 additions
and
175 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
import React, { useCallback, useMemo, useState } from 'react' | ||
import django from 'django' | ||
|
||
import { createOrModifyRating } from './rating_api' | ||
import RatingButton from './RatingButton' | ||
import config from '../../../static/config' | ||
|
||
const translations = { | ||
likes: django.gettext('Likes'), | ||
dislikes: django.gettext('Dislikes') | ||
} | ||
|
||
const getRedirectUrl = (id) => config.getLoginUrl() + (id ? encodeURIComponent('?comment=' + id) : '') | ||
|
||
const RatingBox = ({ | ||
positiveRatings, | ||
negativeRatings, | ||
userHasRating, | ||
userRating, | ||
userRatingId, | ||
isReadOnly, | ||
contentType, | ||
objectId, | ||
authenticatedAs, | ||
isComment, | ||
render | ||
}) => { | ||
const [ratings, setRatings] = useState({ negative: negativeRatings, positive: positiveRatings }) | ||
const [userRatingData, setUserRatingData] = useState({ userHasRating, userRating, userRatingId }) | ||
|
||
const clickHandler = useCallback(async (number) => { | ||
if (!authenticatedAs) { | ||
window.location.href = getRedirectUrl(isComment && objectId) | ||
} | ||
const [ratings, _userRatingData] = await createOrModifyRating(number, objectId, contentType, userRatingData.userRatingId) | ||
|
||
setRatings(ratings) | ||
setUserRatingData({ ...userRatingData, ..._userRatingData }) | ||
}, [authenticatedAs, objectId, contentType, userRatingData, isComment]) | ||
|
||
const customChildren = useMemo(() => { | ||
if (render && typeof render === 'function') { | ||
return render({ ratings, userRatingData, isReadOnly, clickHandler }) | ||
} | ||
return null | ||
}, [clickHandler, isReadOnly, ratings, render, userRatingData]) | ||
|
||
// return either custom html from the render prop or the default | ||
return customChildren ?? ( | ||
<div className="rating" data-testid="rating-box"> | ||
<RatingButton | ||
isReadOnly={isReadOnly} | ||
onClick={clickHandler} | ||
active={userRatingData.userRating === 1} | ||
rating={1} | ||
authenticatedAs={authenticatedAs} | ||
> | ||
<i className="fa fa-thumbs-up" aria-hidden="true" />{' '} | ||
{ratings.positive}{' '} | ||
<span className="rating__label">{translations.likes}</span> | ||
</RatingButton> | ||
<RatingButton | ||
isReadOnly={isReadOnly} | ||
onClick={clickHandler} | ||
active={userRatingData.userRating === -1} | ||
rating={-1} | ||
authenticatedAs={authenticatedAs} | ||
> | ||
<i className="fa fa-thumbs-down" aria-hidden="true" />{' '} | ||
{ratings.negative}{' '} | ||
<span className="rating__label">{translations.dislikes}</span> | ||
</RatingButton> | ||
</div> | ||
) | ||
} | ||
|
||
export default RatingBox |
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,44 @@ | ||
import React from 'react' | ||
import django from 'django' | ||
|
||
const translations = { | ||
upvote: django.gettext('Click to like'), | ||
downvote: django.gettext('Click to dislike') | ||
} | ||
|
||
const RatingButton = ({ | ||
rating, | ||
active, | ||
onClick, | ||
authenticatedAs, | ||
isReadOnly, | ||
children | ||
}) => { | ||
const onClickWrapper = () => { | ||
if (isReadOnly) { | ||
return null | ||
} | ||
|
||
onClick(active ? 0 : rating) | ||
} | ||
|
||
const type = rating === -1 ? 'down' : 'up' | ||
const cssClasses = active | ||
? 'rating-button rating-' + type + ' is-selected' | ||
: 'rating-button rating-' + type | ||
|
||
return ( | ||
<button | ||
aria-pressed={active} | ||
className={cssClasses} | ||
disabled={authenticatedAs !== null && isReadOnly} | ||
onClick={onClickWrapper} | ||
type="button" | ||
> | ||
{children} | ||
<span className="aural">{type === 'up' ? translations.upvote : translations.downvote}</span> | ||
</button> | ||
) | ||
} | ||
|
||
export default RatingButton |
Oops, something went wrong.