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

Merge branch 'refactor/api_call' into 'master'

Refactor/api call

Closes #4

See merge request faircoop/BankOfTheCommons/wallet-frontend!1
parents a0e33984 8e471a6d
import { createAction } from 'redux-actions';
import CURRENCIES_VALUE from './constants';
import { getAuthToken } from 'utils';
import axios from 'axios'
import CURRENCIES_VALUE from './constants';
import { fetchApi } from 'utils';
const CURRENCIES_VALUE_URL = "exchange/v1/ticker/eur";
const reset = createAction(CURRENCIES_VALUE, () => ({
status: 'initial',
}));
......@@ -25,19 +26,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 +42,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 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 { fetchApi } from 'utils';
const EARNINGS_URL = 'user/v1/wallet/earnings';
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 +41,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 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 from './constants';
import { fetchApi } from 'utils';
const LAST_TRANSACTIONS_URL = "user/v1/last";
const reset = createAction(LAST_TRANSACTIONS, () => ({
status: 'initial',
......@@ -25,15 +25,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 +43,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 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 from './constants';
import { fetchApi } from 'utils';
const MONTH_EARNINGS_URL = "user/v1/wallet/monthearnings";
const reset = createAction(MONTH_EARNINGS, () => ({
status: 'initial',
......@@ -25,15 +25,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 +43,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 default MONTH_EARNINGS;
\ No newline at end of file
export default MONTH_EARNINGS;
import { createAction } from 'redux-actions';
import TRANSACTIONS from './constants';
import TRANSACTIONS from './constants';
import { getAuthToken } from 'utils';
import axios from 'axios'
import { fetchApi } from 'utils';
const TRANSACTIONS_URL = "user/v1/last";
const reset = createAction(TRANSACTIONS, () => ({
......@@ -25,15 +27,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 +45,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 default TRANSACTIONS;
\ No newline at end of file
export default TRANSACTIONS;
import { createAction } from 'redux-actions';
import { getAuthToken, logOut } from 'utils';
import axios from 'axios';
import { fetchApi, logOut } from 'utils';
import USER, { WALLET } from './constants';
......@@ -42,30 +41,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 +73,4 @@ const actions = {
getMe
};
export default actions;
\ No newline at end of file
export default actions;
export const USER = 'USER'
export const WALLET = 'WALLET'
export default USER;
\ No newline at end of file
export const USER = 'USER';
export const WALLET = 'WALLET';
export default USER;
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 } from './constants';
const LOGIN_URL = 'oauth/v3/token';
const reset = createAction(LOGIN, () => ({
......@@ -30,27 +31,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 +43,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 +63,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
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,35 @@ 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 => {
console.log(`[response] ${response}`);
return response.data;
}).catch(error => {
if(error.response.status === 401 && isLoggedIn()){
logOut();
window.history.go('/login');
}
});
};
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