I am creating chat functionality in my web app. I am using mysql to store data in a table with a userId
and fromId
columns. The userId
is the message recepient while fromId
is the sender.
So, what comes from the GET /messages
endpoint looks something like this:
[
{
"id": 10,
"userId": 2,
"fromId": 4,
"message": "messsage",
"read": 0,
"createdAt": "2022-02-22T11:05:54.000Z",
"updatedAt": "2022-02-22T11:05:54.000Z",
"user.first": "Fourth",
"user.last": "NNe",
"user.email": "hello3@feathersjs.com",
"user.phone": null,
"user.location": null,
"user.isVerified": 0,
"user.roles": "user"
},
{
"id": 9,
"userId": 4,
"fromId": 2,
"message": "messsage",
"read": 0,
"createdAt": "2022-02-22T08:32:01.000Z",
"updatedAt": "2022-02-22T08:32:01.000Z",
"user.first": "Jane",
"user.last": "Doe",
"user.email": "hello1@feathersjs.com",
"user.phone": null,
"user.location": null,
"user.isVerified": 0,
"user.roles": "user"
},
{
"id": 8,
"userId": 2,
"fromId": 3,
"message": "messsage",
"read": 1,
"createdAt": "2022-02-22T08:31:52.000Z",
"updatedAt": "2022-02-22T08:31:52.000Z",
"user.first": "Third",
"user.last": "Tatu",
"user.email": "hello2@feathersjs.com",
"user.phone": null,
"user.location": null,
"user.isVerified": 0,
"user.roles": "user"
},
{
"id": 7,
"userId": 2,
"fromId": 4,
"message": "messsage",
"read": 1,
"createdAt": "2022-02-22T08:31:48.000Z",
"updatedAt": "2022-02-22T08:31:48.000Z",
"user.first": "Fourth",
"user.last": "NNe",
"user.email": "hello3@feathersjs.com",
"user.phone": null,
"user.location": null,
"user.isVerified": 0,
"user.roles": "user"
},
{
"id": 6,
"userId": 3,
"fromId": 2,
"message": "messsage",
"read": 0,
"createdAt": "2022-02-22T07:29:49.000Z",
"updatedAt": "2022-02-22T07:29:49.000Z",
"user.first": "Jane",
"user.last": "Doe",
"user.email": "hello1@feathersjs.com",
"user.phone": null,
"user.location": null,
"user.isVerified": 0,
"user.roles": "user"
},
{
"id": 5,
"userId": 2,
"fromId": 1,
"message": "messsage",
"read": 0,
"createdAt": "2022-02-22T07:29:48.000Z",
"updatedAt": "2022-02-22T07:29:48.000Z",
"user.first": "Joseph",
"user.last": "Mungai",
"user.email": "hello@feathersjs.com",
"user.phone": null,
"user.location": null,
"user.isVerified": 0,
"user.roles": "admin"
},
{
"id": 4,
"userId": 1,
"fromId": 2,
"message": "messsage",
"read": 0,
"createdAt": "2022-02-22T07:20:37.000Z",
"updatedAt": "2022-02-22T07:20:37.000Z",
"user.first": "Jane",
"user.last": "Doe",
"user.email": "hello1@feathersjs.com",
"user.phone": null,
"user.location": null,
"user.isVerified": 0,
"user.roles": "user"
},
{
"id": 2,
"userId": 2,
"fromId": 1,
"message": "If you have any questions or problems, you can ask them here.",
"read": 0,
"createdAt": "2022-02-22T07:16:04.000Z",
"updatedAt": "2022-02-22T07:16:04.000Z",
"user.first": "Joseph",
"user.last": "Mungai",
"user.email": "hello@feathersjs.com",
"user.phone": null,
"user.location": null,
"user.isVerified": 0,
"user.roles": "admin"
}
]
Now what I can't figure out is how to manipulate the array response to get different arrays of each conversation. I think I should use array.reduce
method but I can't wrap my head around how to do that.
As shown in the image below for the UI, I would like the column marked 'names' in red, to have the different arrays of each conversation. And the column marked 'chat' in blue to have the contents of each of these conversations.
I am pretty sure this is doable, please help.