Skip to content

Commit

Permalink
GEN-637 - feat: make Trustpilot data available globally (#2710)
Browse files Browse the repository at this point in the history
## Describe your changes

* Fetch _Trustpilot_ data for Storyblok pages and make it available globally.

How the whole thing works:

1. Real data is stored in store's [_Edge Config_ storage](https://vercel.com/hedvig/hedvig-dot-com/stores/edge-config/ecfg_viia3ntdquafelygrc3g6lfs3d2a/items#item=trustpilot);
2. This PR will set a cron job that's going to update it every Monday at 8:00 (just picked that number. Gonna double check if that's enough) - check `apps/store/vercel.json` and `apps/store/src/pages/api/cron/trustpilot.ts` files
3. There's a new service - `apps/store/src/services/trustpilot/trustpilot.tsx` - which is responsible to provide a way to retrieve that data from the storage. It also gonna store it in a Jotai's atom so it can be available in the whole app. We then fetch that data and proper hydrate the atom in the `apps/store/src/pages/[[...slug]].tsx` and `apps/store/src/components/LayoutWithMenu/LayoutWithMenu.tsx` files respectively.
4. `TrustpilotBlock` then reads that data from the atom and proper display it.

<img width="974" alt="Screenshot 2023-07-06 at 17 41 54" src="https://github.com/HedvigInsurance/racoon/assets/19200662/50d291c1-cbac-491b-b7ac-de9cc9c54d14">

## Justify why they are needed

Requested by design/editors

Can be tested [here](https://hedvig-dot-com-git-gen-637-feattrustpilot-blocksplit2-hedvig.vercel.app/se/test-home?__vercel_draft=1)
  • Loading branch information
guilhermespopolin committed Jul 10, 2023
1 parent e5bd5b3 commit 327a5bc
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ export const LayoutWithMenu = (props: LayoutWithMenuProps) => {
const { story, globalStory, className, breadcrumbs } = props.children.props

useHydrateProductMetadata(props.children.props[GLOBAL_PRODUCT_METADATA_PROP_NAME])

const handleLocaleChange = useChangeLocale(story)

// Announcements are added as reusable blocks for Page and ProductPage content types
Expand Down
2 changes: 2 additions & 0 deletions apps/store/src/components/ProductPage/ProductPage.types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ProductDataQuery } from '@/services/apollo/generated'
import { Template } from '@/services/PriceCalculator/PriceCalculator.types'
import { ProductStory, StoryblokPageProps } from '@/services/storyblok/storyblok'
import { type TrustpilotData } from '@/services/trustpilot/trustpilot.types'

export type ProductData = Exclude<ProductDataQuery['product'], null | undefined>

Expand All @@ -13,4 +14,5 @@ export type ProductPageProps = StoryblokPageProps & {
priceTemplate: Template
productData: ProductData
initialSelectedVariant?: ProductDataVariant
trustpilot: TrustpilotData | null
}
8 changes: 7 additions & 1 deletion apps/store/src/pages/[[...slug]].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ import {
} from '@/services/storyblok/storyblok'
import { GLOBAL_STORY_PROP_NAME, STORY_PROP_NAME } from '@/services/storyblok/Storyblok.constant'
import { isProductStory } from '@/services/storyblok/Storyblok.helpers'
import { useHydrateTrustpilotData } from '@/services/trustpilot/trustpilot'
import { fetchTrustpilotData } from '@/services/trustpilot/trustpilot'
import { isRoutingLocale } from '@/utils/l10n/localeUtils'

type NextContentPageProps = StoryblokPageProps & { type: 'content' }
Expand All @@ -34,6 +36,8 @@ type NextProductPageProps = ProductPageProps & { type: 'product' }
type PageProps = NextContentPageProps | NextProductPageProps

const NextPage: NextPageWithLayout<PageProps> = (props) => {
useHydrateTrustpilotData(props.trustpilot)

if (props.type === 'product') return <NextProductPage {...props} />
return <NextStoryblokPage {...props} />
}
Expand Down Expand Up @@ -75,11 +79,12 @@ export const getStaticProps: GetStaticProps<PageProps, StoryblokQueryParams> = a
const timerName = `Get static props for ${locale}/${slug} ${draftMode ? '(draft)' : ''}`
console.time(timerName)
const version = draftMode ? 'draft' : 'published'
const [globalStory, translations, productMetadata, breadcrumbs] = await Promise.all([
const [globalStory, translations, productMetadata, breadcrumbs, trustpilot] = await Promise.all([
getGlobalStory({ version, locale }),
serverSideTranslations(locale),
fetchGlobalProductMetadata({ apolloClient }),
fetchBreadcrumbs(slug, { version, locale }),
fetchTrustpilotData(),
]).catch((error) => {
throw new Error(`Failed to fetch data for ${slug}: ${error.message}`, { cause: error })
})
Expand All @@ -101,6 +106,7 @@ export const getStaticProps: GetStaticProps<PageProps, StoryblokQueryParams> = a
[GLOBAL_STORY_PROP_NAME]: globalStory,
[GLOBAL_PRODUCT_METADATA_PROP_NAME]: productMetadata,
breadcrumbs,
trustpilot,
}
const revalidate = process.env.VERCEL_ENV === 'preview' ? 1 : false

Expand Down
2 changes: 2 additions & 0 deletions apps/store/src/services/storyblok/storyblok.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ import { blogBlocks } from '@/features/blog/blogBlocks'
// TODO: get rid of this import, services should avoid feature-imports
import { STORYBLOK_MANYPETS_FOLDER_SLUG } from '@/features/manyPets/manyPets.constants'
import { manyPetsBlocks } from '@/features/manyPets/manyPetsBlocks'
import { TrustpilotData } from '@/services/trustpilot/trustpilot.types'
import { isBrowser } from '@/utils/env'
import { Features } from '@/utils/Features'
import { getLocaleOrFallback, isRoutingLocale } from '@/utils/l10n/localeUtils'
Expand All @@ -98,6 +99,7 @@ export type StoryblokQueryParams = {
export type StoryblokPageProps = {
[STORY_PROP_NAME]: PageStory
[GLOBAL_STORY_PROP_NAME]: GlobalStory
trustpilot: TrustpilotData | null
}

export type StoryblokVersion = 'draft' | 'published'
Expand Down

0 comments on commit 327a5bc

Please sign in to comment.