diff --git a/.eslintrc.json b/.eslintrc.json index df85702..6efdb03 100644 --- a/.eslintrc.json +++ b/.eslintrc.json @@ -4,10 +4,16 @@ "es6": true, "node": true }, + "plugins": [ + "react" + ], "extends": "eslint:recommended", "parserOptions": { "ecmaVersion": 2018, - "sourceType": "module" + "sourceType": "module", + "ecmaFeatures": { + "jsx": true + } }, "rules": { "indent": [ diff --git a/js/api.js b/js/api.js index a760ada..3c5db29 100644 --- a/js/api.js +++ b/js/api.js @@ -1,13 +1,12 @@ -import { createStore, applyMiddleware, combineReducers } from 'redux' -import { composeWithDevTools } from 'redux-devtools-extension' -import thunkMiddleware from 'redux-thunk' -import { errorMessages } from './constants' +import { createStore, applyMiddleware, combineReducers } from 'redux'; +import { composeWithDevTools } from 'redux-devtools-extension'; +import thunkMiddleware from 'redux-thunk'; +import { errorMessages } from './constants'; const axios = require('axios'); const api_url = 'https://api.turnie.re'; - const actiontypes_userinfo = { 'REGISTER' : 'REGISTER', 'REGISTER_RESULT_SUCCESS' : 'REGISTER_RESULT_SUCCESS', @@ -27,7 +26,7 @@ const actiontypes_userinfo = { 'REHYDRATE' : 'USERINFO_REHYDRATE', 'CLEAR' : 'USERINFO_CLEAR', -} +}; const defaultstate_userinfo = { isSignedIn : false, @@ -39,7 +38,7 @@ const defaultstate_userinfo = { client : null, expiry : null, uid : null -} +}; export function postRequest(state, url, data) { return axios.post(api_url + url, data, { @@ -81,7 +80,7 @@ function storeOptionalToken(response) { expiry : response.headers['expiry'], uid : response.headers['uid'] } - }) + }); } } @@ -102,139 +101,136 @@ function checkForAuthenticationHeaders(response) { const reducer_userinfo = (state = defaultstate_userinfo, action) => { switch(action.type) { - case actiontypes_userinfo.REGISTER: - postRequest(state, '/users', { - 'username' : action.parameters.username, - 'email' : action.parameters.email, - 'password' : action.parameters.password - }).then((resp) => { + case actiontypes_userinfo.REGISTER: + postRequest(state, '/users', { + 'username' : action.parameters.username, + 'email' : action.parameters.email, + 'password' : action.parameters.password + }).then((resp) => { + __store.dispatch({ + type : actiontypes_userinfo.REGISTER_RESULT_SUCCESS + }); + storeOptionalToken(resp); + }).catch((error) => { + if (error.response) { __store.dispatch({ - type : actiontypes_userinfo.REGISTER_RESULT_SUCCESS - }); - storeOptionalToken(resp); - }).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, + 'type' : actiontypes_userinfo.REGISTER_RESULT_ERROR, + 'parameters' : { + 'errorMessages' : error.response.data.errors.full_messages } }); - storeOptionalToken(resp); - }).catch((error) => { - if(error.response) { - __store.dispatch({ - 'type' : actiontypes_userinfo.LOGIN_RESULT_ERROR, - 'parameters' : { - 'errorMessages' : error.response.data.errors - } - }); - storeOptionalToken(error.response); - } else { - __store.dispatch({ - 'type' : actiontypes_userinfo.LOGIN_RESULT_ERROR, - 'parameters' : { - 'errorMessages' : [ errorMessages['login_errorunknown']['en'] ] - } - }); + 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, } }); - return Object.assign({}, state, {}); - case actiontypes_userinfo.LOGIN_RESULT_SUCCESS: - return Object.assign({}, state, { - isSignedIn : true, - error : false, - errorMessages : [], - username : action.parameters.username, - }); - case actiontypes_userinfo.LOGIN_RESULT_ERROR: - return Object.assign({}, state, { - error : true, - errorMessages : action.parameters.errorMessages - }); - - case actiontypes_userinfo.LOGOUT: - 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, {}); - 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((error) => { - __store.dispatch({ type: actiontypes_userinfo.CLEAR }); - }); - return Object.assign({}, state, {}); + storeOptionalToken(resp); + }).catch((error) => { + if(error.response) { + __store.dispatch({ + 'type' : actiontypes_userinfo.LOGIN_RESULT_ERROR, + 'parameters' : { + 'errorMessages' : error.response.data.errors + } + }); + storeOptionalToken(error.response); + } else { + __store.dispatch({ + 'type' : actiontypes_userinfo.LOGIN_RESULT_ERROR, + 'parameters' : { + 'errorMessages' : [ errorMessages['login_errorunknown']['en'] ] + } + }); + } + }); + return Object.assign({}, state, {}); + case actiontypes_userinfo.LOGIN_RESULT_SUCCESS: + return Object.assign({}, state, { + isSignedIn : true, + error : false, + errorMessages : [], + username : action.parameters.username, + }); + case actiontypes_userinfo.LOGIN_RESULT_ERROR: + return Object.assign({}, state, { + error : true, + errorMessages : action.parameters.errorMessages + }); + case actiontypes_userinfo.LOGOUT: + deleteRequest(state, '/users/sign_out').then(() => { + __store.dispatch({ type : actiontypes_userinfo.CLEAR }); + }).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: - return Object.assign({}, state, action.parameters); - case actiontypes_userinfo.CLEAR: - return Object.assign({}, state, { - isSignedIn : false, - username : null, - error : false, - errorMessages : [], - - accesstoken : null, - client : null, - expiry : null, - uid : null - }); - default: return state; + accesstoken : null, + client : null, + expiry : null, + uid : null + }); + default: return state; } -} +}; const reducers = { userinfo: reducer_userinfo -} +}; const default_applicationstate = { userinfo : defaultstate_userinfo -} +}; var __store; @@ -245,7 +241,7 @@ export function initializeStore(initialState = default_applicationstate) { composeWithDevTools(applyMiddleware(thunkMiddleware)) ); __store.subscribe(() => { - localStorage.setItem('reduxState', JSON.stringify(__store.getState())) + localStorage.setItem('reduxState', JSON.stringify(__store.getState())); }); return __store; } @@ -276,7 +272,7 @@ export function login(email, password) { email: email, password: password } - }) + }); } export function logout() { diff --git a/package.json b/package.json index 20b6cb6..50ad571 100644 --- a/package.json +++ b/package.json @@ -28,6 +28,7 @@ }, "devDependencies": { "eslint": "^5.9.0", + "eslint-plugin-react": "^7.11.1", "react-editable-list": "0.0.3" } } diff --git a/yarn.lock b/yarn.lock index a411c76..042a220 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1227,6 +1227,14 @@ array-flatten@1.1.1: resolved "https://registry.yarnpkg.com/array-flatten/-/array-flatten-1.1.1.tgz#9a5f699051b1e7073328f2a008968b64ea2955d2" 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: version "0.0.0" 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" xtend "~4.0.0" -es-abstract@^1.5.1: +es-abstract@^1.5.1, es-abstract@^1.7.0: version "1.12.0" resolved "https://registry.yarnpkg.com/es-abstract/-/es-abstract-1.12.0.tgz#9dbbdd27c6856f0001421ca18782d786bf8a6165" 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" 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: version "4.0.0" 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" 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: version "1.0.3" resolved "https://registry.yarnpkg.com/junk/-/junk-1.0.3.tgz#87be63488649cbdca6f53ab39bec9ccd2347f592"