All files / src/components/rule-editor rule-editor.tsx

46.43% Statements 26/56
37.5% Branches 15/40
22.22% Functions 4/18
38.1% Lines 16/42

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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223                                    30x                       30x 30x   30x   30x                     30x 2x 2x 2x                                                   1x 1x                                                                                                 1x 1x   1x                 1x                                                       10x                                                                                                                  
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
 
import {
  Button,
  Card,
  Collapse,
  ControlGroup,
  FormGroup,
  HTMLSelect,
  InputGroup,
  NumericInput,
  Switch,
  TagInput,
} from '@blueprintjs/core';
import { IconNames } from '@blueprintjs/icons';
import React, { useState } from 'react';
 
import { Rule, RuleUtil } from '../../utils/load-rule';
 
import './rule-editor.scss';
 
export interface RuleEditorProps {
  rule: Rule;
  tiers: any[];
  onChange: (newRule: Rule) => void;
  onDelete: () => void;
  moveUp: (() => void) | null;
  moveDown: (() => void) | null;
}
 
export const RuleEditor = React.memo(function RuleEditor(props: RuleEditorProps) {
  const { rule, onChange, tiers, onDelete, moveUp, moveDown } = props;
  const [isOpen, setIsOpen] = useState(true);
  if (!rule) return null;
 
  function removeTier(key: string) {
    const newTierReplicants = Object.assign({}, rule.tieredReplicants);
    delete newTierReplicants[key];
 
    const newRule = Object.assign({}, rule, { tieredReplicants: newTierReplicants });
    onChange(newRule);
  }
 
  function addTier() {
    let newTierName = tiers[0];
 
    if (rule.tieredReplicants) {
      for (const tier of tiers) {
        if (rule.tieredReplicants[tier] === undefined) {
          newTierName = tier;
          break;
        }
      }
    }
 
    onChange(RuleUtil.addTieredReplicant(rule, newTierName, 1));
  }
 
  function renderTiers() {
    const tieredReplicants = rule.tieredReplicants;
    Eif (!tieredReplicants) return;
 
    const ruleTiers = Object.keys(tieredReplicants).sort();
    return ruleTiers.map(tier => {
      return (
        <ControlGroup key={tier}>
          <Button minimal style={{ pointerEvents: 'none' }}>
            Replicants:
          </Button>
          <NumericInput
            value={tieredReplicants[tier]}
            onValueChange={(v: number) => {
              if (isNaN(v)) return;
              onChange(RuleUtil.addTieredReplicant(rule, tier, v));
            }}
            min={1}
            max={256}
          />
          <Button minimal style={{ pointerEvents: 'none' }}>
            Tier:
          </Button>
          <HTMLSelect
            fill
            value={tier}
            onChange={(e: any) =>
              onChange(RuleUtil.renameTieredReplicants(rule, tier, e.target.value))
            }
          >
            {tiers
              .filter(t => t === tier || !tieredReplicants[t])
              .map(t => {
                return (
                  <option key={t} value={t}>
                    {t}
                  </option>
                );
              })}
          </HTMLSelect>
          <Button
            disabled={ruleTiers.length === 1}
            onClick={() => removeTier(tier)}
            icon={IconNames.TRASH}
          />
        </ControlGroup>
      );
    });
  }
 
  function renderTierAdder() {
    const { rule, tiers } = props;
    Iif (Object.keys(rule.tieredReplicants || {}).length >= Object.keys(tiers).length) return;
 
    return (
      <FormGroup className="right">
        <Button onClick={addTier} minimal icon={IconNames.PLUS}>
          Add a tier
        </Button>
      </FormGroup>
    );
  }
 
  return (
    <div className="rule-editor">
      <div className="title">
        <Button
          className="left"
          minimal
          rightIcon={isOpen ? IconNames.CARET_DOWN : IconNames.CARET_RIGHT}
          onClick={() => setIsOpen(!isOpen)}
        >
          {RuleUtil.ruleToString(rule)}
        </Button>
        <div className="spacer" />
        {moveUp && <Button minimal icon={IconNames.ARROW_UP} onClick={moveUp} />}
        {moveDown && <Button minimal icon={IconNames.ARROW_DOWN} onClick={moveDown} />}
        <Button minimal icon={IconNames.TRASH} onClick={onDelete} />
      </div>
 
      <Collapse isOpen={isOpen}>
        <Card>
          <FormGroup>
            <ControlGroup>
              <HTMLSelect
                value={rule.type}
                onChange={(e: any) =>
                  onChange(RuleUtil.changeRuleType(rule, e.target.value as any))
                }
              >
                {RuleUtil.TYPES.map(type => {
                  return (
                    <option key={type} value={type}>
                      {type}
                    </option>
                  );
                })}
              </HTMLSelect>
              {RuleUtil.hasPeriod(rule) && (
                <InputGroup
                  value={rule.period || ''}
                  onChange={(e: any) =>
                    onChange(RuleUtil.changePeriod(rule, e.target.value as any))
                  }
                  placeholder="P1D"
                />
              )}
              {RuleUtil.hasIncludeFuture(rule) && (
                <Switch
                  className="include-future"
                  checked={rule.includeFuture || false}
                  label="Include future"
                  onChange={() => {
                    onChange(RuleUtil.changeIncludeFuture(rule, !rule.includeFuture));
                  }}
                />
              )}
              {RuleUtil.hasInterval(rule) && (
                <InputGroup
                  value={rule.interval || ''}
                  onChange={(e: any) =>
                    onChange(RuleUtil.changeInterval(rule, e.target.value as any))
                  }
                  placeholder="2010-01-01/2020-01-01"
                />
              )}
            </ControlGroup>
          </FormGroup>
          {RuleUtil.hasTieredReplicants(rule) && (
            <FormGroup>
              {renderTiers()}
              {renderTierAdder()}
            </FormGroup>
          )}
          {RuleUtil.hasColocatedDataSources(rule) && (
            <FormGroup label="Colocated datasources">
              <TagInput
                values={rule.colocatedDataSources || []}
                onChange={(v: any) => onChange(RuleUtil.changeColocatedDataSources(rule, v))}
                fill
              />
            </FormGroup>
          )}
        </Card>
      </Collapse>
    </div>
  );
});