forked from MarkUsProject/Markus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathta_table.jsx
More file actions
145 lines (136 loc) · 3.83 KB
/
ta_table.jsx
File metadata and controls
145 lines (136 loc) · 3.83 KB
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import React from "react";
import {createRoot} from "react-dom/client";
import PropTypes from "prop-types";
import ReactTable from "react-table";
import {selectFilter} from "./Helpers/table_helpers";
class TATable extends React.Component {
constructor() {
super();
this.state = {
data: [],
loading: true,
counts: {all: 0, active: 0, inactive: 0},
};
this.fetchData = this.fetchData.bind(this);
}
componentDidMount() {
this.fetchData();
}
fetchData() {
fetch(Routes.course_tas_path(this.props.course_id), {
headers: {
Accept: "application/json",
},
})
.then(response => {
if (response.ok) {
return response.json();
}
})
.then(res => {
this.setState({data: res.data, counts: res.counts, loading: false});
});
}
removeTA = ta_id => {
fetch(Routes.course_ta_path(this.props.course_id, ta_id), {
method: "DELETE",
headers: {
"Content-Type": "application/json",
"X-CSRF-Token": document.querySelector('[name="csrf-token"]').content,
},
})
.then(response => {
if (response.ok) {
this.fetchData();
}
})
.catch(error => {
console.error("Error removing TA:", error);
});
};
render() {
const {data} = this.state;
return (
<ReactTable
data={data}
columns={[
{
Header: I18n.t("activerecord.attributes.user.user_name"),
accessor: "user_name",
},
{
Header: I18n.t("activerecord.attributes.user.first_name"),
accessor: "first_name",
},
{
Header: I18n.t("activerecord.attributes.user.last_name"),
accessor: "last_name",
},
{
Header: I18n.t("activerecord.attributes.user.email"),
accessor: "email",
},
{
Header: I18n.t("roles.active") + "?",
accessor: "hidden",
Cell: ({value}) => (value ? I18n.t("roles.inactive") : I18n.t("roles.active")),
filterMethod: (filter, row) => {
if (filter.value === "all") {
return true;
} else {
return (
(filter.value === "active" && !row[filter.id]) ||
(filter.value === "inactive" && row[filter.id])
);
}
},
Filter: selectFilter,
filterOptions: [
{
value: "active",
text: `${I18n.t("roles.active")} (${this.state.counts.active})`,
},
{
value: "inactive",
text: `${I18n.t("roles.inactive")} (${this.state.counts.inactive})`,
},
],
filterAllOptionText: `${I18n.t("all")} (${this.state.counts.all})`,
},
{
Header: I18n.t("actions"),
accessor: "id",
Cell: data => (
<>
<span>
<a href={Routes.edit_course_ta_path(this.props.course_id, data.value)}>
{I18n.t("edit")}
</a>
</span>
|
<span>
<a href="#" onClick={() => this.removeTA(data.value)}>
{I18n.t("remove")}
</a>
</span>
</>
),
filterable: false,
sortable: false,
},
]}
filterable
loading={this.state.loading}
noDataText={I18n.t("tas.empty_table")}
/>
);
}
}
TATable.propTypes = {
course_id: PropTypes.number,
};
function makeTATable(elem, props) {
const root = createRoot(elem);
root.render(<TATable {...props} />);
}
export {makeTATable, TATable};