Skip to content

Commit

Permalink
toggle on/off functional.
Browse files Browse the repository at this point in the history
  • Loading branch information
bengarrett committed Apr 30, 2024
1 parent 902e0f6 commit 9c90a48
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 30 deletions.
33 changes: 27 additions & 6 deletions assets/js/layout-htmx.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,22 @@ export function htmxEvents() {
afterRequest(event, `uploader-text-releaser-1`, `uploader-text-alert`);
afterRequest(event, `uploader-text-releaser-2`, `uploader-text-alert`);

afterRecord(event, `artifact-editor-hidden`, `artifact-editor-alert`);
afterRecord(event, `artifact-editor-public`, `artifact-editor-alert`);
afterRecord(
event,
`artifact-editor-hidden`,
`artifact-editor-public`,
`artifact-editor-alert`
);
afterRecord(
event,
`artifact-editor-public`,
`artifact-editor-hidden`,
`artifact-editor-alert`
);
});
}

function afterRecord(event, inputId, alertId) {
function afterRecord(event, inputId, revertId, alertId) {
if (event.detail.elt === null) return;
if (event.detail.elt.id !== `${inputId}`) return;

Expand All @@ -47,7 +57,7 @@ function afterRecord(event, inputId, alertId) {
return recordSuccess(event, inputId, liveAlert);
}
if (event.detail.failed && event.detail.xhr) {
return recordError(event, liveAlert);
return recordError(event, revertId, liveAlert);
}
errorBrowser(liveAlert);
}
Expand Down Expand Up @@ -84,10 +94,21 @@ function recordSuccess(event, inputId, alertElm) {
* @param {CustomEvent} event - The event object containing the XHR details.
* @param {HTMLElement} alertElm - The alert element to display the error message.
*/
function recordError(event, alertElm) {
function recordError(event, revertId, alertElm) {
const xhr = event.detail.xhr;
alertElm.innerText = `Could not update the database record, ${xhr.responseText}.`;
alertElm.innerText = `${timeNow()} Could not update the database record, ${xhr.responseText}.`;
alertElm.classList.remove("d-none");
document.getElementById(revertId).checked = true;
}

function timeNow() {
let now = new Date();
let hours = now.getHours();
let minutes = now.getMinutes();
let seconds = now.getSeconds();
minutes = (minutes < 10 ? "0" : "") + minutes;
seconds = (seconds < 10 ? "0" : "") + seconds;
return hours + ":" + minutes + ":" + seconds;
}

/**
Expand Down
46 changes: 23 additions & 23 deletions handler/htmx/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,33 @@ package htmx

import (
"net/http"
"strconv"

"github.com/Defacto2/server/model"
"github.com/labstack/echo/v4"
)

// badRequest returns a JSON response with a 400 status code,
// the server cannot or will not process the request due to something that is perceived to be a client error.
func badRequest(c echo.Context, err error) error {
return c.JSON(http.StatusBadRequest, map[string]string{"error": "bad request " + err.Error()})
}

// RecordToggle handles the post submission for the File artifact is online and public toggle.
func RecordToggle(c echo.Context, state bool) error {
return c.String(http.StatusBadRequest, "bad request")
// key := c.FormValue("artifact-editor-key")
// fmt.Println("key: ", key)
// // var f Form
// // if err := c.Bind(&f); err != nil {
// // return badRequest(c, err)
// // }
// // if state {
// // if err := model.UpdateOnline(c, int64(f.ID)); err != nil {
// // return badRequest(c, err)
// // }
// // return c.JSON(http.StatusOK, f)
// // }
// // if err := model.UpdateOffline(c, int64(f.ID)); err != nil {
// // return badRequest(c, err)
// // }
// return c.String(http.StatusOK, "keyyer: "+key)
key := c.FormValue("artifact-editor-key")
id, err := strconv.Atoi(key)
if err != nil {
return badRequest(c, err)
}
if state {
if err := model.UpdateOnline(c, int64(id)); err != nil {
return badRequest(c, err)
}
return c.String(http.StatusOK, "online")
}
if err := model.UpdateOffline(c, int64(id)); err != nil {
return badRequest(c, err)
}
return c.String(http.StatusOK, "offline")
}

// badRequest returns an error response with a 400 status code,
// the server cannot or will not process the request due to something that is perceived to be a client error.
func badRequest(c echo.Context, err error) error {
return c.String(http.StatusBadRequest, err.Error())
}
2 changes: 1 addition & 1 deletion public/js/layout.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 9c90a48

Please sign in to comment.