1
+ import * as React from 'react' ;
2
+ import useFetchState , {
3
+ FetchState ,
4
+ FetchStateCallbackPromise ,
5
+ } from '~/shared/utilities/useFetchState' ;
6
+ import { CreateWorkspaceData } from '~/app/types' ;
7
+ import { useNotebookAPI } from '~/app/hooks/useNotebookAPI' ;
8
+ import { Workspace } from '~/shared/types' ;
9
+ import { createWorkspaceCall } from '~/app/pages/Workspaces/utils' ;
10
+ import { APIOptions } from '~/shared/api/types' ;
11
+
12
+ const useCreateWorkspace = ( namespace : string , formData : CreateWorkspaceData ) :
13
+ FetchState < Workspace | null > => {
14
+ const { api, apiAvailable } = useNotebookAPI ( ) ;
15
+
16
+ const call = React . useCallback < FetchStateCallbackPromise < Workspace | null > > (
17
+ ( opts : APIOptions ) => {
18
+ if ( ! apiAvailable ) {
19
+ return Promise . reject ( new Error ( 'API not yet available' ) ) ;
20
+ }
21
+ if ( ! namespace ) {
22
+ return Promise . reject ( new Error ( 'namespace is not available yet' ) ) ;
23
+ }
24
+ if ( ! formData ) {
25
+ return Promise . reject ( new Error ( 'formData is not available yet' ) ) ;
26
+ }
27
+ return createWorkspaceCall ( opts , api , namespace , formData ) . then (
28
+ ( result ) => result . workspace
29
+ ) ;
30
+ } ,
31
+ [ api , apiAvailable , namespace , formData ] ,
32
+ ) ;
33
+
34
+ return useFetchState ( call , null ) ;
35
+ } ;
36
+
37
+ export default useCreateWorkspace ;
0 commit comments