mirror of
https://github.com/codex-team/codex.docs.git
synced 2025-08-09 15:35:25 +02:00
Fixed redirect after page creation
This commit is contained in:
parent
db55a7a7d2
commit
88172e7d34
8 changed files with 69 additions and 21 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
3
src/constants/aliasTypes.js
Normal file
3
src/constants/aliasTypes.js
Normal file
|
@ -0,0 +1,3 @@
|
|||
module.exports = {
|
||||
PAGE: 'page'
|
||||
};
|
|
@ -1,4 +1,4 @@
|
|||
const Model = require('../models/alias');
|
||||
const Alias = require('../models/alias');
|
||||
|
||||
/**
|
||||
* @class Aliases
|
||||
|
@ -7,19 +7,19 @@ const Model = require('../models/alias');
|
|||
class Aliases {
|
||||
/**
|
||||
* @static
|
||||
* Find and return id of entity with given alias
|
||||
* Find and return entity with given alias
|
||||
*
|
||||
* @param {string} alias - alias of entity
|
||||
* @returns {Promise<string>}
|
||||
* @param {string} aliasName - alias name of entity
|
||||
* @returns {Promise<Alias>}
|
||||
*/
|
||||
static async get(alias) {
|
||||
const id = await Model.get(alias);
|
||||
console.log('id', id);
|
||||
if (!id) {
|
||||
static async get(aliasName) {
|
||||
const alias = await Alias.get(aliasName.slice(1));
|
||||
|
||||
if (!alias.id) {
|
||||
throw new Error('Entity with given alias does not exist');
|
||||
}
|
||||
|
||||
return id;
|
||||
return alias;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,4 +1,7 @@
|
|||
const Model = require('../models/page');
|
||||
const Alias = require('../models/alias');
|
||||
const aliasTypes = require('../constants/aliasTypes');
|
||||
const getHashFromString = require('../utils/hash');
|
||||
|
||||
/**
|
||||
* @class Pages
|
||||
|
@ -50,10 +53,22 @@ class Pages {
|
|||
static async insert(data) {
|
||||
try {
|
||||
Pages.validate(data);
|
||||
|
||||
console.log('data', data);
|
||||
const page = new Model(data);
|
||||
|
||||
return page.save();
|
||||
const pagePromise = page.save();
|
||||
|
||||
pagePromise.then(() => {
|
||||
const alias = new Alias({
|
||||
id: page._id,
|
||||
type: aliasTypes.PAGE,
|
||||
hash: getHashFromString(page.uri).toString()
|
||||
});
|
||||
|
||||
alias.save();
|
||||
});
|
||||
|
||||
return pagePromise;
|
||||
} catch (validationError) {
|
||||
throw new Error(validationError);
|
||||
}
|
||||
|
|
|
@ -115,7 +115,7 @@ export default class Writing {
|
|||
response = await response.json();
|
||||
|
||||
if (response.success) {
|
||||
document.location = '/page/' + response.result._id;
|
||||
window.location.pathname = response.result.uri;
|
||||
} else {
|
||||
alert(response.error);
|
||||
console.log('Validation failed:', response.error);
|
||||
|
|
|
@ -3,14 +3,17 @@ const getHashFromString = require('../utils/hash');
|
|||
|
||||
/**
|
||||
* @typedef {Object} AliasData
|
||||
* @property {string} _id - alias id
|
||||
* @property {string} hash - alias hash
|
||||
* @property {string} type - entity type
|
||||
* @property {string} hash - entity hash
|
||||
* @property {string} id - entity id
|
||||
*
|
||||
*/
|
||||
|
||||
/**
|
||||
* @class Alias
|
||||
* @property {string} _id - alias id
|
||||
* @property {string} hash - alias hash
|
||||
* @property {string} type - entity type
|
||||
* @property {string} id - entity title
|
||||
*/
|
||||
|
@ -21,7 +24,7 @@ class Alias {
|
|||
* @returns {Promise<Alias>}
|
||||
*/
|
||||
static async get(aliasName) {
|
||||
const hash = getHashFromString(aliasName.slice(1)).toString();
|
||||
const hash = getHashFromString(aliasName).toString();
|
||||
const data = await aliasesDb.findOne({hash});
|
||||
|
||||
return new Alias(data);
|
||||
|
@ -36,10 +39,28 @@ class Alias {
|
|||
if (data === null) {
|
||||
data = {};
|
||||
}
|
||||
if (data._id) {
|
||||
this._id = data._id;
|
||||
}
|
||||
this.id = data.id;
|
||||
this.type = data.type;
|
||||
this.hash = data.hash;
|
||||
}
|
||||
|
||||
/**
|
||||
* Save or update page data in the database
|
||||
*
|
||||
* @returns {Promise<Alias>}
|
||||
*/
|
||||
async save() {
|
||||
if (!this._id) {
|
||||
const insertedRow = await aliasesDb.insert({id: this.id, type: this.type, hash: this.hash});
|
||||
} else {
|
||||
await aliasesDb.update({_id: this._id}, this.data);
|
||||
}
|
||||
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = Alias;
|
||||
|
|
|
@ -72,7 +72,7 @@ class Page {
|
|||
|
||||
this.body = body || this.body;
|
||||
this.title = this.extractTitleFromBody();
|
||||
this.uri = uri || '';
|
||||
this.uri = uri || this.title.toLowerCase().split(' ').join('-');
|
||||
this._parent = parent || this._parent;
|
||||
}
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
const express = require('express');
|
||||
const router = express.Router();
|
||||
const Aliases = require('../controllers/aliases');
|
||||
const Pages = require('../controllers/pages');
|
||||
const aliasTypes = require('../constants/aliasTypes');
|
||||
|
||||
/**
|
||||
* GET /*
|
||||
|
@ -10,12 +12,19 @@ const Aliases = require('../controllers/aliases');
|
|||
router.get('*', async (req, res) => {
|
||||
try {
|
||||
console.log('url ', req.originalUrl);
|
||||
const id = await Aliases.get(req.originalUrl);
|
||||
const alias = await Aliases.get(req.originalUrl);
|
||||
|
||||
res.json({
|
||||
success: true,
|
||||
// result: page.data
|
||||
switch (alias.type) {
|
||||
case aliasTypes.PAGE: {
|
||||
let page = await Pages.get(alias.id);
|
||||
|
||||
let pageParent = await page.parent;
|
||||
|
||||
res.render('pages/page', {
|
||||
page, pageParent
|
||||
});
|
||||
}
|
||||
}
|
||||
} catch (err) {
|
||||
res.status(400).json({
|
||||
success: false,
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue