All files / publisher-admin/pages/Releases AddTrackPanel.tsx

71.79% Statements 28/39
54.54% Branches 12/22
71.42% Functions 5/7
72.97% Lines 27/37

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                                              10x 10x 10x 10x   10x 10x       10x 2x     2x   2x 2x 2x 2x 2x     2x         2x   2x 1x 1x 1x 1x   1x   2x     10x                                     2x 2x                                                                                                                                                                                           3x                    
import {
  Accordion,
  Button,
  Input,
  Panel,
  Spinner,
} from "@canonical/react-components";
import { useState } from "react";
import { usePackage } from "../../hooks";
 
type AddTrackPanelProps = {
  charmName: string;
  onClose: () => void;
  setSelectedTrack: (track: string) => void;
  onSuccess?: () => void;
};
 
export default function AddTrackPanel({
  charmName,
  onClose,
  setSelectedTrack,
  onSuccess,
}: AddTrackPanelProps) {
  const [trackName, setTrackName] = useState("");
  const [versionPattern, setVersionPattern] = useState("");
  const [phasingPercentage, setPhasingPercentage] = useState("");
  const { refetch } = usePackage(charmName);
 
  const [loading, setLoading] = useState(false);
  const [errors, setErrors] = useState<{ [key: string]: string | undefined }>(
    {}
  );
 
  const submit = async () => {
    Iif (!trackName || !!Object.keys(errors).filter((key) => errors[key]).length)
      return;
 
    setLoading(true);
 
    const formData = new FormData();
    formData.append("csrf_token", window.CSRF_TOKEN);
    formData.append("track-name", trackName);
    Iif (versionPattern) formData.append("version-pattern", versionPattern);
    Iif (phasingPercentage)
      formData.append("auto-phasing-percentage", phasingPercentage);
 
    const response = await fetch(`/${charmName}/create-track`, {
      method: "POST",
      body: formData,
    });
 
    const responseData = await response.json();
 
    if (response.ok) {
      refetch();
      setSelectedTrack(trackName);
      onClose();
      onSuccess?.();
    } else {
      setErrors({ trackName: responseData.error });
    }
    setLoading(false);
  };
 
  return (
    <Panel
      wrapContent={false}
      header={
        <div className="p-panel__header u-no-padding--left u-no-padding--right">
          <h4 className="p-panel__title">Add track</h4>
        </div>
      }
    >
      <div
        className="p-panel__content u-no-padding--top u-no-padding--bottom"
        style={{ overflow: "auto" }}
      >
        <Input
          help="Name should follow the track creation guardrails (TCG)"
          placeholder="display-name-123"
          label="* Track name"
          type="text"
          onChange={(e) => {
            setTrackName(e.target.value);
            setErrors({ ...errors, trackName: undefined });
          }}
          value={trackName}
          error={errors?.trackName}
        />
        <Accordion
          sections={[
            {
              key: "optional properties",
              title: "Optional Properties",
              content: (
                <>
                  <Input
                    help="Version pattern should follow glob pattern"
                    placeholder="?.v.*"
                    label="Version pattern"
                    type="text"
                    onChange={(e) => setVersionPattern(e.target.value)}
                    value={versionPattern}
                  />
                  <Input
                    help={
                      <>
                        The percentage is a value from 0 to 100
                        <br />
                        Releases will be done progressively on the track and its
                        percentage will be incremented automatically.
                      </>
                    }
                    placeholder="14.9"
                    label="Automatic phasing percentage"
                    error={errors?.phasingPercentage}
                    type="text"
                    onChange={(e) => {
                      const phasingPercentageValue = Number(e.target.value);
                      if (isNaN(phasingPercentageValue)) {
                        setErrors({
                          phasingPercentage: "Percentage must be a number",
                        });
                      } else if (
                        phasingPercentageValue &&
                        (phasingPercentageValue > 100 ||
                          phasingPercentageValue < 0)
                      ) {
                        setErrors({
                          phasingPercentage:
                            "Percentage must be between 0 and 100",
                        });
                      } else {
                        setErrors({ ...errors, phasingPercentage: undefined });
                      }
                      setPhasingPercentage(e.target.value);
                    }}
                    value={phasingPercentage}
                  />
                </>
              ),
            },
          ]}
        />
        <p>* Mandatory fields</p>
      </div>
      <div>
        <p>
          <a
            target="_blank"
            href="https://juju.is/docs/juju/channel"
            rel="noreferrer"
          >
            Learn about tracks
          </a>{" "}
          or{" "}
          <a
            target="_blank"
            href="https://discourse.charmhub.io"
            rel="noreferrer"
          >
            contact us
          </a>{" "}
          for help.
        </p>
      </div>
      <div className="p-panel__footer u-align--right">
        <div className="u-fixed-width">
          <Button className="u-no-margin--bottom" onClick={onClose}>
            Cancel
          </Button>
          <Button
            appearance="positive"
            onClick={submit}
            hasIcon={loading}
            disabled={
              loading ||
              !trackName ||
              !!Object.keys(errors).filter((key) => errors[key]).length
            }
          >
            {loading ? <Spinner text="Loading..." /> : "Add Track"}
          </Button>
        </div>
      </div>
    </Panel>
  );
}