From ae5ffe0cd1d566446809707d867b1d3f8523de02 Mon Sep 17 00:00:00 2001 From: Robbie Antenesse Date: Sat, 13 Jan 2018 12:09:15 -0700 Subject: [PATCH] Add request() to Helpers and replace all fetches with it. --- src/Helpers.js | 13 ++++ .../management/AccountManager/LoginForm.jsx | 28 ++------- .../management/AccountManager/index.jsx | 19 +----- src/managers/Updater.js | 60 ++++--------------- src/managers/Word.js | 21 ++----- 5 files changed, 39 insertions(+), 102 deletions(-) diff --git a/src/Helpers.js b/src/Helpers.js index 4d813eb..0f283c2 100644 --- a/src/Helpers.js +++ b/src/Helpers.js @@ -199,3 +199,16 @@ export function getWordsStats (words, partsOfSpeech, isCaseSensitive = false) { return wordStats; } + +export function request (action, data, callback) { + const request = new Request('./api/', { + method: 'POST', + mode: 'cors', + redirect: 'follow', + headers: new Headers({ + 'Content-Type': 'application/json' + }), + body: JSON.stringify(Object.assign({action}, data)), + }); + return fetch(request).then(response => response.json()).then(callback); +} diff --git a/src/components/management/AccountManager/LoginForm.jsx b/src/components/management/AccountManager/LoginForm.jsx index 20f6ba5..4f93c07 100644 --- a/src/components/management/AccountManager/LoginForm.jsx +++ b/src/components/management/AccountManager/LoginForm.jsx @@ -7,7 +7,7 @@ import store from 'store'; import { Modal } from '../../structure/Modal'; import { SearchBox } from '../../management/SearchBox'; -import helpMarkdown from '../../../assets/text/help.md'; +import { request } from '../../../Helpers'; export class LoginForm extends Component { constructor (props) { @@ -81,30 +81,10 @@ export class LoginForm extends Component { if (uniqueFields.includes(field)) { const errorFieldName = `${field}Error`; - const request = new Request('./api/', { - method: 'POST', - mode: 'cors', - redirect: 'follow', - headers: new Headers({ - 'Content-Type': 'application/json' - }), - body: JSON.stringify({ - action: 'login', - email, - password, - }), - }); - return fetch(request).then(response => response.json()).then(responseJSON => { - const { data, error } = responseJSON; - if (error) { - console.error(data); - } else { - store.set('LexicongaToken', data); - this.setState({ isLoggedIn: true }, () => { - this.props.updater.sync(); - }); - } + request('check-email', value, (response) => { + }); + if (value === '') { isUnique = false; fieldUpdate[errorFieldName] = 'This field must not be blank'; diff --git a/src/components/management/AccountManager/index.jsx b/src/components/management/AccountManager/index.jsx index eb06ba1..f93c0c7 100644 --- a/src/components/management/AccountManager/index.jsx +++ b/src/components/management/AccountManager/index.jsx @@ -7,7 +7,7 @@ import store from 'store'; import { Modal } from '../../structure/Modal'; import { LoginForm } from './LoginForm'; -import helpMarkdown from '../../../assets/text/help.md'; +import { request } from '../../../Helpers'; export class AccountManager extends Component { constructor (props) { @@ -23,21 +23,8 @@ export class AccountManager extends Component { } logIn (email, password) { - const request = new Request('./api/', { - method: 'POST', - mode: 'cors', - redirect: 'follow', - headers: new Headers({ - 'Content-Type': 'application/json' - }), - body: JSON.stringify({ - action: 'login', - email, - password, - }), - }); - return fetch(request).then(response => response.json()).then(responseJSON => { - const {data, error} = responseJSON; + return request('login', { email, password }, response => { + const { data, error } = response; if (error) { console.error(data); } else { diff --git a/src/managers/Updater.js b/src/managers/Updater.js index e5bacec..c24e271 100644 --- a/src/managers/Updater.js +++ b/src/managers/Updater.js @@ -1,6 +1,6 @@ import store from 'store'; -import { timestampInSeconds } from "../Helpers"; +import { timestampInSeconds, request } from "../Helpers"; export class Updater { constructor (appWithDictionaryState, dictionary) { @@ -86,58 +86,24 @@ export class Updater { } sendDictionaryDetails (dictionaryDetails) { - const request = new Request('./api/', { - method: 'POST', - mode: 'cors', - redirect: 'follow', - headers: new Headers({ - 'Content-Type': 'application/json' - }), - body: JSON.stringify({ - action: 'set-dictionary-details', - token: store.get('LexicongaToken'), - details: dictionaryDetails, - }), - }); - return fetch(request).then(response => response.json()).then(responseJSON => { - console.log(responseJSON); - }); + return request('set-dictionary-details', { + token: store.get('LexicongaToken'), + details: dictionaryDetails, + }, response => console.log(response)) } sendWords (words) { - const request = new Request('./api/', { - method: 'POST', - mode: 'cors', - redirect: 'follow', - headers: new Headers({ - 'Content-Type': 'application/json' - }), - body: JSON.stringify({ - action: 'set-dictionary-words', - token: store.get('LexicongaToken'), - words, - }), - }); - return fetch(request).then(response => response.json()).then(responseJSON => { - console.log(responseJSON); - }); + return request('set-dictionary-words', { + token: store.get('LexicongaToken'), + words, + }, response => console.log(response)); } sync () { - const request = new Request('./api/', { - method: 'POST', - mode: 'cors', - redirect: 'follow', - headers: new Headers({ - 'Content-Type': 'application/json' - }), - body: JSON.stringify({ - action: 'get-current-dictionary', - token: store.get('LexicongaToken'), - }), - }); - return fetch(request).then(response => response.json()).then(responseJSON => { - const {data, error} = responseJSON; + return request('get-current-dictionary', { + token: store.get('LexicongaToken'), + }, response => { + const { data, error } = response; if (error) { console.error(data); } else { diff --git a/src/managers/Word.js b/src/managers/Word.js index 7933e3e..30fa102 100644 --- a/src/managers/Word.js +++ b/src/managers/Word.js @@ -1,7 +1,7 @@ import PropTypes from 'prop-types'; import store from 'store'; import wordDb from './WordDatabase'; -import {timestampInSeconds} from '../Helpers'; +import { timestampInSeconds, request } from '../Helpers'; export class Word { constructor (values = {}) { @@ -80,21 +80,12 @@ export class Word { } send () { - const request = new Request('./api/', { - method: 'POST', - mode: 'cors', - redirect: 'follow', - headers: new Headers({ - 'Content-Type': 'application/json' - }), - body: JSON.stringify({ - action: 'set-dictionary-words', + const token = store.get('LexicongaToken'); + if (token) { + return request('set-dictionary-words', { token: store.get('LexicongaToken'), words: [this], - }), - }); - return fetch(request).then(response => response.json()).then(responseJSON => { - console.log(responseJSON); - }); + }, response => console.log(response)); + } } }