|
@@ -1,22 +1,21 @@
|
|
|
import { useState, useRef, useEffect } from 'react';
|
|
|
import {BrowserRouter as Router, Route, Link, Switch, Redirect, useHistory} from 'react-router-dom';
|
|
|
-import {gql, urlUpload, actionPromise, actionGoodCard, getGoods, updateImgAction, actionUserUpdate} from "../reducers";
|
|
|
+import {gql, urlUpload, actionPromise, actionGoodCard, actionUserFindOne, getGoods, updateImgAction, actionUserUpdate, actionAuthLogout, actionAuthLogin} from "../reducers";
|
|
|
import {createStore, combineReducers, applyMiddleware, bindActionCreators} from 'redux';
|
|
|
import { connect } from 'react-redux';
|
|
|
|
|
|
-const UpdateForm = ({updateImg, updateUser, id}) => {
|
|
|
- const history = useHistory();
|
|
|
- const formRef = useRef(null);
|
|
|
- const [nick, changeNick] = useState(null);
|
|
|
- const [login, changeLogin] = useState(null);
|
|
|
- const [password, changePassword] = useState(null);
|
|
|
- const [password2, changePassword2] = useState(null);
|
|
|
- const [isUpdateImg, changeImg] = useState(false);
|
|
|
- const [isUpdatePage, changePage] = useState(false);
|
|
|
- const [newImage, getImage] = useState({});
|
|
|
-
|
|
|
- useEffect(() => isUpdatePage && window.location.reload()[isUpdatePage])
|
|
|
- console.log(formRef);
|
|
|
+
|
|
|
+
|
|
|
+const UpdateAdminForm = ({onLogin, onLogout, data, getData, updateImg, updateUser, id, token}) => {
|
|
|
+ const history = useHistory();
|
|
|
+ const formRef = useRef(null);
|
|
|
+ const [nick, changeNick] = useState(null);
|
|
|
+ const [login, changeLogin] = useState(null);
|
|
|
+ const [password, changePassword] = useState(null);
|
|
|
+ const [password2, changePassword2] = useState(null);
|
|
|
+ const [isUpdateImg, changeImg] = useState(false);
|
|
|
+ const [isUpdatePage, changePage] = useState(false);
|
|
|
+ const [newImage, getImage] = useState({});
|
|
|
|
|
|
return(
|
|
|
<>
|
|
@@ -26,11 +25,14 @@ const UpdateForm = ({updateImg, updateUser, id}) => {
|
|
|
<input onChange = {(e) => changeLogin(e.target.value)} placeholder = "Новий логін"/>
|
|
|
<input onChange = {(e) => changePassword(e.target.value)} placeholder = "Новий пароль"/>
|
|
|
<input onChange = {(e) => changePassword2(e.target.value)} placeholder = "Повторіть пароль"/>
|
|
|
+ <button>Назначити адміном</button>
|
|
|
+ <button>Зняти з адміна</button>
|
|
|
+ <button>Зтерти юзера</button>
|
|
|
<form className = "updateImg" action="/upload" method="post" enctype="multipart/form-data" id='form' ref={formRef}
|
|
|
onChange = {
|
|
|
() => {
|
|
|
- updateImg(formRef, getImage);
|
|
|
- changeImg(true);
|
|
|
+ updateImg(formRef);
|
|
|
+ // changeImg(true);
|
|
|
}}>
|
|
|
<input type="file" name="photo" id='photo'/>
|
|
|
</form>
|
|
@@ -59,6 +61,82 @@ const UpdateForm = ({updateImg, updateUser, id}) => {
|
|
|
changePage(true);
|
|
|
|
|
|
|
|
|
+ }}>Зберегти</button>
|
|
|
+ </div>
|
|
|
+ </>
|
|
|
+ )
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+const UpdateForm = ({state, onLogin, onLogout, isImg, data, getData, updateImg, updateUser, id, token}) => {
|
|
|
+ const history = useHistory();
|
|
|
+ const formRef = useRef(null);
|
|
|
+ const [nick, changeNick] = useState(null);
|
|
|
+ const [login, changeLogin] = useState(null);
|
|
|
+ const [password, changePassword] = useState(null);
|
|
|
+ const [password2, changePassword2] = useState(null);
|
|
|
+ const [isUpdateImg, changeImg] = useState(false);
|
|
|
+ const [isUpdatePage, changePage] = useState(false);
|
|
|
+ const [newImage, getImage] = useState({});
|
|
|
+
|
|
|
+ useEffect(() => {
|
|
|
+ if(isUpdatePage) {
|
|
|
+ let jwt = token;
|
|
|
+ onLogout();
|
|
|
+ onLogin(jwt);
|
|
|
+ history.push("/login")
|
|
|
+
|
|
|
+ }
|
|
|
+ },
|
|
|
+ [isUpdatePage])
|
|
|
+
|
|
|
+ return(
|
|
|
+ <>
|
|
|
+ <div className = "updateWrapper">
|
|
|
+ <h4>Редагування</h4>
|
|
|
+ <input onChange = {(e) => changeNick(e.target.value)} placeholder = "Новий нікнейм"/>
|
|
|
+ <input onChange = {(e) => changeLogin(e.target.value)} placeholder = "Новий логін"/>
|
|
|
+ <input onChange = {(e) => changePassword(e.target.value)} placeholder = "Новий пароль"/>
|
|
|
+ <input onChange = {(e) => changePassword2(e.target.value)} placeholder = "Повторіть пароль"/>
|
|
|
+ <input type="file" name="photo" id='photo'
|
|
|
+ onChange={(ev) => {
|
|
|
+ updateImg(ev.target.files[0])
|
|
|
+
|
|
|
+ changeImg(true);
|
|
|
+ }
|
|
|
+ }/>
|
|
|
+ <button onClick = {() => {
|
|
|
+
|
|
|
+ let newUser = {}
|
|
|
+ newUser._id = id;
|
|
|
+ if(nick){
|
|
|
+ newUser.nick = nick;
|
|
|
+ }
|
|
|
+ if(login) {
|
|
|
+ newUser.login = login;
|
|
|
+ }
|
|
|
+ if (password && password == password2) {
|
|
|
+ newUser.password = password
|
|
|
+ } else if (password !== password2) {
|
|
|
+ history.push("/loginError");
|
|
|
+ return
|
|
|
+ }
|
|
|
+ if(isUpdateImg) {
|
|
|
+
|
|
|
+ }
|
|
|
+ updateUser(newUser);
|
|
|
+ changePage(true)
|
|
|
+ // let jwt = token;
|
|
|
+
|
|
|
+ // onLogout();
|
|
|
+ // onLogin(jwt)
|
|
|
+ // history.push("/login/")
|
|
|
+
|
|
|
+
|
|
|
}}>Зберегти</button>
|
|
|
</div>
|
|
|
|
|
@@ -69,15 +147,17 @@ const UpdateForm = ({updateImg, updateUser, id}) => {
|
|
|
|
|
|
const mapStateToProps = state => ({
|
|
|
state: state,
|
|
|
- // data: state.promiseRed && state.promiseRed.user &&
|
|
|
- // state.promiseRed.user.payload && state.promiseRed.user.payload.data &&
|
|
|
- // state.promiseRed.user.payload.data.UserFindOne
|
|
|
- // basket: state.basket,
|
|
|
- // GoodsArr: arrFromObj(ObjFilter(state.basket, "price")),
|
|
|
- // order: orderArr(arrFromObj(ObjFilter(state.basket, "price"))),
|
|
|
- // orderDone: state.promiseRed && state.promiseRed.order &&
|
|
|
- // state.promiseRed.order.payload &&
|
|
|
- // state.promiseRed.order.payload.data.OrderUpsert
|
|
|
+
|
|
|
+ data: state.promiseRed && state.promiseRed.user &&
|
|
|
+ state.promiseRed.user.payload && state.promiseRed.user.payload.data &&
|
|
|
+ state.promiseRed.user.payload.data.UserFindOne,
|
|
|
+
|
|
|
+ token: state.auth && state.auth.token,
|
|
|
+
|
|
|
+ isImg: state.promiseRed && state.promiseRed.photo &&
|
|
|
+ state.promiseRed.photo.payload && state.promiseRed.photo.payload.data &&
|
|
|
+ state.promiseRed.photo.payload
|
|
|
+
|
|
|
});
|
|
|
|
|
|
|
|
@@ -85,12 +165,17 @@ const mapStateToProps = state => ({
|
|
|
const mapDispatchToProps = dispatch => bindActionCreators({
|
|
|
updateImg: updateImgAction,
|
|
|
updateUser: actionUserUpdate,
|
|
|
+ getData: actionUserFindOne,
|
|
|
+ onLogout: actionAuthLogout,
|
|
|
+ onLogin: actionAuthLogin
|
|
|
// onDel: actionCartDelete,
|
|
|
// onClear: actionCartClear,
|
|
|
// onOrder: actionOrder
|
|
|
}, dispatch);
|
|
|
|
|
|
|
|
|
+const CUpdateAdminForm = connect(mapStateToProps, mapDispatchToProps)(UpdateAdminForm)
|
|
|
+
|
|
|
const CUpdateForm = connect(mapStateToProps, mapDispatchToProps)(UpdateForm)
|
|
|
|
|
|
-export default CUpdateForm;
|
|
|
+export {CUpdateForm};
|