All files / src/components/AddCondition Table.tsx

0% Statements 0/27
0% Branches 0/8
0% Functions 0/6
0% Lines 0/27

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96                                                                                                                                                                                               
import { useSelector } from "react-redux";
import { additionalStateSelector } from "../../store/features/drawers/drawersSlice.ts";
import useEdgeConditions from "../../hooks/playbooks/useEdgeConditions.ts";
import { operationOptions } from "../../utils/conditionals/operationOptions.ts";
import { tableOptions } from "../../utils/conditionals/typeOptions/index.ts";
import HandleTypes from "./HandleTypes.tsx";
import { InputTypes } from "../../types/inputs/inputTypes.ts";
import CustomInput from "../Inputs/CustomInput.tsx";
import useIsPrefetched from "../../hooks/playbooks/useIsPrefetched.ts";
import { RuleType } from "../common/Conditions/types/RuleTypes.ts";
 
function Table({ condition, conditionIndex, rule }) {
  const { id } = useSelector(additionalStateSelector);
  const { handleRule } = useEdgeConditions(id);
  const isPrefetched = useIsPrefetched();
 
  const handleChange = (val: string | undefined, type: string) => {
    handleRule(type, val, conditionIndex, RuleType.RULE);
  };
 
  const checkIfNumeric = rule.isNumeric || rule.type === "ROW_COUNT";
 
  const threshold = checkIfNumeric
    ? rule.numeric_value_threshold
    : rule.string_value_threshold;
 
  return (
    <div className="flex flex-wrap gap-2">
      <CustomInput
        inputType={InputTypes.DROPDOWN}
        error={undefined}
        options={tableOptions}
        value={rule.type}
        placeholder={`Select Type`}
        handleChange={(id: string) =>
          handleChange(id, `${condition.type?.toLowerCase()}.type`)
        }
        disabled={!!isPrefetched}
      />
 
      <HandleTypes
        condition={condition}
        rule={rule}
        conditionIndex={conditionIndex}
      />
 
      <CustomInput
        inputType={InputTypes.DROPDOWN}
        error={undefined}
        options={
          checkIfNumeric
            ? operationOptions
            : operationOptions.filter((e) => e.id === "EQUAL_O")
        }
        value={rule.operator}
        placeholder={`Select Operator`}
        handleChange={(id: string) =>
          handleChange(id, `${condition.type?.toLowerCase()}.operator`)
        }
        disabled={!!isPrefetched}
      />
 
      <CustomInput
        inputType={InputTypes.TEXT}
        disabled={!!isPrefetched}
        handleChange={(val: string) => {
          if (checkIfNumeric) {
            handleChange(
              undefined,
              `${condition.type?.toLowerCase()}.string_value_threshold`,
            );
            handleChange(
              val,
              `${condition.type?.toLowerCase()}.numeric_value_threshold`,
            );
          } else {
            handleChange(
              undefined,
              `${condition.type?.toLowerCase()}.numeric_value_threshold`,
            );
            handleChange(
              val,
              `${condition.type?.toLowerCase()}.string_value_threshold`,
            );
          }
        }}
        value={threshold}
        placeholder={"Enter threshold of condition"}
        className="!w-[200px]"
      />
    </div>
  );
}
 
export default Table;