mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-08-09 15:35:25 +02:00
remove static method
This commit is contained in:
parent
c7f5cec062
commit
5e57693189
6 changed files with 55 additions and 28 deletions
2
public/dist/main.bundle.js
vendored
2
public/dist/main.bundle.js
vendored
File diff suppressed because one or more lines are too long
|
@ -48,26 +48,47 @@ class Pages {
|
|||
* @returns {Promise<Page>}
|
||||
*/
|
||||
static async insert(data) {
|
||||
if (!Pages.validate(data)) {
|
||||
throw new Error('Invalid request format');
|
||||
try {
|
||||
Pages.validate(data);
|
||||
|
||||
const page = new Model(data);
|
||||
|
||||
return page.save();
|
||||
} catch (validationError) {
|
||||
throw new Error(validationError);
|
||||
}
|
||||
|
||||
const page = new Model(data);
|
||||
|
||||
return page.save();
|
||||
}
|
||||
|
||||
/**
|
||||
* Check PageData object for required fields
|
||||
*
|
||||
* @param {PageData} data
|
||||
* @returns {boolean}
|
||||
* @throws {Error} - validation error
|
||||
*/
|
||||
static validate(data) {
|
||||
let allRequiredFields = Pages.REQUIRED_FIELDS.every(field => typeof data[field] !== 'undefined');
|
||||
let hasTitle = Model.extractTitleFromBlocks(data.body);
|
||||
const allRequiredFields = Pages.REQUIRED_FIELDS.every(field => typeof data[field] !== 'undefined');
|
||||
|
||||
return allRequiredFields && hasTitle;
|
||||
if (!allRequiredFields) {
|
||||
throw new Error('Some of required fields is missed');
|
||||
}
|
||||
|
||||
const hasBlocks = data.body && data.body.blocks && Array.isArray(data.body.blocks) && data.body.blocks.length > 0;
|
||||
|
||||
if (!hasBlocks) {
|
||||
throw new Error('Page body is invalid');
|
||||
}
|
||||
|
||||
const hasHeaderAsFirstBlock = data.body.blocks[0].type === 'header';
|
||||
|
||||
if (!hasHeaderAsFirstBlock) {
|
||||
throw new Error('First page Block must be a Header');
|
||||
}
|
||||
|
||||
const headerIsNotEmpty = data.body.blocks[0].data.text.replace('<br>', '').trim() !== '';
|
||||
|
||||
if (!headerIsNotEmpty) {
|
||||
throw new Error('Please, fill page Header');
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -87,16 +87,21 @@ export default class Writing {
|
|||
const writingData = await this.getData();
|
||||
|
||||
try {
|
||||
const response = await fetch('/page', {
|
||||
let response = await fetch('/page', {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
"Content-Type": "application/json; charset=utf-8",
|
||||
},
|
||||
body: JSON.stringify(writingData),
|
||||
}).then(response => response.json());
|
||||
});
|
||||
|
||||
response = await response.json();
|
||||
|
||||
if (response.success){
|
||||
document.location = '/page/' + response.result._id;
|
||||
} else {
|
||||
alert(response.error);
|
||||
console.log('Validation failed:', response.error);
|
||||
}
|
||||
|
||||
} catch (sendingError) {
|
||||
|
|
|
@ -70,7 +70,7 @@ class Page {
|
|||
const {body, parent} = pageData;
|
||||
|
||||
this.body = body || this.body;
|
||||
this.title = Page.extractTitleFromBlocks(body || this.body);
|
||||
this.title = this.extractTitleFromBody();
|
||||
this._parent = parent || this._parent;
|
||||
}
|
||||
|
||||
|
@ -90,11 +90,10 @@ class Page {
|
|||
|
||||
/**
|
||||
* Extract first header from editor data
|
||||
* @param {{blocks, time, version}} editorData
|
||||
* @return {string}
|
||||
*/
|
||||
static extractTitleFromBlocks(editorData) {
|
||||
const headerBlock = editorData ? editorData.blocks.find(block => block.type === 'header') : '';
|
||||
extractTitleFromBody() {
|
||||
const headerBlock = this.body ? this.body.blocks.find(block => block.type === 'header') : '';
|
||||
|
||||
return headerBlock ? headerBlock.data.text : '';
|
||||
}
|
||||
|
|
|
@ -265,19 +265,21 @@ describe('Page model', () => {
|
|||
});
|
||||
|
||||
it('Extracting title from page body', async () => {
|
||||
const body = {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Page header'
|
||||
const pageData = {
|
||||
body: {
|
||||
blocks: [
|
||||
{
|
||||
type: 'header',
|
||||
data: {
|
||||
text: 'Page header'
|
||||
}
|
||||
}
|
||||
}
|
||||
]
|
||||
]
|
||||
}
|
||||
};
|
||||
|
||||
const title = Page.extractTitleFromBlocks(body);
|
||||
const page = new Page(pageData);
|
||||
|
||||
expect(title).to.equal(body.blocks[0].data.text);
|
||||
expect(page.title).to.equal(pageData.body.blocks[0].data.text);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -61,7 +61,7 @@ describe('Pages REST: ', () => {
|
|||
const {success, error} = res.body;
|
||||
|
||||
expect(success).to.be.false;
|
||||
expect(error).to.equal('Invalid request format');
|
||||
expect(error).to.equal('Error: Some of required fields is missed');
|
||||
});
|
||||
|
||||
it('Finding page', async () => {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue