forked from developer-job-simulation/javascript-crm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathui.js
57 lines (49 loc) · 1.81 KB
/
ui.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
import { at } from "lodash";
import {fetchCompanies} from "./api";
import {
ACCOUNT_EXECUTIVE_FIELD_NAME,
COMPANIES_TABLE_HEADERS,
COMPANY_NAME_FIELD_NAME,
CREATED_AT_FIELD_NAME,
REVENUE_YTD_FIELD_NAME,
STATUS_FIELD_NAME
} from "./constants";
export const makeTable = async () => {
const companies = await fetchCompanies();
// Print result of api call to the developer console
// Uncomment if you need it for debugging.
// While this method of logging variables of interest to the console is primitive, but often highly valuable debugging technique
// console.log(companies);
// Initialize new array and push a header row
const companiesToDisplay = [];
companiesToDisplay.push(COMPANIES_TABLE_HEADERS);
// Here we simply rearrange company fields in the order in which we want to display them in UI
companies.map(company => {
const row = [];
row.push(
company[COMPANY_NAME_FIELD_NAME],
company[STATUS_FIELD_NAME],
convertDate(company[CREATED_AT_FIELD_NAME]),
convertNumbers(company[REVENUE_YTD_FIELD_NAME]),
company[ACCOUNT_EXECUTIVE_FIELD_NAME]
);
companiesToDisplay.push(row);
});
// Programmatically create html table
const table = document.createElement("table");
document.body.appendChild(table); // Drew the main table node on the document
companiesToDisplay.forEach(row => {
const tr = table.insertRow(); //Create a new row
row.forEach(column => {
const td = tr.insertCell();
td.innerText = column; // Take string from placeholder variable and append it to <tr> node
});
});
function convertDate(date){
let reformatDate = new Date(date);
return reformatDate.toLocaleTimeString("de-DE", { timeStyle: "short"});
}
function convertNumbers(number){
return number.toLocaleString("de-DE").replaceAll(".", " ");
}
};