Skip to content

Commit

Permalink
add rating count
Browse files Browse the repository at this point in the history
  • Loading branch information
maxaleks committed Aug 27, 2024
1 parent 18eb812 commit 64c4d1c
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 5 deletions.
1 change: 1 addition & 0 deletions types/client/marketplace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ export type MarketplaceAppOverview = MarketplaceAppPreview & MarketplaceAppSocia
export type AppRating = {
recordId: string;
value: number | undefined;
count?: number;
}

export type MarketplaceAppWithSecurityReport = MarketplaceAppOverview & {
Expand Down
2 changes: 2 additions & 0 deletions ui/marketplace/Rating/Rating.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ const Rating = ({
<>
<Stars filledIndex={ (rating?.value || 0) - 1 }/>
<Text fontSize="md" ml={ 2 }>{ rating?.value }</Text>
{ rating?.count && <Text variant="secondary" fontSize="md" ml={ 1 }>({ rating?.count })</Text> }
</>
) }
<Box ref={ popoverRef }>
<Popover isOpen={ isOpen } placement="bottom" isLazy>
<PopoverTrigger>
<TriggerButton
rating={ rating?.value }
count={ rating?.count }
fullView={ fullView }
isActive={ isOpen }
onClick={ onToggle }
Expand Down
8 changes: 5 additions & 3 deletions ui/marketplace/Rating/TriggerButton.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
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 useIsMobile from 'lib/hooks/useIsMobile';
Expand All @@ -7,6 +7,7 @@ import IconSvg from 'ui/shared/IconSvg';

type Props = {
rating?: number;
count?: number;
fullView?: boolean;
isActive: boolean;
onClick: () => void;
Expand All @@ -24,7 +25,7 @@ const getTooltipText = (canRate: boolean | undefined) => {
};

const TriggerButton = (
{ rating, fullView, isActive, onClick, canRate }: Props,
{ rating, count, fullView, isActive, onClick, canRate }: Props,
ref: React.ForwardedRef<HTMLButtonElement>,
) => {
const textColor = useColorModeValue('blackAlpha.800', 'whiteAlpha.800');
Expand Down Expand Up @@ -75,8 +76,9 @@ const TriggerButton = (
/>
) }
{ (rating && !fullView) ? (
<chakra.span color={ textColor } transition="inherit">
<chakra.span color={ textColor } transition="inherit" display="inline-flex">
{ rating }
<Text variant="secondary" ml={ 1 }>({ count })</Text>
</chakra.span>
) : (
'Rate it!'
Expand Down
5 changes: 3 additions & 2 deletions ui/marketplace/Rating/useRatings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,12 @@ export type RateFunction = (

function formatRatings(data: Airtable.Records<Airtable.FieldSet>) {
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;
acc[appId] = {
recordId: record.id,
value: fields.rating,
count: fields.count,
};
return acc;
}, {});
Expand Down Expand Up @@ -63,7 +64,7 @@ export default function useRatings() {
return;
}
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);
setRatings(ratings);
} catch (error) {
Expand Down

0 comments on commit 64c4d1c

Please sign in to comment.