-
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.
feat: refactoring of code and addition of new e2e test suite for inst…
…agram posts
- Loading branch information
Showing
20 changed files
with
379 additions
and
227 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 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 |
---|---|---|
|
@@ -9,7 +9,7 @@ | |
], | ||
"@payload-config": [ | ||
"./src/payload.config.ts" | ||
] | ||
], | ||
}, | ||
"baseUrl": ".", | ||
"lib": [ | ||
|
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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,37 @@ | ||
import { QueryClient, useMutation, useQuery } from '@tanstack/react-query' | ||
import { instagramCollectionEndpoint } from '../../plugin' | ||
import { PostType } from '../../types' | ||
|
||
type UseInstagramCollectionType = { | ||
queryClient: QueryClient | ||
} | ||
|
||
const useInstagramCollection = ({ queryClient }: UseInstagramCollectionType) => { | ||
const { data: instagramCollection } = useQuery<{ docs: PostType[] }>({ | ||
queryKey: [instagramCollectionEndpoint], | ||
queryFn: () => fetch(instagramCollectionEndpoint).then(res => res.json()), | ||
}) | ||
|
||
const { mutate: mutateInstagramCollection } = useMutation({ | ||
mutationFn: (post: PostType) => | ||
fetch(instagramCollectionEndpoint, { | ||
headers: { | ||
Accept: 'application/json', | ||
'Content-Type': 'application/json', | ||
}, | ||
method: 'POST', | ||
body: JSON.stringify(post), | ||
credentials: 'include', | ||
}).then(res => { | ||
return res.json() | ||
}), | ||
onSuccess: res => { | ||
queryClient.invalidateQueries({ queryKey: [instagramCollectionEndpoint] }) | ||
alert(`Status: ${res?.message}`) | ||
}, | ||
}) | ||
|
||
return { instagramCollection, mutateInstagramCollection } | ||
} | ||
|
||
export default useInstagramCollection |
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,40 @@ | ||
import { useQuery } from '@tanstack/react-query' | ||
import { PostType } from '../../types' | ||
|
||
type UseInstagramPostsType = { | ||
endpoint: string | ||
} | ||
|
||
type UseInstagramResponseType = { | ||
isPending: boolean | ||
error: Error | null | ||
isFetching: boolean | ||
isLoading: boolean | ||
response: { | ||
data: PostType[] | ||
paging: { before: string; after: string } | ||
} | ||
} | ||
|
||
const useInstagramPosts = ({ endpoint }: UseInstagramPostsType): UseInstagramResponseType => { | ||
const { | ||
isPending, | ||
error, | ||
data: response, | ||
isFetching, | ||
isLoading, | ||
} = useQuery({ | ||
queryKey: [endpoint], | ||
queryFn: () => | ||
fetch(endpoint).then(res => { | ||
if (!res.ok) throw new Error(res.status.toString()) | ||
return res.json() | ||
}), | ||
staleTime: 1000 * 60 * 5, | ||
refetchOnMount: false, | ||
refetchOnWindowFocus: false, | ||
}) | ||
return { isPending, error, response, isFetching, isLoading } | ||
} | ||
|
||
export default useInstagramPosts |
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,35 @@ | ||
import { QueryClient, useMutation } from '@tanstack/react-query' | ||
import { addAccessTokenEndpoint } from '../../plugin' | ||
|
||
type UseTokenType = { | ||
endpoint: string | ||
queryClient: QueryClient | ||
setToken: React.Dispatch<React.SetStateAction<string>> | ||
} | ||
|
||
const useToken = ({ endpoint, queryClient, setToken }: UseTokenType) => { | ||
const { mutate: addAccessToken } = useMutation({ | ||
mutationFn: (body: { accessToken: string }) => | ||
fetch(addAccessTokenEndpoint, { | ||
body: JSON.stringify(body), | ||
method: 'POST', | ||
credentials: 'include', | ||
}).then(res => { | ||
if (!res.ok) throw new Error(res.status.toString()) | ||
return res.json() | ||
}), | ||
onSuccess: _res => { | ||
queryClient.invalidateQueries({ queryKey: [endpoint] }) | ||
alert('Status: token successfully added!') | ||
setToken('') | ||
}, | ||
onError: _res => { | ||
alert('Status: the token provided is not correct, try again') | ||
setToken('') | ||
}, | ||
}) | ||
|
||
return { addAccessToken } | ||
} | ||
|
||
export default useToken |
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
Oops, something went wrong.