Skip to content

Commit abebe12

Browse files
fix(ticket): disallow empty message (#1103)
<img width="1086" src="https://github.com/user-attachments/assets/1c5f9b28-3cde-437b-b33f-e7e8b7fb7d91" /> ### PR Checklist - [x] The PR title follows [conventional commits](https://www.conventionalcommits.org/en/v1.0.0/) - [x] Is this closing an open issue? If so, link it, else include a proper description of the changes and rason behind them. - [x] Does the PR have changes to the frontend? If so, include screenshots or a recording of the changes. <br/>If it affect colors, please include screenshots/recording in both light and dark mode. - [x] Does the PR have changes to the backend? If so, make sure tests are added. <br/>And if changing dababase queries, be sure you have ran `sqlx prepare` and committed the changes in the `.sqlx` directory. Co-authored-by: Leo Kettmeir <crowlkats@toaxl.com>
1 parent d2edda0 commit abebe12

File tree

3 files changed

+33
-1
lines changed

3 files changed

+33
-1
lines changed

api/src/api/errors.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -245,6 +245,10 @@ errors!(
245245
status: NOT_FOUND,
246246
"The requested ticket was not found.",
247247
},
248+
TicketMessageEmpty {
249+
status: BAD_REQUEST,
250+
"The ticket message is empty.",
251+
},
248252
TicketMetaNotValid {
249253
status: BAD_REQUEST,
250254
"The metadata for the ticket is not in a valid format, should be a key-value of strings.",

api/src/api/tickets.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,10 @@ pub async fn post_message_handler(
117117
return Err(ApiError::TicketNotFound);
118118
}
119119

120+
if new_message.message.is_empty() {
121+
return Err(ApiError::TicketMessageEmpty);
122+
}
123+
120124
let (message, message_author) = db
121125
.ticket_add_message(id, current_user.id, new_message)
122126
.await?;

frontend/islands/TicketMessageInput.tsx

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// Copyright 2024 the JSR authors. All rights reserved. MIT license.
2+
import { useEffect, useState } from "preact/hooks";
23
import { TbCheck, TbClock } from "tb-icons";
34
import {
45
AdminUpdateTicketRequest,
@@ -13,13 +14,29 @@ export function TicketMessageInput(
1314
{ ticket, user }: { ticket: Ticket; user: FullUser },
1415
) {
1516
const message = useSignal("");
17+
const [error, setError] = useState<string | null>(null);
18+
19+
useEffect(() => {
20+
if (error) {
21+
const timeout = setTimeout(() => {
22+
setError(null);
23+
}, 3000); // 3 seconds
24+
25+
return () => clearTimeout(timeout);
26+
}
27+
}, [error]);
1628

1729
return (
1830
<form
1931
class="space-y-5"
2032
onSubmit={(e) => {
2133
e.preventDefault();
2234

35+
if (message.value.trim() === "") {
36+
setError("Message cannot be empty");
37+
return;
38+
}
39+
2340
api.post(
2441
path`/tickets/${ticket.id}`,
2542
{
@@ -42,7 +59,14 @@ export function TicketMessageInput(
4259
placeholder="Type your message here..."
4360
onChange={(e) => message.value = e.currentTarget!.value}
4461
/>
45-
<div class="flex justify-end gap-4">
62+
<div class="flex justify-end gap-4 items-center">
63+
{error && (
64+
<div class="text-red-500 font-semibold">
65+
<p>
66+
{error}
67+
</p>
68+
</div>
69+
)}
4670
<button type="submit" class="button-primary">Send message</button>
4771
{user.isStaff && (
4872
<button

0 commit comments

Comments
 (0)