Skip to content

Commit

Permalink
Fix create model form validation error
Browse files Browse the repository at this point in the history
  • Loading branch information
steverydz committed Jul 19, 2023
1 parent 31cb7ae commit c33f6bd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 23 deletions.
44 changes: 22 additions & 22 deletions static/js/brand-store/components/Models/CreateModelForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import React from "react";
import { useNavigate, useParams } from "react-router-dom";
import { useRecoilState, useRecoilValue, useSetRecoilState } from "recoil";
import { useMutation } from "react-query";
import { Input, Button, Notification } from "@canonical/react-components";
import { Input, Button } from "@canonical/react-components";
import randomstring from "randomstring";

import { checkModelNameExists } from "../../utils";
Expand All @@ -14,13 +14,11 @@ import type { Store, Model } from "../../types/shared";

type Props = {
setShowNotification: Function;
showErrorNotification: boolean;
setShowErrorNotification: Function;
};

function CreateModelForm({
setShowNotification,
showErrorNotification,
setShowErrorNotification,
}: Props) {
const navigate = useNavigate();
Expand All @@ -31,6 +29,18 @@ function CreateModelForm({
const modelsList = useRecoilValue(filteredModelsListState);
const setModelsList = useSetRecoilState<Array<Model>>(modelsListState);

const handleError = () => {
setShowErrorNotification(true);
setModelsList((oldModelsList: Array<Model>) => {
return oldModelsList.filter((model) => model.name !== newModel.name);
});
navigate(`/admin/${id}/models`);
setNewModel({ name: "", apiKey: "" });
setTimeout(() => {
setShowErrorNotification(false);
}, 5000);
};

const mutation = useMutation({
mutationFn: (newModel: { name: string; apiKey: string }) => {
const formData = new FormData();
Expand All @@ -39,6 +49,8 @@ function CreateModelForm({
formData.set("name", newModel.name);
formData.set("api_key", newModel.apiKey);

navigate(`/admin/${id}/models`);

setModelsList((oldModelsList: Array<Model>) => {
return [
{
Expand All @@ -58,23 +70,21 @@ function CreateModelForm({
},
onSuccess: (response) => {
if (!response.ok) {
setShowErrorNotification(true);
setModelsList((oldModelsList: Array<Model>) => {
return oldModelsList.filter((model) => model.name !== newModel.name);
});
handleError();
throw new Error(`${response.status} ${response.statusText}`);
}

if (response.ok) {
navigate(`/admin/${id}/models`);
setNewModel({ name: "", apiKey: "" });
setShowNotification(true);
setNewModel({ name: "", apiKey: "" });
navigate(`/admin/${id}/models`);
setTimeout(() => {
setShowNotification(false);
}, 5000);
}
},
onError: () => {
setModelsList((oldModelsList: Array<Model>) => {
return oldModelsList.filter((model) => model.name !== newModel.name);
});
handleError();
throw new Error("Unable to create a new model");
},
});
Expand All @@ -100,16 +110,6 @@ function CreateModelForm({
{currentStore.name}
</p>
)}
{showErrorNotification && (
<Notification
severity="negative"
onDismiss={() => {
setShowErrorNotification(false);
}}
>
Unable to create model
</Notification>
)}
<Input
type="text"
id="model-name-field"
Expand Down
13 changes: 12 additions & 1 deletion static/js/brand-store/components/Models/Models.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,18 @@ function Models() {
</Notification>
</div>
)}
{showErrorNotification && (
<div className="u-fixed-width">
<Notification
severity="negative"
onDismiss={() => {
setShowErrorNotification(false);
}}
>
Unable to create model
</Notification>
</div>
)}
<Row>
<Col size={6}>
<Link className="p-button" to={`/admin/${id}/models/create`}>
Expand Down Expand Up @@ -140,7 +152,6 @@ function Models() {
>
<CreateModelForm
setShowNotification={setShowNotification}
showErrorNotification={showErrorNotification}
setShowErrorNotification={setShowErrorNotification}
/>
</aside>
Expand Down

0 comments on commit c33f6bd

Please sign in to comment.