Skip to content

Commit b805754

Browse files
authored
Merge pull request #1452 from andrewn/chore/config-handler
Helpers for config and API client
2 parents 8c33c6e + 6d90dd2 commit b805754

20 files changed

+155
-117
lines changed

Diff for: client/components/Nav.jsx

+6-7
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@ import * as projectActions from '../modules/IDE/actions/project';
1010
import { setAllAccessibleOutput } from '../modules/IDE/actions/preferences';
1111
import { logoutUser } from '../modules/User/actions';
1212

13+
import getConfig from '../utils/getConfig';
1314
import { metaKeyName, } from '../utils/metaKey';
1415

1516
import CaretLeftIcon from '../images/left-arrow.svg';
1617
import TriangleIcon from '../images/down-filled-triangle.svg';
1718
import LogoIcon from '../images/p5js-logo-small.svg';
1819

19-
const __process = (typeof global !== 'undefined' ? global : window).process;
20-
2120
class Nav extends React.PureComponent {
2221
constructor(props) {
2322
super(props);
@@ -272,7 +271,7 @@ class Nav extends React.PureComponent {
272271
New
273272
</button>
274273
</li>
275-
{ __process.env.LOGIN_ENABLED && (!this.props.project.owner || this.isUserOwner()) &&
274+
{ getConfig('LOGIN_ENABLED') && (!this.props.project.owner || this.isUserOwner()) &&
276275
<li className="nav__dropdown-item">
277276
<button
278277
onClick={this.handleSave}
@@ -324,7 +323,7 @@ class Nav extends React.PureComponent {
324323
Open
325324
</Link>
326325
</li> }
327-
{__process.env.UI_COLLECTIONS_ENABLED &&
326+
{getConfig('UI_COLLECTIONS_ENABLED') &&
328327
this.props.user.authenticated &&
329328
this.props.project.id &&
330329
<li className="nav__dropdown-item">
@@ -337,7 +336,7 @@ class Nav extends React.PureComponent {
337336
Add to Collection
338337
</Link>
339338
</li>}
340-
{ __process.env.EXAMPLES_ENABLED &&
339+
{ getConfig('EXAMPLES_ENABLED') &&
341340
<li className="nav__dropdown-item">
342341
<Link
343342
to="/p5/sketches"
@@ -587,7 +586,7 @@ class Nav extends React.PureComponent {
587586
My sketches
588587
</Link>
589588
</li>
590-
{__process.env.UI_COLLECTIONS_ENABLED &&
589+
{getConfig('UI_COLLECTIONS_ENABLED') &&
591590
<li className="nav__dropdown-item">
592591
<Link
593592
to={`/${this.props.user.username}/collections`}
@@ -635,7 +634,7 @@ class Nav extends React.PureComponent {
635634
}
636635

637636
renderUserMenu(navDropdownState) {
638-
const isLoginEnabled = __process.env.LOGIN_ENABLED;
637+
const isLoginEnabled = getConfig('LOGIN_ENABLED');
639638
const isAuthenticated = this.props.user.authenticated;
640639

641640
if (isLoginEnabled && isAuthenticated) {

Diff for: client/modules/App/App.jsx

+2-3
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
11
import PropTypes from 'prop-types';
22
import React from 'react';
33
import { connect } from 'react-redux';
4+
import getConfig from '../../utils/getConfig';
45
import DevTools from './components/DevTools';
56
import { setPreviousPath } from '../IDE/actions/ide';
67

7-
const __process = (typeof global !== 'undefined' ? global : window).process;
8-
98
class App extends React.Component {
109
constructor(props, context) {
1110
super(props, context);
@@ -35,7 +34,7 @@ class App extends React.Component {
3534
render() {
3635
return (
3736
<div className="app">
38-
{this.state.isMounted && !window.devToolsExtension && __process.env.NODE_ENV === 'development' && <DevTools />}
37+
{this.state.isMounted && !window.devToolsExtension && getConfig('NODE_ENV') === 'development' && <DevTools />}
3938
{this.props.children}
4039
</div>
4140
);

Diff for: client/modules/IDE/actions/assets.js

+3-6
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
1-
import axios from 'axios';
1+
import apiClient from '../../../utils/apiClient';
22
import * as ActionTypes from '../../../constants';
33
import { startLoader, stopLoader } from './loader';
44

5-
const __process = (typeof global !== 'undefined' ? global : window).process;
6-
const ROOT_URL = __process.env.API_URL;
7-
85
function setAssets(assets, totalSize) {
96
return {
107
type: ActionTypes.SET_ASSETS,
@@ -16,7 +13,7 @@ function setAssets(assets, totalSize) {
1613
export function getAssets() {
1714
return (dispatch) => {
1815
dispatch(startLoader());
19-
axios.get(`${ROOT_URL}/S3/objects`, { withCredentials: true })
16+
apiClient.get('/S3/objects')
2017
.then((response) => {
2118
dispatch(setAssets(response.data.assets, response.data.totalSize));
2219
dispatch(stopLoader());
@@ -39,7 +36,7 @@ export function deleteAsset(assetKey) {
3936

4037
export function deleteAssetRequest(assetKey) {
4138
return (dispatch) => {
42-
axios.delete(`${ROOT_URL}/S3/${assetKey}`, { withCredentials: true })
39+
apiClient.delete(`/S3/${assetKey}`)
4340
.then((response) => {
4441
dispatch(deleteAsset(assetKey));
4542
})

Diff for: client/modules/IDE/actions/collections.js

+14-16
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,9 @@
1-
import axios from 'axios';
21
import { browserHistory } from 'react-router';
2+
import apiClient from '../../../utils/apiClient';
33
import * as ActionTypes from '../../../constants';
44
import { startLoader, stopLoader } from './loader';
55
import { setToastText, showToast } from './toast';
66

7-
const __process = (typeof global !== 'undefined' ? global : window).process;
8-
const ROOT_URL = __process.env.API_URL;
97

108
const TOAST_DISPLAY_TIME_MS = 1500;
119

@@ -15,11 +13,11 @@ export function getCollections(username) {
1513
dispatch(startLoader());
1614
let url;
1715
if (username) {
18-
url = `${ROOT_URL}/${username}/collections`;
16+
url = `/${username}/collections`;
1917
} else {
20-
url = `${ROOT_URL}/collections`;
18+
url = '/collections';
2119
}
22-
axios.get(url, { withCredentials: true })
20+
apiClient.get(url)
2321
.then((response) => {
2422
dispatch({
2523
type: ActionTypes.SET_COLLECTIONS,
@@ -41,8 +39,8 @@ export function getCollections(username) {
4139
export function createCollection(collection) {
4240
return (dispatch) => {
4341
dispatch(startLoader());
44-
const url = `${ROOT_URL}/collections`;
45-
return axios.post(url, collection, { withCredentials: true })
42+
const url = '/collections';
43+
return apiClient.post(url, collection)
4644
.then((response) => {
4745
dispatch({
4846
type: ActionTypes.CREATE_COLLECTION
@@ -73,8 +71,8 @@ export function createCollection(collection) {
7371
export function addToCollection(collectionId, projectId) {
7472
return (dispatch) => {
7573
dispatch(startLoader());
76-
const url = `${ROOT_URL}/collections/${collectionId}/${projectId}`;
77-
return axios.post(url, { withCredentials: true })
74+
const url = `/collections/${collectionId}/${projectId}`;
75+
return apiClient.post(url)
7876
.then((response) => {
7977
dispatch({
8078
type: ActionTypes.ADD_TO_COLLECTION,
@@ -105,8 +103,8 @@ export function addToCollection(collectionId, projectId) {
105103
export function removeFromCollection(collectionId, projectId) {
106104
return (dispatch) => {
107105
dispatch(startLoader());
108-
const url = `${ROOT_URL}/collections/${collectionId}/${projectId}`;
109-
return axios.delete(url, { withCredentials: true })
106+
const url = `/collections/${collectionId}/${projectId}`;
107+
return apiClient.delete(url)
110108
.then((response) => {
111109
dispatch({
112110
type: ActionTypes.REMOVE_FROM_COLLECTION,
@@ -136,8 +134,8 @@ export function removeFromCollection(collectionId, projectId) {
136134

137135
export function editCollection(collectionId, { name, description }) {
138136
return (dispatch) => {
139-
const url = `${ROOT_URL}/collections/${collectionId}`;
140-
return axios.patch(url, { name, description }, { withCredentials: true })
137+
const url = `/collections/${collectionId}`;
138+
return apiClient.patch(url, { name, description })
141139
.then((response) => {
142140
dispatch({
143141
type: ActionTypes.EDIT_COLLECTION,
@@ -159,8 +157,8 @@ export function editCollection(collectionId, { name, description }) {
159157

160158
export function deleteCollection(collectionId) {
161159
return (dispatch) => {
162-
const url = `${ROOT_URL}/collections/${collectionId}`;
163-
return axios.delete(url, { withCredentials: true })
160+
const url = `/collections/${collectionId}`;
161+
return apiClient.delete(url)
164162
.then((response) => {
165163
dispatch({
166164
type: ActionTypes.DELETE_COLLECTION,

Diff for: client/modules/IDE/actions/files.js

+4-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import axios from 'axios';
21
import objectID from 'bson-objectid';
32
import blobUtil from 'blob-util';
43
import { reset } from 'redux-form';
4+
import apiClient from '../../../utils/apiClient';
55
import * as ActionTypes from '../../../constants';
66
import { setUnsavedChanges, closeNewFolderModal, closeNewFileModal } from './ide';
77
import { setProjectSavedTime } from './project';
88

9-
const __process = (typeof global !== 'undefined' ? global : window).process;
10-
const ROOT_URL = __process.env.API_URL;
119

1210
function appendToFilename(filename, string) {
1311
const dotIndex = filename.lastIndexOf('.');
@@ -50,7 +48,7 @@ export function createFile(formProps) {
5048
parentId,
5149
children: []
5250
};
53-
axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true })
51+
apiClient.post(`/projects/${state.project.id}/files`, postParams)
5452
.then((response) => {
5553
dispatch({
5654
type: ActionTypes.CREATE_FILE,
@@ -106,7 +104,7 @@ export function createFolder(formProps) {
106104
parentId,
107105
fileType: 'folder'
108106
};
109-
axios.post(`${ROOT_URL}/projects/${state.project.id}/files`, postParams, { withCredentials: true })
107+
apiClient.post(`/projects/${state.project.id}/files`, postParams)
110108
.then((response) => {
111109
dispatch({
112110
type: ActionTypes.CREATE_FILE,
@@ -161,7 +159,7 @@ export function deleteFile(id, parentId) {
161159
parentId
162160
}
163161
};
164-
axios.delete(`${ROOT_URL}/projects/${state.project.id}/files/${id}`, deleteConfig, { withCredentials: true })
162+
apiClient.delete(`/projects/${state.project.id}/files/${id}`, deleteConfig)
165163
.then(() => {
166164
dispatch({
167165
type: ActionTypes.DELETE_FILE,

Diff for: client/modules/IDE/actions/preferences.js

+2-5
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,8 @@
1-
import axios from 'axios';
1+
import apiClient from '../../../utils/apiClient';
22
import * as ActionTypes from '../../../constants';
33

4-
const __process = (typeof global !== 'undefined' ? global : window).process;
5-
const ROOT_URL = __process.env.API_URL;
6-
74
function updatePreferences(formParams, dispatch) {
8-
axios.put(`${ROOT_URL}/preferences`, formParams, { withCredentials: true })
5+
apiClient.put('/preferences', formParams)
96
.then(() => {
107
})
118
.catch((error) => {

Diff for: client/modules/IDE/actions/project.js

+11-11
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import { browserHistory } from 'react-router';
2-
import axios from 'axios';
32
import objectID from 'bson-objectid';
43
import each from 'async/each';
54
import isEqual from 'lodash/isEqual';
5+
import apiClient from '../../../utils/apiClient';
6+
import getConfig from '../../../utils/getConfig';
67
import * as ActionTypes from '../../../constants';
78
import { showToast, setToastText } from './toast';
89
import {
@@ -14,8 +15,7 @@ import {
1415
} from './ide';
1516
import { clearState, saveState } from '../../../persistState';
1617

17-
const __process = (typeof global !== 'undefined' ? global : window).process;
18-
const ROOT_URL = __process.env.API_URL;
18+
const ROOT_URL = getConfig('API_URL');
1919

2020
export function setProject(project) {
2121
return {
@@ -52,7 +52,7 @@ export function setNewProject(project) {
5252
export function getProject(id, username) {
5353
return (dispatch, getState) => {
5454
dispatch(justOpenedProject());
55-
axios.get(`${ROOT_URL}/${username}/projects/${id}`, { withCredentials: true })
55+
apiClient.get(`/${username}/projects/${id}`)
5656
.then((response) => {
5757
dispatch(setProject(response.data));
5858
dispatch(setUnsavedChanges(false));
@@ -142,7 +142,7 @@ export function saveProject(selectedFile = null, autosave = false) {
142142
fileToUpdate.content = selectedFile.content;
143143
}
144144
if (state.project.id) {
145-
return axios.put(`${ROOT_URL}/projects/${state.project.id}`, formParams, { withCredentials: true })
145+
return apiClient.put(`/projects/${state.project.id}`, formParams)
146146
.then((response) => {
147147
dispatch(endSavingProject());
148148
dispatch(setUnsavedChanges(false));
@@ -177,7 +177,7 @@ export function saveProject(selectedFile = null, autosave = false) {
177177
});
178178
}
179179

180-
return axios.post(`${ROOT_URL}/projects`, formParams, { withCredentials: true })
180+
return apiClient.post('/projects', formParams)
181181
.then((response) => {
182182
dispatch(endSavingProject());
183183
const { hasChanges, synchedProject } = getSynchedProject(getState(), response.data);
@@ -260,7 +260,7 @@ export function cloneProject(id) {
260260
if (!id) {
261261
resolve(getState());
262262
} else {
263-
fetch(`${ROOT_URL}/projects/${id}`)
263+
apiClient.get(`/projects/${id}`)
264264
.then(res => res.json())
265265
.then(data => resolve({
266266
files: data.files,
@@ -287,7 +287,7 @@ export function cloneProject(id) {
287287
const formParams = {
288288
url: file.url
289289
};
290-
axios.post(`${ROOT_URL}/S3/copy`, formParams, { withCredentials: true })
290+
apiClient.post('/S3/copy', formParams)
291291
.then((response) => {
292292
file.url = response.data.url;
293293
callback(null);
@@ -298,7 +298,7 @@ export function cloneProject(id) {
298298
}, (err) => {
299299
// if not errors in duplicating the files on S3, then duplicate it
300300
const formParams = Object.assign({}, { name: `${state.project.name} copy` }, { files: newFiles });
301-
axios.post(`${ROOT_URL}/projects`, formParams, { withCredentials: true })
301+
apiClient.post('/projects', formParams)
302302
.then((response) => {
303303
browserHistory.push(`/${response.data.user.username}/sketches/${response.data.id}`);
304304
dispatch(setNewProject(response.data));
@@ -337,7 +337,7 @@ export function setProjectSavedTime(updatedAt) {
337337
export function changeProjectName(id, newName) {
338338
return (dispatch, getState) => {
339339
const state = getState();
340-
axios.put(`${ROOT_URL}/projects/${id}`, { name: newName }, { withCredentials: true })
340+
apiClient.put(`/projects/${id}`, { name: newName })
341341
.then((response) => {
342342
if (response.status === 200) {
343343
dispatch({
@@ -364,7 +364,7 @@ export function changeProjectName(id, newName) {
364364

365365
export function deleteProject(id) {
366366
return (dispatch, getState) => {
367-
axios.delete(`${ROOT_URL}/projects/${id}`, { withCredentials: true })
367+
apiClient.delete(`/projects/${id}`)
368368
.then(() => {
369369
const state = getState();
370370
if (id === state.project.id) {

Diff for: client/modules/IDE/actions/projects.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
import axios from 'axios';
1+
import apiClient from '../../../utils/apiClient';
22
import * as ActionTypes from '../../../constants';
33
import { startLoader, stopLoader } from './loader';
44

5-
const __process = (typeof global !== 'undefined' ? global : window).process;
6-
const ROOT_URL = __process.env.API_URL;
7-
85
// eslint-disable-next-line
96
export function getProjects(username) {
107
return (dispatch) => {
118
dispatch(startLoader());
129
let url;
1310
if (username) {
14-
url = `${ROOT_URL}/${username}/projects`;
11+
url = `/${username}/projects`;
1512
} else {
16-
url = `${ROOT_URL}/projects`;
13+
url = '/projects';
1714
}
18-
axios.get(url, { withCredentials: true })
15+
apiClient.get(url)
1916
.then((response) => {
2017
dispatch({
2118
type: ActionTypes.SET_PROJECTS,

0 commit comments

Comments
 (0)