CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutSign UpSign In
sagemathinc

Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place.

GitHub Repository: sagemathinc/cocalc
Path: blob/master/src/packages/frontend/admin/users/actions.ts
Views: 687
1
/*
2
* This file is part of CoCalc: Copyright © 2020 Sagemath, Inc.
3
* License: MS-RSL – see LICENSE.md for details
4
*/
5
6
import { fromJS, List } from "immutable";
7
8
import { Actions, redux } from "../../app-framework";
9
import { user_search, User } from "../../frame-editors/generic/client";
10
import { sortBy } from "lodash";
11
import { StoreState, User as ImmutableUser, store } from "./store";
12
13
function user_sort_key(user: User): number {
14
if (user.last_active) {
15
return -user.last_active;
16
}
17
if (user.created) {
18
return -user.created;
19
}
20
return 0;
21
}
22
23
export class AdminUsersActions extends Actions<StoreState> {
24
public set_query(query: string): void {
25
this.setState({ query: query });
26
}
27
28
public clear_status(): void {
29
this.setState({ status: "" });
30
}
31
32
public set_status(status: string): void {
33
this.setState({ status: status });
34
}
35
36
public async search(): Promise<void> {
37
this.set_status("Searching...");
38
39
let result;
40
try {
41
result = await user_search({
42
query: store.get("query").trim().toLowerCase(), // backend assumes lower case
43
admin: true,
44
limit: 100,
45
});
46
} catch (err) {
47
this.set_status(`ERROR -- ${err}`);
48
return;
49
}
50
if (result == null) {
51
this.set_status("ERROR");
52
return;
53
}
54
55
const result_sorted = sortBy(result, user_sort_key);
56
this.set_status("");
57
58
this.setState({
59
result: fromJS(result_sorted) as unknown as List<ImmutableUser>,
60
});
61
}
62
63
public set_view(view: boolean): void {
64
this.setState({ view });
65
}
66
}
67
68
// The ?? is just to support hot module reload.
69
export const actions =
70
redux.getActions("admin-users") ??
71
redux.createActions("admin-users", AdminUsersActions);
72
73