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
export const useAPI = (query) => { | |
const data = useMemo(() => { | |
return wrapGraphQL(query); | |
}, [query]); | |
return { data }; | |
}; |
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
export const wrapGraphQL = (query) => { | |
let status = "pending"; | |
let result = null; | |
const suspender = client | |
.query({ | |
query, | |
fetchPolicy: "network-only" | |
}) | |
.then((response) => { | |
status = "success"; |
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
import { gql } from "@apollo/client"; | |
export const GET_LAUNCHES = gql` | |
query GetLaunches { | |
launchesPast(limit: 10) { | |
mission_name | |
launch_date_local | |
launch_site { | |
site_name_long | |
} |
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
import { ApolloClient, InMemoryCache } from "@apollo/client"; | |
export const client = new ApolloClient({ | |
uri: "https://api.spacex.land/graphql", | |
cache: new InMemoryCache() | |
}); |
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
const initialResource = fetchProfileData(); | |
function ProfilePage() { | |
return ( | |
<Suspense fallback={<h1>Loading profile...</h1>}> | |
<ProfileDetails resource={initialResource} /> | |
</Suspense> | |
); | |
} |
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
{ | |
"op": "replace", | |
"path": ["profile"], | |
"value": {"name": "Veria", "age": 5} | |
} |
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
import produce from "immer" | |
const baseState = [ | |
{ | |
todo: "Learn typescript", | |
done: true | |
}, | |
{ | |
todo: "Try immer", | |
done: false |
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
import produce, { applyPatches, enablePatches } from "immer"; | |
enablePatches(); | |
export const RESET = "RESET"; | |
export const OPEN_DOOR = "OPEN_DOOR"; | |
export const UNDO = "UNDO"; | |
let inverseChanges = []; | |
export const reducer = (state, action) => { |
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
import produce, { enablePatches } from "immer"; | |
// some previous code is ommited for brevity | |
enablePatches(); | |
let inverseChanges = []; | |
export const reducer = (state, action) => { | |
return produce( |
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
const [state, dispatch] = useReducer(reducer, INITIAL_STATE); | |
const { doors } = state; |