@@ -18,8 +18,8 @@ import {
1818} from "../../model/schemas" ;
1919import { formatJavaScript } from "../../view/codeFormatting" ;
2020import { Await , FinalResult } from "../../types/helpers" ;
21- import { fetchController , FetchGenerator , getJSON } from "../../model/fetching" ;
22- import { useMemo } from "react " ;
21+ import { fetchJSONComponent , getJSON } from "../../model/fetching" ;
22+ import { useAsyncFunc } from "../../view/async " ;
2323
2424export let meta : MetaFunction = ( ) => {
2525 return {
@@ -66,24 +66,6 @@ function useFetch<Data>(source: URL): null | Data {
6666 return data ;
6767}
6868
69- function usePromise < Data > ( source : Promise < Data > ) : null | Data {
70- const [ data , setData ] = useState < Data | null > ( null ) ;
71-
72- useEffect ( ( ) => {
73- source . then ( setData ) ;
74- } , [ source ] ) ;
75-
76- return data ;
77- }
78-
79- function useAsyncFunc < Data > (
80- source : ( ) => Promise < Data > ,
81- dependencies : Array < any >
82- ) : null | Data {
83- const promise = useMemo ( source , dependencies ) ;
84- return usePromise ( promise ) ;
85- }
86-
8769interface AWSRegion {
8870 primary : string ;
8971 secondary : string ;
@@ -131,12 +113,12 @@ function* SwapiPersonSchema(): SchemaGenerator<SwapiPersonData> {
131113 name,
132114 height,
133115 mass,
134- films
116+ films,
135117 } ;
136118}
137119function * SwapiPersonResource ( ) {
138- const rawData : unknown = yield getJSON ( ' /api/people/1' ) ;
139-
120+ const rawData : unknown = yield getJSON ( " /api/people/1" ) ;
121+
140122 const parsedData = parseJSON ( rawData , function * Schema ( ) {
141123 const name : string = yield read . string ( "name" ) ;
142124 const height : string = yield read . string ( "height" ) ;
@@ -147,15 +129,15 @@ function* SwapiPersonResource() {
147129 name,
148130 height,
149131 mass,
150- films
132+ films,
151133 } ;
152134 } ) ;
153135
154136 return parsedData ;
155137}
156- type SwapiPersonData2 = FinalResult < ReturnType < typeof SwapiPersonResource > >
138+ type SwapiPersonData2 = FinalResult < ReturnType < typeof SwapiPersonResource > > ;
157139
158- export default function MakeRenderer ( ) {
140+ export default function MakeSchema ( ) {
159141 const data : Await < ReturnType < typeof loader > > = useRouteData ( ) ;
160142
161143 function renderExample ( input : any ) : JSX . Element {
@@ -183,12 +165,13 @@ export default function MakeRenderer() {
183165 const personData =
184166 personDataRaw != null ? parseJSON ( personDataRaw , SwapiPersonSchema ) : null ;
185167
186- const personData2 = useAsyncFunc ( ( ) =>
187- fetchController (
188- new URL ( "https://swapi.py4e.com/" ) ,
189- SwapiPersonResource
190- ) ,
191- [ ] ) ;
168+ const personData2 = useAsyncFunc (
169+ ( ) =>
170+ fetchJSONComponent ( SwapiPersonResource , {
171+ baseURL : new URL ( "https://swapi.py4e.com/" ) ,
172+ } ) ,
173+ [ ]
174+ ) ;
192175
193176 return (
194177 < main data-measure = "center" >
0 commit comments