-
Notifications
You must be signed in to change notification settings - Fork 0
/
integrateDatabase.ts
86 lines (71 loc) · 2.22 KB
/
integrateDatabase.ts
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
import { SurfaceRoute, UIElementInputSelectOptions } from "@netlify/sdk";
import { UpstashRedisDatabase } from "../../index";
const route = new SurfaceRoute("/integrate-database");
route.onLoad(async (state) => {
const { fetch, picker } = state;
const databasesResponse = await fetch("get-databases");
const databases = (await databasesResponse.json()) as UpstashRedisDatabase[];
const databasePicker = picker.getFormElementById<UIElementInputSelectOptions>(
"integrate-form",
"upstash-database",
);
if (databasePicker) {
databasePicker.options = databases.map((database) => ({
label: database.database_name,
value: database.database_id,
}));
}
});
route.addSection(
{
id: "integratate-section",
description: "",
title: "Easily configure your Upstash account",
},
(section) => {
section.addForm(
{
id: "integrate-form",
title: "Select your Redis database",
savingLabel: "Integrating",
saveLabel: "Integrate",
hideCancel: true,
onSubmit: async (state) => {
const { fetch, picker, integrationNavigation } = state;
const databaseId = picker.getFormInputValue(
"integrate-form",
"upstash-database",
);
const databasesResponse = await fetch("get-databases");
const databases =
(await databasesResponse.json()) as UpstashRedisDatabase[];
const database = databases.find(
(database) => database.database_id === databaseId,
);
if (!database) {
throw new Error("Database not found");
}
const integrateResponse = await fetch("integrate", {
method: "POST",
body: JSON.stringify({
databaseId,
databaseName: database.database_name,
}),
});
if (integrateResponse.status === 200) {
integrationNavigation.navigateTo("/");
} else {
throw new Error("Something went wrong");
}
},
},
(card) => {
card.addInputSelect({
id: "upstash-database",
label: "Upstash Database",
});
},
);
},
);
export default route;