Skip to content

Commit

Permalink
FeaturePanel: small refactorings of FHRS (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
zbycz authored Sep 10, 2023
1 parent 3c83713 commit 974d1ac
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 37 deletions.
76 changes: 40 additions & 36 deletions src/components/FeaturePanel/renderers/FoodHygieneRatingScheme.tsx
Original file line number Diff line number Diff line change
@@ -1,24 +1,9 @@
import React, { useEffect, useState } from 'react';
import { Typography } from '@material-ui/core';
import { Tooltip, Typography } from '@material-ui/core';
import { Restaurant } from '@material-ui/icons';
import styled from 'styled-components';
import { getEstablishmentRatingValue } from '../../../services/fhrsApi';

interface StarRatingProps {
stars: number;
maxStars: number;
}

const StarRating = ({ stars, maxStars }: StarRatingProps) => {
const starArray = new Array(maxStars).fill(0);
return (
<div>
{starArray.map((_, i) => (
<span>{i < stars ? '★' : '☆'}</span>
))}
</div>
);
};

const useLoadingState = () => {
const [rating, setRating] = useState<number>();
const [error, setError] = useState<string>();
Expand All @@ -43,6 +28,26 @@ const useLoadingState = () => {
return { rating, error, loading, startRating, finishRating, failRating };
};

const Wrapper = styled.div`
display: flex;
gap: 0.5rem;
.MuiRating-root {
margin-top: -2px;
}
`;

const RatingRound = styled.span`
border-radius: 50%;
background-color: #1a6500;
color: #fff;
padding: 0.25rem 0.5rem;
font-size: 0.75rem;
font-weight: bold;
position: relative;
top: -1px;
`;

export const FoodHygieneRatingSchemeRenderer = ({ v }) => {
const { rating, error, loading, startRating, finishRating, failRating } =
useLoadingState();
Expand All @@ -60,15 +65,6 @@ export const FoodHygieneRatingSchemeRenderer = ({ v }) => {
loadData();
}, []);

const linkStyle: React.CSSProperties = {
display: 'flex',
alignItems: 'center',
gap: '0.5rem',
};
const pStyle: React.CSSProperties = {
marginBlock: '0',
};

return (
<>
{loading ? (
Expand All @@ -80,20 +76,28 @@ export const FoodHygieneRatingSchemeRenderer = ({ v }) => {
) : (
<>
<Restaurant fontSize="small" />
{Number.isNaN(rating) || error ? (
<Typography color="error">No rating available</Typography>
) : (
<>
<Wrapper>
<Tooltip
arrow
interactive
title="Food Hygiene Rating Scheme (only in UK)"
placement="bottom-end"
>
<a
href={`https://ratings.food.gov.uk/business/${v}`}
title="Food Hygiene Rating Scheme"
style={linkStyle}
className="colorInherit"
>
<p style={pStyle}>FHRS</p>
<StarRating stars={rating} maxStars={5} />
FHRS{' '}
{Number.isNaN(rating) || error ? (
<Typography color="textSecondary">
(Error while fetching rating)
</Typography>
) : (
<RatingRound>{rating}</RatingRound>
)}
</a>
</>
)}
</Tooltip>
</Wrapper>
</>
)}
</>
Expand Down
4 changes: 4 additions & 0 deletions src/helpers/theme.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ const lightTheme = createMuiTheme({
},
text: {
panelHeading: 'rgba(0, 0, 0, 0.7)',
primary: '#000',
primaryInvert: '#fff',
},
appBackground: '#f8f4f0',
panelBackground: '#fafafa',
Expand Down Expand Up @@ -52,6 +54,8 @@ const darkTheme = createMuiTheme({
},
text: {
panelHeading: 'rgba(255, 255, 255, 0.85)',
primary: '#fff',
primaryInvert: '#000',
},
appBackground: grey['900'],
panelBackground: grey['800'],
Expand Down
2 changes: 1 addition & 1 deletion src/services/__tests__/fhrsApi.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('fetchRating', () => {
});

const rating = await getEstablishmentRatingValue(269382);
expect(rating).toBeLessThanOrEqual(5);
expect(rating).toBe(5);
fetchJsonMock.mockRestore();
});
});

1 comment on commit 974d1ac

@vercel
Copy link

@vercel vercel bot commented on 974d1ac Sep 10, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

osmapp – ./

osmapp-zbycz.vercel.app
osmapp.vercel.app
osmapp.org
osmapp-git-prod-zbycz.vercel.app

Please sign in to comment.