Update words to the database

This commit is contained in:
Robbie Antenesse 2018-01-12 10:10:24 -07:00
parent 7e7ffb8525
commit 23263051f5
3 changed files with 24 additions and 8 deletions

View File

@ -137,12 +137,7 @@ class User {
$dictionary = $user_data->dictionary;
$updated_words = $this->dictionary->setWords($dictionary, $words);
if ($updated_words > 0) {
if ($updated_words === count($words)) {
return true;
} else if ($updated_words < count($words)) {
// TODO: Handle this
return 'mostly';
}
return true;
}
}
return false;

View File

@ -186,12 +186,12 @@ switch ($action) {
), 200);
}
return Response::json(array(
'data' => 'Could not set dictionary: invalid token',
'data' => 'Could not set words: invalid token',
'error' => true,
), 401);
}
return Response::json(array(
'data' => 'Could not set dictionary: required data missing',
'data' => 'Could not set words: required data missing',
'error' => true,
), 400);
}

View File

@ -49,6 +49,7 @@ export class Word {
.then((id) => {
this.id = id;
console.log('Word added successfully');
this.send();
})
.catch(error => {
console.error(error);
@ -61,6 +62,7 @@ export class Word {
return wordDb.words.put(this)
.then((id) => {
console.log('Word modified successfully');
this.send();
})
.catch(error => {
console.error(error);
@ -76,4 +78,23 @@ export class Word {
console.error(error);
});
}
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',
token: 'eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpZCI6MTUxLCJpc01lbWJlciI6ZmFsc2UsImRpY3Rpb25hcnkiOjM1M30.4HRuWY8arkjjYLgQ0Cq4a6v-eXwLTD24oENL8E4I5o0',
words: [this],
}),
});
return fetch(request).then(response => response.json()).then(responseJSON => {
console.log(responseJSON);
});
}
}