From 22089ef7a2c6cb0753f809caf3a18c00bcec13d6 Mon Sep 17 00:00:00 2001 From: prince-deriv Date: Tue, 12 Dec 2023 10:16:08 +0400 Subject: [PATCH] chore: remove trustpilot hook --- .../horizontal/horizontal.stories.tsx | 14 +- .../src/lib/social-proof/horizontal/index.tsx | 7 +- libs/blocks/src/lib/social-proof/types.ts | 2 +- .../src/lib/social-proof/vertical/index.tsx | 8 +- .../vertical/vertical.stories.tsx | 14 +- libs/hooks/src/index.ts | 1 - libs/hooks/src/lib/use-trustpilot/index.ts | 153 ------------------ .../src/lib/home/sections/review/index.tsx | 2 +- 8 files changed, 20 insertions(+), 181 deletions(-) delete mode 100644 libs/hooks/src/lib/use-trustpilot/index.ts diff --git a/libs/blocks/src/lib/social-proof/horizontal/horizontal.stories.tsx b/libs/blocks/src/lib/social-proof/horizontal/horizontal.stories.tsx index 279d5c4b0..05c71e97f 100644 --- a/libs/blocks/src/lib/social-proof/horizontal/horizontal.stories.tsx +++ b/libs/blocks/src/lib/social-proof/horizontal/horizontal.stories.tsx @@ -17,7 +17,7 @@ export const Default: Story = { 'Excellent', `TrustScore ${trustScore} out of 5 based on ${numberOfReviews} reviews`, ], - staticData: { + data: { trustScore: 4.5, numberOfReviews: '45454', stars: 4.4, @@ -40,7 +40,7 @@ export const CustomTheme: Story = { 'Excellent', `TrustScore ${trustScore} out of 5 based on ${numberOfReviews} reviews`, ], - staticData: { + data: { trustScore: 4.5, numberOfReviews: '45454', stars: 4.4, @@ -67,7 +67,7 @@ export const BadScore: Story = { 'Excellent', `TrustScore ${trustScore} out of 5 based on ${numberOfReviews} reviews`, ], - staticData: { + data: { trustScore: 0.6, numberOfReviews: '45454', stars: 0.6, @@ -90,7 +90,7 @@ export const PoorScore: Story = { 'Excellent', `TrustScore ${trustScore} out of 5 based on ${numberOfReviews} reviews`, ], - staticData: { + data: { trustScore: 1.6, numberOfReviews: '45454', stars: 1.6, @@ -113,7 +113,7 @@ export const AverageScore: Story = { 'Excellent', `TrustScore ${trustScore} out of 5 based on ${numberOfReviews} reviews`, ], - staticData: { + data: { trustScore: 2.5, numberOfReviews: '45454', stars: 2.5, @@ -136,7 +136,7 @@ export const GreatScore: Story = { 'Excellent', `TrustScore ${trustScore} out of 5 based on ${numberOfReviews} reviews`, ], - staticData: { + data: { trustScore: 3.8, numberOfReviews: '99990', stars: 3.8, @@ -159,7 +159,7 @@ export const ExcellentScore: Story = { 'Excellent', `TrustScore ${trustScore} out of 5 based on ${numberOfReviews} reviews`, ], - staticData: { + data: { trustScore: 4.8, numberOfReviews: '45454', stars: 4.8, diff --git a/libs/blocks/src/lib/social-proof/horizontal/index.tsx b/libs/blocks/src/lib/social-proof/horizontal/index.tsx index 469afd650..04cbbf7b3 100644 --- a/libs/blocks/src/lib/social-proof/horizontal/index.tsx +++ b/libs/blocks/src/lib/social-proof/horizontal/index.tsx @@ -1,6 +1,5 @@ import { Section, Text, qtMerge } from '@deriv/quill-design'; import { Rating } from '@deriv-com/components'; -import { useTrustpilotApi } from '@deriv-com/hooks'; import { SocialProofProps } from '../types'; import { getThemeClassNames } from '../classnames'; import { redirectToTrustpilot } from '../helper'; @@ -10,10 +9,8 @@ const SocialProofHorizontal = ({ className, content, logo, - staticData = null, + data = null, }: SocialProofProps) => { - const { data } = useTrustpilotApi(staticData); - const themeClassNames = getThemeClassNames(theme); return ( @@ -25,7 +22,7 @@ const SocialProofHorizontal = ({ )} >
{data && ( diff --git a/libs/blocks/src/lib/social-proof/types.ts b/libs/blocks/src/lib/social-proof/types.ts index d1e34219a..865026992 100644 --- a/libs/blocks/src/lib/social-proof/types.ts +++ b/libs/blocks/src/lib/social-proof/types.ts @@ -14,7 +14,7 @@ export interface SocialProofProps { content?: ({ ...props }: TPilotDataProps) => string[] | ReactNode[]; className?: string; logo: ReactNode; - staticData?: TPilotDataProps | null; + data?: TPilotDataProps | null; } export interface SocialProofThemeClassNames { diff --git a/libs/blocks/src/lib/social-proof/vertical/index.tsx b/libs/blocks/src/lib/social-proof/vertical/index.tsx index 7c63dde52..6b9a0a540 100644 --- a/libs/blocks/src/lib/social-proof/vertical/index.tsx +++ b/libs/blocks/src/lib/social-proof/vertical/index.tsx @@ -1,7 +1,5 @@ import { Heading, Section, Text, qtMerge } from '@deriv/quill-design'; import { Rating } from '@deriv-com/components'; -import { useTrustpilotApi } from '@deriv-com/hooks'; - import { SocialProofProps } from '../types'; import { getThemeClassNames } from '../classnames'; import { redirectToTrustpilot } from '../helper'; @@ -15,11 +13,9 @@ const SocialProofVertical = ({ theme, className, content, - staticData, + data, logo, }: SocialProofVerticalProps) => { - const { data } = useTrustpilotApi(staticData); - const themeClassNames = getThemeClassNames(theme); return ( @@ -31,7 +27,7 @@ const SocialProofVertical = ({ )} >
{title && ( diff --git a/libs/blocks/src/lib/social-proof/vertical/vertical.stories.tsx b/libs/blocks/src/lib/social-proof/vertical/vertical.stories.tsx index d24d1e9a7..5b100997f 100644 --- a/libs/blocks/src/lib/social-proof/vertical/vertical.stories.tsx +++ b/libs/blocks/src/lib/social-proof/vertical/vertical.stories.tsx @@ -16,7 +16,7 @@ export const Default: Story = { `TrustScore ${trustScore}`, `${numberOfReviews} reviews`, ], - staticData: { + data: { trustScore: 4.5, numberOfReviews: '45454', stars: 4.4, @@ -38,7 +38,7 @@ export const CustomTheme: Story = { `TrustScore ${trustScore}`, `${numberOfReviews} reviews`, ], - staticData: { + data: { trustScore: 4.5, numberOfReviews: '45454', stars: 4.4, @@ -65,7 +65,7 @@ export const BadScore: Story = { `TrustScore ${trustScore}`, `${numberOfReviews} reviews`, ], - staticData: { + data: { trustScore: 0.6, numberOfReviews: '45454', stars: 0.6, @@ -87,7 +87,7 @@ export const PoorScore: Story = { `TrustScore ${trustScore}`, `${numberOfReviews} reviews`, ], - staticData: { + data: { trustScore: 1.6, numberOfReviews: '45454', stars: 1.6, @@ -109,7 +109,7 @@ export const AverageScore: Story = { `TrustScore ${trustScore}`, `${numberOfReviews} reviews`, ], - staticData: { + data: { trustScore: 2.5, numberOfReviews: '45454', stars: 2.5, @@ -131,7 +131,7 @@ export const GreatScore: Story = { `TrustScore ${trustScore}`, `${numberOfReviews} reviews`, ], - staticData: { + data: { trustScore: 3.8, numberOfReviews: '99990', stars: 3.8, @@ -153,7 +153,7 @@ export const ExcellentScore: Story = { `TrustScore ${trustScore}`, `${numberOfReviews} reviews`, ], - staticData: { + data: { trustScore: 4.8, numberOfReviews: '45454', stars: 4.8, diff --git a/libs/hooks/src/index.ts b/libs/hooks/src/index.ts index d9c320ea4..d732317bb 100644 --- a/libs/hooks/src/index.ts +++ b/libs/hooks/src/index.ts @@ -1,5 +1,4 @@ export * from './lib/use-build-variant'; export * from './lib/use-navigation'; export * from './lib/use-shared-link'; -export * from './lib/use-trustpilot'; export * from './lib/use-lang-switcher'; diff --git a/libs/hooks/src/lib/use-trustpilot/index.ts b/libs/hooks/src/lib/use-trustpilot/index.ts deleted file mode 100644 index 72ebfd476..000000000 --- a/libs/hooks/src/lib/use-trustpilot/index.ts +++ /dev/null @@ -1,153 +0,0 @@ -import { useState, useEffect } from 'react'; - -export interface TrustPilotDataProps { - id?: string; - country?: string; - name?: { - referring?: [string]; - identifying?: string; - }; - score?: { - stars: number; - trustScore: number; - }; - numberOfReviews?: { - total: number; - usedForTrustScoreCalculation: number; - oneStar: number; - twoStars: number; - threeStars: number; - fourStars: number; - fiveStars: number; - }; -} - -export interface TPilotDataProps { - numberOfReviews: string; - stars: number; - trustScore: number; -} - -interface TrustpilotResult { - data: TPilotDataProps | null | undefined; - isLoading: boolean; - hasError: Error | null | unknown; -} - -interface StorageSetterProps { - key: string; - value: { [key: string]: string | number }; - expirationInDays?: number; -} - -/*** NOTE: we need to move this to the utilities later */ - -const setLocalItemWithExpiry = ({ - key, - value, - expirationInDays = 1, -}: StorageSetterProps) => { - const now = new Date(); - const expiration = now.getTime() + expirationInDays * 24 * 60 * 60 * 1000; // Convert days to milliseconds - - const item = { - value: value, - expiry: expiration, - }; - - if (typeof localStorage !== 'undefined') { - localStorage.setItem(key, JSON.stringify(item)); - } -}; - -const getLocalItemWithExpiry = (key: string) => { - if (typeof localStorage !== 'undefined') { - const itemString = localStorage.getItem(key); - - if (!itemString) { - return null; // Item not found - } - - const item = JSON.parse(itemString); - const now = new Date(); - - if (now.getTime() > item.expiry) { - localStorage.removeItem(key); // Remove the item if it has expired - return null; - } - - return item.value; - } - - return null; -}; - -/********************** */ - -export const useTrustpilotApi = ( - staticData?: TPilotDataProps | null, -): TrustpilotResult => { - const [data, setData] = useState(null); - const [isLoading, setLoading] = useState(true); - const [hasError, setError] = useState(null); - const localDataKey = 'trustpilot-data'; - const localData = getLocalItemWithExpiry(localDataKey); - - useEffect(() => { - const fetchData = async () => { - try { - const appName = - process.env.NEXT_PUBLIC_TRUSTPILOT_APP_NAME || - process.env.GATSBY_TRUSTPILOT_APP_NAME || - ''; - const apiKey = - process.env.NEXT_PUBLIC_TRUSTPILOT_API_KEY || - process.env.GATSBY_TRUSTPILOT_API_KEY || - ''; - - if (!appName || !apiKey) { - throw new Error('Trustpilot app name or API key is missing'); - } - - const url = `https://api.trustpilot.com/v1/business-units/find?name=${appName}&apikey=${apiKey}`; - const response = await fetch(url); - - if (!response.ok) { - throw new Error( - `Network response was not ok: ${response.statusText}`, - ); - } - - const result: TrustPilotDataProps = await response.json(); - const tpData = { - stars: result.score?.stars || 0, - trustScore: result.score?.trustScore || 0, - numberOfReviews: - result.numberOfReviews?.usedForTrustScoreCalculation.toLocaleString() || - '', - }; - - setData(tpData); - setLocalItemWithExpiry({ key: localDataKey, value: tpData }); - } catch (error) { - setError(error); - } finally { - setLoading(false); - } - }; - - if (!staticData) { - if (localData) { - setData(localData); - } else { - fetchData(); - } - } else { - setData(staticData); - } - }, []); - - return { data, isLoading, hasError }; -}; - -export default useTrustpilotApi; diff --git a/libs/templates/src/lib/home/sections/review/index.tsx b/libs/templates/src/lib/home/sections/review/index.tsx index 005223a36..48ee52dc8 100644 --- a/libs/templates/src/lib/home/sections/review/index.tsx +++ b/libs/templates/src/lib/home/sections/review/index.tsx @@ -22,7 +22,7 @@ const ReviewSection = () => { /> } //TODO: We will remove this static data once the data fetching is finalized. - staticData={{ + data={{ trustScore: 4.6, numberOfReviews: '37,133', stars: 4.6,