fix(App.tsx): prevent duplicate alerts from being added to the alertsList

fix(alertContext.tsx): set the error, notice, and success data states before opening the respective alerts
feat(chatModal/index.tsx): add error handling for websocket connection and check backend health before reconnecting
This commit is contained in:
anovazzi1 2023-06-30 16:21:40 -03:00
commit 66b4bce525
3 changed files with 28 additions and 19 deletions

View file

@ -51,6 +51,9 @@ export default function App() {
useEffect(() => { useEffect(() => {
// If there is an error alert open with data, add it to the alertsList // If there is an error alert open with data, add it to the alertsList
if (errorOpen && errorData) { if (errorOpen && errorData) {
if(alertsList.length > 0 && JSON.stringify(alertsList[alertsList.length - 1].data)===JSON.stringify(errorData)){
return;
}
setErrorOpen(false); setErrorOpen(false);
setAlertsList((old) => { setAlertsList((old) => {
let newAlertsList = [ let newAlertsList = [
@ -62,6 +65,9 @@ export default function App() {
} }
// If there is a notice alert open with data, add it to the alertsList // If there is a notice alert open with data, add it to the alertsList
else if (noticeOpen && noticeData) { else if (noticeOpen && noticeData) {
if(alertsList.length > 0 && JSON.stringify(alertsList[alertsList.length - 1].data)===JSON.stringify(noticeData)){
return;
}
setNoticeOpen(false); setNoticeOpen(false);
setAlertsList((old) => { setAlertsList((old) => {
let newAlertsList = [ let newAlertsList = [
@ -73,6 +79,9 @@ export default function App() {
} }
// If there is a success alert open with data, add it to the alertsList // If there is a success alert open with data, add it to the alertsList
else if (successOpen && successData) { else if (successOpen && successData) {
if(alertsList.length > 0 && JSON.stringify(alertsList[alertsList.length - 1].data)===JSON.stringify(successData)){
return;
}
setSuccessOpen(false); setSuccessOpen(false);
setAlertsList((old) => { setAlertsList((old) => {
let newAlertsList = [ let newAlertsList = [

View file

@ -78,9 +78,9 @@ export function AlertProvider({ children }: { children: ReactNode }) {
* @param newState An object containing the new error data, including title and optional list of error messages * @param newState An object containing the new error data, including title and optional list of error messages
*/ */
function setErrorData(newState: { title: string; list?: Array<string> }) { function setErrorData(newState: { title: string; list?: Array<string> }) {
if (newState.title && newState.title !== "") {
setErrorDataState(newState); setErrorDataState(newState);
setErrorOpen(true); setErrorOpen(true);
if (newState.title && newState.title !== "") {
setNotificationCenter(true); setNotificationCenter(true);
pushNotificationList({ pushNotificationList({
type: "error", type: "error",
@ -95,9 +95,9 @@ export function AlertProvider({ children }: { children: ReactNode }) {
* @param newState An object containing the title of the notice and optionally a link. * @param newState An object containing the title of the notice and optionally a link.
*/ */
function setNoticeData(newState: { title: string; link?: string }) { function setNoticeData(newState: { title: string; link?: string }) {
if (newState.title && newState.title !== "") {
setNoticeDataState(newState); setNoticeDataState(newState);
setNoticeOpen(true); setNoticeOpen(true);
if (newState.title && newState.title !== "") {
// Add new notice to notification center // Add new notice to notification center
setNotificationCenter(true); setNotificationCenter(true);
pushNotificationList({ pushNotificationList({
@ -113,11 +113,10 @@ export function AlertProvider({ children }: { children: ReactNode }) {
* @param newState - A state object with a "title" property to set in the success data state. * @param newState - A state object with a "title" property to set in the success data state.
*/ */
function setSuccessData(newState: { title: string }) { function setSuccessData(newState: { title: string }) {
setSuccessDataState(newState); // update the success data state with the provided new state
setSuccessOpen(true); // open the success alert
// If the new state has a "title" property, add a new success notification to the list // If the new state has a "title" property, add a new success notification to the list
if (newState.title && newState.title !== "") { if (newState.title && newState.title !== "") {
setSuccessDataState(newState); // update the success data state with the provided new state
setSuccessOpen(true); // open the success alert
setNotificationCenter(true); // show the notification center setNotificationCenter(true); // show the notification center
pushNotificationList({ pushNotificationList({
// add the new notification to the list // add the new notification to the list

View file

@ -11,6 +11,7 @@ import { ChatMessageType } from "../../types/chat";
import ChatInput from "./chatInput"; import ChatInput from "./chatInput";
import _ from "lodash"; import _ from "lodash";
import { getHealth } from "../../controllers/API";
export default function ChatModal({ export default function ChatModal({
flow, flow,
@ -204,25 +205,25 @@ export default function ChatModal({
handleOnClose(event); handleOnClose(event);
}; };
newWs.onerror = (ev) => { newWs.onerror = (ev) => {
console.log(ev, "error"); getHealth().then((res) => {
if (flow.id === "") { if (res.status === 200) {
connectWS(); connectWS();
} else { }
}).catch((err) => {
setErrorData({ setErrorData({
title: "There was an error on web connection, please: ", // message when the backend failed
title: "The backend is not responding. Please try again later.",
// possible solution list
list: [ list: [
"Refresh the page", "Check your internet connection.",
"Use a new flow tab", "Check if the backend is running."
"Check if the backend is up",
], ],
}); });
} })
}; };
ws.current = newWs; ws.current = newWs;
} catch (error) { } catch (error) {
if (flow.id === "") {
connectWS(); connectWS();
}
console.log(error); console.log(error);
} }
} }