Add request() to Helpers and replace all fetches with it.

This commit is contained in:
Robbie Antenesse 2018-01-13 12:09:15 -07:00
parent 7aa8848fe8
commit ae5ffe0cd1
5 changed files with 39 additions and 102 deletions

View File

@ -199,3 +199,16 @@ export function getWordsStats (words, partsOfSpeech, isCaseSensitive = false) {
return wordStats; 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);
}

View File

@ -7,7 +7,7 @@ import store from 'store';
import { Modal } from '../../structure/Modal'; import { Modal } from '../../structure/Modal';
import { SearchBox } from '../../management/SearchBox'; import { SearchBox } from '../../management/SearchBox';
import helpMarkdown from '../../../assets/text/help.md'; import { request } from '../../../Helpers';
export class LoginForm extends Component { export class LoginForm extends Component {
constructor (props) { constructor (props) {
@ -81,30 +81,10 @@ export class LoginForm extends Component {
if (uniqueFields.includes(field)) { if (uniqueFields.includes(field)) {
const errorFieldName = `${field}Error`; const errorFieldName = `${field}Error`;
const request = new Request('./api/', { request('check-email', value, (response) => {
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();
});
}
}); });
if (value === '') { if (value === '') {
isUnique = false; isUnique = false;
fieldUpdate[errorFieldName] = 'This field must not be blank'; fieldUpdate[errorFieldName] = 'This field must not be blank';

View File

@ -7,7 +7,7 @@ import store from 'store';
import { Modal } from '../../structure/Modal'; import { Modal } from '../../structure/Modal';
import { LoginForm } from './LoginForm'; import { LoginForm } from './LoginForm';
import helpMarkdown from '../../../assets/text/help.md'; import { request } from '../../../Helpers';
export class AccountManager extends Component { export class AccountManager extends Component {
constructor (props) { constructor (props) {
@ -23,21 +23,8 @@ export class AccountManager extends Component {
} }
logIn (email, password) { logIn (email, password) {
const request = new Request('./api/', { return request('login', { email, password }, response => {
method: 'POST', const { data, error } = response;
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) { if (error) {
console.error(data); console.error(data);
} else { } else {

View File

@ -1,6 +1,6 @@
import store from 'store'; import store from 'store';
import { timestampInSeconds } from "../Helpers"; import { timestampInSeconds, request } from "../Helpers";
export class Updater { export class Updater {
constructor (appWithDictionaryState, dictionary) { constructor (appWithDictionaryState, dictionary) {
@ -86,58 +86,24 @@ export class Updater {
} }
sendDictionaryDetails (dictionaryDetails) { sendDictionaryDetails (dictionaryDetails) {
const request = new Request('./api/', { return request('set-dictionary-details', {
method: 'POST', token: store.get('LexicongaToken'),
mode: 'cors', details: dictionaryDetails,
redirect: 'follow', }, response => console.log(response))
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);
});
} }
sendWords (words) { sendWords (words) {
const request = new Request('./api/', { return request('set-dictionary-words', {
method: 'POST', token: store.get('LexicongaToken'),
mode: 'cors', words,
redirect: 'follow', }, response => console.log(response));
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);
});
} }
sync () { sync () {
const request = new Request('./api/', { return request('get-current-dictionary', {
method: 'POST', token: store.get('LexicongaToken'),
mode: 'cors', }, response => {
redirect: 'follow', const { data, error } = response;
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;
if (error) { if (error) {
console.error(data); console.error(data);
} else { } else {

View File

@ -1,7 +1,7 @@
import PropTypes from 'prop-types'; import PropTypes from 'prop-types';
import store from 'store'; import store from 'store';
import wordDb from './WordDatabase'; import wordDb from './WordDatabase';
import {timestampInSeconds} from '../Helpers'; import { timestampInSeconds, request } from '../Helpers';
export class Word { export class Word {
constructor (values = {}) { constructor (values = {}) {
@ -80,21 +80,12 @@ export class Word {
} }
send () { send () {
const request = new Request('./api/', { const token = store.get('LexicongaToken');
method: 'POST', if (token) {
mode: 'cors', return request('set-dictionary-words', {
redirect: 'follow',
headers: new Headers({
'Content-Type': 'application/json'
}),
body: JSON.stringify({
action: 'set-dictionary-words',
token: store.get('LexicongaToken'), token: store.get('LexicongaToken'),
words: [this], words: [this],
}), }, response => console.log(response));
}); }
return fetch(request).then(response => response.json()).then(responseJSON => {
console.log(responseJSON);
});
} }
} }