Skip to content

Commit

Permalink
MG-10 - Scripts management (#14)
Browse files Browse the repository at this point in the history
* add validation errors to js

Signed-off-by: ianmuchyri <[email protected]>

* add users scripts to javascript  file

Signed-off-by: ianmuchyri <[email protected]>

* update things, channels and groups

Signed-off-by: ianmuchyri <[email protected]>

* update entity dependency templates

Signed-off-by: ianmuchyri <[email protected]>

* update formatting to 2 spaces

Signed-off-by: ianmuchyri <[email protected]>

* update js formatting

Signed-off-by: ianmuchyri <[email protected]>

* update bootstrap

Signed-off-by: ianmuchyri <[email protected]>

* split javascript files

Signed-off-by: ianmuchyri <[email protected]>

* update endpoints returns

Signed-off-by: ianmuchyri <[email protected]>

* add clipboard js file

Signed-off-by: ianmuchyri <[email protected]>

* remove unused param

Signed-off-by: ianmuchyri <[email protected]>

---------

Signed-off-by: ianmuchyri <[email protected]>
  • Loading branch information
ianmuchyri authored Nov 29, 2023
1 parent 944914b commit 512485f
Show file tree
Hide file tree
Showing 40 changed files with 6,657 additions and 8,904 deletions.
11 changes: 3 additions & 8 deletions .prettierrc
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
{
"plugins": [
"prettier-plugin-go-template"
],
"plugins": ["prettier-plugin-go-template"],
"overrides": [
{
"files": [
"*.html"
],
"files": ["*.html"],
"options": {
"parser": "go-template"
}
}
],
"goTemplateBracketSpacing": true,
"useTabs": true,
"printWidth": 80,
"printWidth": 100,
"semi": true,
"tabWidth": 2,
"jsxSingleQuote": false,
Expand Down
60 changes: 20 additions & 40 deletions ui/api/endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,8 +273,7 @@ func createUserEndpoint(svc ui.Service) endpoint.Endpoint {
}

return uiRes{
code: http.StatusSeeOther,
headers: map[string]string{"Location": usersAPIEndpoint},
code: http.StatusCreated,
}, nil
}
}
Expand All @@ -291,8 +290,7 @@ func createUsersEndpoint(svc ui.Service) endpoint.Endpoint {
}

return uiRes{
code: http.StatusSeeOther,
headers: map[string]string{"Location": usersAPIEndpoint},
code: http.StatusCreated,
}, nil
}
}
Expand Down Expand Up @@ -353,8 +351,7 @@ func updateUserEndpoint(svc ui.Service) endpoint.Endpoint {
}

return uiRes{
code: http.StatusSeeOther,
headers: map[string]string{"Location": usersAPIEndpoint + "/" + req.id},
code: http.StatusOK,
}, nil
}
}
Expand All @@ -375,8 +372,7 @@ func updateUserTagsEndpoint(svc ui.Service) endpoint.Endpoint {
}

return uiRes{
code: http.StatusSeeOther,
headers: map[string]string{"Location": usersAPIEndpoint + "/" + req.id},
code: http.StatusOK,
}, nil
}
}
Expand All @@ -400,8 +396,7 @@ func updateUserIdentityEndpoint(svc ui.Service) endpoint.Endpoint {
}

return uiRes{
code: http.StatusSeeOther,
headers: map[string]string{"Location": usersAPIEndpoint + "/" + req.id},
code: http.StatusOK,
}, nil
}
}
Expand Down Expand Up @@ -712,8 +707,7 @@ func createThingEndpoint(svc ui.Service) endpoint.Endpoint {
}

return uiRes{
code: http.StatusSeeOther,
headers: map[string]string{"Location": thingsAPIEndpoint},
code: http.StatusCreated,
}, nil
}
}
Expand All @@ -730,8 +724,7 @@ func createThingsEndpoint(svc ui.Service) endpoint.Endpoint {
}

return uiRes{
code: http.StatusSeeOther,
headers: map[string]string{"Location": thingsAPIEndpoint},
code: http.StatusCreated,
}, nil
}
}
Expand Down Expand Up @@ -792,8 +785,7 @@ func updateThingEndpoint(svc ui.Service) endpoint.Endpoint {
}

return uiRes{
code: http.StatusSeeOther,
headers: map[string]string{"Location": thingsAPIEndpoint + "/" + req.id},
code: http.StatusOK,
}, nil
}
}
Expand All @@ -814,8 +806,7 @@ func updateThingTagsEndpoint(svc ui.Service) endpoint.Endpoint {
}

return uiRes{
code: http.StatusSeeOther,
headers: map[string]string{"Location": thingsAPIEndpoint + "/" + req.id},
code: http.StatusOK,
}, nil
}
}
Expand All @@ -832,8 +823,7 @@ func updateThingSecretEndpoint(svc ui.Service) endpoint.Endpoint {
}

return uiRes{
code: http.StatusSeeOther,
headers: map[string]string{"Location": thingsAPIEndpoint + "/" + req.id},
code: http.StatusOK,
}, nil
}
}
Expand All @@ -854,8 +844,7 @@ func updateThingOwnerEndpoint(svc ui.Service) endpoint.Endpoint {
}

return uiRes{
code: http.StatusSeeOther,
headers: map[string]string{"Location": thingsAPIEndpoint + "/" + req.id},
code: http.StatusOK,
}, nil
}
}
Expand Down Expand Up @@ -1003,8 +992,7 @@ func createChannelEndpoint(svc ui.Service) endpoint.Endpoint {
}

return uiRes{
code: http.StatusSeeOther,
headers: map[string]string{"Location": channelsAPIEndpoint},
code: http.StatusCreated,
}, nil
}
}
Expand All @@ -1021,8 +1009,7 @@ func createChannelsEndpoint(svc ui.Service) endpoint.Endpoint {
}

