|
@@ -1,40 +1,39 @@
|
|
|
-import {jwtDecode, gql} from '../App'
|
|
|
-
|
|
|
+import { jwtDecode, gql } from '../App'
|
|
|
|
|
|
const actionPending = name => ({ type: 'PROMISE', status: 'PENDING', name })
|
|
|
+
|
|
|
const actionResolved = (name, payload) => ({ type: 'PROMISE', status: 'RESOLVED', name, payload })
|
|
|
const actionRejected = (name, error) => ({ type: 'PROMISE', status: 'REJECTED', name, error })
|
|
|
-
|
|
|
export const actionPromise = (name, promise) =>
|
|
|
- async dispatch => {
|
|
|
- dispatch(actionPending(name))
|
|
|
- try {
|
|
|
- let payload = await promise
|
|
|
- dispatch(actionResolved(name, payload))
|
|
|
- return payload
|
|
|
- }
|
|
|
- catch (error) {
|
|
|
- dispatch(actionRejected(name, error))
|
|
|
+ async dispatch => {
|
|
|
+ dispatch(actionPending(name))
|
|
|
+ try {
|
|
|
+ let payload = await promise
|
|
|
+ dispatch(actionResolved(name, payload))
|
|
|
+ return payload
|
|
|
+ }
|
|
|
+ catch (error) {
|
|
|
+ dispatch(actionRejected(name, error))
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
export const actionAuthLogin = token => ({ type: 'AUTH_LOGIN', token })
|
|
|
export const actionAuthLogout = () => ({ type: 'AUTH_LOGOUT' })
|
|
|
|
|
|
export const actionLogin = (login, password) =>
|
|
|
- actionPromise('login', gql(`
|
|
|
+ actionPromise('login', gql(`
|
|
|
query log($login:String!, $password:String!) {
|
|
|
login(login: $login, password: $password)
|
|
|
}`, { login, password }))
|
|
|
|
|
|
export const actionFullLogin = (login = 'tst', password = '123') =>
|
|
|
- async dispatch => {
|
|
|
- let token = await dispatch(actionLogin(login, password))
|
|
|
- if (token) {
|
|
|
- await dispatch(actionAuthLogin(token))
|
|
|
- dispatch(actionGetUserData())
|
|
|
+ async dispatch => {
|
|
|
+ let token = await dispatch(actionLogin(login, password))
|
|
|
+ if (token) {
|
|
|
+ await dispatch(actionAuthLogin(token))
|
|
|
+ dispatch(actionGetUserData())
|
|
|
+ }
|
|
|
}
|
|
|
- }
|
|
|
|
|
|
export const actionRegister = (login, password) =>
|
|
|
actionPromise('registration', gql(`
|
|
@@ -47,10 +46,10 @@ export const actionRegister = (login, password) =>
|
|
|
)
|
|
|
|
|
|
export const actionFullRegister = (login = 'tst', password = '123') =>
|
|
|
- async dispatch => {
|
|
|
- await dispatch(actionRegister(login, password))
|
|
|
- await dispatch(actionFullLogin(login, password))
|
|
|
- }
|
|
|
+ async dispatch => {
|
|
|
+ await dispatch(actionRegister(login, password))
|
|
|
+ await dispatch(actionFullLogin(login, password))
|
|
|
+ }
|
|
|
|
|
|
export const actionGetUserData = () => {
|
|
|
let _id = jwtDecode(localStorage.authToken).sub.id
|
|
@@ -61,7 +60,7 @@ export const actionGetUserData = () => {
|
|
|
login, _id, avatar {_id, url, originalFileName}
|
|
|
}
|
|
|
}
|
|
|
- `, { userId: JSON.stringify([{_id}]) }))
|
|
|
+ `, { userId: JSON.stringify([{ _id }]) }))
|
|
|
)
|
|
|
}
|
|
|
|