Commit f58b93f6 authored by Simic Veljko's avatar Simic Veljko
Browse files

use fetchApi

parent a0e33984
import { createAction } from 'redux-actions';
import CURRENCIES_VALUE from './constants';
import { getAuthToken } from 'utils';
import axios from 'axios'
import CURRENCIES_VALUE, { CURRENCIES_VALUE_URL} from './constants';
import { fetchApi } from 'utils';
const reset = createAction(CURRENCIES_VALUE, () => ({
......@@ -25,19 +24,15 @@ const success = createAction(CURRENCIES_VALUE, values => ({
const getValues = () =>
(dispatch) => {
dispatch(begin);
axios({
method: 'GET',
headers: {
Authorization: `Bearer ${getAuthToken()}`
},
url: 'https://api.bankofthecommons.coop/exchange/v1/ticker/eur'
fetchApi({
path: CURRENCIES_VALUE_URL
}).then(response => {
let values = response.data
dispatch(success(values.data))
let values = response;
dispatch(success(values.data));
}).catch(error => {
console.error("ERROR");
});
}
};
const actions = {
reset,
......@@ -45,6 +40,6 @@ const actions = {
fail,
success,
getValues
}
};
export default actions;
\ No newline at end of file
export default actions;
const CURRENCIES_VALUE = "CURRECIES_VALUE"
const CURRENCIES_VALUE = "CURRECIES_VALUE";
export const CURRENCIES_VALUE_URL = "exchange/v1/ticker/eur";
export default CURRENCIES_VALUE;
\ No newline at end of file
export default CURRENCIES_VALUE;
import { createAction } from 'redux-actions';
import EARNINGS from './constants';
import { getAuthToken } from 'utils';
import axios from 'axios'
import EARNINGS, { EARNINGS_URL } from './constants';
import { fetchApi } from 'utils';
const reset = createAction(EARNINGS, () => ({
status: 'initial',
status: 'initial',
}));
const begin = createAction(EARNINGS, () => ({
status: 'pending',
status: 'pending',
}));
const fail = createAction(EARNINGS, error => ({
error,
status: 'error',
error,
status: 'error',
}));
const success = createAction(EARNINGS, earnings=> ({
earnings,
status: 'success'
earnings,
status: 'success'
}));
const getEarnings = () =>
(dispatch) => {
dispatch(begin);
axios({
method: 'GET',
headers: {
Authorization: `Bearer ${getAuthToken()}`
},
url: 'https://api.bankofthecommons.coop/user/v1/wallet/earnings'
}).then(response => {
let earnings = response.data
dispatch(success(earnings.data))
}).catch(error => {
console.error("ERROR");
});
}
(dispatch) => {
dispatch(begin);
fetchApi({
path: EARNINGS_URL
}).then(response => {
let earnings = response;
dispatch(success(earnings.data));
}).catch(error => {
console.error("ERROR");
});
};
const actions = {
reset,
......@@ -46,6 +39,6 @@ const actions = {
fail,
success,
getEarnings
}
};
export default actions;
\ No newline at end of file
export default actions;
const EARNINGS = "EARNINGS"
const EARNINGS = "EARNINGS";
export const EARNINGS_URL = 'user/v1/wallet/earnings';
export default EARNINGS;
\ No newline at end of file
export default EARNINGS;
import { createAction } from 'redux-actions';
import LAST_TRANSACTIONS from './constants';
import { getAuthToken } from 'utils';
import axios from 'axios'
import LAST_TRANSACTIONS, { LAST_TRANSACTIONS_URL} from './constants';
import { fetchApi } from 'utils';
const reset = createAction(LAST_TRANSACTIONS, () => ({
......@@ -25,15 +24,11 @@ const success = createAction(LAST_TRANSACTIONS, transactions => ({
const getLastTransactions = () =>
(dispatch) => {
dispatch(begin);
axios({
method: 'GET',
headers: {
Authorization: `Bearer ${getAuthToken()}`
},
url: 'https://api.bankofthecommons.coop/user/v1/last'
fetchApi({
path: LAST_TRANSACTIONS_URL
}).then(response => {
let transactions = response.data
dispatch(success(transactions.data))
let transactions = response;
dispatch(success(transactions.data));
}).catch(error => {
console.error("ERROR");
});
......@@ -47,4 +42,4 @@ const actions = {
getLastTransactions
}
export default actions;
\ No newline at end of file
export default actions;
const LAST_TRANSACTIONS = "CURRECIES_VALUE"
const LAST_TRANSACTIONS = "CURRECIES_VALUE";
export const LAST_TRANSACTIONS_URL = "user/v1/last";
export default LAST_TRANSACTIONS;
\ No newline at end of file
export default LAST_TRANSACTIONS;
import { createAction } from 'redux-actions';
import MONTH_EARNINGS from './constants';
import { getAuthToken } from 'utils';
import axios from 'axios'
import MONTH_EARNINGS, { MONTH_EARNINGS_URL} from './constants';
import { fetchApi } from 'utils';
const reset = createAction(MONTH_EARNINGS, () => ({
......@@ -25,15 +24,11 @@ const success = createAction(MONTH_EARNINGS, monthEarnings => ({
const getMonthEarnings = () =>
(dispatch) => {
dispatch(begin);
axios({
method: 'GET',
headers: {
Authorization: `Bearer ${getAuthToken()}`
},
url: 'https://api.bankofthecommons.coop/user/v1/wallet/monthearnings'
fetchApi({
path: MONTH_EARNINGS_URL
}).then(response => {
let monthEarnings = response.data
dispatch(success(monthEarnings.data))
let monthEarnings = response.data;
dispatch(success(monthEarnings.data));
}).catch(error => {
console.error("ERROR");
});
......@@ -47,4 +42,4 @@ const actions = {
getMonthEarnings
}
export default actions;
\ No newline at end of file
export default actions;
const MONTH_EARNINGS = "MONTH_EARNINGS"
const MONTH_EARNINGS = "MONTH_EARNINGS";
export const MONTH_EARNINGS_URL = "user/v1/wallet/monthearnings";
export default MONTH_EARNINGS;
\ No newline at end of file
export default MONTH_EARNINGS;
import { createAction } from 'redux-actions';
import TRANSACTIONS from './constants';
import TRANSACTIONS, { TRANSACTIONS_URL} from './constants';
import { getAuthToken } from 'utils';
import axios from 'axios'
import { fetchApi } from 'utils';
const reset = createAction(TRANSACTIONS, () => ({
......@@ -25,15 +25,11 @@ const success = createAction(TRANSACTIONS, transactions => ({
const getTransactions = () =>
(dispatch) => {
dispatch(begin);
axios({
method: 'GET',
headers: {
Authorization: `Bearer ${getAuthToken()}`
},
url: 'https://api.bankofthecommons.coop/user/v1/last'
fetchApi({
path: TRANSACTIONS_URL
}).then(response => {
let transactions = response.data
dispatch(success(transactions.data))
let transactions = response.data;
dispatch(success(transactions.data));
}).catch(error => {
console.error("ERROR");
});
......@@ -47,4 +43,4 @@ const actions = {
getTransactions
}
export default actions;
\ No newline at end of file
export default actions;
const TRANSACTIONS = "CURRECIES_VALUE"
const TRANSACTIONS = "CURRECIES_VALUE";
export const TRANSACTIONS_URL = "user/v1/last";
export default TRANSACTIONS;
\ No newline at end of file
export default TRANSACTIONS;
import { createAction } from 'redux-actions';
import { getAuthToken, logOut } from 'utils';
import { fetchApi, logOut } from 'utils';
import axios from 'axios';
import USER, { WALLET } from './constants';
......@@ -42,30 +42,20 @@ const successWallet = createAction(WALLET, wallet=> ({
const getMe = () =>
(dispatch) => {
dispatch(begin())
dispatch(beginWallet())
dispatch(begin());
dispatch(beginWallet());
Promise.all(
[axios({
method: 'GET',
headers: {
Authorization: `Bearer ${getAuthToken()}`
},
url: 'https://api.bankofthecommons.coop/user/v1/account'
}),
axios({
method: 'GET',
headers: {
Authorization: `Bearer ${getAuthToken()}`
},
url: 'https://api.bankofthecommons.coop/user/v1/wallet'
})]
[
fetchApi({path: 'user/v1/account'}),
fetchApi({path: 'user/v1/wallet'})
]
).then(responses => {
var user = responses[0];
var wallet = responses[1];
dispatch(success(user.data.data))
dispatch(successWallet(wallet.data.data))
dispatch(success(user.data));
dispatch(successWallet(wallet.data));
}).catch(error => {
logOut()
logOut();
dispatch(fail(error));
dispatch(failWallet(error));
});
......@@ -84,4 +74,4 @@ const actions = {
getMe
};
export default actions;
\ No newline at end of file
export default actions;
import { createAction } from 'redux-actions';
import { tokenName, getAuthToken } from '../../utils';
import axios from 'axios';
import LOGIN, { client_id, client_secret } from './constants';
import { fetchApi, tokenName } from 'utils';
import { LOGIN, client_id, client_secret, LOGIN_URL, ACCOUNT } from './constants';
const reset = createAction(LOGIN, () => ({
......@@ -30,27 +29,6 @@ const success_get_me = createAction(LOGIN, user => ({
status: 'success_get_me'
}));
const getMe = () =>
(dispatch) => {
axios({
method: 'GET',
headers: {
Authorization: `Bearer ${getAuthToken()}`
},
url: 'https://api.bankofthecommons.coop/user/v1/account'
}).then(response => {
if (response.status >= 400) {
const error = new Error(response.statusText);
error.response = response;
throw error;
}
let user = response.data
dispatch(success_get_me(user))
}).catch(error => {
dispatch(error(error))
})
}
const login = (username, password, code) =>
(dispatch) => {
......@@ -63,17 +41,16 @@ const login = (username, password, code) =>
scope: "panel",
username,
password
}
console.log(`[debug] Fetch login: ${JSON.stringify(body)}`)
axios({
url: 'https://api.bankofthecommons.coop/oauth/v3/token',
method: 'POST',
data: body
};
fetchApi({
body,
path: LOGIN_URL,
method: 'post'
}).then(response => {
let user = response.data
dispatch(success(user.access_token))
let user = response;
dispatch(success(user.access_token));
}).catch(error => {
dispatch(fail(error.response.data));
dispatch(fail(error));
});
};
......@@ -84,7 +61,6 @@ const actions = {
success,
fail,
login,
getMe
};
export default actions;
\ No newline at end of file
export default actions;
const LOGIN = 'LOGIN';
export const LOGIN = 'LOGIN';
export const client_id = "54_5yhfa1hjaps00s8o0ogssc8oc80ks4so8cg4soko0ok48gwk4w";
export const client_secret = "4hqd4dzsrvcw8osgkkwsgks8gsgw4kg0c0k4wskow800g8scs0";
export default LOGIN;
\ No newline at end of file
export const LOGIN_URL = 'oauth/v3/token';
export const ACCOUNT = 'user/v1/account';
import LOGIN from './constants';
import { LOGIN } from './constants';
export default function login(state = { status: 'initial' }, action) {
switch (action.type) {
case LOGIN : {
const {
const {
token = state.token,
error = state.error,
status = state.status,
} = action.payload
} = action.payload;
return {
token,
token,
error,
status,
};
......@@ -17,4 +17,4 @@ export default function login(state = { status: 'initial' }, action) {
default:
return state;
}
}
\ No newline at end of file
}
import axios from 'axios';
const BACKEND_URL = 'https://api.bankofthecommons.coop';
export const tokenName = 'auth';
export function getAuthToken() {
......@@ -22,4 +24,28 @@ export function requireAuth(nextState, replace) {
if (!isLoggedIn()) {
replace('/');
}
}
\ No newline at end of file
}
export const fetchApi = (args)=>{
const {
path,
body,
method,
} = args;
const url = `${BACKEND_URL}/${path}`;
const newargs = {
url,
data: body,
method: method || 'GET',
headers: {
Authorization: `Bearer ${getAuthToken()}`,
}
};
if (!isLoggedIn()) {
delete newargs.headers.Authorization;
}
return axios(newargs).then(response => {
return response.data;
});
};
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment