Start refactoring warnings

Signed-off-by: Egor Savkin <es@m-labs.hk>
pull/113/head
Egor Savkin 2023-12-04 17:40:23 +08:00
parent e8b1d70356
commit 530be12334
4 changed files with 124 additions and 43 deletions

View File

@ -5,6 +5,7 @@ import {OverlayTrigger} from "react-bootstrap";
import {DialogPopup} from "./options/DialogPopup.jsx";
import {productStyle} from "./utils";
import {Resources} from "./Resources.jsx";
import {Warnings} from "./Warnings";
/**
* Component that renders a product.
@ -132,21 +133,7 @@ export class ProductCartItem extends PureComponent {
<div className="progress-container warning d-flex justify-content-evenly">
{warning &&
(<OverlayTrigger
placement="bottom"
trigger={['click', 'hover', 'focus']}
overlay={
({arrowProps, hasDoneInitialMeasure, show, ...props}) => (
<div className="k-popup-warning" {...props}>
<p className="rule warning">
<i>{warning.message}</i>
</p>
</div>)
}
rootClose
>
<img className="alert-warning" src={`/images${warning.icon}`}/>
</OverlayTrigger>)
(<Warnings warnings={warning} />)
}
{options && (

View File

@ -0,0 +1,25 @@
import {OverlayTrigger} from "react-bootstrap";
import React from "react";
export function Warnings({warnings}) {
return (
<OverlayTrigger
placement="bottom"
trigger={['click', 'hover', 'focus']}
overlay={
({arrowProps, hasDoneInitialMeasure, show, ...props}) => (
<div className="k-popup-warning" {...props}>
<p className="rule warning">
<i>{warnings.message}</i>
</p>
</div>)
}
rootClose
>
<img className="alert-warning" src={`/images${warning.icon}`}/>
</OverlayTrigger>
)
}

View File

@ -1,47 +1,51 @@
function EEMCounter(data, index) {
const process_slots = (item) => {
if (!item.options_data
|| item.options_data.ext_pwr === false
|| item.options_data.mono_eem === false
)
return item.slotOccupied;
else if (item.options_data.ext_pwr === true)
return 0;
else if (item.options_data.mono_eem === true || item.options_data.n_eem === "1 EEM")
return 1;
else if (item.options_data.n_eem === "3 EEM")
return 3;
return item.slotOccupied || 0;
}
export const count_item_occupied_eem = (item) => {
if (!item.options_data
|| item.options_data.ext_pwr === false
|| item.options_data.mono_eem === false
)
return item.slotOccupied;
else if (item.options_data.ext_pwr === true)
return 0;
else if (item.options_data.mono_eem === true || item.options_data.n_eem === "1 EEM")
return 1;
else if (item.options_data.n_eem === "3 EEM")
return 3;
return item.slotOccupied || 0;
}
export const count_item_occupied_clock = (item) => {
return (item.options_data && item.options_data.ext_clk === true) ? 0 : (item.clockOccupied || 0);
}
export const count_item_occupied_idc = (item) => {
return item.idcOccupied || 0;
}
function EEMCounter(data, index) {
let count = 0;
for (let i = index + 1; i < data.length; i++) {
if (data[i].resources.filter((value, _i) => value.name === "eem").length > 0) break;
count += process_slots(data[i]);
if (!!data[i].resources.find((value, _i) => value.name === "eem")) break;
count += count_item_occupied_eem(data[i]);
}
return count;
}
function ClockCounter(data, index) {
const process_slots = (item) => {
return (item.options_data && item.options_data.ext_clk === true) ? 0 : (item.clockOccupied || 0);
}
let count = 0;
for (let i = index + 1; i < data.length; i++) {
if (data[i].resources.filter((value, _i) => value.name === "clk").length > 0) break;
count += process_slots(data[i]);
if (!!data[i].resources.find((value, _i) => value.name === "clk")) break;
count += count_item_occupied_clock(data[i]);
}
return count;
}
function IDCCounter(data, index) {
const process_slots = (item) => {
return item.idcOccupied || 0;
}
let count = 0;
for (let i = index + 1; i < data.length; i++) {
if (data[i].resources.filter((value, _i) => value.name === "idc").length > 0) break;
count += process_slots(data[i]);
if (!!data[i].resources.find((value, _i) => value.name === "idc")) break;
count += count_item_occupied_idc(data[i]);
}
return count;
}

View File

@ -3,4 +3,69 @@
* First idea - there should be either definitive list of available warnings,
* or defined as some json logic leading to the warning message(s), similar way how FillExtData works.
* Second - resources indicator should be separate component
*/
*/
import {count_item_occupied_eem, count_item_occupied_clock, count_item_occupied_idc} from "./count_resources";
const Levels = {
"reminder": {priority: 0, icon: '/images/shop/icon-reminder.svg'},
"warning": {priority: 1, icon: '/images/shop/icon-warning.svg'},
}
const find_in_counters = (counters, name) => {
return counters.find((element) => element.name === name)
}
const find_previous_source = (data, index, source) => {
return data.slice(0, index).find((element) => {element.resources.find((value) => value.name === source)})
}
const not_enough_resource_trigger = (name) => {
return (_data, _index, counters) => {
const eem = find_in_counters(counters, name);
return eem.occupied > eem.max;
}
}
const no_source_trigger = (name) => {
return (data, index, _counters) => {
const occupied = count_item_occupied_eem(data[index]);
if (occupied > 0)
return !!find_previous_source(data, index, name);
return false;
}
}
const Types = {
"eem_resource": {
level: "warning",
trigger: not_enough_resource_trigger("eem"),
message: "Insufficient EEM connectors"
},
"no_eem_source" : {
level: "warning",
trigger: no_source_trigger("eem"),
message: 'This card needs a card that provides a EEM connector (e.g. Kasli) at its left.'
},
"idc_resource": {
level: "warning",
trigger: not_enough_resource_trigger("idc"),
message: "Insufficient IDC connectors."
},
"no_idc_source" : {
level: "warning",
trigger: no_source_trigger("idc"),
message: 'Should be after a Zotino or a HD68-IDC or with another IDC-BNC.'
},
"clk_resource": {
level: "warning",
trigger: not_enough_resource_trigger("clk"),
message: "Insufficient clock connectors."
},
"no_clk_source" : {
level: "warning",
trigger: no_source_trigger("clk"),
message: 'This card needs either a card that provides a clock source (e.g. Kasli or Clocker) at its left or use external clock source.'
},
}