Make GroupEdit functional

This commit is contained in:
Nathan Barber
2021-04-07 12:04:11 -04:00
parent 0f5e86ff06
commit 816eeeb2fc
4 changed files with 119 additions and 127 deletions

File diff suppressed because one or more lines are too long

View File

@@ -1,7 +1,7 @@
import { connect } from "react-redux"; import { connect } from "react-redux";
import { compose, withProps } from "recompose"; import { compose, withProps } from "recompose";
import { jhapiRequest } from "../../util/jhapiUtil"; import { jhapiRequest } from "../../util/jhapiUtil";
import { GroupEdit } from "./GroupEdit.pre"; import GroupEdit from "./GroupEdit.pre";
const withGroupsAPI = withProps((props) => ({ const withGroupsAPI = withProps((props) => ({
addToGroup: (users, groupname) => addToGroup: (users, groupname) =>

View File

@@ -1,51 +1,29 @@
import React, { Component } from "react"; import React, { Component, useState } from "react";
import { Link } from "react-router-dom"; import { Link } from "react-router-dom";
import Multiselect from "../Multiselect/Multiselect"; import Multiselect from "../Multiselect/Multiselect";
import PropTypes from "prop-types"; import PropTypes from "prop-types";
export class GroupEdit extends Component { const GroupEdit = (props) => {
static get propTypes() { var [selected, setSelected] = useState([]),
return { [changed, setChanged] = useState(false),
location: PropTypes.shape({ [added, setAdded] = useState(undefined),
state: PropTypes.shape({ [removed, setRemoved] = useState(undefined);
group_data: PropTypes.object,
user_data: PropTypes.array,
callback: PropTypes.func,
}),
}),
history: PropTypes.shape({
push: PropTypes.func,
}),
addToGroup: PropTypes.func,
removeFromGroup: PropTypes.func,
deleteGroup: PropTypes.func,
};
}
constructor(props) {
super(props);
this.state = {
selected: [],
changed: false,
added: undefined,
removed: undefined,
};
}
render() {
if (!this.props.location.state) {
this.props.history.push("/groups");
return <></>;
}
var { group_data, user_data, callback } = this.props.location.state;
var { var {
addToGroup, addToGroup,
removeFromGroup, removeFromGroup,
deleteGroup, deleteGroup,
refreshGroupsData, refreshGroupsData,
} = this.props; history,
location,
} = props;
if (!location.state) {
history.push("/groups");
return <></>;
}
var { group_data, user_data, callback } = location.state;
if (!(group_data && user_data)) return <div></div>; if (!(group_data && user_data)) return <div></div>;
@@ -60,7 +38,8 @@ export class GroupEdit extends Component {
options={user_data.map((e) => e.name)} options={user_data.map((e) => e.name)}
value={group_data.users} value={group_data.users}
onChange={(selection, options) => { onChange={(selection, options) => {
this.setState({ selected: selection, changed: true }); setSelected(selection);
setChanged(true);
}} }}
/> />
<br></br> <br></br>
@@ -73,24 +52,20 @@ export class GroupEdit extends Component {
className="btn btn-primary" className="btn btn-primary"
onClick={() => { onClick={() => {
// check for changes // check for changes
if (!this.state.changed) { if (!changed) {
this.props.history.push("/groups"); history.push("/groups");
return; return;
} }
let new_users = this.state.selected.filter( let new_users = selected.filter(
(e) => !group_data.users.includes(e) (e) => !group_data.users.includes(e)
); );
let removed_users = group_data.users.filter( let removed_users = group_data.users.filter(
(e) => !this.state.selected.includes(e) (e) => !selected.includes(e)
); );
this.setState( setAdded(new_users);
Object.assign({}, this.state, { setRemoved(removed_users);
added: new_users,
removed: removed_users,
})
);
let promiseQueue = []; let promiseQueue = [];
if (new_users.length > 0) if (new_users.length > 0)
@@ -104,7 +79,7 @@ export class GroupEdit extends Component {
.then((e) => callback()) .then((e) => callback())
.catch((err) => console.log(err)); .catch((err) => console.log(err));
this.props.history.push("/groups"); history.push("/groups");
}} }}
> >
Apply Apply
@@ -116,7 +91,7 @@ export class GroupEdit extends Component {
var groupName = group_data.name; var groupName = group_data.name;
deleteGroup(groupName) deleteGroup(groupName)
.then(refreshGroupsData()) .then(refreshGroupsData())
.then(this.props.history.push("/groups")) .then(history.push("/groups"))
.catch((err) => console.log(err)); .catch((err) => console.log(err));
}} }}
> >
@@ -128,5 +103,22 @@ export class GroupEdit extends Component {
</div> </div>
</div> </div>
); );
} };
}
GroupEdit.propTypes = {
location: PropTypes.shape({
state: PropTypes.shape({
group_data: PropTypes.object,
user_data: PropTypes.array,
callback: PropTypes.func,
}),
}),
history: PropTypes.shape({
push: PropTypes.func,
}),
addToGroup: PropTypes.func,
removeFromGroup: PropTypes.func,
deleteGroup: PropTypes.func,
};
export default GroupEdit;

File diff suppressed because one or more lines are too long