Skip to content

Commit a66697a

Browse files
committed
convert system notifications to modern functional react component
1 parent fcd2084 commit a66697a

File tree

1 file changed

+43
-68
lines changed

1 file changed

+43
-68
lines changed

src/packages/frontend/admin/system-notifications.tsx

Lines changed: 43 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -5,47 +5,28 @@
55

66
import { Map } from "immutable";
77
import { Alert, Button, Card, Input, Popconfirm, Space } from "antd";
8-
import {
9-
Component,
10-
rclass,
11-
redux,
12-
rtypes,
13-
} from "@cocalc/frontend/app-framework";
148
import { Icon, Loading, Title } from "@cocalc/frontend/components";
159
import { plural } from "@cocalc/util/misc";
10+
import { useState } from "react";
11+
import { useActions, useRedux } from "@cocalc/frontend/app-framework";
1612

17-
interface Props {
18-
notifications?: Map<string, any>;
19-
}
20-
21-
interface State {
22-
state: "view" | "edit";
23-
mesg?: string;
24-
}
13+
export function SystemNotifications({}) {
14+
const [state, setState] = useState<"view" | "edit">("view");
15+
const [mesg, setMesg] = useState<string>("");
16+
const notifications = useRedux("system_notifications", "notifications");
17+
const actions = useActions("system_notifications");
2518

26-
class SystemNotifications extends Component<Props, State> {
27-
constructor(props, state) {
28-
super(props, state);
29-
this.state = { state: "view" };
30-
}
31-
32-
static reduxProps(): any {
33-
return {
34-
system_notifications: { notifications: rtypes.immutable },
35-
};
36-
}
37-
38-
render_mark_done() {
39-
if (!this.props.notifications) return;
19+
function render_mark_done() {
20+
if (!notifications) return;
4021
let open = 0;
41-
this.props.notifications.map((mesg: Map<string, any>) => {
22+
notifications.map((mesg: Map<string, any>) => {
4223
if (mesg && !mesg.get("done")) {
4324
open += 1;
4425
}
4526
});
4627
if (open > 0) {
4728
return (
48-
<Button onClick={() => this.mark_all_done()}>
29+
<Button onClick={() => mark_all_done()}>
4930
Mark {open} {plural(open, "Notification")} Done
5031
</Button>
5132
);
@@ -54,34 +35,33 @@ class SystemNotifications extends Component<Props, State> {
5435
}
5536
}
5637

57-
render_buttons() {
38+
function render_buttons() {
5839
return (
5940
<Space>
60-
<Button onClick={() => this.setState({ state: "edit", mesg: "" })}>
41+
<Button
42+
onClick={() => {
43+
setState("edit");
44+
setMesg("");
45+
}}
46+
>
6147
Compose...
6248
</Button>
63-
{this.render_mark_done()}
49+
{render_mark_done()}
6450
</Space>
6551
);
6652
}
6753

68-
render_editor() {
54+
function render_editor() {
6955
return (
7056
<Card>
7157
<Input.TextArea
7258
autoFocus
73-
value={this.state.mesg}
59+
value={mesg}
7460
rows={3}
75-
onChange={(e) =>
76-
this.setState({
77-
mesg: e.target.value,
78-
})
79-
}
61+
onChange={(e) => setMesg(e.target.value)}
8062
/>
8163
<Space style={{ marginTop: "15px" }}>
82-
<Button onClick={() => this.setState({ state: "view" })}>
83-
Cancel
84-
</Button>
64+
<Button onClick={() => setState("view")}>Cancel</Button>
8565
<Popconfirm
8666
title="Send notification?"
8767
description={
@@ -90,11 +70,11 @@ class SystemNotifications extends Component<Props, State> {
9070
once in the upper right until you explicitly mark it done (they
9171
can dismiss it).
9272
<hr />
93-
<Alert message={this.state.mesg} />
73+
<Alert message={mesg} />
9474
</div>
9575
}
9676
onConfirm={() => {
97-
this.send();
77+
send();
9878
}}
9979
>
10080
<Button danger>
@@ -106,40 +86,35 @@ class SystemNotifications extends Component<Props, State> {
10686
);
10787
}
10888

109-
send(): void {
110-
this.setState({ state: "view" });
111-
if (!this.state.mesg) return;
112-
(redux.getActions("system_notifications") as any).send_message({
113-
text: this.state.mesg.trim(),
89+
function send(): void {
90+
setState("view");
91+
if (!mesg) return;
92+
actions.send_message({
93+
text: mesg.trim(),
11494
priority: "high",
11595
});
11696
}
11797

118-
mark_all_done(): void {
119-
(redux.getActions("system_notifications") as any).mark_all_done();
98+
function mark_all_done(): void {
99+
actions.mark_all_done();
120100
}
121101

122-
render_body() {
123-
if (this.props.notifications == null) {
102+
function render_body() {
103+
if (notifications == null) {
124104
return <Loading />;
125105
}
126-
switch (this.state.state) {
106+
switch (state) {
127107
case "view":
128-
return this.render_buttons();
108+
return render_buttons();
129109
case "edit":
130-
return this.render_editor();
110+
return render_editor();
131111
}
132112
}
133113

134-
render() {
135-
return (
136-
<div>
137-
<Title level={4}>System Notifications</Title>
138-
{this.render_body()}
139-
</div>
140-
);
141-
}
114+
return (
115+
<div>
116+
<Title level={4}>System Notifications</Title>
117+
{render_body()}
118+
</div>
119+
);
142120
}
143-
144-
const SystemNotifications0 = rclass(SystemNotifications);
145-
export { SystemNotifications0 as SystemNotifications };

0 commit comments

Comments
 (0)