Skip to content

Commit

Permalink
Merge pull request #5 from BuildFire/A-H
Browse files Browse the repository at this point in the history
Final release.
  • Loading branch information
ahabeb authored Nov 18, 2020
2 parents 8630b93 + 48b11a7 commit 789d934
Show file tree
Hide file tree
Showing 24 changed files with 891 additions and 294 deletions.
145 changes: 93 additions & 52 deletions control/content/app.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
// The big object that contains all the metrics
let metrics = {};

// Object containes a specific client history
let histories = {};

// Client profile (client query)
let clientProfile = "";

// We used nodeSelector to determine where are we inside the big object
let nodeSelector = "metrics";

Expand Down Expand Up @@ -39,9 +45,15 @@ buildfire.messaging.sendMessageToWidget({
cmd: "refresh",
});

// Initialize clinet history
Histories.getHistories(clientProfile).then((result) => {
histories = result;
});

// To get all metrics and start rendering
Metrics.getMetrics().then((result) => {
metrics = result;

initMaterialComponents();
// To prevent Functional Tests from Applying these lines where it will cause some errors
renderInit();
Expand Down Expand Up @@ -240,19 +252,25 @@ const createMetric = () => {
// Metric fields validation
if (inputValidation()) {
// Empty the form fields after submitting
metricFields.createdBy = `${currentUser.firstName} ${currentUser.lastName}`;
metricFields.lastUpdatedBy = `${currentUser.firstName} ${currentUser.lastName}`;
metricFields.createdBy =
currentUser && currentUser.username ? currentUser.username : null;
metricFields.lastUpdatedBy =
currentUser && currentUser.username ? currentUser.username : null;
metricFields.order = metricsList.childNodes.length;

let newMetric = new Metric(metricFields);

// Save metric
Metrics.insert(
{
nodeSelector,
metricsId: metrics.id,
},
new Metric(metricFields)
newMetric
).then((result) => {
// Assign the metrics value to metrics
metrics = result;

renderInit();
goToMetricspage();
});
Expand All @@ -263,7 +281,8 @@ const updateMetrics = (item) => {
// Metric fields validation
if (inputValidation()) {
let updateObj = {};
updateObj.lastUpdatedBy = `${currentUser.firstName} ${currentUser.lastName}`;
updateObj.lastUpdatedBy =
currentUser && currentUser.username ? currentUser.username : null;

for (let prop in metricFields) {
// To determine which fileds are needed to be updated
Expand Down Expand Up @@ -315,6 +334,7 @@ const updateMetricDB = (updateObj, itemId) => {
itemId
).then((result) => {
metrics = result;

renderInit();
goToMetricspage();
});
Expand Down Expand Up @@ -376,36 +396,44 @@ const inputValidation = () => {
// To initialize and prepare metrics to be rendered
const renderInit = () => {
metricsContainer = metricsList;
// Extract the desired metrics (children) from the big object using nodeSelector
let readyMetrics = helpers.nodeSplitter(nodeSelector, metrics);
let metricsChildren = readyMetrics.metricsChildren;

wysiwygSetContent(readyMetrics.description);

let currentMetricList = [];
// Prepare metrics to be rendered (Object to Array)
for (let metricId in metricsChildren) {
metricsChildren[metricId].id = metricId;
let newMetric = new Metric(metricsChildren[metricId]);
Metric.getHistoryValue(newMetric);
currentMetricList.push(newMetric);
}

// To show messages while metrics being rendered or if there is no metrics at all
let spinner = document.getElementById("spinner");
if (currentMetricList.length === 0) {
spinner.innerHTML = "No metrics have been added yet.";
spinner.classList.remove("loaded");
metricsContainer.innerHTML = "";
} else {
spinner.innerHTML = "";
spinner.classList.add("loaded");
metricsContainer.innerHTML = "";
}
// Filter Metrics before rendering
helpers
.filterCustomerMetrics(metrics, clientProfile)
.then((filteredMetrics) => {
metrics.metrics = filteredMetrics;

// Extract the desired metrics (children) from the big object using nodeSelector
let readyMetrics = helpers.nodeSplitter(nodeSelector, metrics);
let metricsChildren = readyMetrics.metricsChildren;

wysiwygSetContent(readyMetrics.description);

let currentMetricList = [];
// Prepare metrics to be rendered (Object to Array)
for (let metricId in metricsChildren) {
metricsChildren[metricId].id = metricId;
let newMetric = new Metric(metricsChildren[metricId]);
Metric.getHistoryValue(newMetric);
currentMetricList.push(newMetric);
}

currentMetricList = helpers.sortMetrics(currentMetricList, metricsSortBy);
// To show messages while metrics being rendered or if there is no metrics at all
let spinner = document.getElementById("spinner");
if (currentMetricList.length === 0) {
spinner.innerHTML = "No metrics have been added yet.";
spinner.classList.remove("loaded");
metricsContainer.innerHTML = "";
} else {
spinner.innerHTML = "";
spinner.classList.add("loaded");
metricsContainer.innerHTML = "";
}

render(currentMetricList);
currentMetricList = helpers.sortMetrics(currentMetricList, metricsSortBy);

render(currentMetricList);
});
};

// To render metrics
Expand Down Expand Up @@ -479,14 +507,16 @@ const deleteItem = (item, index, callback) => {
if (e) console.error(e);
if (data && data.selectedButton.key == "y") {
sortableList.items.splice(index, 1);
Metrics.delete({ nodeSelector, metricsId: metrics.id }, item.id)
.then((result) => {
// Delete the item from the client history db
Metrics.delete({ nodeSelector, metricsId: metrics.id }, item.id).then(
(result) => {
// Assign the metrics value to metrics
metrics = result;

callback(metrics);
})
.finally(() => {
renderInit();
});
}
);
}
}
);
Expand Down Expand Up @@ -555,21 +585,29 @@ const pushBreadcrumb = (breadcrumb, data) => {

// To synchronize with the widget
buildfire.messaging.onReceivedMessage = (message) => {
// If message has title then it is a push breacrumb
if (message.title) {
nodeSelector = message.nodeSelector;
pushBreadcrumb(message.title, { nodeSelector });
// If it is not then it is a backward breadcrumb
} else {
if (nodeSelector !== message.nodeSelector) {
if (breadcrumbsHistory && breadcrumbsHistory.length) {
// If message has title then it is a push breacrumb
if (message.title) {
nodeSelector = message.nodeSelector;
bread.removeChild(bread.lastChild);
bread.removeChild(bread.lastChild);
breadcrumbsHistory.pop();
pushBreadcrumb(message.title, { nodeSelector });
// If it is not then it is a backward breadcrumb
} else {
if (
nodeSelector &&
nodeSelector !== message.nodeSelector &&
bread.children.length > 2
) {
nodeSelector = message.nodeSelector;
bread.removeChild(bread.lastChild);
bread.removeChild(bread.lastChild);
breadcrumbsHistory.pop();
} else {
return;
}
}
renderInit();
goToMetricspage();
}
renderInit();
goToMetricspage();
};

// To handle users' choices for sorting
Expand All @@ -585,10 +623,6 @@ const onSortByChange = () => {
).then((result) => {
metrics = result;
metricsSortBy = sortBy;
// if (metricsSortBy === "highest" || metricsSortBy === "lowest") {
// // Disable manual sorting
// sortableList.sortableList.option("disabled", true);
// }
renderInit();
});
};
Expand All @@ -600,5 +634,12 @@ const updateDescription = (description) => {
"description"
).then((result) => {
metrics = result;

// Filter Metrics on data change
helpers
.filterCustomerMetrics(metrics, clientProfile)
.then((filteredMetrics) => {
metrics.metrics = filteredMetrics;
});
});
};
2 changes: 2 additions & 0 deletions control/content/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -284,6 +284,8 @@ <h1 class="section__title">Metrics</h1>
<script src="js/classes/analytics.js"></script>
<script src="js/classes/metric.js"></script>
<script src="js/classes/metrics.js"></script>
<script src="js/classes/history.js"></script>
<script src="js/classes/histories.js"></script>

<script src="js/lib/sortableList.js"></script>
<script src="js/authManager.js"></script>
Expand Down
82 changes: 82 additions & 0 deletions control/content/js/classes/histories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
class Histories {
constructor() {}

// Get the big Object (history object)
static getHistories(clientProfile) {
return new Promise((resolve, reject) => {
buildfire.publicData.get(
`history${clientProfile}`,
(err, result) => {
if (err) reject(err);
else {
// Check if there is already objects in the database
if (!result.data.metrics) {
// If there is no object, then create the parent object
buildfire.publicData.save(
{ metrics: {} },
`history${clientProfile}`,
(err, result) => {
if (err) reject(err);
else {
this.getHistories().then((result) => {
resolve(result);
});
}
}
);
} else {
result.data.id = result.id;
resolve(result.data);
}
}
}
);
});
}

// // Add new history in the big object (Control Panel Only)
// static insert({ clientProfile, nodeSelector, historyId }, history) {
// return new Promise((resolve, reject) => {
// if (!nodeSelector) reject("nodeSelector not provided");
// if (!historyId) reject("historyId not provided");

// buildfire.publicData.update(
// historyId,
// { $set: { [`${nodeSelector}.${history.id}`]: history } },
// `history${clientProfile}`,
// (err, result) => {
// if (err) reject(err);
// else {
// result.data.id = historyId;
// resolve(result.data);
// }
// }
// );
// });
// }

// // To delete any history (Control Panel Only)
// static delete({ clientProfile, nodeSelector, historyId }, id) {
// return new Promise((resolve, reject) => {
// if (!nodeSelector) reject("nodeSelector not provided");
// if (!historyId) reject("historyId not provided");

// buildfire.publicData.update(
// historyId,
// {
// $unset: {
// [`${nodeSelector}.${id}`]: "",
// },
// },
// `history${clientProfile}`,
// (err, result) => {
// if (err) reject(err);
// else {
// result.data.id = historyId;
// resolve(result.data);
// }
// }
// );
// });
// }
}
6 changes: 6 additions & 0 deletions control/content/js/classes/history.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
class ClientHistory {
constructor(data = {}) {
this.id = data.id || "";
this.history = data.history || [];
}
}
4 changes: 2 additions & 2 deletions control/content/js/classes/metric.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class Metric {
constructor(data = {}) {
this.id = data.id || "";
this.id = data.id || helpers.uuidv4();
this.title = data.title || "";
this.icon = data.icon || "";
this.min = data.min || 0;
Expand Down Expand Up @@ -43,7 +43,7 @@ class Metric {
return avg;
}
}
//TODO: check if it affect the avg claculations
// TODO: check if it affect the avg claculations
return 0;
}
}
Loading

0 comments on commit 789d934

Please sign in to comment.