Refactored client.js to have no knowledge of the redux stores
This commit is contained in:
parent
39467192f0
commit
589bc90605
5 changed files with 84 additions and 77 deletions
|
|
@ -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": {
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
);
|
||||
};
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -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());
|
||||
};
|
||||
}
|
||||
// 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);
|
||||
// };
|
||||
// }
|
||||
|
|
@ -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);
|
||||
};
|
||||
}
|
||||
|
|
@ -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() {
|
||||
|
|
|
|||
Loading…
Reference in a new issue