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