return uiRes{
code: http.StatusSeeOther,
headers: map[string]string{"Location": channelsAPIEndpoint},
code: http.StatusCreated,
}, nil
}
}
Expand Down Expand Up @@ -1084,8 +1071,7 @@ func updateChannelEndpoint(svc ui.Service) endpoint.Endpoint {
}

return uiRes{
code: http.StatusSeeOther,
headers: map[string]string{"Location": channelsAPIEndpoint + "/" + req.id},
code: http.StatusOK,
}, nil
}
}
Expand Down Expand Up @@ -1257,8 +1243,7 @@ func createGroupEndpoint(svc ui.Service) endpoint.Endpoint {
}

return uiRes{
code: http.StatusSeeOther,
headers: map[string]string{"Location": groupsAPIEndpoint},
code: http.StatusCreated,
}, nil
}
}
Expand All @@ -1275,8 +1260,7 @@ func createGroupsEndpoint(svc ui.Service) endpoint.Endpoint {
}

return uiRes{
code: http.StatusSeeOther,
headers: map[string]string{"Location": groupsAPIEndpoint},
code: http.StatusCreated,
}, nil
}
}
Expand Down Expand Up @@ -1338,8 +1322,7 @@ func updateGroupEndpoint(svc ui.Service) endpoint.Endpoint {
}

return uiRes{
code: http.StatusSeeOther,
headers: map[string]string{"Location": groupsAPIEndpoint + "/" + req.id},
code: http.StatusOK,
}, nil
}
}
Expand Down Expand Up @@ -1547,8 +1530,7 @@ func updateBootstrap(svc ui.Service) endpoint.Endpoint {
}

return uiRes{
code: http.StatusSeeOther,
headers: map[string]string{"Location": bootstrapAPIEndpoint + "/" + req.id},
code: http.StatusOK,
}, nil
}
}
Expand All @@ -1569,8 +1551,7 @@ func updateBootstrapConnections(svc ui.Service) endpoint.Endpoint {
}

return uiRes{
code: http.StatusSeeOther,
headers: map[string]string{"Location": bootstrapAPIEndpoint + "/" + req.id},
code: http.StatusOK,
}, nil
}
}
Expand All @@ -1593,8 +1574,7 @@ func updateBootstrapCerts(svc ui.Service) endpoint.Endpoint {
}

return uiRes{
code: http.StatusSeeOther,
headers: map[string]string{"Location": bootstrapAPIEndpoint + "/" + req.thingID},
code: http.StatusOK,
}, nil
}
}
Expand Down
32 changes: 16 additions & 16 deletions ui/web/static/js/clipboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,21 @@

//function to copy the ID to the clipboard
function copyToClipboard(button) {
var clientIDElement = button.previousElementSibling.firstChild;
var clientId = clientIDElement.textContent;
var clientIDElement = button.previousElementSibling.firstChild;
var clientId = clientIDElement.textContent;

navigator.clipboard.writeText(clientId).then(
function () {
//change the copy icon to indicate success
button.innerHTML = `<i class="fas fa-check success-icon">`;
setTimeout(function () {
//revert the copy icon after a short delay
button.innerHTML = `<i class ="far fa-copy">`;
}, 1000);
},
function (error) {
//handle error
console.error("failed to copy to clipboard: ", error);
},
);
navigator.clipboard.writeText(clientId).then(
function () {
//change the copy icon to indicate success
button.innerHTML = `<i class="fas fa-check success-icon">`;
setTimeout(function () {
//revert the copy icon after a short delay
button.innerHTML = `<i class ="far fa-copy">`;
}, 1000);
},
function (error) {
//handle error
console.error("failed to copy to clipboard: ", error);
},
);
}
13 changes: 13 additions & 0 deletions ui/web/static/js/errors.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

export function displayErrorMessage(errorMessage, divName) {
const errorDiv = document.getElementById(divName);
errorDiv.style.display = "block";
errorDiv.innerHTML = errorMessage;
}

export function removeErrorMessage(divName) {
const errorDiv = document.getElementById(divName);
errorDiv.style.display = "none";
}
63 changes: 63 additions & 0 deletions ui/web/static/js/forms.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// Copyright (c) Abstract Machines
// SPDX-License-Identifier: Apache-2.0

// config parameters are: formId, url, alertDiv, modal
export function submitCreateForm(config) {
const form = document.getElementById(config.formId);
form.addEventListener("submit", function (event) {
event.preventDefault();
const formData = new FormData(form);

fetch(config.url, {
method: "POST",
body: formData,
})
.then(function (response) {
switch (response.status) {
case 409:
showAlert("entity already exists!", config.alertDiv);
break;
case 400:
showAlert("invalid file contents!", config.alertDiv);
break;
case 415:
showAlert("invalid file type!", config.alertDiv);
break;
default:
form.reset();
config.modal.hide();
window.location.reload();
}
})
.catch((error) => {
console.error("error submitting form: ", error);
});
});
}

export function submitUpdateForm(config) {
fetch(config.url, {
method: "POST",
body: JSON.stringify(config.data),
headers: {
"Content-Type": "application/json",
},
}).then((response) => {
switch (response.status) {
case 409:
showAlert("entity already exists!", config.alertDiv);
break;
default:
window.location.reload();
}
});
}

function showAlert(errorMessage, alertDiv) {
const alert = document.getElementById(alertDiv);
alert.innerHTML = `
<div class="alert alert-danger alert-dismissable fade show d-flex flex-row justify-content-between" role="alert">
<div>${errorMessage}</div>
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="close"></button>
</div> `;
}
Loading

0 comments on commit 512485f

Please sign in to comment.