1
0
Fork 0
mirror of https://github.com/codex-team/codex.docs.git synced 2025-08-09 15:35:25 +02:00

add client-side Header validation

This commit is contained in:
Peter Savchenko 2018-10-04 01:17:00 +03:00
parent 96e4620df8
commit c7f5cec062
No known key found for this signature in database
GPG key ID: 63E739583C761566
2 changed files with 39 additions and 23 deletions

File diff suppressed because one or more lines are too long

View file

@ -1,6 +1,13 @@
/**
* Module for pages create/edit
*/
/**
* @typedef {object} editorData
* @property {{type, data}[]} blocks - array of Blocks
* @property {string} version - used Editor version
* @property {number} time - saving time
*/
export default class Writing {
/**
* Creates base properties
@ -54,41 +61,50 @@ export default class Writing {
/**
* Returns all writing form data
* @return {Promise.<{}>}
* @throws {Error} - validation error
* @return {Promise.<{parent: string, body: {editorData}}>}
*/
async getData(){
return this.editor.save()
.then((editorData) => {
let writingData = {};
const editorData = await this.editor.save();
const firstBlock = editorData.blocks.length ? editorData.blocks[0] : null;
const title = firstBlock && firstBlock.type === 'header' ? firstBlock.data.text : null;
writingData.parent = this.nodes.parentIdSelector.value;
writingData.body = editorData;
if (!title) {
throw new Error('Entry should start with Header');
}
return writingData;
});
return {
parent: this.nodes.parentIdSelector.value,
body: editorData
};
}
/**
* Handler for clicks on the Save button
*/
async saveButtonClicked(){
const writingData = await this.getData();
try {
const writingData = await this.getData();
try {
const response = await fetch('/page', {
method: 'PUT',
headers: {
"Content-Type": "application/json; charset=utf-8",
},
body: JSON.stringify(writingData),
}).then(response => response.json());
fetch('/page', {
method: 'PUT',
headers: {
"Content-Type": "application/json; charset=utf-8",
},
body: JSON.stringify(writingData),
})
.then(response => response.json())
.then(response => {
console.log('resp', response);
if (response.success){
document.location = '/page/' + response.result._id;
}
})
.catch(console.log);
} catch (sendingError) {
console.log('Saving request failed:', sendingError);
}
} catch (savingError){
alert(savingError);
console.log('Saving error: ', savingError);
}
}
}