Skip to content

Commit d273d30

Browse files
committed
Support for environment configs and a new, simple api client using it
1 parent b3967e5 commit d273d30

File tree

5 files changed

+26
-35
lines changed

5 files changed

+26
-35
lines changed

src/actions/account.js

+2-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import fetch from 'isomorphic-fetch';
21
import { newPurchase } from './purchase';
2+
import { apiCall } from '../api';
33

44
export const REQUEST_ACCOUNT = 'REQUEST_ACCOUNT';
55
export const LOGIN_ACCOUNT = 'LOGIN_ACCOUNT';
@@ -25,13 +25,7 @@ export function login(cardId) {
2525
if(!account.length) {
2626
dispatch(requestAccount(cardId));
2727
if(cardId) {
28-
return fetch(`http://dev.foocash.me/api/accounts/${cardId}/`, {
29-
headers: {
30-
'Accept': 'application/json',
31-
'Content-Type': 'application/json',
32-
'Authorization': 'Token 03be53c1ab8f74edac76bd60695f84f089634c80'
33-
}
34-
})
28+
return apiCall(`/accounts/${cardId}/`)
3529
.then(response => response.json())
3630
.then(data => {
3731
dispatch(newPurchase());

src/actions/product.js

+3-19
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fetch from 'isomorphic-fetch';
1+
import { apiCall } from '../api';
22

33
export const ADD_PRODUCT = 'ADD_PRODUCT';
44
export const INCREASE_PRODUCT_QTY = 'INCREASE_PRODUCT_QTY';
@@ -70,15 +70,7 @@ class ProductFetcher {
7070
dispatch(requestProduct(ean));
7171
dispatch(receiveProduct(this.cache.get(ean, true)));
7272

73-
this.promises[ean] = fetch(
74-
`http://dev.foocash.me/api/products/?code=${ean}`, {
75-
headers: {
76-
'Accept': 'application/json',
77-
'Content-Type': 'application/json',
78-
'Authorization': 'Token 03be53c1ab8f74edac76bd60695f84f089634c80'
79-
}
80-
}
81-
)
73+
this.promises[ean] = apiCall(`/products/?code=${ean}`)
8274
.then(response => response.json())
8375
.then(data => {
8476
if (data.length) {
@@ -93,15 +85,7 @@ class ProductFetcher {
9385
// product record not found in the cache, fetch it and dispatch
9486
// appropriate actions
9587
dispatch(requestProduct(ean));
96-
this.promises[ean] = fetch(
97-
`http://dev.foocash.me/api/products/?code=${ean}`, {
98-
headers: {
99-
'Accept': 'application/json',
100-
'Content-Type': 'application/json',
101-
'Authorization': 'Token 03be53c1ab8f74edac76bd60695f84f089634c80'
102-
}
103-
}
104-
)
88+
this.promises[ean] = apiCall(`/products/?code=${ean}`)
10589
.then(response => response.json())
10690
.then(data => {
10791
if (data.length) {

src/actions/purchase.js

+2-7
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import fetch from 'isomorphic-fetch';
1+
import { apiCall } from '../api';
22
import { clearProducts } from './product';
33
import { clearAccount } from './account';
44

@@ -47,13 +47,8 @@ export function requestPurchase() {
4747
account,
4848
products
4949
} = getState();
50-
return fetch(`http://dev.foocash.me/api/purchases/`, {
50+
return apiCall('/purchases/', {
5151
method: 'post',
52-
headers: {
53-
'Accept': 'application/json',
54-
'Content-Type': 'application/json',
55-
'Authorization': 'Token ###'
56-
},
5752
body: JSON.stringify({
5853
account_id: account.id,
5954
products: products.products.filter(

src/api.js

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import fetch from 'isomorphic-fetch';
2+
3+
export function apiCall(endpoint, options) {
4+
var apiHost = ENV.api.host;
5+
var apiKey = ENV.api.key;
6+
7+
return fetch(`${apiHost}/api${endpoint}`, {
8+
headers: {
9+
'Accept': 'application/json',
10+
'Content-Type': 'application/json',
11+
'Authorization': `Token ${apiKey}`
12+
},
13+
...options
14+
});
15+
}

webpack.config.js

+4-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@ module.exports = {
1515
plugins: [
1616
new webpack.optimize.OccurenceOrderPlugin(),
1717
new webpack.HotModuleReplacementPlugin(),
18-
new webpack.NoErrorsPlugin()
18+
new webpack.NoErrorsPlugin(),
19+
new webpack.DefinePlugin({
20+
ENV: JSON.stringify(require(path.join(__dirname, process.env.SETTINGS)))
21+
}),
1922
],
2023
module: {
2124
loaders: [{

0 commit comments

Comments
 (0)