-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added events for eventBusForHost for getting data for statistic from …
…endpoint
- Loading branch information
1 parent
b8d42a7
commit b9bdadb
Showing
5 changed files
with
152 additions
and
19 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 |
---|---|---|
@@ -1,3 +1,48 @@ | ||
export enum EVENT_NAMES_FROM_STATISTIC { | ||
BACK_FROM_STATISTIC = "back_from_statistic", | ||
} | ||
|
||
export enum EVENT_NAMES_TO_STATISTIC { | ||
REQUEST_STATISTIC_DATA = "request_statistic_data", | ||
RECEIVE_STATISTIC_DATA = "receive_statistic_data", | ||
RECEIVE_STATISTIC_DATA_ERROR = "receive_statistic_data_error", | ||
} | ||
|
||
interface BaseAction { | ||
type: EVENT_NAMES_FROM_STATISTIC | EVENT_NAMES_TO_STATISTIC; | ||
payload?: { data: string }; | ||
} | ||
|
||
export interface ActionToStatistic extends BaseAction { | ||
type: EVENT_NAMES_TO_STATISTIC; | ||
} | ||
export interface RequestDataForStatistic extends ActionToStatistic { | ||
type: EVENT_NAMES_TO_STATISTIC.REQUEST_STATISTIC_DATA; | ||
} | ||
|
||
export function isActionToStatistic( | ||
action: unknown, | ||
): action is ActionToStatistic { | ||
if (!action) return false; | ||
if (typeof action !== "object") return false; | ||
if (!("type" in action)) return false; | ||
if (typeof action.type !== "string") return false; | ||
const ALL_EVENT_NAMES: Record<string, string> = { | ||
...EVENT_NAMES_TO_STATISTIC, | ||
}; | ||
return Object.values(ALL_EVENT_NAMES).includes(action.type); | ||
} | ||
|
||
export function isRequestDataForStatistic( | ||
action: unknown, | ||
): action is RequestDataForStatistic { | ||
if (!isActionToStatistic(action)) return false; | ||
return action.type === EVENT_NAMES_TO_STATISTIC.REQUEST_STATISTIC_DATA; | ||
} | ||
|
||
export function isReceiveDataForStatistic( | ||
action: unknown, | ||
): action is RequestDataForStatistic { | ||
if (!isActionToStatistic(action)) return false; | ||
return action.type === EVENT_NAMES_TO_STATISTIC.RECEIVE_STATISTIC_DATA; | ||
} |
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
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
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 |
---|---|---|
@@ -1,16 +1,55 @@ | ||
import { EVENT_NAMES_FROM_STATISTIC } from "../events"; | ||
import { | ||
EVENT_NAMES_FROM_STATISTIC, | ||
EVENT_NAMES_TO_STATISTIC, | ||
isReceiveDataForStatistic, | ||
} from "../events"; | ||
import { usePostMessage } from "./usePostMessage"; | ||
import { useEffect, useState } from "react"; | ||
import { StatisticData } from "../services/refact"; | ||
|
||
export const useEventBusForStatistic = () => { | ||
const postMessage = usePostMessage(); | ||
const [statisticData, setStatisticData] = useState<StatisticData | null>( | ||
null, | ||
); | ||
|
||
function backFromStatistic() { | ||
const backFromStatistic = () => { | ||
postMessage({ | ||
type: EVENT_NAMES_FROM_STATISTIC.BACK_FROM_STATISTIC, | ||
}); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
const listener = (event: MessageEvent) => { | ||
if (isReceiveDataForStatistic(event.data)) { | ||
if (event.data.payload !== undefined) { | ||
const parsedData = JSON.parse( | ||
event.data.payload.data, | ||
) as StatisticData; | ||
setStatisticData(parsedData); | ||
} | ||
} | ||
}; | ||
|
||
window.addEventListener("message", listener); | ||
|
||
return () => { | ||
window.removeEventListener("message", listener); | ||
}; | ||
}, []); | ||
|
||
useEffect(() => { | ||
const requestStatisticData = () => { | ||
postMessage({ | ||
type: EVENT_NAMES_TO_STATISTIC.REQUEST_STATISTIC_DATA, | ||
}); | ||
}; | ||
|
||
requestStatisticData(); | ||
}, [postMessage]); | ||
|
||
return { | ||
backFromStatistic, | ||
statisticData, | ||
}; | ||
}; |
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