Skip to content

Commit 788de88

Browse files
committed
Update template notification methods
1 parent 7227a42 commit 788de88

File tree

11 files changed

+318
-314
lines changed

11 files changed

+318
-314
lines changed

coderd/apidoc/docs.go

Lines changed: 23 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

coderd/apidoc/swagger.json

Lines changed: 15 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/api/notifications.md

Lines changed: 20 additions & 12 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

docs/api/schemas.md

Lines changed: 35 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

enterprise/coderd/notifications.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ func (api *API) updateNotificationTemplateMethod(rw http.ResponseWriter, r *http
4242
}
4343

4444
var nm database.NullNotificationMethod
45-
if err := nm.Scan(req.Method); err != nil || !nm.Valid || !nm.NotificationMethod.Valid() {
45+
if err := nm.Scan(string(req.Method)); err != nil || !nm.Valid || !nm.NotificationMethod.Valid() {
4646
vals := database.AllNotificationMethodValues()
4747
acceptable := make([]string, len(vals))
4848
for i, v := range vals {

site/src/api/api.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2020,6 +2020,24 @@ class ApiMethods {
20202020
);
20212021
return res.data;
20222022
};
2023+
2024+
getNotificationDispatchMethods = async () => {
2025+
const res = await this.axios.get<TypesGen.NotificationMethodsResponse>(
2026+
`/api/v2/notifications/dispatch-methods`,
2027+
);
2028+
return res.data;
2029+
};
2030+
2031+
updateNotificationTemplateMethod = async (
2032+
templateId: string,
2033+
req: TypesGen.UpdateNotificationTemplateMethod,
2034+
) => {
2035+
const res = await this.axios.put<void>(
2036+
`/api/v2/notifications/templates/${templateId}/method`,
2037+
req,
2038+
);
2039+
return res.data;
2040+
};
20232041
}
20242042

20252043
// This is a hard coded CSRF token/cookie pair for local development. In prod,

site/src/api/queries/notifications.ts

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ import type { QueryClient, UseMutationOptions } from "react-query";
22
import { API } from "api/api";
33
import type {
44
NotificationPreference,
5+
NotificationTemplate,
6+
UpdateNotificationTemplateMethod,
57
UpdateUserNotificationPreferences,
68
} from "api/typesGenerated";
79

@@ -53,3 +55,33 @@ export const systemNotificationTemplatesByGroup = () => {
5355
queryFn: () => API.getSystemNotificationTemplates(),
5456
};
5557
};
58+
59+
export function selectTemplatesByGroup(
60+
data: NotificationTemplate[],
61+
): Record<string, NotificationTemplate[]> {
62+
return data.reduce(
63+
(acc, tpl) => {
64+
if (!acc[tpl.group]) {
65+
acc[tpl.group] = [];
66+
}
67+
acc[tpl.group].push(tpl);
68+
return acc;
69+
},
70+
{} as Record<string, NotificationTemplate[]>,
71+
);
72+
}
73+
74+
export const notificationDispatchMethods = () => {
75+
return {
76+
staleTime: Infinity,
77+
queryKey: ["notifications", "dispatchMethods"],
78+
queryFn: () => API.getNotificationDispatchMethods(),
79+
};
80+
};
81+
82+
export const updateNotificationTemplateMethod = (templateId: string) => {
83+
return {
84+
mutationFn: (req: UpdateNotificationTemplateMethod) =>
85+
API.updateNotificationTemplateMethod(templateId, req),
86+
};
87+
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import EmailIcon from "@mui/icons-material/EmailOutlined";
2+
import DeploymentIcon from "@mui/icons-material/LanguageOutlined";
3+
import WebhookIcon from "@mui/icons-material/WebhookOutlined";
4+
5+
export const methodIcons: Record<string, typeof EmailIcon> = {
6+
"": DeploymentIcon,
7+
smtp: EmailIcon,
8+
webhook: WebhookIcon,
9+
};
10+
11+
const methodLabels: Record<string, string> = {
12+
"": "Default",
13+
smtp: "SMTP",
14+
webhook: "Webhook",
15+
};
16+
17+
export const methodLabel = (method: string, defaultMethod?: string) => {
18+
return method === "" && defaultMethod
19+
? `${methodLabels[method]} - ${methodLabels[defaultMethod]}`
20+
: methodLabels[method];
21+
};

0 commit comments

Comments
 (0)