Convert redux state access to hooks

This commit is contained in:
Nathan Barber
2021-04-08 17:21:02 -04:00
parent c7dcb4db85
commit 21f4988f24
14 changed files with 4106 additions and 4891 deletions

View File

@@ -1,21 +0,0 @@
import { connect } from "react-redux";
import { compose, withProps } from "recompose";
import { jhapiRequest } from "../../util/jhapiUtil";
import AddUser from "./AddUser.pre";
const withUserAPI = withProps((props) => ({
addUsers: (usernames, admin) =>
jhapiRequest("/users", "POST", { usernames, admin }),
failRegexEvent: () =>
alert(
"Removed " +
JSON.stringify(removed_users) +
" for either containing special characters or being too short."
),
refreshUserData: () =>
jhapiRequest("/users", "GET")
.then((data) => data.json())
.then((data) => props.dispatch({ type: "USER_DATA", value: data })),
}));
export default compose(connect(), withUserAPI)(AddUser);

View File

@@ -1,11 +1,23 @@
import React, { useState } from "react";
import { useDispatch } from "react-redux";
import { compose, withProps } from "recompose";
import { Link } from "react-router-dom";
import PropTypes from "prop-types";
import { jhapiRequest } from "../../util/jhapiUtil";
const AddUser = (props) => {
var [users, setUsers] = useState([]),
[admin, setAdmin] = useState(false);
var dispatch = useDispatch();
var dispatchUserData = (data) => {
dispatch({
type: "USER_DATA",
value: data,
});
};
var { addUsers, failRegexEvent, refreshUserData, history } = props;
return (
@@ -66,7 +78,11 @@ const AddUser = (props) => {
}
addUsers(filtered_users, admin)
.then(() => refreshUserData())
.then(
refreshUserData()
.then((data) => dispatchUserData(data))
.catch((err) => console.log(err))
)
.then(() => history.push("/"))
.catch((err) => console.log(err));
}}
@@ -91,4 +107,17 @@ AddUser.propTypes = {
}),
};
export default AddUser;
const withUserAPI = withProps((props) => ({
addUsers: (usernames, admin) =>
jhapiRequest("/users", "POST", { usernames, admin }),
failRegexEvent: () =>
alert(
"Removed " +
JSON.stringify(removed_users) +
" for either containing special characters or being too short."
),
refreshUserData: () =>
jhapiRequest("/users", "GET").then((data) => data.json()),
}));
export default compose(withUserAPI)(AddUser);