Commit 64c4d1cd authored by Max Alekseenko's avatar Max Alekseenko

add rating count

parent 18eb8120
...@@ -29,6 +29,7 @@ export type MarketplaceAppOverview = MarketplaceAppPreview & MarketplaceAppSocia ...@@ -29,6 +29,7 @@ export type MarketplaceAppOverview = MarketplaceAppPreview & MarketplaceAppSocia
export type AppRating = { export type AppRating = {
recordId: string; recordId: string;
value: number | undefined; value: number | undefined;
count?: number;
} }
export type MarketplaceAppWithSecurityReport = MarketplaceAppOverview & { export type MarketplaceAppWithSecurityReport = MarketplaceAppOverview & {
......
...@@ -51,6 +51,7 @@ const Rating = ({ ...@@ -51,6 +51,7 @@ const Rating = ({
<> <>
<Stars filledIndex={ (rating?.value || 0) - 1 }/> <Stars filledIndex={ (rating?.value || 0) - 1 }/>
<Text fontSize="md" ml={ 2 }>{ rating?.value }</Text> <Text fontSize="md" ml={ 2 }>{ rating?.value }</Text>
{ rating?.count && <Text variant="secondary" fontSize="md" ml={ 1 }>({ rating?.count })</Text> }
</> </>
) } ) }
<Box ref={ popoverRef }> <Box ref={ popoverRef }>
...@@ -58,6 +59,7 @@ const Rating = ({ ...@@ -58,6 +59,7 @@ const Rating = ({
<PopoverTrigger> <PopoverTrigger>
<TriggerButton <TriggerButton
rating={ rating?.value } rating={ rating?.value }
count={ rating?.count }
fullView={ fullView } fullView={ fullView }
isActive={ isOpen } isActive={ isOpen }
onClick={ onToggle } onClick={ onToggle }
......
import { Button, chakra, useColorModeValue, Tooltip, useDisclosure } from '@chakra-ui/react'; import { Button, chakra, useColorModeValue, Tooltip, useDisclosure, Text } from '@chakra-ui/react';
import React from 'react'; import React from 'react';
import useIsMobile from 'lib/hooks/useIsMobile'; import useIsMobile from 'lib/hooks/useIsMobile';
...@@ -7,6 +7,7 @@ import IconSvg from 'ui/shared/IconSvg'; ...@@ -7,6 +7,7 @@ import IconSvg from 'ui/shared/IconSvg';
type Props = { type Props = {
rating?: number; rating?: number;
count?: number;
fullView?: boolean; fullView?: boolean;
isActive: boolean; isActive: boolean;
onClick: () => void; onClick: () => void;
...@@ -24,7 +25,7 @@ const getTooltipText = (canRate: boolean | undefined) => { ...@@ -24,7 +25,7 @@ const getTooltipText = (canRate: boolean | undefined) => {
}; };
const TriggerButton = ( const TriggerButton = (
{ rating, fullView, isActive, onClick, canRate }: Props, { rating, count, fullView, isActive, onClick, canRate }: Props,
ref: React.ForwardedRef<HTMLButtonElement>, ref: React.ForwardedRef<HTMLButtonElement>,
) => { ) => {
const textColor = useColorModeValue('blackAlpha.800', 'whiteAlpha.800'); const textColor = useColorModeValue('blackAlpha.800', 'whiteAlpha.800');
...@@ -75,8 +76,9 @@ const TriggerButton = ( ...@@ -75,8 +76,9 @@ const TriggerButton = (
/> />
) } ) }
{ (rating && !fullView) ? ( { (rating && !fullView) ? (
<chakra.span color={ textColor } transition="inherit"> <chakra.span color={ textColor } transition="inherit" display="inline-flex">
{ rating } { rating }
<Text variant="secondary" ml={ 1 }>({ count })</Text>
</chakra.span> </chakra.span>
) : ( ) : (
'Rate it!' 'Rate it!'
......
...@@ -28,11 +28,12 @@ export type RateFunction = ( ...@@ -28,11 +28,12 @@ export type RateFunction = (
function formatRatings(data: Airtable.Records<Airtable.FieldSet>) { function formatRatings(data: Airtable.Records<Airtable.FieldSet>) {
return data.reduce((acc: Record<string, AppRating>, record) => { return data.reduce((acc: Record<string, AppRating>, record) => {
const fields = record.fields as { appId: string | Array<string>; rating: number | undefined }; const fields = record.fields as { appId: string | Array<string>; rating: number | undefined; count?: number };
const appId = Array.isArray(fields.appId) ? fields.appId[0] : fields.appId; const appId = Array.isArray(fields.appId) ? fields.appId[0] : fields.appId;
acc[appId] = { acc[appId] = {
recordId: record.id, recordId: record.id,
value: fields.rating, value: fields.rating,
count: fields.count,
}; };
return acc; return acc;
}, {}); }, {});
...@@ -63,7 +64,7 @@ export default function useRatings() { ...@@ -63,7 +64,7 @@ export default function useRatings() {
return; return;
} }
try { try {
const data = await airtable('apps_ratings').select({ fields: [ 'appId', 'rating' ] }).all(); const data = await airtable('apps_ratings').select({ fields: [ 'appId', 'rating', 'count' ] }).all();
const ratings = formatRatings(data); const ratings = formatRatings(data);
setRatings(ratings); setRatings(ratings);
} catch (error) { } catch (error) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment