generated from ciampo/_nextjs-template
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
GH-54 feat(reviews): read reviews data and use it to populate structu…
…red data
- Loading branch information
Showing
4 changed files
with
148 additions
and
73 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import React, { useReducer, useContext, createContext } from 'react'; | ||
|
||
type Action = | ||
| { | ||
type: 'FETCH_INIT'; | ||
} | ||
| { | ||
type: 'FETCH_SUCCESS'; | ||
payload: { | ||
reviewCount: number; | ||
ratingValue: number; | ||
documentId: null | string; | ||
}; | ||
} | ||
| { | ||
type: 'FETCH_ERROR'; | ||
}; | ||
type Dispatch = (action: Action) => void; | ||
type State = { | ||
isLoading: boolean; | ||
isError: boolean; | ||
data: { | ||
reviewCount: number; | ||
ratingValue: number; | ||
documentId: null | string; | ||
}; | ||
}; | ||
|
||
const PostReviewsStateContext = createContext<State | undefined>(undefined); | ||
const PostReviewsDispatchContext = createContext<Dispatch | undefined>(undefined); | ||
|
||
const postReviewsContext = (state: State, action: Action): State => { | ||
switch (action.type) { | ||
case 'FETCH_INIT': | ||
return { | ||
...state, | ||
isError: false, | ||
isLoading: true, | ||
}; | ||
case 'FETCH_SUCCESS': | ||
return { | ||
...state, | ||
isError: false, | ||
isLoading: false, | ||
data: action.payload || [], | ||
}; | ||
case 'FETCH_ERROR': | ||
return { | ||
...state, | ||
isError: true, | ||
isLoading: false, | ||
}; | ||
} | ||
}; | ||
|
||
const PostReviewsProvider: React.FC = ({ children }) => { | ||
const [state, dispatch] = useReducer(postReviewsContext, { | ||
isLoading: false, | ||
isError: false, | ||
data: { | ||
reviewCount: 0, | ||
ratingValue: -1, | ||
documentId: null, | ||
}, | ||
}); | ||
|
||
return ( | ||
<PostReviewsStateContext.Provider value={state}> | ||
<PostReviewsDispatchContext.Provider value={dispatch}> | ||
{children} | ||
</PostReviewsDispatchContext.Provider> | ||
</PostReviewsStateContext.Provider> | ||
); | ||
}; | ||
|
||
function usePostReviewsState(): State { | ||
const context = useContext(PostReviewsStateContext); | ||
if (context === undefined) { | ||
throw new Error('usePostReviewsState must be used within a PostReviewsProvider'); | ||
} | ||
return context; | ||
} | ||
function usePostReviewsDispatch(): Dispatch { | ||
const context = useContext(PostReviewsDispatchContext); | ||
if (context === undefined) { | ||
throw new Error('usePostReviewsDispatch must be used within a PostReviewsProvider'); | ||
} | ||
return context; | ||
} | ||
export { PostReviewsProvider, usePostReviewsState, usePostReviewsDispatch }; |
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,15 @@ | ||
import sanityClient from '@sanity/client'; | ||
|
||
const client = sanityClient({ | ||
projectId: process.env.NEXT_PUBLIC_SANITY_PROJECT_ID || '', | ||
dataset: 'production', | ||
token: process.env.NEXT_PUBLIC_SANITY_READ_TOKEN || '', | ||
// Always use the freshest data (as we're going to save it to disk) | ||
useCdn: false, | ||
}); | ||
|
||
export const getPostReviews = async (postId: string): Promise<{ reviews: number[] }> => | ||
await client.fetch(`*[_id == "${postId}"] {reviews[]}[0]`); | ||
|
||
// export const submitPostReview = async (postId: string, rating: number): Promise<unknown> => | ||
// await client.patch(postId).setIfMissing({ reviews: [] }).append('reviews', [rating]).commit(); |
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