Skip to content

Commit

Permalink
Merge pull request #726 from fallken/fix-return-to-last-collection-pa…
Browse files Browse the repository at this point in the history
…ge-after-document-update-#724

fix return to last collection page after document update #724
  • Loading branch information
shakaran committed Sep 9, 2022
2 parents e7584d5 + f65a76f commit da25e2b
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 10 deletions.
7 changes: 5 additions & 2 deletions lib/routes/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const routes = function (config) {
editorTheme: config.options.editorTheme,
docLength: bson.toString(req.document).split(/\r\n|\r|\n/).length,
docString: bson.toString(req.document),
skip: req.query.skip || 0,
};

res.render('document', ctx);
Expand Down Expand Up @@ -64,6 +65,7 @@ const routes = function (config) {

exp.updateDocument = async function (req, res) {
const doc = req.body.document;
const skip = req.query.skip || 0;

if (doc === undefined || doc.length === 0) {
req.session.error = 'You forgot to enter a document!';
Expand All @@ -83,10 +85,11 @@ const routes = function (config) {

await req.collection.updateOne(req.document, { $set: docBSON }).then(() => {
req.session.success = 'Document updated!';

if (config.options.persistEditMode === true) {
res.redirect(buildDocumentURL(res.locals.baseHref, req.dbName, req.collectionName, req.document._id));
res.redirect(buildDocumentURL(res.locals.baseHref, req.dbName, req.collectionName, req.document._id, { skip }));
} else {
res.redirect(buildCollectionURL(res.locals.baseHref, req.dbName, req.collectionName));
res.redirect(buildCollectionURL(res.locals.baseHref, req.dbName, req.collectionName, { skip }));
}
}).catch((err) => {
// document was not saved
Expand Down
20 changes: 16 additions & 4 deletions lib/utils.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import _ from 'lodash-es';
import querystring from 'querystring';

// Given some size in bytes, returns it in a converted, friendly size
// credits: http://stackoverflow.com/users/1596799/aliceljm
Expand Down Expand Up @@ -97,12 +98,23 @@ export const buildDatabaseURL = function (base, dbName) {
return base + 'db/' + encodeURIComponent(dbName);
};

export const buildCollectionURL = function (base, dbName, collectionName) {
return buildDatabaseURL(base, dbName) + '/' + encodeURIComponent(collectionName);
export const buildCollectionURL = function (base, dbName, collectionName, queryOptions = {}) {
let url = buildDatabaseURL(base, dbName) + '/' + encodeURIComponent(collectionName);

if (Object.keys(queryOptions).length) {
url += '?' + querystring.encode(queryOptions);
}
return url;
};

export const buildDocumentURL = function (base, dbName, collectionName, documentId) {
return buildCollectionURL(base, dbName, collectionName) + '/' + encodeURIComponent(JSON.stringify(documentId));
export const buildDocumentURL = function (base, dbName, collectionName, documentId, queryOptions = {}) {
let url = buildCollectionURL(base, dbName, collectionName) + '/' + encodeURIComponent(JSON.stringify(documentId));

if (Object.keys(queryOptions).length) {
url += '?' + querystring.encode(queryOptions);
}

return url;
};

export const isValidDatabaseName = function (name) {
Expand Down
4 changes: 2 additions & 2 deletions lib/views/collection.html
Original file line number Diff line number Diff line change
Expand Up @@ -262,9 +262,9 @@ <h2>Add Indexes</h2>
</thead>
{% for document in docs %}
{% if document._id._bsontype == 'Binary' %}
<tr onclick="loadDocument('{{ collectionUrl }}/{{ document._id | json | safe | url_encode }}?subtype={{ document._id.sub_type }}')">
<tr onclick="loadDocument('{{ collectionUrl }}/{{ document._id | json | safe | url_encode }}?subtype={{ document._id.sub_type }}&skip={{skip}}')">
{% else %}
<tr onclick="loadDocument('{{ collectionUrl }}/{{ document._id | json | safe | url_encode }}')">
<tr onclick="loadDocument('{{ collectionUrl }}/{{ document._id | json | safe | url_encode }}?skip={{skip}}')">
{% endif %}
{% for column in columns %}
<td><div class="tableContent">
Expand Down
4 changes: 2 additions & 2 deletions lib/views/document.html
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,12 @@
{% if document._id._bsontype == 'Binary' %}
<form method="POST"
id="documentEditForm"
action="{{ collectionUrl }}/{{ document._id | json | safe | url_encode }}?subtype={{ document._id.sub_type }}"
action="{{ collectionUrl }}/{{ document._id | json | safe | url_encode }}?subtype={{ document._id.sub_type }}&skip={{skip}}"
>
{% else %}
<form method="POST"
id="documentEditForm"
action="{{ collectionUrl }}/{{ document._id | json | safe | url_encode }}"
action="{{ collectionUrl }}/{{ document._id | json | safe | url_encode }}?skip={{skip}}"
>
{% endif %}

Expand Down

0 comments on commit da25e2b

Please sign in to comment.