<script src="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.0.2/mocha.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.15.0/lodash.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/babel-polyfill/6.13.0/polyfill.js"></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/mocha/3.0.2/mocha.css">
<script src="https://cdnjs.cloudflare.com/ajax/libs/chai/3.5.0/chai.js"></script>
<!-- No need to change this -->
<!-- Mocha test output goes here. -->
<div id="mocha"></div>
Babel + JSX
/*
* Implement a function that returns the summary of the current user's latest conversations,
* sorted by the latest message's timestamp (most recent conversation first).
*
* Make sure to have good unit tests in addition to the provided integration test!
*
* You have the following REST API available (base URL provided as a constant):
*
* Get current user's conversations: GET /conversations
* Get messages in a conversation: GET /conversations/:conversation_id/messages
* Get user by ID: GET /users/:user_id
*
* The result should be an array of objects of the following shape/type:
* {
* id : string;
* latest_message: {
* id : string;
* body : string;
* from_user : {
* id: string;
* avatar_url: string;
* };
* created_at : ISOString;
* };
* }
*
*/
const API_BASE_URL = "https://ui-developer-backend.herokuapp.com/api";
async function getRecentConversationSummaries() {
var result = [];
try{ // First retrieve all the conversations
const conversations = await fetch(API_BASE_URL+"/conversations").then(function(response) {
if(response.ok) {
return response.json();
} else {
console.error('Error while fetching conversations.');
}
});
// Then retrieve all their messages
var promises = [];
var messages = [];
for(var i = 0; i < conversations.length; i++){
promises.push(fetch(API_BASE_URL+"/conversations/"+conversations[i].id+"/messages").then(function(response) {
if(response.ok) {
return response.json();
} else {
console.error('Error while fetching messages.');
}
}))
}
// Sorted by latest
messages = await Promise.all(promises).then(values => {
for(var i = 0; i < values.length; i++){
values[i].sort(datetimeSort);
}
return values;
});
// Next we need users info
var users = [];
var usersIds = messages.map(function(message){
return message[0].from_user_id;
});
...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.