Skip to content

Commit

Permalink
make thwClient generic
Browse files Browse the repository at this point in the history
  • Loading branch information
tmanundercover committed Sep 25, 2023
1 parent 847f7a2 commit c108030
Show file tree
Hide file tree
Showing 5 changed files with 172 additions and 220 deletions.
2 changes: 1 addition & 1 deletion .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
171 changes: 169 additions & 2 deletions src/components/block-content-ui/cmsClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<SanityLandingPage> => {
return sanityClient
Expand Down Expand Up @@ -292,6 +293,167 @@ const fetchRefs = async (sanityRefs: SanityRef[]): Promise<any> => {
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<SanityMenuContainer> => {
// 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,
Expand All @@ -305,5 +467,10 @@ export default {
fetchLandingPageFooterMenu,
fetchBlogCategories,
fetchBlogGroup,
fetchWhySwitch
fetchWhySwitch,
useFetchPageBySlugQuery,
useFetchMenuBySlugQuery,
useFetchServicesQuery,
useFetchRefsQuery,
useFetchMenuByRefQuery
}
4 changes: 2 additions & 2 deletions src/components/page-context/PageProvider.tsx
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down Expand Up @@ -82,7 +82,7 @@ const PageProvider: FunctionComponent<IProps & PropsWithChildren> = (
const [state, dispatch] = useReducer(reducer, initialState)


const loadedPageQuery = thwClient.useFetchPageBySlugQuery(state.pageSlug)
const loadedPageQuery = cmsClient.useFetchPageBySlugQuery(state.pageSlug)

React.useEffect(() => {
if (state.analyticsId) {
Expand Down
Loading

0 comments on commit c108030

Please sign in to comment.