import toast from 'react-hot-toast';
import dataJSON from '../../public/data.json';
//TODO: rewrite
const createToast = (title: string, msg: string, type: number) => {
toast.custom((t) => (
))
};
// let dataJSON: any;
// let headers = new Headers();
// headers.append('Access-Control-Allow-Origin', 'http://127.0.0.1:8000');
// headers.append("Access-Control-Allow-Methods", 'POST');
// headers.append("Access-Control-Allow-Headers", 'Content-Type, Authorization');
// fetch("http://127.0.0.1:8000/data",{method:'POST',headers:headers})
// .then(response => {
// return response
// })
// .then(data => {
// console.log(data);
// dataJSON=data;
// })
const fireToast = () => {
const alertSettings = localStorage.getItem("alertSettings");
if (alertSettings) {
for (const alertSetting of JSON.parse(alertSettings)) {
console.log(alertSetting);
const value = isNaN(parseFloat(alertSetting.value)) ? alertSetting.value : parseFloat(alertSetting.value);
const para = alertSetting.criterion < 2 ? "delta_" + alertSetting.para : alertSetting.para;
if (alertSetting.id == "ALL") {
Object.keys(dataJSON).map((id: string) => {
const condition = alertSetting.criterion == '0' ? value <= -1 * dataJSON[id][para] :
alertSetting.criterion == '1' || alertSetting.criterion == '3' ? value >= dataJSON[id][para] :
alertSetting.criterion == '2' ? value <= dataJSON[id][para] :
value == dataJSON[id][para];
const realValue = alertSetting.criterion == '0' ? dataJSON[id][para] * -1 : dataJSON[id][para];
if (condition) {
const msg = `${alertSetting.para} of ${id} ${alertSetting.criterion == 0 ? "goes down by" : alertSetting.criterion == 1 ? "goes up by" : alertSetting.criterion == 2 ? "is smaller than" : alertSetting.criterion == 3 ? "is greater than" : "is equal to"} ${realValue}`;
createToast(id, msg, alertSetting.type)
}
}
);
}
else {
const id = alertSetting.id;
const condition = alertSetting.criterion == '0' ? value >= -1 * dataJSON[id][para] :
alertSetting.criterion == '1' || alertSetting.criterion == '3' ? value >= dataJSON[id][para] :
alertSetting.criterion == '2' ? value <= dataJSON[id][para] :
value == dataJSON[id][para];
const realValue = alertSetting.criterion == '0' ? dataJSON[id][para] * -1 : dataJSON[id][para];
if (condition) {
const msg = `${alertSetting.para} of ${id} ${alertSetting.criterion == 0 ? "goes down by" : alertSetting.criterion == 1 ? "goes up by" : alertSetting.criterion == 2 ? "is smaller than" : alertSetting.criterion == 3 ? "is greater than" : "is equal to"} ${realValue}`;
createToast(id, msg, alertSetting.type)
}
}
};
}
}
export default fireToast;