@@ -22,7 +22,6 @@ import { useAddResourceToClientIdMutation } from "../adminapi/mutations/addResou
2222
2323import ScreenContent from "../../ScreenContent" ;
2424import ShowError from "../../ShowError" ;
25- import ShowLoading from "../../ShowLoading" ;
2625import { updateClientConfig } from "./EditOAuthClientForm" ;
2726import NavBreadcrumb , { BreadcrumbItem } from "../../NavBreadcrumb" ;
2827import {
@@ -47,6 +46,7 @@ import {
4746} from "../../hook/useAppSecretConfigForm" ;
4847import LinkButton from "../../LinkButton" ;
4948import { useAppContext } from "../../context/AppContext" ;
49+ import { useLoadableView } from "../../hook/useLoadableView" ;
5050
5151interface FormState {
5252 clients : OAuthClientConfig [ ] ;
@@ -167,28 +167,23 @@ function getNextStep(state: FormState): FormStep | null {
167167
168168interface CreateOAuthClientContentProps {
169169 form : AppSecretConfigFormModel < FormState > ;
170+ hasNoAPIResources : boolean ;
170171}
171172
172173interface StepSelectApplicationTypeProps {
173174 client : OAuthClientConfig ;
174175 form : AppSecretConfigFormModel < FormState > ;
175176 onClickSave : ( ) => void ;
177+ hasNoAPIResources : boolean ;
176178}
177179
178180const StepSelectApplicationType : React . VFC < StepSelectApplicationTypeProps > =
179181 function StepSelectApplicationType ( props ) {
180- const { client, form, onClickSave } = props ;
182+ const { client, form, onClickSave, hasNoAPIResources } = props ;
181183 const { appNodeID } = useAppContext ( ) ;
182184 const { state, setState, isDirty, isUpdating } = form ;
183185 const { renderToString } = useContext ( Context ) ;
184186
185- const { data } = useResourcesQueryQuery ( {
186- variables : {
187- first : 1 ,
188- } ,
189- fetchPolicy : "cache-and-network" ,
190- } ) ;
191-
192187 const onClientConfigChange = useCallback (
193188 ( newClient : OAuthClientConfig ) => {
194189 setState ( ( s ) => ( { ...s , newClient } ) ) ;
@@ -217,8 +212,6 @@ const StepSelectApplicationType: React.VFC<StepSelectApplicationTypeProps> =
217212 } ;
218213 } , [ ] ) ;
219214
220- const hasNoAPIResources = ( data ?. resources ?. totalCount ?? 0 ) === 0 ;
221-
222215 const options : IChoiceGroupOption [ ] = useMemo ( ( ) => {
223216 return [
224217 {
@@ -476,7 +469,8 @@ const StepAuthorizeResource: React.VFC<StepAuthorizeResourceProps> =
476469
477470const CreateOAuthClientContent : React . VFC < CreateOAuthClientContentProps > =
478471 function CreateOAuthClientContent ( props ) {
479- const { state, setState, save } = props . form ;
472+ const { form, hasNoAPIResources } = props ;
473+ const { state, setState, save } = form ;
480474 const { appID } = useParams ( ) as { appID : string } ;
481475 const navigate = useNavigate ( ) ;
482476
@@ -554,14 +548,15 @@ const CreateOAuthClientContent: React.VFC<CreateOAuthClientContentProps> =
554548 { state . step === FormStep . SelectType ? (
555549 < StepSelectApplicationType
556550 client = { client }
557- form = { props . form }
551+ form = { form }
558552 onClickSave = { onClickSave }
553+ hasNoAPIResources = { hasNoAPIResources }
559554 />
560555 ) : null }
561556 { state . step === FormStep . AuthorizeResource ? (
562557 < StepAuthorizeResource
563558 client = { client }
564- form = { props . form }
559+ form = { form }
565560 onClickSave = { onClickSave }
566561 />
567562 ) : null }
@@ -573,6 +568,16 @@ const CreateOAuthClientScreen: React.VFC = function CreateOAuthClientScreen() {
573568 const { appID } = useParams ( ) as { appID : string } ;
574569 const [ addResource ] = useAddResourceToClientIdMutation ( ) ;
575570
571+ const resourceCountQuery = useResourcesQueryQuery ( {
572+ variables : {
573+ first : 1 ,
574+ } ,
575+ fetchPolicy : "cache-and-network" ,
576+ } ) ;
577+
578+ const hasNoAPIResources =
579+ ( resourceCountQuery . data ?. resources ?. totalCount ?? 0 ) === 0 ;
580+
576581 const form = useAppSecretConfigForm ( {
577582 appID,
578583 secretVisitToken : null ,
@@ -600,8 +605,6 @@ const CreateOAuthClientScreen: React.VFC = function CreateOAuthClientScreen() {
600605 ) ,
601606 } ) ;
602607
603- const { isLoading, loadError, reload, updateError, isUpdating } = form ;
604-
605608 const errorRules = useMemo (
606609 ( ) => [
607610 makeValidationErrorMatchUnknownKindParseRule (
@@ -616,22 +619,31 @@ const CreateOAuthClientScreen: React.VFC = function CreateOAuthClientScreen() {
616619 [ appID ]
617620 ) ;
618621
619- if ( isLoading ) {
620- return < ShowLoading /> ;
621- }
622-
623- if ( loadError ) {
624- return < ShowError error = { loadError } onRetry = { reload } /> ;
625- }
626-
627- return (
628- < FormProvider loading = { isUpdating } error = { updateError } rules = { errorRules } >
629- < FormErrorMessageBar />
630- < div className = "flex-1 overflow-y-auto flex flex-col" >
631- < CreateOAuthClientContent form = { form } />
632- </ div >
633- </ FormProvider >
634- ) ;
622+ return useLoadableView ( {
623+ loadables : [
624+ form ,
625+ {
626+ isLoading : resourceCountQuery . loading ,
627+ loadError : resourceCountQuery . error ,
628+ reload : resourceCountQuery . refetch ,
629+ } ,
630+ ] as const ,
631+ render : ( [ form ] ) => (
632+ < FormProvider
633+ loading = { form . isUpdating }
634+ error = { form . updateError }
635+ rules = { errorRules }
636+ >
637+ < FormErrorMessageBar />
638+ < div className = "flex-1 overflow-y-auto flex flex-col" >
639+ < CreateOAuthClientContent
640+ form = { form }
641+ hasNoAPIResources = { hasNoAPIResources }
642+ />
643+ </ div >
644+ </ FormProvider >
645+ ) ,
646+ } ) ;
635647} ;
636648
637649export default CreateOAuthClientScreen ;
0 commit comments