Created
December 21, 2023 22:14
-
-
Save davidr64/3e6883fd84852893fbc46cb4bbc06d54 to your computer and use it in GitHub Desktop.
New Quote in React Option 1 (Provider does the loading, single object for state)
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 React, { createContext } from "react"; | |
function QuoteRouter() { | |
return ( | |
// Apollo Provider | |
// INTL provider | |
// FreatureProvider at router level rather than page level | |
<FeatureProvider> | |
<NewQuote /> | |
</FeatureProvider> | |
); | |
} | |
function NewQuote() { | |
//Single object for state | |
const [state, setState] = React.useState({ | |
client: null, | |
lineItems: [], | |
///.... | |
}); | |
return <QuoteForm state={state} setState={setState} />; | |
} | |
function EditQuoteLoader() { | |
//useLoadQuote is a custom hook that calls apollo query | |
// There will be a loading state | |
const { availableFeatures } = useFeatureContext(FeatureContext); | |
const { quote, isLoading } = useLoadQuote(availableFeatures); | |
return <EditQuote quote={quote} />; | |
} | |
function EditQuote({ quote }: { quote: any }) { | |
const [state, setState] = React.useState(quote); | |
return <QuoteForm state={state} setState={setState} />; | |
} | |
function QuoteForm({ | |
state, | |
setState, | |
}: //only one state and corresponding setter passed to quote form | |
{ | |
state: any; | |
setState: (state: any) => void; | |
}) { | |
return ( | |
<Form> | |
<ClientPicker state={state} setState={setState} /> | |
<LineItems state={state} setState={setState} /> | |
<Button type="submit" /> | |
</Form> | |
); | |
} | |
function FeatureProvider(props: { children: React.ReactNode }) { | |
//Feature provider directly calls loading hook | |
//useLoadAvailableFeatures is a wrapper calling apollo query | |
const { availableFeatures, isLoading } = useLoadAvailableFeatures(); | |
return ( | |
<FeatureContext.Provider value={availableFeatures}> | |
{props.children} | |
</FeatureContext.Provider> | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment