From a2115a8df55859c2dcc4ba25bb207e3647b55573 Mon Sep 17 00:00:00 2001 From: Guilherme Popolin Date: Fri, 7 Jul 2023 10:51:29 +0200 Subject: [PATCH] feat: added trustpilot service to retrieve Trustpilot data --- apps/store/.env.local.example | 4 ++ .../src/services/trustpilot/trustpilot.tsx | 59 +++++++++++++++++++ .../services/trustpilot/trustpilot.types.ts | 4 ++ 3 files changed, 67 insertions(+) create mode 100644 apps/store/src/services/trustpilot/trustpilot.tsx create mode 100644 apps/store/src/services/trustpilot/trustpilot.types.ts diff --git a/apps/store/.env.local.example b/apps/store/.env.local.example index 33f3fc6a0b..81ef6c622f 100644 --- a/apps/store/.env.local.example +++ b/apps/store/.env.local.example @@ -48,6 +48,10 @@ EDGE_CONFIG= EDGE_CONFIG_MANAGE_API_URL=https://api.vercel.com/v1/edge-config/:configId/items?teamId=:teamId EDGE_CONFIG_MANAGE_API_TOKEN=:token +# Trustpilot +TRUSTPILOT_HEDVIG_BUSINESS_UNIT_ID= +TRUSTPILOT_API_KEY= + FALLBACK_ORIGIN_URL=https://www.dev.hedvigit.com NEXT_PUBLIC_EXPERIMENT_ID= diff --git a/apps/store/src/services/trustpilot/trustpilot.tsx b/apps/store/src/services/trustpilot/trustpilot.tsx new file mode 100644 index 0000000000..3fc4f848f6 --- /dev/null +++ b/apps/store/src/services/trustpilot/trustpilot.tsx @@ -0,0 +1,59 @@ +import { atom, useAtomValue } from 'jotai' +import { useHydrateAtoms } from 'jotai/utils' +import { type TrustpilotData } from './trustpilot.types' + +type JSONResponse = { + score: { + trustScore: number + } + numberOfReviews: { + total: number + } +} + +const trustpilotAtom = atom(null) + +export const useTrustpilotData = () => { + return useAtomValue(trustpilotAtom) +} + +export const useHydrateTrustpilotData = (data: TrustpilotData | null) => { + useHydrateAtoms([[trustpilotAtom, data] as const]) +} + +export const fetchTrustpilotData = async () => { + try { + const hedvigBusinessUnitId = process.env.TRUSTPILOT_HEDVIG_BUSINESS_UNIT_ID + if (!hedvigBusinessUnitId) { + throw new Error('HEDVIG_BUSINESS_UNIT_ID is not configured') + } + + const trustpilotApiKey = process.env.TRUSTPILOT_API_KEY + if (!trustpilotApiKey) { + throw new Error('`TRUSTPILOT_API_KEY` is not configured') + } + + const response = await fetch( + `https://api.trustpilot.com/v1/business-units/${hedvigBusinessUnitId}`, + { + headers: { + apiKey: trustpilotApiKey, + }, + }, + ) + + if (!response.ok) { + throw new Error(`Failed to retrieve Trustpilot information: ${response.statusText}`) + } + + const { score, numberOfReviews }: JSONResponse = await response.json() + + return { + score: score.trustScore, + totalReviews: numberOfReviews.total, + } + } catch (error) { + console.error(error) + return null + } +} diff --git a/apps/store/src/services/trustpilot/trustpilot.types.ts b/apps/store/src/services/trustpilot/trustpilot.types.ts new file mode 100644 index 0000000000..91575a3624 --- /dev/null +++ b/apps/store/src/services/trustpilot/trustpilot.types.ts @@ -0,0 +1,4 @@ +export type TrustpilotData = { + score: number + totalReviews: number +}