From c108030824b6a326e240744b26243266e1100e91 Mon Sep 17 00:00:00 2001 From: "Mr. Singleton" Date: Mon, 25 Sep 2023 13:29:45 -0400 Subject: [PATCH] make thwClient generic --- .idea/modules.xml | 2 +- ...-freewill-baptist-church.iml => resume.iml | 0 src/components/block-content-ui/cmsClient.ts | 171 +++++++++++++- src/components/page-context/PageProvider.tsx | 4 +- src/components/transform-hw/thwClient.ts | 215 ------------------ 5 files changed, 172 insertions(+), 220 deletions(-) rename st-joseph-freewill-baptist-church.iml => resume.iml (100%) delete mode 100644 src/components/transform-hw/thwClient.ts diff --git a/.idea/modules.xml b/.idea/modules.xml index ed5c4cec..6acac044 100644 --- a/.idea/modules.xml +++ b/.idea/modules.xml @@ -2,7 +2,7 @@ - + \ No newline at end of file diff --git a/st-joseph-freewill-baptist-church.iml b/resume.iml similarity index 100% rename from st-joseph-freewill-baptist-church.iml rename to resume.iml diff --git a/src/components/block-content-ui/cmsClient.ts b/src/components/block-content-ui/cmsClient.ts index 468984bc..0a091810 100644 --- a/src/components/block-content-ui/cmsClient.ts +++ b/src/components/block-content-ui/cmsClient.ts @@ -7,10 +7,11 @@ import { SanityRef, SanityTransformHwHomePage } from '../../common/sanityIo/Types' -import {WhySwitchSectionType} from "../BlockContentTypes"; +import {ThwServiceItemType, WhySwitchSectionType} from "../BlockContentTypes"; import GroqQueries from "../../utils/groqQueries"; import groqQueries from "../../utils/groqQueries"; import {SanityHomePage} from "./static-pages/cmsStaticPagesClient"; +import {useQuery} from "react-query"; const fetchLandingPage = (slug: string): Promise => { return sanityClient @@ -292,6 +293,167 @@ const fetchRefs = async (sanityRefs: SanityRef[]): Promise => { return Promise.all([...services, otherRefs]) } +const useFetchPageBySlugQuery = (slug: string) => { + console.log("slug", slug) + return useQuery( + ['fetchPageBySlug', slug], + async ({queryKey}) => { + const [_, pageSlug] = queryKey + + console.log(" Lookin for page with slug", pageSlug, queryKey) + if (pageSlug) { + return sanityClient + .fetch( + `*[slug.current == $pageSlug && _type == "homePage"]{ + ${groqQueries.HOMEPAGE} + }`, {pageSlug}) + .then((result) => { + if (result.length === 0) { + return Promise.reject(Error("No results returned")) + } + return result[0] + }).catch(() => { + return Promise.reject(Error("Sanity Error getting pageSlug " + pageSlug)) + }) + } else { + return Promise.reject(Error("No page slug passed")) + } + }, + {} + ); +} +const useFetchMenuBySlugQuery = (menuSlug: string) => { + console.log("fetching menu with slug", menuSlug) + return useQuery( + menuSlug, + () => { + return sanityClient + .fetch( + `*[slug.current == $menuSlug]{ + ${GroqQueries.MENUGROUPCONTAINER} + }`, {menuSlug: menuSlug ?? 'header-menu'} + ) + .then((data: SanityMenuContainer[]) => { + return data[0] + }) + }, + { + staleTime: Infinity, + refetchInterval: false, + refetchOnWindowFocus: false + } + ); +} + +const useFetchMenuByRefQuery = (headerMenuRef?: SanityRef) => { + console.log("fetching menu with ref", headerMenuRef) + + const menuId = headerMenuRef?._ref ?? "no-id" + + return useQuery( + menuId, + () => { + return sanityClient + .fetch( + `*[_id == $menuId && _type == "menuContainer"]{ + ${groqQueries.MENUGROUPCONTAINER} + }`, {menuId}) + .then((result) => { + if (result.length === 0) { + + return Promise.reject(Error("No results returned")) + } + return result[0] + }).catch(() => { + return Promise.reject(Error("Sanity Error getting pageSlug " + menuId)) + }) + }, + { + staleTime: Infinity, + refetchInterval: false, + refetchOnWindowFocus: false, + refetchOnMount: false, + } + ); + +} + +// +// const fetchLandingPageFooterMenu = (footerSlug?: string): Promise => { +// const slug = footerSlug ?? 'footer-menu' +// +// return sanityClient +// .fetch( +// `*[_type=="menuContainer" && slug.current == $slug]{ +// ${GroqQueries.MENUGROUPCONTAINER} +// }`, {slug} +// ) +// .then((data: SanityMenuContainer[]) => { +// return data[0] +// }) +// } + +const useFetchRefsQuery = (refs: SanityRef[]) => { + return useQuery( + ['fetchRefs'], + async () => { + return fetchRefs(refs) + .then((results: any[]) => { + if (results.length === 0) { + console.log("whew! after fetching a bunch of refs ", results) + } + return results + }).catch((e: any) => { + console.log("error getting services", e) + return [] + }) + }, + { + staleTime: Infinity, + refetchInterval: false, + refetchOnWindowFocus: false + } + ); +} + + +const useFetchServicesQuery = (pageSlug?: string) => { + return useQuery( + 'fetchServices', + async () => { + console.log("fetchings services", pageSlug) + const serviceSlug = pageSlug + + let serviceSlugClause: string = '' + if (serviceSlug) { + serviceSlugClause = " && slug.current != $serviceSlug" + } + + const query = `*[_type == "transformServiceItem"${serviceSlugClause}]{ + ${groqQueries.SERVICE} + }` + + const params = serviceSlug ? {serviceSlug: serviceSlug} : {} + + return sanityClient + .fetch(query, params) + .then((results: ThwServiceItemType[]) => { + if (results.length === 0) { + console.log("No Services present") + } + return results + }).catch((e: any) => { + console.log("error getting services", e) + return [] + }) + } + , { + staleTime: Infinity, + refetchInterval: false, + refetchOnWindowFocus: false + }); +} + export default { fetchRef, fetchRefs, @@ -305,5 +467,10 @@ export default { fetchLandingPageFooterMenu, fetchBlogCategories, fetchBlogGroup, - fetchWhySwitch + fetchWhySwitch, + useFetchPageBySlugQuery, + useFetchMenuBySlugQuery, + useFetchServicesQuery, + useFetchRefsQuery, + useFetchMenuByRefQuery } \ No newline at end of file diff --git a/src/components/page-context/PageProvider.tsx b/src/components/page-context/PageProvider.tsx index fbfc3d9c..fccbd948 100644 --- a/src/components/page-context/PageProvider.tsx +++ b/src/components/page-context/PageProvider.tsx @@ -1,10 +1,10 @@ import React, {FunctionComponent, PropsWithChildren, useContext, useMemo, useReducer,} from 'react'; import {SanityMenuContainer, SanityTransformHwHomePage} from "../../common/sanityIo/Types"; -import thwClient from "../transform-hw/thwClient"; import {ThwServiceItemNoRefType} from "../BlockContentTypes"; import PageContext from './PageContext'; import SnackbarContext from "../modal-context/SnackbarContext"; import {v4 as uuidv4} from 'uuid' +import cmsClient from "../block-content-ui/cmsClient"; type IProps = {}; type PageProviderState = { @@ -82,7 +82,7 @@ const PageProvider: FunctionComponent = ( const [state, dispatch] = useReducer(reducer, initialState) - const loadedPageQuery = thwClient.useFetchPageBySlugQuery(state.pageSlug) + const loadedPageQuery = cmsClient.useFetchPageBySlugQuery(state.pageSlug) React.useEffect(() => { if (state.analyticsId) { diff --git a/src/components/transform-hw/thwClient.ts b/src/components/transform-hw/thwClient.ts deleted file mode 100644 index ea8dc062..00000000 --- a/src/components/transform-hw/thwClient.ts +++ /dev/null @@ -1,215 +0,0 @@ -import {useQuery} from "react-query"; -import sanityClient from "../../sanityClient"; -import groqQueries from "../../utils/groqQueries"; -import GroqQueries from "../../utils/groqQueries"; -import {SanityMenuContainer, SanityRef} from "../../common/sanityIo/Types"; -import {ThwServiceItemType} from "../BlockContentTypes"; -import {useContext} from "react"; -import PageContext from "../page-context/PageContext"; - -const useFetchPageBySlugQuery = (slug: string) => { - console.log("slug", slug) - return useQuery( - ['fetchPageBySlug', slug], - async ({queryKey}) => { - const [_, pageSlug] = queryKey - - console.log(" Lookin for page with slug", pageSlug, queryKey) - if (pageSlug) { - return sanityClient - .fetch( - `*[slug.current == $pageSlug && _type == "homePage"]{ - ${groqQueries.HOMEPAGE} - }`, {pageSlug}) - .then((result) => { - if (result.length === 0) { - return Promise.reject(Error("No results returned")) - } - return result[0] - }).catch(() => { - return Promise.reject(Error("Sanity Error getting pageSlug " + pageSlug)) - }) - } else { - return Promise.reject(Error("No page slug passed")) - } - }, - {} - ); -} -const useFetchMenuBySlugQuery = (menuSlug: string) => { - console.log("fetching menu with slug", menuSlug) - return useQuery( - menuSlug, - () => { - return sanityClient - .fetch( - `*[slug.current == $menuSlug]{ - ${GroqQueries.MENUGROUPCONTAINER} - }`, {menuSlug: menuSlug ?? 'header-menu'} - ) - .then((data: SanityMenuContainer[]) => { - return data[0] - }) - }, - { - staleTime: Infinity, - refetchInterval: false, - refetchOnWindowFocus: false - } - ); -} - -const useFetchMenuByRefQuery = (headerMenuRef?: SanityRef) => { - console.log("fetching menu with ref", headerMenuRef) - - const menuId = headerMenuRef?._ref ?? "no-id" - - return useQuery( - menuId, - () => { - return sanityClient - .fetch( - `*[_id == $menuId && _type == "menuContainer"]{ - ${groqQueries.MENUGROUPCONTAINER} - }`, {menuId}) - .then((result) => { - if (result.length === 0) { - - return Promise.reject(Error("No results returned")) - } - return result[0] - }).catch(() => { - return Promise.reject(Error("Sanity Error getting pageSlug " + menuId)) - }) - }, - { - staleTime: Infinity, - refetchInterval: false, - refetchOnWindowFocus: false, - refetchOnMount: false, - } - ); - -} - -// -// const fetchLandingPageFooterMenu = (footerSlug?: string): Promise => { -// const slug = footerSlug ?? 'footer-menu' -// -// return sanityClient -// .fetch( -// `*[_type=="menuContainer" && slug.current == $slug]{ -// ${GroqQueries.MENUGROUPCONTAINER} -// }`, {slug} -// ) -// .then((data: SanityMenuContainer[]) => { -// return data[0] -// }) -// } -const fetchRefs = async (sanityRefs: SanityRef[]): Promise => { - - console.log("get these refs", sanityRefs) - - let servicesRefs: string[] = [] - let otherContentRefs: string[] = [] - - sanityRefs?.forEach((sanityRef) => { - console.log("does match?", sanityRef._type, groqQueries.SANITY_TYPES_ENUM.SERVICE, sanityRef._type === groqQueries.SANITY_TYPES_ENUM.SERVICE) - if (sanityRef._type == groqQueries.SANITY_TYPES_ENUM.SERVICE) { - servicesRefs.push(sanityRef._ref) - } - - otherContentRefs.push(sanityRef._ref) - }) - - console.log("Division of labor", sanityRefs, otherContentRefs) - const servicesQuery = `*[_type == ${groqQueries.SANITY_TYPES_ENUM.SERVICE} && _id in $references]{ - ${groqQueries.SERVICE} - }` - - const services = await sanityClient - .fetch( - servicesQuery, - {references: servicesRefs} - ) - - const otherRefs = await sanityClient - .fetch( - `*[_id in $references]`, - {references: otherContentRefs} - ) - - console.log("Done Division of labor", services, otherRefs) - - return Promise.all([...services, otherRefs]) -} - -const useFetchRefsQuery = (refs: SanityRef[]) => { - return useQuery( - ['fetchRefs'], - async () => { - return fetchRefs(refs) - .then((results: any[]) => { - if (results.length === 0) { - console.log("whew! after fetching a bunch of refs ", results) - } - return results - }).catch((e: any) => { - console.log("error getting services", e) - return [] - }) - }, - { - staleTime: Infinity, - refetchInterval: false, - refetchOnWindowFocus: false - } - ); -} - - -const useFetchServicesQuery = (pageSlug?: string) => { - return useQuery( - 'fetchServices', - async () => { - console.log("fetchings services", pageSlug) - const serviceSlug = pageSlug - - let serviceSlugClause: string = '' - if (serviceSlug) { - serviceSlugClause = " && slug.current != $serviceSlug" - } - - const query = `*[_type == "transformServiceItem"${serviceSlugClause}]{ - ${groqQueries.SERVICE} - }` - - const params = serviceSlug ? {serviceSlug: serviceSlug} : {} - - return sanityClient - .fetch(query, params) - .then((results: ThwServiceItemType[]) => { - if (results.length === 0) { - console.log("No Services present") - } - return results - }).catch((e: any) => { - console.log("error getting services", e) - return [] - }) - } - , { - staleTime: Infinity, - refetchInterval: false, - refetchOnWindowFocus: false - }); -} - -export default { - fetchRefs, - useFetchPageBySlugQuery, - useFetchMenuBySlugQuery, - useFetchServicesQuery, - useFetchRefsQuery, - useFetchMenuByRefQuery -} \ No newline at end of file