Last active
May 2, 2022 20:49
-
-
Save the-glima/55cec1a5c24a2e454561606e0271126d to your computer and use it in GitHub Desktop.
NexHealth: Front-end Challenge JS Part B
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// CodeSandbox: https://codesandbox.io/s/fe-code-challenge-example-52iitk | |
// Part B - Solution | |
import "./styles.css"; | |
import * as partA from "./part-a.js"; | |
// Append | |
const appendDupes = (group, wrapper) => { | |
const listItems = sortByDate(group[1]).map(buidlCard).join(""); | |
const html = ` | |
<li class="dupe"> | |
<h2>Dupes: ${group[0]}</h2> | |
<ul>${listItems}</ul> | |
</li> | |
`; | |
wrapper.insertAdjacentHTML("afterbegin", html); | |
}; | |
const buidlCard = (item) => { | |
const appointment = item.appointment; | |
const time = getFormattedDate(appointment.created_at, null, "short"); | |
const date = getFormattedDate(appointment.created_at); | |
return ` | |
<li class="card"> | |
<span class="small">${time} ${date}</span> | |
<h3 class="card-title"> | |
<span>${appointment.actor_name}</span> | |
<span class="rated-info">rated his visit | |
${appointment.context.rating_value} out of | |
${appointment.context.max_rating_value} | |
</span> | |
</h3> | |
<span class="small">Appointment date ${date}</span> | |
</li> | |
`; | |
}; | |
// Init | |
(async () => { | |
const $wrapper = document.getElementById("dupes-list"); | |
const dupes = await partA.filterDupes(false); | |
if (dupes) { | |
Object.entries(dupes).forEach((dupe) => appendDupes(dupe, $wrapper)); | |
} else { | |
const html = ` | |
<li class="dupe"> | |
<p>No data found</p> | |
</li> | |
`; | |
$wrapper.insertAdjacentHTML("afterbegin", html); | |
} | |
})(); | |
// Helpers Functions | |
const getFormattedDate = (date, dateStyle = "medium", timeStyle = null) => { | |
const newDate = new Date(date); | |
const options = { | |
...(dateStyle && { dateStyle: dateStyle }), | |
...(timeStyle && { timeStyle: timeStyle }) | |
}; | |
return new Intl.DateTimeFormat("en-US", options).format(newDate); | |
}; | |
const sortByDate = (data) => | |
data.sort( | |
(a, b) => | |
new Date(b.appointment.created_at) - new Date(a.appointment.created_at) | |
); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment