Skip to content

Commit

Permalink
feat: allow users to reply on other logs
Browse files Browse the repository at this point in the history
  • Loading branch information
mvegter committed Jun 6, 2020
1 parent b14954b commit 035c0bb
Show file tree
Hide file tree
Showing 10 changed files with 125 additions and 9 deletions.
8 changes: 8 additions & 0 deletions lib/database/adapters/LogAdapter.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,14 @@ class LogAdapter {
text: entityObject.text,
};

if (entityObject.rootLogId) {
databaseObject.rootLogId = entityObject.rootLogId;
}

if (entityObject.parentLogId) {
databaseObject.parentLogId = entityObject.parentLogId;
}

return databaseObject;
}
}
Expand Down
3 changes: 3 additions & 0 deletions lib/domain/dtos/CreateLogDto.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
*/

const Joi = require('@hapi/joi');
const EntityIdDto = require('./EntityIdDto');

const BodyDto = Joi.object({
title: Joi.string()
Expand All @@ -21,6 +22,8 @@ const BodyDto = Joi.object({
text: Joi.string()
.required()
.min(3),
rootLogId: EntityIdDto.optional(),
parentLogId: EntityIdDto.optional(),
});

const QueryDto = Joi.object({
Expand Down
22 changes: 20 additions & 2 deletions lib/public/components/Post/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,32 @@ const ACTIVE_FIELDS = {
},
};

/**
* Returns a reply to Log button.
*
* @param {*} model Pass the model to access the defined functions.
* @param {Object} post all data related to the post
* @return {vnode} The reply button.
*/
const replyButton = (model, post) => h('a.btn.btn-primary', {
id: `reply-to-${post.id}`,
style: {
float: 'right',
},
onclick: (e) => model.router.handleLinkEvent(e),
href: `?page=create-log-entry&rootLogId=${post.rootLogId}&parentLogId=${post.id}`,
}, 'Reply');

/**
* A singular post which is part of a log
*
* @param {*} model Pass the model to access the defined functions.
* @param {Object} post all data related to the post
* @param {Number} index the identification index of the post
* @param {Boolean} highlight indicator if this post should be highlighted
* @return {vnode} Returns a post
*/
const entry = (post, index, highlight) => h(`.w-100.flex-row.shadow-level1${highlight === true ? '.bg-success' : ''}`, {
const entry = (model, post, index, highlight) => h(`.w-100.flex-row.shadow-level1${highlight ? '.bg-success' : ''}`, {
id: `post${index}`,
style: {
margin: '5px',
Expand All @@ -66,7 +84,7 @@ const entry = (post, index, highlight) => h(`.w-100.flex-row.shadow-level1${high
]),
]),
h('.w-80.shadow-level1', { style: { padding: '10px' } }, [
h('h4', post.title),
h('h4', post.title, replyButton(model, post)),
h('.w-100#post-content', post.text),
]),
]);
Expand Down
12 changes: 12 additions & 0 deletions lib/public/views/Logs/Create/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,18 @@ const createScreen = (model) => {
const disabled = !(title.length > 2 && text.length > 2);
const data = model.logs.getCreatedLog();

if (model.router.params.rootLogId) {
model.logs.rootLogId = parseInt(model.router.params.rootLogId, 10);
} else {
model.logs.rootLogId = -1;
}

if (model.router.params.parentLogId) {
model.logs.parentLogId = parseInt(model.router.params.parentLogId, 10);
} else {
model.logs.rootLogId = -1;
}

return h('div#create-log', [
data.isFailure() && data.payload.map((error) =>
h('.alert.alert-danger', h('b', `${error.title}: `), error.detail)),
Expand Down
2 changes: 1 addition & 1 deletion lib/public/views/Logs/Details/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ const logDetailScreen = (model) => {
style: {
'margin-left': `${2 * level}rem`,
},
}, PostBox(item, item.id, item.id === id));
}, PostBox(model, item, item.id, item.id === id));

// eslint-disable-next-line require-jsdoc
const tree = (obj, level = 0, items = []) => {
Expand Down
22 changes: 17 additions & 5 deletions lib/public/views/Logs/Logs.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,26 @@ export default class Overview extends Observable {
this.createdLog = RemoteData.loading();
this.notify();

const response = await fetchClient('api/logs', {
const body = {
title: this.getTitle(),
text: this.getText(),
};

if (this.rootLogId > 0) {
body.rootLogId = this.rootLogId;
}

if (this.parentLogId > 0) {
body.parentLogId = this.parentLogId;
}

const response = await fetchClient('/api/logs', {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
title: this.getTitle(),
text: this.getText(),
}),
body: JSON.stringify(body),
});

const result = await response.json();
Expand Down Expand Up @@ -352,6 +362,8 @@ export default class Overview extends Observable {
* @returns {undefined}
*/
clearEditor() {
this.rootLogId = -1;
this.parentLogId = -1;
this.isPreviewActive = false;
this.text = '';
this.title = '';
Expand Down
4 changes: 4 additions & 0 deletions spec/openapi-source.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,10 @@ components:
$ref: '#/components/schemas/LogTitle'
text:
$ref: '#/components/schemas/LogText'
rootLogId:
$ref: '#/components/schemas/EntityId'
parentLogId:
$ref: '#/components/schemas/EntityId'
required:
- title
- text
Expand Down
6 changes: 5 additions & 1 deletion spec/openapi.yaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Generated on Sat, 06 Jun 2020 10:08:26 GMT
# Generated on Sat, 06 Jun 2020 13:55:40 GMT

openapi: 3.0.0
info:
Expand Down Expand Up @@ -386,6 +386,10 @@ components:
$ref: '#/components/schemas/LogTitle'
text:
$ref: '#/components/schemas/LogText'
rootLogId:
$ref: '#/components/schemas/EntityId'
parentLogId:
$ref: '#/components/schemas/EntityId'
required:
- title
- text
Expand Down
29 changes: 29 additions & 0 deletions test/e2e/logs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -503,6 +503,8 @@ module.exports = () => {
.send({
title: 'Yet another run',
text: 'Text of yet another run',
rootLogId: 1,
parentLogId: 2,
})
.expect(201)
.end((err, res) => {
Expand All @@ -515,6 +517,33 @@ module.exports = () => {
expect(res).to.satisfyApiSpec;

expect(res.body.data.title).to.equal('Yet another run');
expect(res.body.data.rootLogId).to.equal(1);
expect(res.body.data.parentLogId).to.equal(2);

done();
});
});

it('should return 201 if a proper body was sent', (done) => {
request(server)
.post('/api/logs')
.send({
title: 'Yet another run',
text: 'Text of yet another run',
})
.expect(201)
.end((err, res) => {
if (err) {
done(err);
return;
}

// Response must satisfy the OpenAPI specification
expect(res).to.satisfyApiSpec;

expect(res.body.data.title).to.equal('Yet another run');
expect(res.body.data.rootLogId).to.equal(res.body.data.id);
expect(res.body.data.parentLogId).to.equal(res.body.data.id);

done();
});
Expand Down
26 changes: 26 additions & 0 deletions test/public/logs/detail.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,30 @@ module.exports = () => {
const message = await page.evaluate((element) => element.innerText, error);
expect(message).to.equal('Log with this id (99999999) could not be found:');
});

it('should have a button to reply on a entry', async () => {
const rootLogId = 1;
const parentLogId = 2;
await page.goto(`${url}/?page=entry&id=${rootLogId}`);
await page.waitFor(250);

// We expect there to be at least one post in this log entry
await page.click(`#reply-to-${parentLogId}`);
await page.waitFor(250);

const redirectedUrl = await page.url();
const expectedUrl = `${url}/?page=create-log-entry&rootLogId=${rootLogId}&parentLogId=${parentLogId}`;
expect(redirectedUrl).to.equal(expectedUrl);

const title = 'Test the reply button';
const text = 'Test the reply button';

await page.type('#title', title);
// eslint-disable-next-line no-undef
await page.evaluate((text) => model.logs.editor.setValue(text), text);

// Create the new log
await page.click('#send');
await page.waitFor(100);
});
};

0 comments on commit 035c0bb

Please sign in to comment.