-
Notifications
You must be signed in to change notification settings - Fork 252
Users table refactor #7598
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Users table refactor #7598
Changes from all commits
d422456
56494d7
eb56394
4777218
a18cbfe
e9f6d90
48d6c81
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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"; | ||
| 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> | ||
| | | ||
| <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", | ||
| }, | ||
|
|
@@ -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) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 => { | ||
|
|
@@ -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})`, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
| }, | ||
| { | ||
| 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> | ||
| | | ||
| <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")} /> | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Note that the |
||
| ); | ||
| } | ||
| } | ||
|
|
||
There was a problem hiding this comment.
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.