-
Notifications
You must be signed in to change notification settings - Fork 38
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Zustand + React Query setup * Integrating Zustand, RQ and api.github with a basic implementation
- Loading branch information
Showing
13 changed files
with
246 additions
and
12 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
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
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 |
---|---|---|
@@ -1,11 +1,74 @@ | ||
import React from 'react'; | ||
import './App.css'; | ||
import {useGitDetails} from "./store"; | ||
import {useUserInfo} from "./hooks"; | ||
|
||
|
||
export const Left= (): React.JSX.Element => { | ||
const {addToken, addBaseUrl} = useGitDetails(); | ||
|
||
const addTokenHandler = (event: React.MouseEvent<HTMLButtonElement>) => { | ||
event.preventDefault(); | ||
|
||
|
||
addToken("<YOUR_TOKEN>"); | ||
addBaseUrl("https://api.github.com"); | ||
} | ||
|
||
return ( | ||
<> | ||
<button onClick={ | ||
addTokenHandler | ||
}>Click Me To Get User Info | ||
</button> | ||
</> | ||
|
||
); | ||
} | ||
|
||
export const Right = (): React.JSX.Element => { | ||
const {token, baseUrl} = useGitDetails(); | ||
|
||
if (!token || !baseUrl){ | ||
return ( | ||
<div>Enter your token and base url</div> | ||
) | ||
} | ||
|
||
return ( | ||
<UserInfo/> | ||
); | ||
} | ||
|
||
const UserInfo = (): React.JSX.Element => { | ||
const {isPending, data, error} = useUserInfo(); | ||
|
||
if (isPending) { | ||
return ( | ||
<div>Loading...</div> | ||
) | ||
} | ||
|
||
if (error) { | ||
return ( | ||
<div>Error: {error.message}</div> | ||
) | ||
} | ||
|
||
return ( | ||
<div> | ||
<h1>{data?.name}</h1> | ||
<p>{data?.email}</p> | ||
</div> | ||
); | ||
} | ||
|
||
|
||
export const App = (): React.JSX.Element => { | ||
return ( | ||
<div className="App">Hello World</div> | ||
<> | ||
<Left/> <Right/> | ||
</> | ||
); | ||
} | ||
|
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,17 @@ | ||
import axios from 'axios' | ||
import {useGitDetails} from "../store"; | ||
|
||
export const gitApi = axios.create({ | ||
baseURL: useGitDetails.getState().baseUrl, | ||
timeout: 1000, | ||
headers: { | ||
'Authorization': `Bearer ${useGitDetails.getState().token}`, | ||
'Content-Type': 'application/json', | ||
}, | ||
}) | ||
|
||
// TODO: Auth token in not updating when it changes. | ||
useGitDetails.subscribe(state => { | ||
gitApi.defaults.baseURL = state.baseUrl | ||
gitApi.defaults.headers.common['Authorization'] = `Bearer ${state.token}` | ||
}) |
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 @@ | ||
export {gitApi} from "./git"; |
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 @@ | ||
export {useUserInfo} from "./useUserInfo"; |
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,16 @@ | ||
import {gitApi} from "../api"; | ||
import {useQuery} from "@tanstack/react-query"; | ||
|
||
|
||
const fetchUserInfo = async () => { | ||
const {data} = await gitApi.get('/user'); | ||
return data; | ||
} | ||
|
||
export const useUserInfo = () => { | ||
return useQuery({ | ||
queryKey: ['user-info'], | ||
queryFn: fetchUserInfo, | ||
retry: 1 | ||
}) | ||
} |
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 @@ | ||
export {ReactQueryProvider} from "./react-query-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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import {PropsWithChildren} from "react"; | ||
import {QueryClient, QueryClientProvider} from "@tanstack/react-query"; | ||
import {ReactQueryDevtools} from "@tanstack/react-query-devtools"; | ||
|
||
const queryClient = new QueryClient(); | ||
|
||
export const ReactQueryProvider = ({children}: PropsWithChildren) => { | ||
return ( | ||
<QueryClientProvider client={queryClient}> | ||
{children} | ||
<ReactQueryDevtools initialIsOpen={false} /> | ||
</QueryClientProvider> | ||
) | ||
} |
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,24 @@ | ||
import { create } from "zustand"; | ||
import { persist } from "zustand/middleware"; | ||
|
||
type gitDetails = { | ||
baseUrl: string; | ||
token: string; | ||
addBaseUrl: (baseUrl: string) => void; | ||
addToken: (token: string) => void; | ||
|
||
} | ||
|
||
export const useGitDetails = create( | ||
persist<gitDetails>( | ||
(set: any) => ({ | ||
baseUrl: "", | ||
token: "", | ||
addBaseUrl: (baseUrl: string) => set(() => ({baseUrl})), | ||
addToken: (token: string) => set(() => ({token})), | ||
}), | ||
{ | ||
name: "git-details", | ||
} | ||
) | ||
) |
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 @@ | ||
export {useGitDetails} from "./git-details"; |