Преглед на файлове

action registration added

pocu46 преди 3 години
родител
ревизия
0f5b390bd7
променени са 3 файла, в които са добавени 43 реда и са изтрити 2 реда
  1. 33 0
      hipstagram/src/Actions/action_registration.js
  2. 6 0
      hipstagram/src/Redux/network_reducer.js
  3. 4 2
      hipstagram/src/Redux/promise_reducer.js

+ 33 - 0
hipstagram/src/Actions/action_registration.js

@@ -0,0 +1,33 @@
+import { actionAuthLogin } from '../Redux/auth_reducer';
+import { actionLoginNetworkError, actionLoginNetworkErrorReset } from '../Redux/network_reducer';
+import { actionPromise } from '../Redux/promise_reducer';
+import { actionLogin } from './action_login';
+
+export const actionRegistrations = (login, password) => async (dispatch) => {
+    console.log(login, password)
+    let result = await dispatch(actionPromise('Registration', gql(`mutation newUser($loginStr: String!, $passwordStr: String!){
+        createUser(login: $loginStr, password: $passwordStr){
+            _id
+            login
+            }
+        }`,
+        {
+            "loginStr": login,
+            "passwordStr": password
+        })
+    )
+    )
+    // console.log(result)
+    if(result?.data?.createUser===null) {
+        //  такой пользователь есть
+    } else {
+        if (result?.data?.createUser?.id) {
+            console.log('Registered successfull')
+            actionLogin(login, password)
+        } else {
+            console.log("Registration failed")
+            dispatch(actionLoginNetworkError())
+            setTimeout(() => { actionLoginNetworkErrorReset() }, 5000)
+        }
+    }
+}

+ 6 - 0
hipstagram/src/Redux/network_reducer.js

@@ -1,5 +1,7 @@
 const LOGINNETWORKERROR = 'LOGINNETWORKERROR';
 const LOGINNETWORKERRORRESET = 'LOGINNETWORKERRORRESET';
+const LOGIN = 'LOGIN';
+const LOGOUT = 'LOGOUT';
 
 export const actionLoginNetworkError = () => {
     return {
@@ -31,6 +33,10 @@ export const networkErrorReducer = (state, action) => {
                 ...state,
                 loginNetworkError: false
             }
+        case LOGIN:
+            return {};
+        case LOGOUT:
+            return {};
         default:
             return state
     }

+ 4 - 2
hipstagram/src/Redux/promise_reducer.js

@@ -32,19 +32,21 @@ const promiseReducer = (state={}, action) => {
 }
 
 export const actionPromise = (name, promise) => {
+
     const actionPending = () => ({ type: PROMISE, name, status: PENDING, payload: null, error: null })
     const actionResolved = (payload) => ({ type: PROMISE, name, status: RESOLVED, payload, error: null })
     const actionRejected = (error) => ({ type: PROMISE, name, status: REJECTED, payload: null, error })
+
     return async (dispatch) => {
         dispatch(actionPending())
         let result;
         try {
             result = await promise
-            dispatch(actionResolved(result))
+            dispatch(actionResolved(result))   // { type: PROMISE, name, status: RESOLVED, payload: result, error: null }
         }
         catch (e) {
             history.push("/404")
-            dispatch(actionRejected(e))
+            dispatch(actionRejected(e))   // { type: PROMISE, name, status: REJECTED, payload: null, error: e }
         }
         return result
     }