diff --git a/src/components/habits.component.tsx b/src/components/habits.component.tsx index fb7c6b4..a4fe5c1 100644 --- a/src/components/habits.component.tsx +++ b/src/components/habits.component.tsx @@ -6,8 +6,7 @@ import { } from "$components/buttons.component"; import { FormField } from "$components/fields.component"; import { - Notification, - notificationListId, + Notification } from "$components/notifications.component"; import { LimitPaginationRadio } from "$components/pagination.component"; import { Habit } from "$db/models"; @@ -17,13 +16,13 @@ import clsx from "clsx"; export type HabitsProps = { habits: Habit[] }; export function CreateHabitForm() { - const targetId = "habit-list"; + const targetId = "habit-container"; const createHabitErrorMessageId = "create-habit-error"; const createHabitFormRef = "create-habit-form"; return (
() return html( <> - () ), async ({ html, req }) => { const { date, id } = req.valid("param"); - const updatedExistingHabit = await habitService.updateDateInHistory(id, date); - + const updatedExistingHabit = await habitService.updateDateInHistory( + id, + date + ); + return html( () return text("Please select another color than black", 400); } - const [createdHabit, habitsCount] = await executeHandlerForSessionUser( + const createdHabit = await executeHandlerForSessionUser( async (user) => - Promise.all([ - habitService.create({ - ...body, - userId: user.id, - }), - habitService.count(user.id), - ]), + habitService.create({ + ...body, + userId: user.id, + }), sessionUser ); @@ -230,19 +231,49 @@ export const habitApiController = new Hono<{ Variables: AppVariables }>() res.headers.append("HX-Reswap", "innerHTML"); return text("An error occured", 500); } - if (habitsCount === 0) { - res.headers.append("HX-Reswap", "outerHTML"); - return html(, 201); - } + + const notification: Notification = { + type: "success", + message: "Habit created successfully", + }; + + // Refetch new habit list + const url = getURL(req); + const { limit, page, search } = getPaginationQueries(url); + const [habits, count] = await executeHandlerForSessionUser( + async (user) => + search + ? await habitService.findManyWithCountByTitle( + search, + user.id, + page > 1 ? page * limit : limit + ) + : await habitService.findManyWithCountByUserId( + user.id, + page > 1 ? page * limit : limit + ), + sessionUser + ); + return html( - , - 201 + <> + + {habits.length || search ? ( + + ) : ( + + )} + ); } ) @@ -358,56 +389,56 @@ export const habitApiController = new Hono<{ Variables: AppVariables }>() /> ); }) -.delete( - "/bulk", - zValidator("query", z.object({ items: z.array(z.string()) })), - async ({ req, html, get }) => { - const { items } = req.valid("query"); - const deletedResults = await habitService.deleteBulkIds(items); - const notification: Notification = { - type: "success", - message: `${deletedResults.deletedCount} selected habits deleted successfully`, - }; - - // Refetch new habit list - const url = getURL(req); - const { limit, page, search } = getPaginationQueries(url); - const sessionUser = get("sessionUser"); - const [habits, count] = await executeHandlerForSessionUser( - async (user) => - search - ? await habitService.findManyWithCountByTitle( - search, - user.id, - page > 1 ? page * limit : limit - ) - : await habitService.findManyWithCountByUserId( - user.id, - page > 1 ? page * limit : limit - ), - sessionUser - ); + .delete( + "/bulk", + zValidator("query", z.object({ items: z.array(z.string()) })), + async ({ req, html, get }) => { + const { items } = req.valid("query"); + const deletedResults = await habitService.deleteBulkIds(items); + const notification: Notification = { + type: "success", + message: `${deletedResults.deletedCount} selected habits deleted successfully`, + }; - return html( - <> - + search + ? await habitService.findManyWithCountByTitle( + search, + user.id, + page > 1 ? page * limit : limit + ) + : await habitService.findManyWithCountByUserId( + user.id, + page > 1 ? page * limit : limit + ), + sessionUser + ); + + return html( + <> + - {habits.length || search ? ( - - ) : ( - - )} - - ); - } -) -.route("/:id", habitIdApiController); + {habits.length || search ? ( + + ) : ( + + )} + + ); + } + ) + .route("/:id", habitIdApiController); diff --git a/src/script/alpine/data/habit/habit-item.js b/src/script/alpine/data/habit/habit-item.js index 01f9736..6c50f40 100644 --- a/src/script/alpine/data/habit/habit-item.js +++ b/src/script/alpine/data/habit/habit-item.js @@ -17,19 +17,12 @@ * @param {Object} props * @param {string} props.tooltipInformation * @param {string} props.itemId - * @param {import("$components/notifications.component").Notification} [props.xNotification] * @return {import("alpinejs").AlpineComponent} */ -export function habitItemData({itemId, tooltipInformation, xNotification}) { +export function habitItemData({itemId, tooltipInformation}) { return { itemId, - xNotification, tooltipInformation, - init() { - if (this.xNotification) { - this.$notify(this.xNotification); - } - }, addItemToSet() { this.itemIdsToDelete.add(this.itemId); },