Skip to content

Commit

Permalink
Fix form using json
Browse files Browse the repository at this point in the history
  • Loading branch information
uittenbroekrobbert committed May 14, 2024
1 parent 13f920b commit 13daaaf
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 17 deletions.
14 changes: 12 additions & 2 deletions tad/routers/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,19 @@ async def test():
return [{"username": "Rick"}, {"username": "Morty"}]


# TODO this is an ugly work-around, we need a JSON object instead
@router.post("/move", response_class=HTMLResponse)
async def move_task(
async def move_task(request: Request):
json = await request.json()
print(json)
task = tasks_service.move_task(
int(json["taskId"]), int(json["statusId"]), json["previousSiblingId"], json["nextSiblingId"]
)
return templates.TemplateResponse(request=request, name="task.jinja", context={"task": task})


# TODO this is an ugly work-around, we need a JSON object instead
@router.post("/move-form", response_class=HTMLResponse)
async def move_task_form(
request: Request,
taskId: Annotated[int, Form()],
statusId: Annotated[int, Form()],
Expand Down
9 changes: 5 additions & 4 deletions tad/services/tasks_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ def _get_task_by_id(self, task_id: int) -> Task:
return next(task for task in self._tasks if task.id == task_id)

def _get_status_by_id(self, status_id: int) -> Status:
print(status_id)
return next(status for status in self._statuses if status.id == status_id)

def get_statuses(self) -> []:
Expand Down Expand Up @@ -78,14 +79,14 @@ def move_task(self, task_id, status_id, previous_sibling_id, next_sibling_id) ->
if not previous_sibling_id and not next_sibling_id:
task.sort_order = 10
elif previous_sibling_id and next_sibling_id:
previous_task = self.get_task(previous_sibling_id)
next_task = self.get_task(next_sibling_id)
previous_task = self.get_task(int(previous_sibling_id))
next_task = self.get_task(int(next_sibling_id))
new_sort_order = previous_task.sort_order + ((next_task.sort_order - previous_task.sort_order) / 2)
task.sort_order = new_sort_order
elif previous_sibling_id and not next_sibling_id:
previous_task = self.get_task(previous_sibling_id)
previous_task = self.get_task(int(previous_sibling_id))
task.sort_order = previous_task.sort_order + 10
elif not previous_sibling_id and next_sibling_id:
next_task = self.get_task(next_sibling_id)
next_task = self.get_task(int(next_sibling_id))
task.sort_order = next_task.sort_order / 2
return task
File renamed without changes.
22 changes: 15 additions & 7 deletions tad/site/static/js/tad.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,33 @@ window.onload = function () {
animation: 150,
onEnd: function (evt) {
console.log(evt);
let previousSiblingId = evt.item.previousElementSibling ? evt.item.previousElementSibling.getAttribute("data-id") : "";
let nextSiblingId = evt.item.nextElementSibling ? evt.item.nextElementSibling.getAttribute("data-id") : "";
let data = {
"taskId": evt.item.getAttribute("data-id"),
"statusId": evt.to.getAttribute("data-id"),
"previousSiblingId": previousSiblingId,
"nextSiblingId": nextSiblingId
}
if (evt.item.previousElementSibling) {
data["previousSiblingId"] = evt.item.previousElementSibling.getAttribute("data-id");
}
if (evt.item.nextElementSibling) {
data["nextSiblingId"] = evt.item.nextElementSibling.getAttribute("data-id");
}

console.log(JSON.stringify(data));

let headers = {
'Content-Type': 'application/json'
};
let targetId = "#" + evt.item.getAttribute("id");
let form = document.getElementById("cardMovedForm");
document.getElementsByName("taskId")[0].value = evt.item.getAttribute("data-id");
document.getElementsByName("statusId")[0].value = evt.to.getAttribute("data-id");
document.getElementsByName("previousSiblingId")[0].value = previousSiblingId;
document.getElementsByName("nextSiblingId")[0].value = nextSiblingId;
form.setAttribute("hx-target", targetId);
console.log(form)
htmx.trigger("#cardMovedForm", "cardmoved");

// TODO: this must be a JSON post, but I can not get it to work with HTMX...
// https://htmx.org/api/#ajax
htmx.ajax('POST', "/tasks/move", {values: data, target: targetId, swap: 'outerHTML'})
//htmx.ajax('POST', "/tasks/move", {values: JSON.stringify({ data: data }), headers: headers, target: targetId, swap: 'outerHTML'})
}
});
}
Expand Down
9 changes: 5 additions & 4 deletions tad/site/templates/default_layout.jinja
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,11 @@
</nav>
</header>
<div class="container" >
<form id="cardMovedForm" hx-post="/cards/move" hx-ext="json-enc" hx-trigger="cardmoved" hx-target="#board" class="">
<input id="fromList" type="hidden" name="from" value="board">
<input id="toList" type="hidden" name="to" value="board">
<input id="movedCard" type="hidden" name="movedCard" value="">
<form id="cardMovedForm" hx-post="/tasks/move" hx-ext="json-enc" hx-trigger="cardmoved" hx-swap="outerHTML" hx-target="#board" class="">
<input type="hidden" name="taskId" value="">
<input type="hidden" name="statusId" value="">
<input type="hidden" name="previousSiblingId" value="">
<input type="hidden" name="nextSiblingId" value="">
</form>
<h1 class="margin-bottom--large">Project X</h1>
<div class="section group">
Expand Down

0 comments on commit 13daaaf

Please sign in to comment.