Skip to content

Commit

Permalink
Order messages correctly in chat (GladysAssistant#806)
Browse files Browse the repository at this point in the history
  • Loading branch information
atrovato authored and NickDub committed Aug 7, 2020
1 parent 284a0ec commit e86e1b9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 13 deletions.
13 changes: 3 additions & 10 deletions front/src/actions/message.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,7 @@ import uuid from 'uuid';
const TYPING_MIN_TIME = 400;
const TYPING_MAX_TIME = 600;

const sortMessages = messages =>
messages.sort((a, b) => {
if (a.created_at < b.created_at) {
return -1;
}
if (a.created_at > b.created_at) {
return 1;
}
return 0;
});
const sortMessages = messages => messages.sort((a, b) => a.created_at - b.created_at);

function createActions(store) {
const actions = {
Expand All @@ -32,6 +23,8 @@ function createActions(store) {
});
try {
let messages = await state.httpClient.get('/api/v1/message');
// Force date usage
messages.forEach(message => (message.created_at = new Date(message.created_at)));
messages = sortMessages(messages);
store.setState({
messages,
Expand Down
6 changes: 3 additions & 3 deletions front/src/config/demo.json
Original file line number Diff line number Diff line change
Expand Up @@ -465,23 +465,23 @@
"receiver_id": "0cd30aef-9c4e-4a23-88e3-3547971296e5",
"text": "It's a clear day today. Temperature outside is 26°C.",
"is_read": true,
"created_at": "2019-02-12T07:49:07.556Z"
"created_at": "2019-02-12T07:45:07.556Z"
},
{
"id": "247b1dd0-6fab-47a8-a9c8-1405deae0ae8",
"sender_id": "0cd30aef-9c4e-4a23-88e3-3547971296e5",
"receiver_id": null,
"text": "What's the weather like?",
"is_read": true,
"created_at": "2019-02-12T07:49:07.556Z"
"created_at": "2019-02-12T07:44:07.556Z"
},
{
"id": "247b1dd0-6fab-47a8-a9c8-1405deae0ae8",
"sender_id": null,
"receiver_id": "0cd30aef-9c4e-4a23-88e3-3547971296e5",
"text": "It's 24°C in the kitchen.",
"is_read": true,
"created_at": "2019-02-12T07:49:07.556Z"
"created_at": "2019-02-12T07:49:07.557Z"
},
{
"id": "247b1dd0-6fab-47a8-a9c8-1405deae0ae8",
Expand Down

0 comments on commit e86e1b9

Please sign in to comment.