Skip to content

Commit 7f88be3

Browse files
committed
chore: Update API key functionality when creating model
1 parent 5091d81 commit 7f88be3

2 files changed

Lines changed: 58 additions & 27 deletions

File tree

static/js/publisher/pages/Models/CreateModelForm.tsx

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import { checkModelNameExists, setPageTitle } from "../../utils";
99

1010
import {
1111
modelsListState,
12-
newModelState,
1312
filteredModelsListState,
1413
} from "../../state/modelsState";
1514
import {
@@ -33,7 +32,8 @@ function CreateModelForm({
3332
const location = useLocation();
3433
const { id } = useParams();
3534
const brandId = useAtomValue(brandIdState);
36-
const [newModel, setNewModel] = useAtom(newModelState);
35+
const [newModelName, setNewModelName] = useState("");
36+
const [apiKey, setApiKey] = useState("");
3737
const stores = useAtom(brandStoresState);
3838
const currentStore = stores[0].find((store: Store) => store.id === id);
3939
const modelsList = useAtomValue(filteredModelsListState);
@@ -47,10 +47,11 @@ function CreateModelForm({
4747
setShowErrorNotification(true);
4848
setIsSaving(false);
4949
setModelsList((oldModelsList: Array<Model>) => {
50-
return oldModelsList.filter((model) => model.name !== newModel.name);
50+
return oldModelsList.filter((model) => model.name !== newModelName);
5151
});
5252
navigate(`/admin/${id}/models`);
53-
setNewModel({ name: "", apiKey: "" });
53+
setNewModelName("");
54+
setApiKey("");
5455
setTimeout(() => {
5556
setShowErrorNotification(false);
5657
}, 5000);
@@ -66,8 +67,8 @@ function CreateModelForm({
6667
formData.set("name", newModel.name);
6768
formData.set("api_key", newModel.apiKey);
6869

69-
setNewModel({ name: "", apiKey: "" });
70-
70+
setNewModelName("");
71+
setApiKey("");
7172
setModelsList((oldModelsList: Array<Model>) => {
7273
return [
7374
{
@@ -115,7 +116,7 @@ function CreateModelForm({
115116
<form
116117
onSubmit={(event) => {
117118
event.preventDefault();
118-
mutation.mutate({ name: newModel.name, apiKey: newModel.apiKey });
119+
mutation.mutate({ name: newModelName, apiKey });
119120
}}
120121
style={{ height: "100%" }}
121122
>
@@ -144,14 +145,13 @@ function CreateModelForm({
144145
placeholder="e.g. display-name-123"
145146
label="Name"
146147
help="Name should contain lowercase alphanumeric characters and hyphens only"
147-
value={newModel.name}
148+
value={newModelName}
148149
onChange={(e) => {
149-
const value = e.target.value;
150-
setNewModel({ ...newModel, name: value });
150+
setNewModelName(e.target.value);
151151
}}
152152
error={
153-
checkModelNameExists(newModel.name, modelsList)
154-
? `Model ${newModel.name} already exists`
153+
checkModelNameExists(newModelName, modelsList)
154+
? `Model ${newModelName} already exists`
155155
: null
156156
}
157157
required
@@ -160,21 +160,20 @@ function CreateModelForm({
160160
type="text"
161161
id="api-key-field"
162162
label="API key"
163-
value={newModel.apiKey}
164-
placeholder="yx6dnxsWQ3XUB5gza8idCuMvwmxtk1xBpa9by8TuMit5dgGnv"
163+
value={apiKey}
165164
className="read-only-dark"
166-
readOnly
165+
required
166+
maxLength={50}
167+
onChange={(e) => {
168+
setApiKey(e.target.value);
169+
}}
170+
help={`API key should be 50 alphanumeric characters long (${apiKey.length}/50)`}
167171
/>
168172
<Button
169173
type="button"
170174
className="u-no-margin--bottom"
171175
onClick={() => {
172-
setNewModel({
173-
...newModel,
174-
apiKey: randomstring.generate({
175-
length: 50,
176-
}),
177-
});
176+
setApiKey(randomstring.generate({ length: 50 }));
178177
}}
179178
>
180179
Generate key
@@ -188,7 +187,8 @@ function CreateModelForm({
188187
className="p-button u-no-margin--bottom"
189188
to={`/admin/${id}/models`}
190189
onClick={() => {
191-
setNewModel({ name: "", apiKey: "" });
190+
setNewModelName("");
191+
setApiKey("");
192192
setShowErrorNotification(false);
193193
}}
194194
>
@@ -199,8 +199,9 @@ function CreateModelForm({
199199
appearance="positive"
200200
className="u-no-margin--bottom u-no-margin--right"
201201
disabled={
202-
!newModel.name ||
203-
checkModelNameExists(newModel.name, modelsList)
202+
!newModelName ||
203+
apiKey.length !== 50 ||
204+
checkModelNameExists(newModelName, modelsList)
204205
}
205206
>
206207
Add model

static/js/publisher/pages/Models/__tests__/CreateModelForm.test.tsx

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,24 +30,54 @@ const renderComponent = () => {
3030
};
3131

3232
describe("CreateModelForm", () => {
33-
it("disables 'Add model' button if no new model name", async () => {
33+
it("disables 'Add model' button if no new model name or API key", async () => {
3434
renderComponent();
3535
expect(screen.getByRole("button", { name: "Add model" })).toHaveAttribute(
3636
"aria-disabled",
3737
"true",
3838
);
3939
});
4040

41-
it("enables 'Add model' button if there is a new model name", async () => {
41+
it("disables 'Add model' button if new model name but no API key", async () => {
4242
const user = userEvent.setup();
4343
renderComponent();
4444
await user.type(
4545
screen.getByRole("textbox", { name: "Name" }),
4646
"test-model-name",
4747
);
48+
expect(screen.getByRole("button", { name: "Add model" })).toHaveAttribute(
49+
"aria-disabled",
50+
"true",
51+
);
52+
});
53+
54+
it("disables 'Add model' button if new API key but no new model name", async () => {
55+
const user = userEvent.setup();
56+
renderComponent();
57+
await user.type(
58+
screen.getByRole("textbox", { name: "API key" }),
59+
"AJXdC7aAMrQfElvccQAV0lPkJ0bEnmdjDxuL6C1Di0kxILmiyk",
60+
);
61+
expect(screen.getByRole("button", { name: "Add model" })).toHaveAttribute(
62+
"aria-disabled",
63+
"true",
64+
);
65+
});
66+
67+
it("enables 'Add model' button if there is a new model name and API key", async () => {
68+
const user = userEvent.setup();
69+
renderComponent();
70+
await user.type(
71+
screen.getByRole("textbox", { name: "Name" }),
72+
"test-model-name",
73+
);
74+
await user.type(
75+
screen.getByRole("textbox", { name: "API key" }),
76+
"AJXdC7aAMrQfElvccQAV0lPkJ0bEnmdjDxuL6C1Di0kxILmiyk",
77+
);
4878
expect(
4979
screen.getByRole("button", { name: "Add model" }),
50-
).not.toBeDisabled();
80+
).not.toHaveAttribute("aria-disabled");
5181
});
5282

5383
it("generates an API key when clicking 'Generate key'", async () => {

0 commit comments

Comments
 (0)