-
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.
Merge pull request #25 from bmschwartz/develop
Develop
- Loading branch information
Showing
8 changed files
with
218 additions
and
185 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 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,108 @@ | ||
import React, { createContext, ReactNode, useEffect, useState } from 'react'; | ||
|
||
import { execute, GetPictureRequestDocument } from '@/graphql/client'; | ||
import { CreateRequestCommentParams, useComments } from '@/hooks/useComments'; | ||
import { useIpfs } from '@/hooks/useIpfs'; | ||
import { CreateRequestSubmissionParams, useSubmissions } from '@/hooks/useSubmissions'; | ||
import { RequestComment } from '@/types/comment'; | ||
import { Request } from '@/types/request'; | ||
import { RequestSubmission } from '@/types/submission'; | ||
import { mapPictureRequest } from '@/utils/mappers'; | ||
|
||
export interface RequestDetailContextType { | ||
request: Request | null; | ||
comments: RequestComment[]; | ||
submissions: RequestSubmission[]; | ||
loading: boolean; | ||
fetchRequest: (id: string) => void; | ||
createComment: (params: CreateRequestCommentParams) => Promise<RequestComment>; | ||
createSubmission: (params: CreateRequestSubmissionParams) => Promise<RequestSubmission>; | ||
} | ||
|
||
interface RequestDetailProviderProps { | ||
children: ReactNode; | ||
requestId: string; | ||
} | ||
|
||
export const RequestDetailContext = createContext<RequestDetailContextType | undefined>(undefined); | ||
|
||
export const RequestDetailProvider = ({ children, requestId }: RequestDetailProviderProps) => { | ||
const [loading, setLoading] = useState<boolean>(true); | ||
const [request, setRequest] = useState<Request | null>(null); | ||
const [comments, setComments] = useState<RequestComment[]>([]); | ||
const [submissions, setSubmissions] = useState<RequestSubmission[]>([]); | ||
|
||
const { fetchIPFSData } = useIpfs(); | ||
const { createRequestComment, pollForNewComment, fetchComments } = useComments(); | ||
const { createRequestSubmission, pollForNewSubmission, fetchSubmissions } = useSubmissions(); | ||
|
||
const fetchRequest = async (id: string) => { | ||
try { | ||
const result = await execute(GetPictureRequestDocument, { id }); | ||
const request = result?.data?.pictureRequest; | ||
if (request) { | ||
const ipfsData = await fetchIPFSData(request.ipfsHash); | ||
return mapPictureRequest({ ...request, ...ipfsData }); | ||
} | ||
} catch (error) { | ||
console.error('Error fetching request:', error); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
const fetchAllRequestDetails = async () => { | ||
if (requestId) { | ||
setLoading(true); | ||
|
||
const [request, comments, submissions] = await Promise.all([ | ||
fetchRequest(requestId), | ||
fetchComments(requestId), | ||
fetchSubmissions(requestId), | ||
]); | ||
|
||
if (request) { | ||
setRequest(request); | ||
} | ||
setComments(comments); | ||
setSubmissions(submissions); | ||
setLoading(false); | ||
} | ||
}; | ||
|
||
fetchAllRequestDetails(); | ||
}, [requestId]); | ||
|
||
const createSubmission = async (params: CreateRequestSubmissionParams): Promise<RequestSubmission> => { | ||
const optimisticSubmission = await createRequestSubmission(params); | ||
setSubmissions((prevSubmissions) => [...prevSubmissions, optimisticSubmission]); | ||
|
||
pollForNewSubmission(optimisticSubmission.id, (polledSubmission: RequestSubmission) => { | ||
setSubmissions((prevSubmissions) => | ||
prevSubmissions.map((submission) => (submission.id === optimisticSubmission.id ? polledSubmission : submission)) | ||
); | ||
}); | ||
|
||
return optimisticSubmission; | ||
}; | ||
|
||
const createComment = async (params: CreateRequestCommentParams): Promise<RequestComment> => { | ||
const optimisticComment = await createRequestComment(params); | ||
setComments((prevComments) => [...prevComments, optimisticComment]); | ||
|
||
pollForNewComment(optimisticComment.id, (polledComment: RequestComment) => { | ||
setComments((prevComments) => | ||
prevComments.map((comment) => (comment.id === optimisticComment.id ? polledComment : comment)) | ||
); | ||
}); | ||
|
||
return optimisticComment; | ||
}; | ||
|
||
return ( | ||
<RequestDetailContext.Provider | ||
value={{ request, comments, submissions, loading, createComment, createSubmission, fetchRequest }} | ||
> | ||
{children} | ||
</RequestDetailContext.Provider> | ||
); | ||
}; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import { useContext } from 'react'; | ||
|
||
import { RequestDetailContext, RequestDetailContextType } from '@/contexts/RequestDetailContext'; | ||
|
||
export const useRequestDetail = (): RequestDetailContextType => { | ||
const context = useContext(RequestDetailContext); | ||
if (!context) { | ||
throw new Error('useRequestDetail must be used within a RequestDetailProvider'); | ||
} | ||
return context; | ||
}; |
Oops, something went wrong.