Skip to content

Commit 3f2804c

Browse files
committed
persist username via localStorage and implement conditional profile layout badge
1 parent bf24c91 commit 3f2804c

2 files changed

Lines changed: 32 additions & 13 deletions

File tree

chat-app/client/src/App.tsx

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ import { type Message } from "./types/type";
33
import FormInput from "./components/ChatInput";
44

55
function App() {
6+
const savedUsername = localStorage.getItem("chat_username") || "";
67
const initialForm = {
7-
username: "",
8+
username: savedUsername,
89
text: "",
910
};
1011
const messagesEndRef = useRef<HTMLDivElement>(null);
@@ -91,6 +92,10 @@ function App() {
9192

9293
const handleFormChat = async (e: React.FormEvent<HTMLFormElement>) => {
9394
e.preventDefault();
95+
if (!formChat.username.trim() || !formChat.text.trim()) {
96+
showNotification("Username and message cannot be empty!", "text-red-500");
97+
return;
98+
}
9499
try {
95100
const response = await fetch(`${port}/messages`, {
96101
method: "POST",
@@ -103,8 +108,8 @@ function App() {
103108
}
104109
showNotification("Message sent successfully!", "text-green-500");
105110
await arrayMessages();
106-
107-
setFormChat({ ...initialForm });
111+
localStorage.setItem("chat_username", formChat.username);
112+
setFormChat({ username: formChat.username, text: "" });
108113
} catch (err: unknown) {
109114
const errorMessage =
110115
err instanceof Error ? err.message : "An unknown error occurred";
@@ -216,6 +221,7 @@ function App() {
216221
onSubmit={handleFormChat}
217222
alert={alert}
218223
messageType={messageType}
224+
isNameSaved={!!localStorage.getItem("chat_username")}
219225
/>
220226
</div>
221227

chat-app/client/src/components/ChatInput.tsx

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ interface FormInputProps {
99
>;
1010
alert: string;
1111
messageType: string;
12+
isNameSaved: boolean;
1213
}
1314

1415
const FormInput = ({
@@ -17,6 +18,7 @@ const FormInput = ({
1718
setFormChat,
1819
messageType,
1920
alert,
21+
isNameSaved,
2022
}: FormInputProps) => {
2123
return (
2224
<div className="w-full p-4 border-t border-slate-700 bg-slate-900">
@@ -28,16 +30,27 @@ const FormInput = ({
2830
{alert}
2931
</p>
3032
)}
31-
<input
32-
type="text"
33-
placeholder="Enter your name.."
34-
required
35-
className="w-1/4 max-w-[150px] px-3 py-2 bg-slate-800 text-slate-100 border border-slate-600 rounded-lg outline-none focus:border-indigo-500 transition-all text-sm"
36-
value={formChat.username}
37-
onChange={(e) =>
38-
setFormChat({ ...formChat, username: e.target.value })
39-
}
40-
/>
33+
{!isNameSaved ? (
34+
<input
35+
type="text"
36+
placeholder="Enter your name.."
37+
required
38+
className="w-1/4 max-w-[150px] px-3 py-2 bg-slate-800 text-slate-100 border border-slate-600 rounded-lg outline-none focus:border-indigo-500 transition-all text-sm"
39+
value={formChat.username}
40+
onChange={(e) =>
41+
setFormChat({ ...formChat, username: e.target.value })
42+
}
43+
/>
44+
) : (
45+
<div className="w-1/4 max-w-[150px] flex items-center space-x-1.5 text-xs text-slate-400 font-semibold px-2 py-2 bg-slate-800/60 border border-slate-700/50 rounded-lg truncate select-none">
46+
<span className="w-2 h-2 min-w-[8px] bg-emerald-400 rounded-full animate-pulse" />
47+
<span className="truncate">
48+
User:{" "}
49+
<strong className="text-emerald-400">{formChat.username}</strong>
50+
</span>
51+
</div>
52+
)}
53+
4154
<input
4255
type="text"
4356
placeholder="Start typing a message..."

0 commit comments

Comments
 (0)