Add request() to Helpers and replace all fetches with it.
This commit is contained in:
parent
7aa8848fe8
commit
ae5ffe0cd1
|
@ -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);
|
||||
}
|
||||
|
|
|
@ -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';
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue