Olga_Brekhuntsova 2 years ago
parent
commit
20f5e94629

+ 4 - 3
project_marketplace/src/App.js

@@ -26,10 +26,10 @@ store.subscribe(() => console.log(store.getState()))
 
 function App() {
   const [open, setOpen] = useState(false);
-  // useEffect(() => { store.getState().auth.token? setOpen(true) : setOpen(false) }, [localStorage.authToken])
+  useEffect(() => { store.getState().auth.token? setOpen(true) : setOpen(false) }, [])
  
   return (
-      <Router history={history}>
+      // <Router history={history}>
     <Provider store={store}>
    {/* <CPageRoute> */}
       {!open && <Registration/>}
@@ -49,7 +49,8 @@ function App() {
         </BodyWrapper>}
          {/* </CPageRoute> */}
     </Provider>
-  </Router>)
+  // </Router >
+  )
 }
 
 export default App;

+ 5 - 11
project_marketplace/src/components/Registration/LoginForm/LoginForm.jsx

@@ -8,11 +8,7 @@ const LoginForm = ({ handleOpen, onLogin }) => {
   const [login, setLogin] = useState('');
   const [password, setPassword] = useState('');
 
-  const handleUserInput = (inputValue, inputType) => {
-    (inputType === 'login') && setLogin(inputValue);
-    (inputType === 'password') && setPassword(inputValue);
-  };
-  return <> 
+   return <> 
                  <div>     <DialogTitle id="registration">LOG IN</DialogTitle>
                             <DialogContent>
                                 <TextField autoFocus margin='dense'
@@ -20,7 +16,7 @@ const LoginForm = ({ handleOpen, onLogin }) => {
                                     label='Login'
           type='text'
           value={login}
-          onChange={e => { handleUserInput(setLogin(e.target.value, 'login')) }}
+          onChange={e => setLogin(e.target.value) }
                                     fullWidth>
                                 </TextField>
                                 <TextField margin='dense'
@@ -28,18 +24,16 @@ const LoginForm = ({ handleOpen, onLogin }) => {
                                     label='Password'
           type='password'
           value={password}
-          onChange={e => { handleUserInput(setPassword(e.target.value, 'password')) }}
+          onChange={e => setPassword(e.target.value) }
                                     fullWidth>
                                 </TextField>
                                 
                             </DialogContent>
       <DialogActions sx={{ display: "block" }}>
      
-        <Button type="submit" onClick={() => {onLogin(login, password)}}>  Log in </Button>
+        <Button onClick={() => onLogin(login, password)}>  Log in </Button>
        <div> Don't you have an account?
-        <Button onClick={handleOpen}>Sign up</Button></div>
-     
-  
+        <Button onClick={handleOpen}>Sign up</Button></div>      
           </DialogActions>
           </div>
            </>

+ 20 - 2
project_marketplace/src/requests/actions.js

@@ -68,10 +68,28 @@ const actionLogin = (login, password) =>
         let token = await dispatch(actionPromise("login", gql(` query login($login:String!, $password:String!){
     login(login:$login, password:$password)
   }`,
-    { "login": login, "password": password })));
+    { login, password })));
         if (token) { await dispatch(actionAuthLogin(token)) }
     };
 
+
+
+//  const actionLogin = (login, password) => async (dispatch) => {
+//   let token = await dispatch(
+//     actionPromise(
+//       'auth',
+//       gql(
+//         ` query login($login:String!, $password:String!){
+//             login(login:$login, password:$password)} `,
+//         { login, password },
+//       ),
+//     ),
+//   )
+//   if (token) {
+//     dispatch(actionAuthLogin(token))
+//   }
+// }
+
 const actionAuthLogin = (token) => ({ type: "AUTH_LOGIN", token });
 
 
@@ -79,7 +97,7 @@ const actionFullLogin = (login, password) => ({ type: 'FULL_LOGIN', login, passw
 
 const actionAuthLogout = () => ({ type: "AUTH_LOGOUT" });
 
-//Занрузка всех объявлений
+//Загрузка всех объявлений
 const actionAllAds = () => 
     actionPromise('allAds', gql(`query ads($query: String){
         AdFind(query: $query) {

+ 18 - 42
project_marketplace/src/requests/reducers.js

@@ -38,6 +38,7 @@ const jwtDecode = token => {
     }
 }
 
+
 function promiseReducer(state = {}, { type, name, status, payload, error }) {
     if (type === "PROMISE") {
         return {
@@ -48,48 +49,23 @@ function promiseReducer(state = {}, { type, name, status, payload, error }) {
     return state;
 }
 
-function authReducer(state, {type, token}){
-        if (!state) {
-            if (localStorage.authToken) {
-                type = 'AUTH_LOGIN'
-                token = localStorage.authToken
-            } else {
-                return {}
-            }
-        }
-        if (type === 'AUTH_LOGIN') {
-            let auth = jwtDecode(token)
-            if (auth) {
-                localStorage.authToken = token
-                return { token, payload: auth }
-            }
-        }
-        if (type === 'AUTH_LOGOUT') {
-            localStorage.authToken = ''
-            console.log(localStorage.authToken)
-            return {}
-        }
-        return state
-}
-    
-// function authReducer(state, {type, token}){
-//     if (state === undefined && localStorage.authToken){
-//         token = localStorage.authToken;
-//         // token = '';
-//         type = 'AUTH_LOGIN'
-//     }   
-//     if (type === 'AUTH_LOGIN'){
-//             if(jwtDecode(token)){
-//                 localStorage.authToken = token;
-//                 return { token, payload: jwtDecode(token) };
-//             }
-//         }
-//     if (type === 'AUTH_LOGOUT'){
-//         localStorage.authToken = '';
-//         return {}
-//     }
-//     return state || {}
-// }
+    function authReducer(state, { type, token }) {
+    if (state === undefined && localStorage.authToken) {
+      token = localStorage.authToken
+      type = 'AUTH_LOGIN'
+    }
+    if (type === 'AUTH_LOGIN') {
+      if (jwtDecode(token)) {
+        localStorage.authToken = token
+        return { token, payload: jwtDecode(token) }
+      }
+    }
+    if (type === 'AUTH_LOGOUT') {
+      localStorage.authToken = ''
+      return {}
+    }
+    return state || {}
+  }
 
 const store = createStore(combineReducers({
     promise: promiseReducer,