|
@@ -1,22 +1,68 @@
|
|
import http from '../utils/http'
|
|
import http from '../utils/http'
|
|
-import {
|
|
|
|
- REQUEST_START,
|
|
|
|
- REQUEST_OK,
|
|
|
|
- REQUEST_KO,
|
|
|
|
- SHOW_SPINNER,
|
|
|
|
- HIDE_SPINNER,
|
|
|
|
- SHOW_NOTIFICATION,
|
|
|
|
- HIDE_NOTIFICATION
|
|
|
|
-} from '../constants/ActionTypes'
|
|
|
|
|
|
+import { getToken } from '../utils/auth'
|
|
import { isArray } from 'lodash'
|
|
import { isArray } from 'lodash'
|
|
|
|
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ *
|
|
|
|
+ * @param {*} username
|
|
|
|
+ * @param {*} password
|
|
|
|
+ */
|
|
|
|
+export const login = (username, password) => async dispatch => {
|
|
|
|
+ dispatch({ type: 'LOGIN_REQUEST' })
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ const response = await http.post('auth/get_token/', { username, password })
|
|
|
|
+
|
|
|
|
+ if (response.status === 200) {
|
|
|
|
+ dispatch({ type: 'LOGIN_SUCCESS', payload: response.data })
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (response.status === 401) {
|
|
|
|
+ dispatch({ type: 'LOGIN_FAILURE' })
|
|
|
|
+ }
|
|
|
|
+ } catch(error) {
|
|
|
|
+ dispatch({ type: 'LOGIN_FAILURE' })
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ *
|
|
|
|
+ */
|
|
|
|
+export const checkToken = () => async dispatch => {
|
|
|
|
+ dispatch({ type: 'CHECK_TOKEN_REQUEST' })
|
|
|
|
+
|
|
|
|
+ try {
|
|
|
|
+ const response = await http.post('auth/check_token/', { token: getToken() })
|
|
|
|
+
|
|
|
|
+ if (response.status === 200) {
|
|
|
|
+ dispatch({ type: 'CHECK_TOKEN_SUCCESS', payload: response.data })
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ if (response.status === 401) {
|
|
|
|
+ dispatch({ type: 'CHECK_TOKEN_FAILURE' })
|
|
|
|
+ }
|
|
|
|
+ } catch (error) {
|
|
|
|
+ dispatch({ type: 'CHECK_TOKEN_FAILURE' })
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+/**
|
|
|
|
+ *
|
|
|
|
+ */
|
|
|
|
+export const logout = () => {
|
|
|
|
+ return {
|
|
|
|
+ type: 'LOGOUT_REQUEST'
|
|
|
|
+ }
|
|
|
|
+}
|
|
|
|
+
|
|
/**
|
|
/**
|
|
*
|
|
*
|
|
* @param {*} message
|
|
* @param {*} message
|
|
*/
|
|
*/
|
|
export const spinner = (show, message) => dispatch => {
|
|
export const spinner = (show, message) => dispatch => {
|
|
dispatch({
|
|
dispatch({
|
|
- type: show ? SHOW_SPINNER : HIDE_SPINNER,
|
|
|
|
|
|
+ type: show ? 'SHOW_SPINNER' : 'HIDE_SPINNER',
|
|
payload: message
|
|
payload: message
|
|
})
|
|
})
|
|
}
|
|
}
|
|
@@ -27,7 +73,7 @@ export const spinner = (show, message) => dispatch => {
|
|
*/
|
|
*/
|
|
export const notify = message => dispatch => {
|
|
export const notify = message => dispatch => {
|
|
dispatch({
|
|
dispatch({
|
|
- type: message ? SHOW_NOTIFICATION : HIDE_NOTIFICATION,
|
|
|
|
|
|
+ type: message ? 'SHOW_NOTIFICATION' : 'HIDE_NOTIFICATION',
|
|
payload: { message }
|
|
payload: { message }
|
|
})
|
|
})
|
|
}
|
|
}
|
|
@@ -37,16 +83,13 @@ export const notify = message => dispatch => {
|
|
* @param {*} resource
|
|
* @param {*} resource
|
|
*/
|
|
*/
|
|
export const get = resource => async dispatch => {
|
|
export const get = resource => async dispatch => {
|
|
- dispatch({
|
|
|
|
- type: REQUEST_START
|
|
|
|
- })
|
|
|
|
|
|
+ dispatch({ type: 'REQUEST_INIT' })
|
|
|
|
|
|
try {
|
|
try {
|
|
- // const response = await axios.get(`${API_URL}${resource}`)
|
|
|
|
const response = await http.get(`${resource}`)
|
|
const response = await http.get(`${resource}`)
|
|
- dispatch(ok(response.data))
|
|
|
|
|
|
+ dispatch({ type: 'REQUEST_OK', payload: response.data })
|
|
} catch (error) {
|
|
} catch (error) {
|
|
- dispatch(ko(resource, error.response))
|
|
|
|
|
|
+ dispatch({ type: 'REQUEST_KO', payload: error.response.status })
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
@@ -55,9 +98,7 @@ export const get = resource => async dispatch => {
|
|
* @param {*} resource
|
|
* @param {*} resource
|
|
*/
|
|
*/
|
|
export const post = (resources, data) => async dispatch => {
|
|
export const post = (resources, data) => async dispatch => {
|
|
- dispatch({
|
|
|
|
- type: REQUEST_START
|
|
|
|
- })
|
|
|
|
|
|
+ dispatch({ type: 'REQUEST_INIT' })
|
|
|
|
|
|
data = data || {}
|
|
data = data || {}
|
|
|
|
|
|
@@ -65,42 +106,17 @@ export const post = (resources, data) => async dispatch => {
|
|
for (let r of resources) {
|
|
for (let r of resources) {
|
|
try {
|
|
try {
|
|
const response = await http.post(`${r}`, data)
|
|
const response = await http.post(`${r}`, data)
|
|
- dispatch(ok(response.data))
|
|
|
|
|
|
+ dispatch({ type: 'REQUEST_OK', payload: response.data })
|
|
} catch (error) {
|
|
} catch (error) {
|
|
- dispatch(ko(r, error))
|
|
|
|
|
|
+ dispatch({ type: 'REQUEST_KO', payload: error.response.status })
|
|
}
|
|
}
|
|
}
|
|
}
|
|
} else {
|
|
} else {
|
|
try {
|
|
try {
|
|
const response = await http.post(`${resources}`, data)
|
|
const response = await http.post(`${resources}`, data)
|
|
- dispatch(ok(response.data))
|
|
|
|
|
|
+ dispatch({ type: 'REQUEST_OK', payload: response.data })
|
|
} catch (error) {
|
|
} catch (error) {
|
|
- dispatch(ko(resources, error))
|
|
|
|
|
|
+ dispatch({ type: 'REQUEST_KO', payload: error.response.status })
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
-/**
|
|
|
|
- *
|
|
|
|
- * @param {*} resource
|
|
|
|
- * @param {*} payload
|
|
|
|
- */
|
|
|
|
-const ok = payload => {
|
|
|
|
- return {
|
|
|
|
- type: REQUEST_OK,
|
|
|
|
- payload
|
|
|
|
- }
|
|
|
|
-}
|
|
|
|
-
|
|
|
|
-/**
|
|
|
|
- *
|
|
|
|
- * @param {*} resource
|
|
|
|
- * @param {*} payload
|
|
|
|
- */
|
|
|
|
-const ko = (resource, payload) => {
|
|
|
|
- return {
|
|
|
|
- type: REQUEST_KO,
|
|
|
|
- resource,
|
|
|
|
- payload
|
|
|
|
- }
|
|
|
|
-}
|
|
|