From 0f28d966ada52c91f9d2a45a6a4186e8fe6e97d1 Mon Sep 17 00:00:00 2001 From: Carlos Wu Fei Date: Sun, 10 Sep 2023 15:49:27 +0100 Subject: [PATCH] Clean up Web application --- web/src/pages/users/UserForm.jsx | 143 ------------------------ web/src/pages/users/Users.jsx | 154 -------------------------- web/src/pages/users/actions.js | 184 ------------------------------- web/src/pages/users/reducer.js | 103 ----------------- web/src/pages/users/saga.js | 85 -------------- web/src/reducer.js | 2 - web/src/router/routes.js | 10 -- web/src/sagas.js | 9 -- 8 files changed, 690 deletions(-) delete mode 100644 web/src/pages/users/UserForm.jsx delete mode 100644 web/src/pages/users/Users.jsx delete mode 100644 web/src/pages/users/actions.js delete mode 100644 web/src/pages/users/reducer.js delete mode 100644 web/src/pages/users/saga.js diff --git a/web/src/pages/users/UserForm.jsx b/web/src/pages/users/UserForm.jsx deleted file mode 100644 index 7b3d99380..000000000 --- a/web/src/pages/users/UserForm.jsx +++ /dev/null @@ -1,143 +0,0 @@ -import React, { useEffect, useRef, useState } from "react"; -import { Button, Col, Form, FormFeedback, FormGroup, Input, Label, Row } from "reactstrap"; -import { checkValue } from "../../validations"; - -const UserForm = ({ currentUser, handleSubmit }) => { - let mounted = useRef(); - const [id, setId] = useState(null) - const [email, setEmail] = useState(""); - const [emailError, setEmailError] = useState(""); - const [username, setUsername] = useState(""); - const [password, setPassword] = useState(""); - const [passwordError, setPasswordError] = useState(""); - const [about, setAbout] = useState(""); - - useEffect(() => { - if (!mounted.current) { - // do componentDidMount logic - - mounted.current = true; - } else { - if (!checkValue(currentUser) && currentUser.id !== id) { - setEmail(currentUser.email); - setUsername(currentUser.username); - setPassword(currentUser.password); - setAbout(currentUser.about); - setId(currentUser.id) - } - } - }, [currentUser, id]); - - const validateFields = () => { - if (checkValue(email)) { - setEmailError("Email is required!") - } - if (checkValue(password)) { - setPasswordError("Password is required!") - } - return false - } - - const onSubmit = (e) => { - e.preventDefault(); - validateFields(); - const submitForm = { - id: currentUser?.id, - email: email, - password: password, - username: username || "", - description: about || "", - } - handleSubmit(submitForm) - } - - return ( -
- - - - - setEmail(e.target.value)} - /> - {!checkValue(emailError) && ( - - Email is required - - )} - - - - - - { - setUsername(e.target.value); - }} - /> - - - - - - - - setPassword(e.target.value)} - /> - {!checkValue(passwordError) && ( - - Password is required - - )} - - - - - - - - - - - - - - setAbout(e.target.value)} - /> - - - - -
- -
-
-
- ); -}; - -export default UserForm; diff --git a/web/src/pages/users/Users.jsx b/web/src/pages/users/Users.jsx deleted file mode 100644 index 6721674de..000000000 --- a/web/src/pages/users/Users.jsx +++ /dev/null @@ -1,154 +0,0 @@ -import moment from "moment"; -import React from "react"; -import { connect } from "react-redux"; -import { - Button, - Card, - CardBody, - CardHeader, - CardTitle, - Col, - Row, -} from "reactstrap"; -import { getToken } from "../../request"; -import { checkValue } from "../../validations"; -import { getUsers, registerUser, editUser, deleteUser } from "./actions"; -import UserForm from "./UserForm"; - -class Users extends React.Component { - constructor(props) { - super(props); - this.state = { - currentUser: props.currentUser, - }; - } - - componentDidMount = () => { - this.props.getUsers(); - }; - - componentDidUpdate = (props, state) => { - if ( - !checkValue(this.props.currentUser) && - props.currentUser !== this.props.currentUser - ) { - this.setState({ - currentUser: this.props.currentUser, - }); - } - }; - - handleSubmit = (userData) => { - if (checkValue(userData.id)) { - this.props.registerUser(userData); - } else { - this.props.editUser(userData); - } - }; - - editUser = (id) => { - this.setState({ - currentUser: this.props.users.find((x) => x.id === id), - }); - }; - - deleteUser = (id) => { - this.props.deleteUser(id); - }; - - render() { - return ( - <> -
- - - - - Users list - - -
    - {this.props.users && - this.props.users.map((x, i) => ( -
  • - - {x.email} - {!checkValue(x.last_login) && ( - {moment(x.last_login.$date).fromNow()} - )} - {x.username} - - {" "} - - - -
  • - ))} -
-
-
- - - - - Edit user - - - {!checkValue(this.state.currentUser) && ( - - )} - - - -
- - - - - Register new user - - - - - - - -
- - ); - } -} - -const mapStateToProps = (s) => { - const { users } = s.usersReducer; - const token = getToken(); - let currentUser = null; - if (users) { - currentUser = users.find((x) => x.access_token === token); - } - - return { - users: users, - currentUser: currentUser, - }; -}; - -export default connect(mapStateToProps, { - getUsers, - registerUser, - editUser, - deleteUser, -})(Users); diff --git a/web/src/pages/users/actions.js b/web/src/pages/users/actions.js deleted file mode 100644 index 5361a48b4..000000000 --- a/web/src/pages/users/actions.js +++ /dev/null @@ -1,184 +0,0 @@ -import { addNotification } from "../../validations"; - -export const GET_USERS = "GET_USERS"; -export const GET_USERS_SUCCESS = "GET_USERS_SUCCESS"; -export const GET_USERS_ERROR = "GET_USERS_ERROR"; - -export const REGISTER_USER = "REGISTER_USER"; -export const REGISTER_USER_SUCCESS = "REGISTER_USER_SUCCESS"; -export const REGISTER_USER_ERROR = "REGISTER_USER_ERROR"; -export const EDIT_USER = "EDIT_USER"; -export const EDIT_USER_SUCCESS = "EDIT_USER_SUCCESS"; -export const EDIT_USER_ERROR = "EDIT_USER_ERROR"; -export const DELETE_USER = "DELETE_USER"; -export const DELETE_USER_SUCCESS = "DELETE_USER_SUCCESS"; -export const DELETE_USER_ERROR = "DELETE_USER_ERROR"; - -/** - * Retrieve list of users - * Optional id - * @return {object} An action object with a type of BOT - */ -export function getUsers(id = null) { - return { - type: GET_USERS, - id, - }; -} - -/** - * Retrieve list of users successful state - * - * @param {array} response object with users in the data property - * @return {object} redux state and payload - */ -export function getUsersSucceded(res) { - return { - type: GET_USERS_SUCCESS, - users: res.data, - message: res.message, - }; -} - -/** - * Network error, bad request errors, binance errors - * @param {object} { "error": 1, "message": "Failed to retrieve" } - */ -export function getUsersFailed(res) { - return { - type: GET_USERS_ERROR, - message: res.message, - error: res.error, - }; -} - -/** - * Edit an existent user - * @return {object} with id required, returned to the saga to dispatch the API call - */ -export function editUser(payload) { - return { - type: EDIT_USER, - data: payload, - id: payload.id - }; -} - -/** - * Edit user successful state, nothing is returned, there maybe a success message - * @return {object} Success message - */ -export function editUserSucceded(res) { - if (res.error === 1) { - addNotification("Some errors encountered", res.message, "error"); - } else { - addNotification("SUCCESS!", res.message, "success"); - } - return { - type: EDIT_USER_SUCCESS, - message: res.message, - }; -} - -/** - * Network error - */ -export function editUserFailed(res) { - return { - type: EDIT_USER_ERROR, - data: res.error, - }; -} - -/** - * Create new bot - * - * @return {object} An action object with a type of BOT - */ -export function registerUser(payload) { - return { - type: REGISTER_USER, - data: payload, - }; -} - -/** - * Dispatched when the repositories are loaded by the request saga - * - * @param {array} repos The repository data - * @param {string} username The current username - * - * @return {object} An action object with a type of BOT_SUCCESS passing the repos - */ -export function registerUserSucceeded(res) { - if (res.error === 1) { - addNotification("Some errors encountered", res.message, "error"); - } else { - addNotification("Success!", res.message, "success"); - } - - return { - type: REGISTER_USER_SUCCESS, - message: res.message, - data: res.data - }; -} - -/** - * Dispatched when loading the repositories fails - * - * @param {object} error The error - * - * @return {object} An action object with a type of BOT_ERROR passing the error - */ -export function registerUserFailed(res) { - return { - type: REGISTER_USER_ERROR, - ...res, - }; -} - -/** - * Delete single user - * @param {string} id of the user passed to saga - * @return {object} An action object with a type of BOT - */ -export function deleteUser(id) { - return { - type: DELETE_USER, - id: id, - }; -} - -/** - * Dispatched when the repositories are loaded by the request saga - * - * @param {array} repos The repository data - * @param {string} username The current username - * - * @return {object} An action object with a type of BOT_SUCCESS passing the repos - */ -export function deleteUserSucceeded(res) { - if (res.error === 1) { - addNotification("Delete user failed!", res.message, "error"); - } else { - addNotification("Delete user succeeded", res.message, "success"); - } - return { - type: DELETE_USER_SUCCESS, - }; -} - -/** - * Dispatched when loading the repositories fails - * - * @param {object} error The error - * - * @return {object} An action object with a type of BOT_ERROR passing the error - */ -export function deleteUserFailed(error) { - addNotification("FAILED!", error.message, "error"); - return { - type: DELETE_USER_ERROR, - }; -} diff --git a/web/src/pages/users/reducer.js b/web/src/pages/users/reducer.js deleted file mode 100644 index e567df5c0..000000000 --- a/web/src/pages/users/reducer.js +++ /dev/null @@ -1,103 +0,0 @@ -import produce from "immer"; -import { - DELETE_USER, - DELETE_USER_ERROR, - DELETE_USER_SUCCESS, - EDIT_USER, - EDIT_USER_ERROR, - EDIT_USER_SUCCESS, - GET_USERS, - GET_USERS_ERROR, - GET_USERS_SUCCESS, - REGISTER_USER, - REGISTER_USER_ERROR, - REGISTER_USER_SUCCESS, -} from "./actions"; - -// The initial state of the App -export const initialState = { - users: null, - userId: null, - deleteUserId: null, -}; - -const usersReducer = produce((draft, action) => { - switch (action.type) { - case GET_USERS: { - return { - users: draft.users, - userId: action.id, - }; - } - case GET_USERS_SUCCESS: { - draft.users = action.users; - return draft; - } - - case GET_USERS_ERROR: { - return { - users: draft.users, - userId: draft.userId, - }; - } - - case REGISTER_USER: { - draft.userData = action.data - return draft; - } - case REGISTER_USER_SUCCESS: { - draft.users.push(action.data); - return draft; - } - - case REGISTER_USER_ERROR: { - return { - users: draft.users, - userData: null, - }; - } - - case EDIT_USER: { - draft.userData = action.data; - draft.userId = action.id - return draft; - } - case EDIT_USER_SUCCESS: { - const findIndex = action.users.findIndex(x => x.id === action.id); - draft.users[findIndex] = action.data; - return draft; - } - - case EDIT_USER_ERROR: { - return { - users: draft.users, - userData: null, - userId: draft.userId, - }; - } - - case DELETE_USER: { - draft.deleteUserId = action.id - return draft; - } - - case DELETE_USER_SUCCESS: { - const users = draft.users.filter((x) => x.id !== draft.deleteUserId); - draft.users = users - draft.deleteUserId = null; - return draft; - } - - case DELETE_USER_ERROR: { - return { - error: action.error, - botActive: draft.botActive, - }; - } - - default: - break; - } -}, initialState); - -export { usersReducer }; diff --git a/web/src/pages/users/saga.js b/web/src/pages/users/saga.js deleted file mode 100644 index d6452f581..000000000 --- a/web/src/pages/users/saga.js +++ /dev/null @@ -1,85 +0,0 @@ -import { call, put, takeLatest } from "redux-saga/effects"; -import { loading } from "../../containers/spinner/actions"; -import request from "../../request"; -import { - deleteUserFailed, - deleteUserSucceeded, - DELETE_USER, - editUserFailed, - editUserSucceded, - EDIT_USER, - getUsersFailed, - getUsersSucceded, - GET_USERS, - registerUserFailed, - registerUserSucceeded, - REGISTER_USER, -} from "./actions"; - -/** - * Bots request/response handler - */ -export function* getUsersApi() { - const requestURL = process.env.REACT_APP_USERS; - try { - yield put(loading(true)); - const res = yield call(request, requestURL); - yield put(getUsersSucceded(res)); - } catch (err) { - yield put(getUsersFailed(err)); - } finally { - yield put(loading(false)); - } -} - -export default function* watchUsersApi() { - yield takeLatest(GET_USERS, getUsersApi); -} - -/** - * Get single bot - */ -export function* editUserApi({ data, id }) { - const requestURL = `${process.env.REACT_APP_USERS}/${id}`; - try { - const res = yield call(request, requestURL, "PUT", data); - yield put(editUserSucceded(res)); - } catch (err) { - yield put(editUserFailed(err)); - } -} - -export function* watchEditUserApi() { - yield takeLatest(EDIT_USER, editUserApi); -} - -/** - * DELETE bot - */ -export function* deleteUserApi({ id }) { - const requestURL = `${process.env.REACT_APP_USERS}/${id}`; - try { - const res = yield call(request, requestURL, "DELETE"); - yield put(deleteUserSucceeded(res)); - } catch (err) { - yield put(deleteUserFailed(err)); - } -} - -export function* watchDeleteUserApi() { - yield takeLatest(DELETE_USER, deleteUserApi); -} - -export function* createUserApi({ data }) { - const requestURL = `${process.env.REACT_APP_REGISTER_USER}`; - try { - const res = yield call(request, requestURL, "POST", data); - yield put(registerUserSucceeded(res)); - } catch (err) { - yield put(registerUserFailed(err)); - } -} - -export function* watchCreateUserApi() { - yield takeLatest(REGISTER_USER, createUserApi); -} diff --git a/web/src/reducer.js b/web/src/reducer.js index 94fd64d19..67e8b8041 100644 --- a/web/src/reducer.js +++ b/web/src/reducer.js @@ -12,7 +12,6 @@ import { import { testBotsReducer } from "./pages/paper-trading/reducer"; import { settingsReducer } from "./pages/bots/reducer"; import { blacklistReducer } from "./pages/research/reducer"; -import { usersReducer } from "./pages/users/reducer"; import { balanceRawReducer, estimateReducer, @@ -32,7 +31,6 @@ const rootReducer = combineReducers({ blacklistReducer, settingsReducer, estimateReducer, - usersReducer, testBotsReducer, gainersLosersReducer, btcBenchmarkReducer, diff --git a/web/src/router/routes.js b/web/src/router/routes.js index 8695a5763..77b5d6428 100644 --- a/web/src/router/routes.js +++ b/web/src/router/routes.js @@ -9,7 +9,6 @@ import TestAutotrade from "../pages/paper-trading/TestAutotrade"; import TestBotForm from "../pages/paper-trading/TestBotForm"; import TestBots from "../pages/paper-trading/TestBots"; import Research from "../pages/research/Research"; -import Users from "../pages/users/Users"; const routes = [ { @@ -120,15 +119,6 @@ const routes = [ nav: true, private: true, }, - { - path: "/users", - name: "Users", - icon: "nc-icon nc-single-02", - component: Users, - layout: "/admin", - nav: true, - private: true, - }, { path: "/registration", name: "Registration", diff --git a/web/src/sagas.js b/web/src/sagas.js index aea49b11b..48af29c1c 100644 --- a/web/src/sagas.js +++ b/web/src/sagas.js @@ -34,11 +34,6 @@ import { watchGetBlacklistApi, watchResearchApi, } from "./pages/research/saga"; -import watchUsersApi, { - watchCreateUserApi, - watchDeleteUserApi, - watchEditUserApi, -} from "./pages/users/saga"; import { watchGetEstimate, watchRawBalance, @@ -67,10 +62,6 @@ export default function* rootSaga() { watchDeleteBlackListApi(), watchAddBlacklistApi(), watchGetEstimate(), - watchUsersApi(), - watchEditUserApi(), - watchDeleteUserApi(), - watchCreateUserApi(), watchGetTestBotsApi(), watchGetTestBotApi(), watchCreateTestBot(),