All files / publisher/pages/BrandStoreSettings BrandStoreSettings.tsx

38.09% Statements 16/42
50% Branches 15/30
30% Functions 3/10
38.09% 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                                15x 15x 15x 15x 15x                                     15x 13x       13x 6x     7x       7x     15x 8x 8x 8x       15x                                                                                                                                                                                                                                                                                          
import { useState, useEffect } from "react";
import { useParams } from "react-router-dom";
import { useQuery } from "react-query";
import {
  Row,
  Col,
  Spinner,
  Form,
  Input,
  PasswordToggle,
  Button,
} from "@canonical/react-components";
 
import Navigation from "../../components/Navigation";
 
function BrandStoreSettings() {
  const { id } = useParams();
  const [isPrivate, setIsPrivate] = useState<boolean>(false);
  const [manualReviewPolicy, setManualReviewPolicy] = useState<string>();
  const [isSaving, setIsSaving] = useState<boolean>(false);
  const { data, isLoading, status, refetch } = useQuery({
    queryKey: ["currentStore"],
    queryFn: async () => {
      const response = await fetch(`/api/store/${id}`);
 
      if (!response.ok) {
        throw new Error("Unable to fetch store");
      }
 
      const responseData = await response.json();
 
      if (!responseData.success) {
        throw new Error("Unable to fetch store");
      }
 
      return responseData.data;
    },
  });
 
  const formDisabled = () => {
    Iif (!data) {
      return true;
    }
 
    if (isPrivate !== data.private) {
      return false;
    }
 
    Iif (manualReviewPolicy !== data["manual-review-policy"]) {
      return false;
    }
 
    return true;
  };
 
  useEffect(() => {
    Eif (data) {
      setIsPrivate(data.private);
      setManualReviewPolicy(data["manual-review-policy"]);
    }
  }, [data]);
 
  return (
    <div className="l-application">
      <Navigation sectionName="settings" />
      <main className="l-main">
        <div className="p-panel--settings">
          <div className="p-panel__content">
            {isLoading && (
              <div className="u-fixed-width">
                <Spinner text="Loading..." />
              </div>
            )}
 
            {!isLoading && status === "success" && data && (
              <>
                <div className="u-fixed-width">
                  <h1 className="p-heading--4">{data.name} / Settings</h1>
                </div>
                <Row>
                  <Col size={7}>
                    <Form
                      onSubmit={async (e) => {
                        e.preventDefault();
 
                        setIsSaving(true);
 
                        const formData = new FormData();
                        formData.set("csrf_token", window.CSRF_TOKEN);
                        formData.set("store-id", id || "");
                        formData.set("private", isPrivate.toString());
                        formData.set(
                          "manual-review-policy",
                          manualReviewPolicy || "",
                        );
 
                        const response = await fetch(
                          `/api/store/${id}/settings`,
                          { method: "PUT", body: formData },
                        );
 
                        if (!response.ok) {
                          throw new Error("Unable to save settings");
                        }
 
                        setTimeout(() => {
                          setIsSaving(false);
                        }, 1500);
 
                        refetch();
                      }}
                      autoComplete="off"
                    >
                      <Input
                        id="is_public"
                        label="Include this store in public lists"
                        type="checkbox"
                        help="This store will not be listed in the store dropdowns like the one in the snap name registration form."
                        onChange={() => {
                          setIsPrivate(!isPrivate);
                        }}
                        checked={!isPrivate}
                      />
 
                      <PasswordToggle
                        defaultValue={id}
                        readOnly={true}
                        label="Store ID"
                        id="store-id"
                      />
 
                      <h2 className="p-heading--4" id="store-id-label">
                        Manual review policy
                      </h2>
                      <Input
                        type="radio"
                        label="Allow"
                        help="Normal review behaviour will be applied, using the result from the automatic review tool checks."
                        name="manual-review-policy"
                        id="manual-review-policy-label-allow"
                        value="allow"
                        onChange={(e) => {
                          setManualReviewPolicy(e.target.value);
                        }}
                        checked={manualReviewPolicy === "allow"}
                      />
                      <Input
                        type="radio"
                        label="Avoid"
                        help="No snap will be left in the manual review queue, even if the automatic review tool found no issues."
                        name="manual-review-policy"
                        id="manual-review-policy-label-avoid"
                        value="avoid"
                        onChange={(e) => {
                          setManualReviewPolicy(e.target.value);
                        }}
                        checked={manualReviewPolicy === "avoid"}
                      />
                      <Input
                        type="radio"
                        label="Require"
                        help="Every snap will be moved to the review queue, even if the automatic review tool found no issues."
                        name="manual-review-policy"
                        id="manual-review-policy-label-require"
                        value="require"
                        onChange={(e) => {
                          setManualReviewPolicy(e.target.value);
                        }}
                        checked={manualReviewPolicy === "require"}
                      />
 
                      <hr />
 
                      <div className="u-align--right">
                        <Button
                          appearance="positive"
                          type="submit"
                          disabled={formDisabled() || isSaving}
                          className={isSaving ? "has-icon" : ""}
                        >
                          {isSaving ? (
                            <>
                              <i className="p-icon--spinner is-light u-animation--spin"></i>
                              <span>Saving...</span>
                            </>
                          ) : (
                            <>Save changes</>
                          )}
                        </Button>
                      </div>
                    </Form>
                  </Col>
                </Row>
              </>
            )}
          </div>
        </div>
      </main>
    </div>
  );
}
 
export default BrandStoreSettings;