Skip to content

Commit

Permalink
Add ApiClient and call it
Browse files Browse the repository at this point in the history
  • Loading branch information
psrpinto committed Sep 5, 2024
1 parent 9606856 commit 1436c03
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/api/ApiClient.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
export interface Foo {
name: string;
}

export class ApiClient {
private readonly url: string;
constructor( opts: { url: string } ) {
this.url = opts.url;
}

async getFoo(): Promise< Foo > {
return new Promise( ( resolve ) => {
try {
resolve( { name: 'foo' } );
} catch ( error ) {
console.log( error );
throw error;
}
} );
}
}
19 changes: 19 additions & 0 deletions src/ui/session/ViewSession.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,33 @@
import { useSessionContext } from '@/ui/session/SessionProvider';
import { ApiClient, Foo } from '@/api/ApiClient';
import { useEffect, useState } from 'react';

export function ViewSession() {
const { session, playgroundInfo } = useSessionContext();

const apiClient = playgroundInfo?.url
? new ApiClient( { url: playgroundInfo?.url } )
: null;

const [ foo, setFoo ] = useState< Foo >();
useEffect( () => {
if ( ! apiClient ) {
return;
}
const getFoo = async () => {
const result = await apiClient?.getFoo();
setFoo( result );
};
void getFoo();
}, [ playgroundInfo ] );

Check warning on line 22 in src/ui/session/ViewSession.tsx

View workflow job for this annotation

GitHub Actions / lint

React Hook useEffect has a missing dependency: 'apiClient'. Either include it or remove the dependency array

return (
<>
<div>view session: { session.id }</div>
{ playgroundInfo?.url ? (
<div>url: { playgroundInfo.url }</div>
) : null }
{ foo ? <div>{ foo.name }</div> : null }
</>
);
}

0 comments on commit 1436c03

Please sign in to comment.