Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
- Refactor `DropDownMenu` class to a new React component (#7592)
- Updated the tag table and "one time only annotations" table to use `@tanstack/react-table` v8 (#7596)
- Updated Python autotest script file to include example of using `pytest.mark` to customize test marks (#7597)
- Refactor `Grader` table in `Users` to use `@tanstack/react-table` v8 (#7598)

## [v2.7.1]

Expand Down
151 changes: 61 additions & 90 deletions app/javascript/Components/ta_table.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,28 +2,77 @@ 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";
import {faPencil, faTrashCan} from "@fortawesome/free-solid-svg-icons";
import {FontAwesomeIcon} from "@fortawesome/react-fontawesome";

import Table from "./table/table";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is good, but there are now unused imports that you can remove.

import {createColumnHelper} from "@tanstack/react-table";

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);

const columnHelper = createColumnHelper();
this.columns = [
columnHelper.accessor("user_name", {
header: () => I18n.t("activerecord.attributes.user.user_name"),
}),
columnHelper.accessor("first_name", {
header: () => I18n.t("activerecord.attributes.user.first_name"),
}),
columnHelper.accessor("last_name", {
header: () => I18n.t("activerecord.attributes.user.last_name"),
}),
columnHelper.accessor("email", {
header: () => I18n.t("activerecord.attributes.user.email"),
}),
columnHelper.accessor("hidden", {
header: () => I18n.t("roles.active") + "?",
filterFn: "equalsString",
meta: {
filterVariant: "select",
},
}),
columnHelper.accessor("id", {
id: "id",
enableSorting: false,
enableColumnFilter: false,
header: () => I18n.t("actions"),
cell: props => (
<span>
<a
href={Routes.edit_course_ta_path(this.props.course_id, props.getValue())}
aria-label={I18n.t("edit")}
title={I18n.t("edit")}
>
<FontAwesomeIcon icon={faPencil} />
</a>
&nbsp;|&nbsp;
<a
href="#"
onClick={() => this.removeTA(props.getValue())}
aria-label={I18n.t("remove")}
title={I18n.t("remove")}
>
<FontAwesomeIcon icon={faTrashCan} />
</a>
</span>
),
}),
];
}

componentDidMount() {
this.fetchData();
this.fetchData().then(data => this.setState({data: this.processData(data)}));
}

fetchData() {
fetch(Routes.course_tas_path(this.props.course_id), {
return fetch(Routes.course_tas_path(this.props.course_id), {
headers: {
Accept: "application/json",
},
Expand All @@ -33,9 +82,12 @@ class TATable extends React.Component {
return response.json();
}
})
.then(res => {
this.setState({data: res.data, counts: res.counts, loading: false});
});
.then(json => json.data);
}

processData(data) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a blank line before this method

data.forEach(row => (row.hidden = I18n.t(row.hidden ? "roles.inactive" : "roles.active")));
return data;
}

removeTA = ta_id => {
Expand All @@ -57,89 +109,8 @@ class TATable extends React.Component {
};

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})`,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@chickenwaddle77 one other thing I forgot to mention: because this part is being deleted, you can also get rid of the counts state attribute, which is no longer used.

},
{
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)}
aria-label={I18n.t("edit")}
title={I18n.t("edit")}
>
<FontAwesomeIcon icon={faPencil} />
</a>
</span>
&nbsp;|&nbsp;
<span>
<a
href="#"
onClick={() => this.removeTA(data.value)}
aria-label={I18n.t("remove")}
title={I18n.t("remove")}
>
<FontAwesomeIcon icon={faTrashCan} />
</a>
</span>
</>
),
filterable: false,
sortable: false,
},
]}
filterable
loading={this.state.loading}
noDataText={I18n.t("tas.empty_table")}
/>
<Table data={this.state.data} columns={this.columns} noDataText={I18n.t("tas.empty_table")} />
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the data variable is defined immediately above this, so you can either delete that variable or just pass in data here.

);
}
}
Expand Down