From 589bc906052e85fb72e964a57822d516ee7e1f6e Mon Sep 17 00:00:00 2001 From: Harrison Healey Date: Tue, 11 Oct 2016 09:52:40 -0400 Subject: [PATCH] Refactored client.js to have no knowledge of the redux stores --- package.json | 2 +- src/actions/client.js | 50 +++++++++++++---------- src/actions/general.js | 60 +++++++++++++--------------- src/actions/helpers.js | 18 +++++++++ src/components/select_server_view.js | 31 ++++---------- 5 files changed, 84 insertions(+), 77 deletions(-) diff --git a/package.json b/package.json index c6cae6225..acf7a8ca6 100644 --- a/package.json +++ b/package.json @@ -28,7 +28,7 @@ "scripts": { "check": "eslint --ext \".jsx\" --ignore-pattern node_modules --quiet .", "start": "node node_modules/react-native/local-cli/cli.js start", - "test": "mocha --compilers js:babel-register", + "test": "mocha --compilers js:babel-register test/**/*.test.js", "postinstall": "remotedev-debugger --hostname localhost --port 5678 --injectserver" }, "babel": { diff --git a/src/actions/client.js b/src/actions/client.js index a4f6166ea..8d4997479 100644 --- a/src/actions/client.js +++ b/src/actions/client.js @@ -3,27 +3,18 @@ import 'isomorphic-fetch'; -import {requestData, requestSuccess, requestFailure} from './helpers.js'; - -// const HEADER_X_VERSION_ID = 'x-versin-id'; -// const HEADER_X_CLUSTER_ID = 'x-cluster-id'; -// const HEADER_TOKEN = 'token'; -const HEADER_BEARER = 'BEARER'; const HEADER_AUTH = 'Authorization'; +const HEADER_BEARER = 'BEARER'; +const HEADER_REQUESTED_WITH = 'X-Requested-With'; export class Client { constructor() { this.teamId = ''; - this.serverVersion = ''; - this.clusterId = ''; + this.serverVersion = ''; // ?? this.logToConsole = false; - this.useToken = false; this.token = ''; this.url = ''; this.urlVersion = '/api/v3'; - this.defaultHeaders = { - 'X-Requested-With': 'XMLHttpRequest' - }; this.translations = { connectionError: 'There appears to be a problem with your internet connection.', @@ -138,26 +129,45 @@ export class Client { } } - doFetch(REQUEST, SUCCESS, FAILURE, url) { - return (dispatch) => { - dispatch(requestData(REQUEST)); + getOptions(options) { + return { + headers: { + [HEADER_AUTH]: this.token, + [HEADER_REQUESTED_WITH]: 'XMLHttpRequest' + }, + ...options + }; + } - return fetch(url).then( + getPing = (onRequest, onSuccess, onFailure) => { + return this.doFetch(`${this.getGeneralRoute()}/ping`, {method: 'get'}, onRequest, onSuccess, onFailure); + } + + getClientConfig = (onRequest, onSuccess, onFailure) => { + return this.doFetch(`${this.getGeneralRoute()}/client_props`, {method: 'get'}, onRequest, onSuccess, onFailure); + } + + doFetch = (url, options, onRequest, onSuccess, onFailure) => { + return () => { + onRequest(); + + return fetch(url, this.getOptions(options)).then( (response) => { return response.json().then((json) => ({json, response})); - }).then(({json, response}) => { + }).then(({response, json}) => { if (!response.ok) { return Promise.reject(json); } - return dispatch(requestSuccess(SUCCESS, json)); + return onSuccess(json); }).catch((err) => { if (this.logToConsole) { console.log(err); // eslint-disable-line no-console } - dispatch(requestFailure(FAILURE, err)); - }); + onFailure(err); + } + ); }; } } diff --git a/src/actions/general.js b/src/actions/general.js index b30d393cb..cd7113b09 100644 --- a/src/actions/general.js +++ b/src/actions/general.js @@ -2,43 +2,37 @@ // See License.txt for license information. import Client from './client.js'; -import {requestData, requestSuccess, requestFailure} from './helpers.js'; - -export const CLIENT_CONFIG_REQUEST = 'CLIENT_CONFIG_REQUEST'; -export const CLIENT_CONFIG_SUCCESS = 'CLIENT_CONFIG_SUCCESS'; -export const CLIENT_CONFIG_FAILURE = 'CLIENT_CONFIG_FAILURE'; - -function fetchClientConfig() { - return (dispatch) => { - dispatch(requestData(CLIENT_CONFIG_REQUEST)); - - Client.getClientConfig( - (data) => { - dispatch(requestSuccess(CLIENT_CONFIG_SUCCESS, data)); - }, - (err) => { - dispatch(requestFailure(CLIENT_CONFIG_FAILURE, err)); - } - ); - }; -} - -export function loadClientConfig() { - return (dispatch, getState) => { // eslint-disable-line no-unused-vars - return dispatch(fetchClientConfig()); - }; -} +import {bindClientFunc} from './helpers.js'; export const PING_REQUEST = 'PING_REQUEST'; export const PING_SUCCESS = 'PING_SUCCESS'; export const PING_FAILURE = 'PING_FAILURE'; -function fetchPing() { - return Client.doFetch(PING_REQUEST, PING_SUCCESS, PING_FAILURE, `${Client.getGeneralRoute()}/ping`); +export function loadPing() { + return bindClientFunc(Client.getPing, PING_REQUEST, PING_SUCCESS, PING_FAILURE); } -export function loadPing() { - return (dispatch) => { - return dispatch(fetchPing()); - }; -} \ No newline at end of file +// export const CLIENT_CONFIG_REQUEST = 'CLIENT_CONFIG_REQUEST'; +// export const CLIENT_CONFIG_SUCCESS = 'CLIENT_CONFIG_SUCCESS'; +// export const CLIENT_CONFIG_FAILURE = 'CLIENT_CONFIG_FAILURE'; + +// function fetchClientConfig() { +// return (dispatch) => { +// dispatch(requestData(CLIENT_CONFIG_REQUEST)); + +// Client.getClientConfig( +// (data) => { +// dispatch(requestSuccess(CLIENT_CONFIG_SUCCESS, data)); +// }, +// (err) => { +// dispatch(requestFailure(CLIENT_CONFIG_FAILURE, err)); +// } +// ); +// }; +// } + +// export function loadClientConfig() { +// return (dispatch, getState) => { +// return dispatch(Client.getClientConfig(CLIENT_CONFIG_REQUEST, CLIENT_CONFIG_SUCCESS, CLIENT_CONFIG_FAILURE), getState); +// }; +// } \ No newline at end of file diff --git a/src/actions/helpers.js b/src/actions/helpers.js index 2413cc057..f1a14818b 100644 --- a/src/actions/helpers.js +++ b/src/actions/helpers.js @@ -29,3 +29,21 @@ export function emptyError() { status_code: '' }; } + +export function bindClientFunc(clientFunc, request, success, failure) { + return (dispatch, getState) => { + function onRequest() { + dispatch(requestData(request)); + } + + function onSuccess(json) { + dispatch(requestSuccess(success, json)); + } + + function onFailure(err) { + dispatch(requestFailure(failure, err)); + } + + return dispatch(clientFunc(onRequest, onSuccess, onFailure), getState); + }; +} \ No newline at end of file diff --git a/src/components/select_server_view.js b/src/components/select_server_view.js index 94d1e9780..883aa7ee8 100644 --- a/src/components/select_server_view.js +++ b/src/components/select_server_view.js @@ -8,7 +8,7 @@ import {connect} from 'react-redux'; import {loadPing} from 'actions/general.js'; import Button from './button.js'; -import {Image, StyleSheet, Text, TextInput, View} from 'react-native'; +import {AsyncStorage, Image, StyleSheet, Text, TextInput, View} from 'react-native'; import KeyboardSpacer from 'react-native-keyboard-spacer'; import {GlobalStyles} from 'styles'; @@ -41,35 +41,20 @@ class SelectServerView extends Component { super(props); this.state = { - serverUrl: '' + serverUrl: 'http://localhost:8065' }; } onClick = () => { Client.setUrl(this.state.serverUrl); + this.props.loadPing().then(() => { - // console.log('FOUND IT - AAAAAAAAAAAAAAAAAA ' + this.props.ping.loading); - // console.log(this.props.ping); + AsyncStorage.setItem('serverUrl', this.state.serverUrl, () => { + if (!this.props.ping.error) { + this.props.onProceed(); + } + }); }); - - // Client.getPing( - // () => { - // AsyncStorage.setItem('serverUrl', this.state.serverUrl, () => { - // this.props.onProceed(); - // }); - - // this.setState({ - // error: '', - // loading: false - // }); - // }, - // () => { - // this.setState({ - // error: 'The URL does not appear to be a Mattermost Server. Please check http vs https. You should not include the team name.', - // loading: false - // }); - // } - // ); } render() {