-
Notifications
You must be signed in to change notification settings - Fork 159
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
69 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / build (windows-latest)
|
||
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)) | ||
}, []); // componentDidMount | ||
|
||
const apiResult = useSyncExternalStore(store.subscribe, () => store.getState().api[requestId]); | ||
return apiResult; | ||
}; | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / build (windows-latest)
|
||
import { Action } from "../../../common/models/redux"; | ||
|
||
export function useDispatch(action: Action) { | ||
|
||
const {store} = React.useContext(ReactReduxContext); | ||
const actionReturned = store.dispatch(action); | ||
return actionReturned; | ||
} | ||
File renamed without changes.
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters