Fix regression messages not being sent from the beginning

This commit is contained in:
Eric NGUYEN 2022-10-03 20:35:25 +02:00
parent 7c8db92453
commit 32684a725b
3 changed files with 79 additions and 73 deletions

View file

@ -0,0 +1,54 @@
import * as React from 'react';
import { IHistoryState } from '../../Interfaces/IHistoryState';
import { IGetFeedbackRequest } from '../../Interfaces/IGetFeedbackRequest';
import { IGetFeedbackResponse } from '../../Interfaces/IGetFeedbackResponse';
import { IMessage } from '../../Interfaces/IMessage';
import { GetCircularReplacerKeepDataStructure } from '../../utils/saveload';
// eslint-disable-next-line @typescript-eslint/strict-boolean-expressions
const myWorker = window.Worker && new Worker('workers/message_worker.js');
export function UseWorker(
state: IHistoryState,
setMessages: React.Dispatch<React.SetStateAction<IMessage[]>>): void {
React.useEffect(() => {
// use webworker for the stringify to avoid freezing
myWorker.postMessage({
state,
url: import.meta.env.VITE_API_GET_FEEDBACK_URL
});
return () => {
};
}, [state]);
React.useEffect(() => {
myWorker.onmessage = (event) => {
setMessages(event.data as IMessage[]);
};
}, [setMessages]);
}
export function UseAsync(
state: IHistoryState,
setMessages: React.Dispatch<React.SetStateAction<IMessage[]>>): void {
React.useEffect(() => {
const request: IGetFeedbackRequest = {
// eslint-disable-next-line @typescript-eslint/naming-convention
ApplicationState: state
};
const dataParsed = JSON.stringify(request, GetCircularReplacerKeepDataStructure());
fetch(import.meta.env.VITE_API_GET_FEEDBACK_URL, {
method: 'POST',
headers: new Headers({
// eslint-disable-next-line @typescript-eslint/naming-convention
'Content-Type': 'application/json'
}),
body: dataParsed
})
.then(async (response) => await response.json()
)
.then(async (json: IGetFeedbackResponse) => {
setMessages(json.messages);
});
}, [state]);
}