Add actually functioning style check and fix style in api.js

This commit is contained in:
JP1998 2018-12-06 09:57:51 +01:00
parent 4b99c4b2dd
commit 5aa97e162d
4 changed files with 161 additions and 132 deletions

View File

@ -4,10 +4,16 @@
"es6": true, "es6": true,
"node": true "node": true
}, },
"plugins": [
"react"
],
"extends": "eslint:recommended", "extends": "eslint:recommended",
"parserOptions": { "parserOptions": {
"ecmaVersion": 2018, "ecmaVersion": 2018,
"sourceType": "module" "sourceType": "module",
"ecmaFeatures": {
"jsx": true
}
}, },
"rules": { "rules": {
"indent": [ "indent": [

256
js/api.js
View File

@ -1,13 +1,12 @@
import { createStore, applyMiddleware, combineReducers } from 'redux' import { createStore, applyMiddleware, combineReducers } from 'redux';
import { composeWithDevTools } from 'redux-devtools-extension' import { composeWithDevTools } from 'redux-devtools-extension';
import thunkMiddleware from 'redux-thunk' import thunkMiddleware from 'redux-thunk';
import { errorMessages } from './constants' import { errorMessages } from './constants';
const axios = require('axios'); const axios = require('axios');
const api_url = 'https://api.turnie.re'; const api_url = 'https://api.turnie.re';
const actiontypes_userinfo = { const actiontypes_userinfo = {
'REGISTER' : 'REGISTER', 'REGISTER' : 'REGISTER',
'REGISTER_RESULT_SUCCESS' : 'REGISTER_RESULT_SUCCESS', 'REGISTER_RESULT_SUCCESS' : 'REGISTER_RESULT_SUCCESS',
@ -27,7 +26,7 @@ const actiontypes_userinfo = {
'REHYDRATE' : 'USERINFO_REHYDRATE', 'REHYDRATE' : 'USERINFO_REHYDRATE',
'CLEAR' : 'USERINFO_CLEAR', 'CLEAR' : 'USERINFO_CLEAR',
} };
const defaultstate_userinfo = { const defaultstate_userinfo = {
isSignedIn : false, isSignedIn : false,
@ -39,7 +38,7 @@ const defaultstate_userinfo = {
client : null, client : null,
expiry : null, expiry : null,
uid : null uid : null
} };
export function postRequest(state, url, data) { export function postRequest(state, url, data) {
return axios.post(api_url + url, data, { return axios.post(api_url + url, data, {
@ -81,7 +80,7 @@ function storeOptionalToken(response) {
expiry : response.headers['expiry'], expiry : response.headers['expiry'],
uid : response.headers['uid'] uid : response.headers['uid']
} }
}) });
} }
} }
@ -102,139 +101,136 @@ function checkForAuthenticationHeaders(response) {
const reducer_userinfo = (state = defaultstate_userinfo, action) => { const reducer_userinfo = (state = defaultstate_userinfo, action) => {
switch(action.type) { switch(action.type) {
case actiontypes_userinfo.REGISTER: case actiontypes_userinfo.REGISTER:
postRequest(state, '/users', { postRequest(state, '/users', {
'username' : action.parameters.username, 'username' : action.parameters.username,
'email' : action.parameters.email, 'email' : action.parameters.email,
'password' : action.parameters.password 'password' : action.parameters.password
}).then((resp) => { }).then((resp) => {
__store.dispatch({
type : actiontypes_userinfo.REGISTER_RESULT_SUCCESS
});
storeOptionalToken(resp);
}).catch((error) => {
if (error.response) {
__store.dispatch({ __store.dispatch({
type : actiontypes_userinfo.REGISTER_RESULT_SUCCESS 'type' : actiontypes_userinfo.REGISTER_RESULT_ERROR,
}); 'parameters' : {
storeOptionalToken(resp); 'errorMessages' : error.response.data.errors.full_messages
}).catch((error) => {
if (error.response) {
__store.dispatch({
'type' : actiontypes_userinfo.REGISTER_RESULT_ERROR,
'parameters' : {
'errorMessages' : error.response.data.errors.full_messages
}
});
storeOptionalToken(error.response);
} else {
__store.dispatch({
'type' : actiontypes_userinfo.REGISTER_RESULT_ERROR,
'parameters' : {
'errorMessages' : [
errorMessages['registration_errorunknown']['en']
]
}
})
}
});
return Object.assign({}, state, {});
case actiontypes_userinfo.REGISTER_RESULT_SUCCESS:
return Object.assign({}, state, {
error : false,
errorMessages : []
});
case actiontypes_userinfo.REGISTER_RESULT_ERROR:
return Object.assign({}, state, {
error : true,
errorMessages : action.parameters.errorMessages
});
case actiontypes_userinfo.LOGIN:
postRequest(state, '/users/sign_in', {
email : action.parameters.email,
password : action.parameters.password
}).then((resp) => {
__store.dispatch({
type : actiontypes_userinfo.LOGIN_RESULT_SUCCESS,
parameters : {
username : resp.data.data.username,
} }
}); });
storeOptionalToken(resp); storeOptionalToken(error.response);
}).catch((error) => { } else {
if(error.response) { __store.dispatch({
__store.dispatch({ 'type' : actiontypes_userinfo.REGISTER_RESULT_ERROR,
'type' : actiontypes_userinfo.LOGIN_RESULT_ERROR, 'parameters' : {
'parameters' : { 'errorMessages' : [
'errorMessages' : error.response.data.errors errorMessages['registration_errorunknown']['en']
} ]
}); }
storeOptionalToken(error.response); });
} else { }
__store.dispatch({ });
'type' : actiontypes_userinfo.LOGIN_RESULT_ERROR, return Object.assign({}, state, {});
'parameters' : { case actiontypes_userinfo.REGISTER_RESULT_SUCCESS:
'errorMessages' : [ errorMessages['login_errorunknown']['en'] ] return Object.assign({}, state, {
} error : false,
}); errorMessages : []
});
case actiontypes_userinfo.REGISTER_RESULT_ERROR:
return Object.assign({}, state, {
error : true,
errorMessages : action.parameters.errorMessages
});
case actiontypes_userinfo.LOGIN:
postRequest(state, '/users/sign_in', {
email : action.parameters.email,
password : action.parameters.password
}).then((resp) => {
__store.dispatch({
type : actiontypes_userinfo.LOGIN_RESULT_SUCCESS,
parameters : {
username : resp.data.data.username,
} }
}); });
return Object.assign({}, state, {}); storeOptionalToken(resp);
case actiontypes_userinfo.LOGIN_RESULT_SUCCESS: }).catch((error) => {
return Object.assign({}, state, { if(error.response) {
isSignedIn : true, __store.dispatch({
error : false, 'type' : actiontypes_userinfo.LOGIN_RESULT_ERROR,
errorMessages : [], 'parameters' : {
username : action.parameters.username, 'errorMessages' : error.response.data.errors
}); }
case actiontypes_userinfo.LOGIN_RESULT_ERROR: });
return Object.assign({}, state, { storeOptionalToken(error.response);
error : true, } else {
errorMessages : action.parameters.errorMessages __store.dispatch({
}); 'type' : actiontypes_userinfo.LOGIN_RESULT_ERROR,
'parameters' : {
case actiontypes_userinfo.LOGOUT: 'errorMessages' : [ errorMessages['login_errorunknown']['en'] ]
deleteRequest(state, '/users/sign_out').then((resp) => { }
__store.dispatch({ type : actiontypes_userinfo.CLEAR }); });
}).catch((error) => { }
__store.dispatch({ type : actiontypes_userinfo.CLEAR }); });
}); return Object.assign({}, state, {});
return Object.assign({}, state, {}); case actiontypes_userinfo.LOGIN_RESULT_SUCCESS:
case actiontypes_userinfo.STORE_AUTH_HEADERS: return Object.assign({}, state, {
return Object.assign({}, state, { isSignedIn : true,
accesstoken : action.parameters.accesstoken, error : false,
client : action.parameters.client, errorMessages : [],
expiry : action.parameters.expiry, username : action.parameters.username,
uid : action.parameters.uid });
}); case actiontypes_userinfo.LOGIN_RESULT_ERROR:
return Object.assign({}, state, {
case actiontypes_userinfo.VERIFY_CREDENTIALS: error : true,
getRequest(state, '/users/validate_token').then((resp) => { errorMessages : action.parameters.errorMessages
storeOptionalToken(resp); });
}).catch((error) => { case actiontypes_userinfo.LOGOUT:
__store.dispatch({ type: actiontypes_userinfo.CLEAR }); deleteRequest(state, '/users/sign_out').then(() => {
}); __store.dispatch({ type : actiontypes_userinfo.CLEAR });
return Object.assign({}, state, {}); }).catch(() => {
__store.dispatch({ type : actiontypes_userinfo.CLEAR });
});
return Object.assign({}, state, {});
case actiontypes_userinfo.STORE_AUTH_HEADERS:
return Object.assign({}, state, {
accesstoken : action.parameters.accesstoken,
client : action.parameters.client,
expiry : action.parameters.expiry,
uid : action.parameters.uid
});
case actiontypes_userinfo.VERIFY_CREDENTIALS:
getRequest(state, '/users/validate_token').then((resp) => {
storeOptionalToken(resp);
}).catch(() => {
__store.dispatch({ type: actiontypes_userinfo.CLEAR });
});
return Object.assign({}, state, {});
case actiontypes_userinfo.REHYDRATE:
return Object.assign({}, state, action.parameters);
case actiontypes_userinfo.CLEAR:
return Object.assign({}, state, {
isSignedIn : false,
username : null,
error : false,
errorMessages : [],
case actiontypes_userinfo.REHYDRATE: accesstoken : null,
return Object.assign({}, state, action.parameters); client : null,
case actiontypes_userinfo.CLEAR: expiry : null,
return Object.assign({}, state, { uid : null
isSignedIn : false, });
username : null, default: return state;
error : false,
errorMessages : [],
accesstoken : null,
client : null,
expiry : null,
uid : null
});
default: return state;
} }
} };
const reducers = { const reducers = {
userinfo: reducer_userinfo userinfo: reducer_userinfo
} };
const default_applicationstate = { const default_applicationstate = {
userinfo : defaultstate_userinfo userinfo : defaultstate_userinfo
} };
var __store; var __store;
@ -245,7 +241,7 @@ export function initializeStore(initialState = default_applicationstate) {
composeWithDevTools(applyMiddleware(thunkMiddleware)) composeWithDevTools(applyMiddleware(thunkMiddleware))
); );
__store.subscribe(() => { __store.subscribe(() => {
localStorage.setItem('reduxState', JSON.stringify(__store.getState())) localStorage.setItem('reduxState', JSON.stringify(__store.getState()));
}); });
return __store; return __store;
} }
@ -276,7 +272,7 @@ export function login(email, password) {
email: email, email: email,
password: password password: password
} }
}) });
} }
export function logout() { export function logout() {

View File

@ -28,6 +28,7 @@
}, },
"devDependencies": { "devDependencies": {
"eslint": "^5.9.0", "eslint": "^5.9.0",
"eslint-plugin-react": "^7.11.1",
"react-editable-list": "0.0.3" "react-editable-list": "0.0.3"
} }
} }

