Skip to content

Commit 3a62d53

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

2 files changed

Lines changed: 59 additions & 27 deletions

File tree

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

Lines changed: 26 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 {
@@ -29,11 +28,13 @@ function CreateModelForm({
2928
setShowNotification,
3029
setShowErrorNotification,
3130
}: Props): React.JSX.Element {
31+
const API_KEY_LENGTH = 50;
3232
const navigate = useNavigate();
3333
const location = useLocation();
3434
const { id } = useParams();
3535
const brandId = useAtomValue(brandIdState);
36-
const [newModel, setNewModel] = useAtom(newModelState);
36+
const [newModelName, setNewModelName] = useState("");
37+
const [apiKey, setApiKey] = useState("");
3738
const stores = useAtom(brandStoresState);
3839
const currentStore = stores[0].find((store: Store) => store.id === id);
3940
const modelsList = useAtomValue(filteredModelsListState);
@@ -47,10 +48,11 @@ function CreateModelForm({
4748
setShowErrorNotification(true);
4849
setIsSaving(false);
4950
setModelsList((oldModelsList: Array<Model>) => {
50-
return oldModelsList.filter((model) => model.name !== newModel.name);
51+
return oldModelsList.filter((model) => model.name !== newModelName);
5152
});
5253
navigate(`/admin/${id}/models`);
53-
setNewModel({ name: "", apiKey: "" });
54+
setNewModelName("");
55+
setApiKey("");
5456
setTimeout(() => {
5557
setShowErrorNotification(false);
5658
}, 5000);
@@ -66,8 +68,8 @@ function CreateModelForm({
6668
formData.set("name", newModel.name);
6769
formData.set("api_key", newModel.apiKey);
6870

69-
setNewModel({ name: "", apiKey: "" });
70-
71+
setNewModelName("");
72+
setApiKey("");
7173
setModelsList((oldModelsList: Array<Model>) => {
7274
return [
7375
{
@@ -115,7 +117,7 @@ function CreateModelForm({
115117
<form
116118
onSubmit={(event) => {
117119
event.preventDefault();
118-
mutation.mutate({ name: newModel.name, apiKey: newModel.apiKey });
120+
mutation.mutate({ name: newModelName, apiKey });
119121
}}
120122
style={{ height: "100%" }}
121123
>
@@ -144,14 +146,13 @@ function CreateModelForm({
144146
placeholder="e.g. display-name-123"
145147
label="Name"
146148
help="Name should contain lowercase alphanumeric characters and hyphens only"
147-
value={newModel.name}
149+
value={newModelName}
148150
onChange={(e) => {
149-
const value = e.target.value;
150-
setNewModel({ ...newModel, name: value });
151+
setNewModelName(e.target.value);
151152
}}
152153
error={
153-
checkModelNameExists(newModel.name, modelsList)
154-
? `Model ${newModel.name} already exists`
154+
checkModelNameExists(newModelName, modelsList)
155+
? `Model ${newModelName} already exists`
155156
: null
156157
}
157158
required
@@ -160,21 +161,20 @@ function CreateModelForm({
160161
type="text"
161162
id="api-key-field"
162163
label="API key"
163-
value={newModel.apiKey}
164-
placeholder="yx6dnxsWQ3XUB5gza8idCuMvwmxtk1xBpa9by8TuMit5dgGnv"
164+
value={apiKey}
165165
className="read-only-dark"
166-
readOnly
166+
required
167+
maxLength={API_KEY_LENGTH}
168+
onChange={(e) => {
169+
setApiKey(e.target.value);
170+
}}
171+
help={`API key should be exactly ${API_KEY_LENGTH} alphanumeric characters long (${apiKey.length}/${API_KEY_LENGTH})`}
167172
/>
168173
<Button
169174
type="button"
170175
className="u-no-margin--bottom"
171176
onClick={() => {
172-
setNewModel({
173-
...newModel,
174-
apiKey: randomstring.generate({
175-
length: 50,
176-
}),
177-
});
177+
setApiKey(randomstring.generate({ length: API_KEY_LENGTH }));
178178
}}
179179
>
180180
Generate key
@@ -188,7 +188,8 @@ function CreateModelForm({
188188
className="p-button u-no-margin--bottom"
189189
to={`/admin/${id}/models`}
190190
onClick={() => {
191-
setNewModel({ name: "", apiKey: "" });
191+
setNewModelName("");
192+
setApiKey("");
192193
setShowErrorNotification(false);
193194
}}
194195
>
@@ -199,8 +200,9 @@ function CreateModelForm({
199200
appearance="positive"
200201
className="u-no-margin--bottom u-no-margin--right"
201202
disabled={
202-
!newModel.name ||
203-
checkModelNameExists(newModel.name, modelsList)
203+
!newModelName ||
204+
apiKey.length !== API_KEY_LENGTH ||
205+
checkModelNameExists(newModelName, modelsList)
204206
}
205207
>
206208
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)