-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3dfd559
commit 98fc4f6
Showing
7 changed files
with
157 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
import { useEffect, useState } from "react"; | ||
import { getHomepageInfo, getTrustedBy } from "./requests"; | ||
import { HomePageCmsInfo, StrapiProps, StrapiResponse } from "./types"; | ||
|
||
function useStrapi({ pathname }: StrapiProps): StrapiResponse { | ||
const [isLoading, setIsLoading] = useState<boolean>(true); | ||
const [strapi, setStrapi] = useState<HomePageCmsInfo | Object>({}); | ||
const getData = () => { | ||
switch (pathname) { | ||
case "/": | ||
Promise.allSettled([getHomepageInfo(), getTrustedBy()]) | ||
.then((results) => { | ||
const homepageInfoResp = | ||
results[0] as PromiseFulfilledResult<unknown>; | ||
const trustedByResp = | ||
results[1] as PromiseFulfilledResult<unknown>; | ||
if ( | ||
homepageInfoResp.status === "fulfilled" && | ||
trustedByResp.status === "fulfilled" | ||
) { | ||
setStrapi((prevState: HomePageCmsInfo) => { | ||
return { | ||
...prevState, | ||
mission_statement: homepageInfoResp.value, | ||
trusted_bies: trustedByResp.value, | ||
}; | ||
}); | ||
} | ||
}) | ||
.catch(() => { | ||
throw new Error("Failed to fetch strapi data"); | ||
}) | ||
.finally(() => { | ||
setIsLoading(false); | ||
}); | ||
break; | ||
case "/team": | ||
Promise.allSettled([getTrustedBy()]) | ||
.then((results) => { | ||
const trustedByResp = | ||
results[0] as PromiseFulfilledResult<unknown>; | ||
if (trustedByResp.status === "fulfilled") { | ||
setStrapi((prevState: HomePageCmsInfo) => { | ||
return { | ||
...prevState, | ||
trusted_bies: trustedByResp.value, | ||
}; | ||
}); | ||
} | ||
}) | ||
.finally(() => { | ||
setIsLoading(false); | ||
}); | ||
break; | ||
default: | ||
setIsLoading(false); | ||
} | ||
}; | ||
useEffect(() => { | ||
getData(); | ||
}, []); | ||
return { strapi, isLoading }; | ||
} | ||
|
||
export default useStrapi; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { TrustedByInfo } from "./types"; | ||
|
||
const cms_base_url = process.env.NEXT_PUBLIC_STRAPI_URL; | ||
//remove /api from base_url and to home_url variable | ||
const cms_home_url = cms_base_url?.replace("/api", ""); | ||
export function getHomepageInfo() { | ||
return new Promise((resolve, reject) => { | ||
fetch(`${cms_base_url}/website-homepage`, { | ||
headers: { | ||
authorization: `bearer ${process.env.NEXT_PUBLIC_STRAPI_API}`, | ||
}, | ||
}) | ||
.then((data) => { | ||
data.json().then(({ data: info }) => { | ||
resolve(info.mission_statement); | ||
}); | ||
}) | ||
.catch((err) => { | ||
reject(err); | ||
}); | ||
}); | ||
} | ||
|
||
export function getTrustedBy() { | ||
return new Promise((resolve, reject) => { | ||
fetch( | ||
`${cms_base_url}/website-trusted-bies?populate[image][fields][0]=url`, | ||
{ | ||
headers: { | ||
authorization: `bearer ${process.env.NEXT_PUBLIC_STRAPI_API}`, | ||
}, | ||
} | ||
).then((data) => { | ||
data.json() | ||
.then(({ data: infos }) => { | ||
const trustedBies = infos.map((infos: TrustedByInfo) => ({ | ||
alt: infos.alt, | ||
src: cms_home_url + infos.image.url, | ||
})); | ||
resolve(trustedBies); | ||
}) | ||
.catch((err) => { | ||
reject(err); | ||
}); | ||
}); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export interface StrapiContext { | ||
strapi: HomePageCmsInfo | Object; | ||
} | ||
|
||
export interface HomePageCmsInfo { | ||
mission_statement: string; | ||
trusted_bies: { | ||
src: string; | ||
alt: string; | ||
}[]; | ||
} | ||
export interface TeamPageCmsInfo { | ||
trusted_bies: { | ||
src: string; | ||
alt: string; | ||
}[]; | ||
} | ||
export interface TrustedByInfo { | ||
alt: string; | ||
image: { | ||
url: string; | ||
}; | ||
} | ||
export interface StrapiResponse { | ||
strapi: HomePageCmsInfo | Object; | ||
isLoading: Boolean; | ||
} | ||
export interface StrapiProps { | ||
pathname: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters