Skip to content

Commit

Permalink
Model service hooks (#4352)
Browse files Browse the repository at this point in the history
* Update models table test

* Convert model service queries to hooks
  • Loading branch information
steverydz authored Jul 25, 2023
1 parent 1ec7c13 commit fd9cdee
Show file tree
Hide file tree
Showing 7 changed files with 122 additions and 119 deletions.
34 changes: 10 additions & 24 deletions static/js/brand-store/components/Model/CreatePolicyForm.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
import React, { useState } from "react";
import React, { useState, useEffect } from "react";
import { useRecoilState } from "recoil";
import { useParams, Link, useNavigate } from "react-router-dom";
import { useQuery, useMutation } from "react-query";
import { useMutation } from "react-query";
import { Button } from "@canonical/react-components";

import { useSigningKeys } from "../../hooks";
import { signingKeysListState } from "../../atoms";

import type { Query } from "../../types/shared";

type Props = {
setShowNotification: Function;
setShowErrorNotification: Function;
Expand All @@ -19,28 +18,9 @@ function CreatePolicyForm({
setShowErrorNotification,
refetchPolicies,
}: Props) {
const getSigningKeys = async () => {
const response = await fetch(`/admin/store/${id}/signing-keys`);

if (!response.ok) {
throw new Error("There was a problem fetching signing keys");
}

const signingKeysData = await response.json();

if (!signingKeysData.success) {
throw new Error(signingKeysData.message);
}

setSigningKeys(signingKeysData.data);
};

const { id, model_id } = useParams();
const navigate = useNavigate();
const { isLoading, isError, error }: Query = useQuery(
"signingKeys",
getSigningKeys
);
const { isLoading, isError, error, data }: any = useSigningKeys(id);
const [signingKeys, setSigningKeys] = useRecoilState(signingKeysListState);
const [selectedSigningKey, setSelectedSigningKey] = useState("");

Expand Down Expand Up @@ -93,6 +73,12 @@ function CreatePolicyForm({
},
});

useEffect(() => {
if (!isLoading && !error) {
setSigningKeys(data);
}
}, [isLoading, error, data]);

return (
<form
onSubmit={(event) => {
Expand Down
35 changes: 11 additions & 24 deletions static/js/brand-store/components/Model/Model.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React, { useState } from "react";
import React, { useState, useEffect } from "react";
import { Link, useParams } from "react-router-dom";
import { useRecoilValue, useSetRecoilState } from "recoil";
import { useMutation, useQuery } from "react-query";
import { useMutation } from "react-query";
import { format } from "date-fns";
import randomstring from "randomstring";
import {
Expand All @@ -12,32 +12,13 @@ import {
Notification,
} from "@canonical/react-components";

import { useModels } from "../../hooks";
import { modelsListState } from "../../atoms";
import { currentModelState } from "../../selectors";

import ModelNav from "./ModelNav";

function Model() {
const getModels = async () => {
if (currentModel) {
return;
}

const response = await fetch(`/admin/store/${id}/models`);

if (!response.ok) {
throw new Error("There was a problem fetching models");
}

const modelsData = await response.json();

if (!modelsData.success) {
throw new Error(modelsData.message);
}

setModelsList(modelsData.data);
};

const mutation = useMutation({
mutationFn: (apiKey: string) => {
const formData = new FormData();
Expand Down Expand Up @@ -74,8 +55,7 @@ function Model() {
const [showSuccessNotification, setShowSuccessNotificaton] = useState(false);
const [showErrorNotification, setShowErrorNotificaton] = useState(false);
const setModelsList = useSetRecoilState<any>(modelsListState);

useQuery("models", getModels);
const { isLoading, error, data } = useModels(id);

const handleError = () => {
setShowErrorNotificaton(true);
Expand All @@ -84,6 +64,12 @@ function Model() {
}, 5000);
};

useEffect(() => {
if (!currentModel && !isLoading && !error) {
setModelsList(data);
}
}, [currentModel, isLoading, error, data]);

return (
<main className="l-main">
<div className="p-panel">
Expand Down Expand Up @@ -246,6 +232,7 @@ function Model() {
<hr style={{ marginTop: "1rem", marginBottom: "1rem" }} />
<div className="u-align--right">
<Button
type="button"
onClick={() => {
setNewApiKey("");
}}
Expand Down
50 changes: 20 additions & 30 deletions static/js/brand-store/components/Model/Policies.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from "react";
import React, { useState, useEffect } from "react";
import {
Link,
useParams,
Expand All @@ -7,48 +7,27 @@ import {
useNavigate,
} from "react-router-dom";
import { useSetRecoilState } from "recoil";
import { useQuery } from "react-query";
import { Row, Col, Notification } from "@canonical/react-components";

import ModelNav from "./ModelNav";
import PoliciesFilter from "./PoliciesFilter";
import PoliciesTable from "./PoliciesTable";
import CreatePolicyForm from "./CreatePolicyForm";

import { usePolicies } from "../../hooks";
import { policiesListFilterState, policiesListState } from "../../atoms";

import { isClosedPanel } from "../../utils";

import type { Policy, Query } from "../../types/shared";
import type { Policy } from "../../types/shared";

function Policies() {
const getPolicies = async () => {
setPoliciesList([]);

const response = await fetch(
`/admin/store/${id}/models/${model_id}/policies`
);

if (!response.ok) {
throw new Error("There was a problem fetching policies");
}

const policiesData = await response.json();

if (!policiesData.success) {
throw new Error(policiesData.message);
}

setPoliciesList(policiesData.data);
setFilter(searchParams.get("filter") || "");
};

const { id, model_id } = useParams();
const location = useLocation();
const navigate = useNavigate();
const { isLoading, isError, error, refetch }: Query = useQuery(
"policies",
getPolicies
const { isLoading, isError, error, refetch, data }: any = usePolicies(
id,
model_id
);
const setPoliciesList = useSetRecoilState<Array<Policy>>(policiesListState);
const setFilter = useSetRecoilState<string>(policiesListFilterState);
Expand All @@ -58,6 +37,15 @@ function Policies() {
false
);

useEffect(() => {
if (!isLoading && !isError) {
setPoliciesList(data);
setFilter(searchParams.get("filter") || "");
} else {
setPoliciesList([]);
}
}, [isLoading, error, data]);

return (
<>
<main className="l-main">
Expand Down Expand Up @@ -110,9 +98,11 @@ function Policies() {
</Col>
</Row>
<div className="u-fixed-width">
{isLoading && <p>Fetching policies...</p>}
{isError && error && <p>Error: {error.message}</p>}
<PoliciesTable />
<>
{isLoading && <p>Fetching policies...</p>}
{isError && error && <p>Error: {error.message}</p>}
<PoliciesTable />
</>
</div>
</div>
</div>
Expand Down
49 changes: 19 additions & 30 deletions static/js/brand-store/components/Models/Models.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import React, { useState } from "react";
import { useQuery } from "react-query";
import { useSetRecoilState, useRecoilState } from "recoil";
import React, { useState, useEffect } from "react";
import { useSetRecoilState } from "recoil";
import {
Link,
useParams,
Expand All @@ -10,6 +9,7 @@ import {
} from "react-router-dom";
import { Row, Col, Notification } from "@canonical/react-components";

import { useModels } from "../../hooks";
import {
modelsListFilterState,
modelsListState,
Expand All @@ -23,28 +23,9 @@ import CreateModelForm from "./CreateModelForm";

import { isClosedPanel } from "../../utils";

import type { Model, Policy, Query } from "../../types/shared";
import type { Model, Policy } from "../../types/shared";

function Models() {
const getModels = async () => {
const response = await fetch(`/admin/store/${id}/models`);

if (!response.ok) {
throw new Error("There was a problem fetching models");
}

const modelsData = await response.json();

if (!modelsData.success) {
throw new Error(modelsData.message);
}

setModelsList(modelsData.data);
setFilter(searchParams.get("filter") || "");

getPolicies(modelsData.data);
};

const getPolicies = async (modelsList: Array<Model>) => {
const data = await Promise.all(
modelsList.map((model) => {
Expand Down Expand Up @@ -74,18 +55,24 @@ function Models() {
const { id } = useParams();
const location = useLocation();
const navigate = useNavigate();
const { isLoading, isError, error }: Query = useQuery("models", getModels);
const { isLoading, isError, error, data }: any = useModels(id);
const setModelsList = useSetRecoilState<Array<Model>>(modelsListState);
const [policies, setPolicies] = useRecoilState<Array<Policy>>(
policiesListState
);
const setPolicies = useSetRecoilState<Array<Policy>>(policiesListState);
const setFilter = useSetRecoilState<string>(modelsListFilterState);
const [searchParams] = useSearchParams();
const [showNotification, setShowNotification] = useState<boolean>(false);
const [showErrorNotification, setShowErrorNotification] = useState<boolean>(
false
);

useEffect(() => {
if (!isLoading && !error) {
setModelsList(data);
setFilter(searchParams.get("filter") || "");
getPolicies(data);
}
}, [isLoading, error, data]);

return (
<>
<main className="l-main">
Expand Down Expand Up @@ -129,9 +116,11 @@ function Models() {
</Col>
</Row>
<div className="u-fixed-width">
{isLoading && <p>Fetching models...</p>}
{isError && error && <p>Error: {error.message}</p>}
<ModelsTable />
<>
{isLoading && <p>Fetching models...</p>}
{isError && error && <p>Error: {error.message}</p>}
<ModelsTable />
</>
</div>
</div>
</div>
Expand Down
4 changes: 2 additions & 2 deletions static/js/brand-store/components/Models/ModelsTable.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe("ModelsTable", () => {
renderComponent();

expect(
screen.getByRole("columnheader", { name: "Name (0)" })
screen.getByRole("columnheader", { name: /Name/ })
).toBeInTheDocument();

expect(
Expand All @@ -51,7 +51,7 @@ describe("ModelsTable", () => {
renderComponent();

const user = userEvent.setup();
const columnHeader = screen.getByRole("columnheader", { name: "Name (0)" });
const columnHeader = screen.getByRole("columnheader", { name: /Name/ });

expect(columnHeader.getAttribute("aria-sort")).toBe("none");

Expand Down
Loading

0 comments on commit fd9cdee

Please sign in to comment.