View File

@ -1227,6 +1227,14 @@ array-flatten@1.1.1:
resolved "https://registry.yarnpkg.com/array-flatten/-/array-flatten-1.1.1.tgz#9a5f699051b1e7073328f2a008968b64ea2955d2" resolved "https://registry.yarnpkg.com/array-flatten/-/array-flatten-1.1.1.tgz#9a5f699051b1e7073328f2a008968b64ea2955d2"
integrity sha1-ml9pkFGx5wczKPKgCJaLZOopVdI= integrity sha1-ml9pkFGx5wczKPKgCJaLZOopVdI=
array-includes@^3.0.3:
version "3.0.3"
resolved "https://registry.yarnpkg.com/array-includes/-/array-includes-3.0.3.tgz#184b48f62d92d7452bb31b323165c7f8bd02266d"
integrity sha1-GEtI9i2S10UrsxsyMWXH+L0CJm0=
dependencies:
define-properties "^1.1.2"
es-abstract "^1.7.0"
array-map@~0.0.0: array-map@~0.0.0:
version "0.0.0" version "0.0.0"
resolved "https://registry.yarnpkg.com/array-map/-/array-map-0.0.0.tgz#88a2bab73d1cf7bcd5c1b118a003f66f665fa662" resolved "https://registry.yarnpkg.com/array-map/-/array-map-0.0.0.tgz#88a2bab73d1cf7bcd5c1b118a003f66f665fa662"
@ -3169,7 +3177,7 @@ error@^7.0.2:
string-template "~0.2.1" string-template "~0.2.1"
xtend "~4.0.0" xtend "~4.0.0"
es-abstract@^1.5.1: es-abstract@^1.5.1, es-abstract@^1.7.0:
version "1.12.0" version "1.12.0"
resolved "https://registry.yarnpkg.com/es-abstract/-/es-abstract-1.12.0.tgz#9dbbdd27c6856f0001421ca18782d786bf8a6165" resolved "https://registry.yarnpkg.com/es-abstract/-/es-abstract-1.12.0.tgz#9dbbdd27c6856f0001421ca18782d786bf8a6165"
integrity sha512-C8Fx/0jFmV5IPoMOFPA9P9G5NtqW+4cOPit3MIuvR2t7Ag2K15EJTpxnHAYTzL+aYQJIESYeXZmDBfOBE1HcpA== integrity sha512-C8Fx/0jFmV5IPoMOFPA9P9G5NtqW+4cOPit3MIuvR2t7Ag2K15EJTpxnHAYTzL+aYQJIESYeXZmDBfOBE1HcpA==
@ -3199,6 +3207,17 @@ escape-string-regexp@^1.0.2, escape-string-regexp@^1.0.5:
resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4" resolved "https://registry.yarnpkg.com/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz#1b61c0562190a8dff6ae3bb2cf0200ca130b86d4"
integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ= integrity sha1-G2HAViGQqN/2rjuyzwIAyhMLhtQ=
eslint-plugin-react@^7.11.1:
version "7.11.1"
resolved "https://registry.yarnpkg.com/eslint-plugin-react/-/eslint-plugin-react-7.11.1.tgz#c01a7af6f17519457d6116aa94fc6d2ccad5443c"
integrity sha512-cVVyMadRyW7qsIUh3FHp3u6QHNhOgVrLQYdQEB1bPWBsgbNCHdFAeNMquBMCcZJu59eNthX053L70l7gRt4SCw==
dependencies:
array-includes "^3.0.3"
doctrine "^2.1.0"
has "^1.0.3"
jsx-ast-utils "^2.0.1"
prop-types "^15.6.2"
eslint-scope@^4.0.0: eslint-scope@^4.0.0:
version "4.0.0" version "4.0.0"
resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-4.0.0.tgz#50bf3071e9338bcdc43331794a0cb533f0136172" resolved "https://registry.yarnpkg.com/eslint-scope/-/eslint-scope-4.0.0.tgz#50bf3071e9338bcdc43331794a0cb533f0136172"
@ -4794,6 +4813,13 @@ jsonify@~0.0.0:
resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73" resolved "https://registry.yarnpkg.com/jsonify/-/jsonify-0.0.0.tgz#2c74b6ee41d93ca51b7b5aaee8f503631d252a73"
integrity sha1-LHS27kHZPKUbe1qu6PUDYx0lKnM= integrity sha1-LHS27kHZPKUbe1qu6PUDYx0lKnM=
jsx-ast-utils@^2.0.1:
version "2.0.1"
resolved "https://registry.yarnpkg.com/jsx-ast-utils/-/jsx-ast-utils-2.0.1.tgz#e801b1b39985e20fffc87b40e3748080e2dcac7f"
integrity sha1-6AGxs5mF4g//yHtA43SAgOLcrH8=
dependencies:
array-includes "^3.0.3"
junk@^1.0.1: junk@^1.0.1:
version "1.0.3" version "1.0.3"
resolved "https://registry.yarnpkg.com/junk/-/junk-1.0.3.tgz#87be63488649cbdca6f53ab39bec9ccd2347f592" resolved "https://registry.yarnpkg.com/junk/-/junk-1.0.3.tgz#87be63488649cbdca6f53ab39bec9ccd2347f592"