Skip to content

Commit

Permalink
useApi
Browse files Browse the repository at this point in the history
  • Loading branch information
panaC committed Sep 11, 2023
1 parent 61048a2 commit 340ea16
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 5 deletions.
25 changes: 25 additions & 0 deletions src/renderer/common/hooks/useApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import * as React from "react";
import { ReactReduxContext} from 'react-redux'

Check failure on line 2 in src/renderer/common/hooks/useApi.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Strings must use doublequote

Check failure on line 2 in src/renderer/common/hooks/useApi.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Missing semicolon
import { v4 as uuidv4 } from "uuid";
import { TApiMethod, TApiMethodName } from "readium-desktop/common/api/api.type";
import { TModuleApi } from "readium-desktop/common/api/moduleApi.type";
import { TMethodApi } from "readium-desktop/common/api/methodApi.type";
import { apiActions } from "readium-desktop/common/redux/actions";
import { ApiResponse } from "readium-desktop/common/redux/states/api";
import { TReturnPromiseOrGeneratorType } from "readium-desktop/typings/api";
import { useSyncExternalStore } from "./useSyncExternalStore";

export function useApi<T extends TApiMethodName>(_requestId: string, apiPath: T, ...requestData: Parameters<TApiMethod[T]>): ApiResponse<TReturnPromiseOrGeneratorType<TApiMethod[T]>> {

const requestId = _requestId || React.useMemo(() => uuidv4(), []);
const { store } = React.useContext(ReactReduxContext);
React.useEffect(() => {
const splitPath = apiPath.split("/");
const moduleId = splitPath[0] as TModuleApi;
const methodId = splitPath[1] as TMethodApi;
store.dispatch(apiActions.request.build(requestId, moduleId, methodId, requestData))

Check failure on line 20 in src/renderer/common/hooks/useApi.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Missing semicolon
}, []); // componentDidMount

const apiResult = useSyncExternalStore(store.subscribe, () => store.getState().api[requestId]);
return apiResult;
};

Check failure on line 25 in src/renderer/common/hooks/useApi.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Newline required at end of file but not found
10 changes: 10 additions & 0 deletions src/renderer/common/hooks/useDispatch.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import * as React from "react";
import { ReactReduxContext} from 'react-redux'

Check failure on line 2 in src/renderer/common/hooks/useDispatch.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Strings must use doublequote

Check failure on line 2 in src/renderer/common/hooks/useDispatch.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Missing semicolon
import { Action } from "../../../common/models/redux";

export function useDispatch(action: Action) {

const {store} = React.useContext(ReactReduxContext);
const actionReturned = store.dispatch(action);
return actionReturned;
}

Check failure on line 10 in src/renderer/common/hooks/useDispatch.ts

View workflow job for this annotation

GitHub Actions / build (windows-latest)

Newline required at end of file but not found
File renamed without changes.
File renamed without changes.
File renamed without changes.
39 changes: 34 additions & 5 deletions src/renderer/library/components/HooksTest.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@
import * as React from "react";
import { log } from "console";
import { ILibraryRootState } from "../redux/states";
import { useTranslator } from "readium-desktop/common/hooks/useTranslator";
import { useSelector } from "readium-desktop/common/hooks/useSelector";
import { useTranslator } from "readium-desktop/renderer/common/hooks/useTranslator";
import { useSelector } from "readium-desktop/renderer/common/hooks/useSelector";
import { useApi } from "readium-desktop/renderer/common/hooks/useApi";
// import { v4 as uuidv4 } from "uuid";
// import { TApiMethod, TApiMethodName } from "readium-desktop/common/api/api.type";
// import { ReactReduxContext} from 'react-redux'
// import { TModuleApi } from "readium-desktop/common/api/moduleApi.type";
// import { TMethodApi } from "readium-desktop/common/api/methodApi.type";
// import { apiActions } from "readium-desktop/common/redux/actions";
// import { useSyncExternalStore } from "readium-desktop/renderer/common/hooks/useSyncExternalStore";

export function HooksTest(props: {name:string}) {

Expand Down Expand Up @@ -35,16 +43,37 @@ export function HooksTest(props: {name:string}) {
// return translator.subscribe(handleLocaleChange);
// }, [translator.subscribe]);
const _ = useTranslator();
// let _ = (_a: any) => {};

// const {store} = React.useContext<ReactReduxContextValue<ILibraryRootState>>(ReactReduxContext);
// const id = useSyncExternalStore(store.subscribe, () => store.getState().win.identifier);
// log(id);

// useSelector custom with just useSyncExternalStore
const id = useSelector((state: ILibraryRootState) => state.win.identifier);
// let id = "";
log(id);

// const requestId = React.useMemo(() => uuidv4(), []);
const requestId: string = null;
log(requestId);
// useApi
// const [requestId] = React.useState('test');
// useDispatchApi(requestId, "publication/findAll");

log(Date.now().toString(), "rendered");
return <h1 style={{position: "absolute", top: 0, zIndex: 999}}>Hello : {name} : id {id} : translate {_("header.books")}</h1>;
}
// const { store } = React.useContext(ReactReduxContext);
// React.useEffect(() => {
// const apiPath = "publication/findAll";
// const requestData: any[] = [];
// const splitPath = apiPath.split("/");
// const moduleId = splitPath[0] as TModuleApi;
// const methodId = splitPath[1] as TMethodApi;
// store.dispatch(apiActions.request.build(requestId, moduleId, methodId, requestData))
// }, []);

// const allPubRes = useSyncExternalStore(store.subscribe, () => store.getState().api[requestId]);
const allPubRes = useApi(requestId, "publication/findAll");
log(allPubRes);
log(Date.now().toString(), "rendered");
return <h1 style={{ position: "absolute", top: 0, zIndex: 999 }}>Hello : {name} : id {id} : translate {_("header.books")}</h1>;
}

0 comments on commit 340ea16

Please sign in to comment.