generated from jphacks/JP_sample
-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
名前を変更する #74
Merged
Merged
名前を変更する #74
Conversation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
TkymHrt
reviewed
Nov 16, 2024
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
名前変更動きました!
バリデーションだけおかしいので修正お願いします。
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
バリデーションが誤動作していたので、GPTが直してくれました。
import { Button } from "@/components/ui/button";
import { Input } from "@/components/ui/input";
import type { DBUser as User } from "@/types";
import { useState } from "react";
export function RenameDialog({
setIsEditing,
setUserData,
}: {
setIsEditing: (value: boolean) => void;
setUserData: (data: User) => void;
}) {
const [name, setName] = useState("");
const [error, setError] = useState("");
const [isComposing, setIsComposing] = useState(false);
const handleInputChange = (input: string) => {
setName(input);
if (!isComposing) {
validateInput(input);
}
};
const handleCompositionStart = () => {
setIsComposing(true);
};
const handleCompositionEnd = (
e: React.CompositionEvent<HTMLInputElement>,
) => {
setIsComposing(false);
const inputValue = e.currentTarget.value;
setName(inputValue);
validateInput(inputValue);
};
const validateInput = (input: string) => {
// ひらがな、カタカナ、漢字、英数字、スペースのみ許可
const isValid = /^[\u3040-\u30FF\u4E00-\u9FFFa-zA-Z0-9\s]*$/.test(input);
if (isValid) {
setError(""); // エラーをクリア
} else {
setError("日本語または英数字以外の文字は入力できません。");
}
};
const handleNameChange = async (newName: string) => {
const userIdString = localStorage.getItem("userID");
if (!userIdString) {
window.location.href = "/login";
return;
}
try {
// ユーザー情報の取得
const userData = JSON.parse(userIdString);
// POSTリクエスト
const response = await fetch(`/api/user/rename/${userData.id}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ name: newName }),
});
// エラーハンドリング
if (!response.ok) {
setError("ユーザー名の更新に失敗しました。再度お試しください。");
throw new Error("データの取得に失敗しました");
}
// 表示データの修正
const data = await response.json();
setUserData(data.putName);
// localStorageのデータも更新
const user: User = { ...userData, name: newName };
localStorage.setItem("userID", JSON.stringify(user));
// ダイアログを閉じる
setIsEditing(false);
} catch (error) {
console.error("エラーが発生しました:", error);
}
};
return (
<div className="flex flex-col gap-2">
<Input
type="text"
placeholder="新しいユーザー名を入力"
value={name}
onChange={(e) => handleInputChange(e.target.value)}
onCompositionStart={handleCompositionStart}
onCompositionEnd={handleCompositionEnd}
aria-label="ユーザー名入力"
/>
{error && <p className="text-red-500 text-sm">{error}</p>}
<div>
<Button
variant="default"
className="bg-[#333333]"
onClick={() => handleNameChange(name)}
disabled={!name.trim() || !!error} // 名前が空またはエラーがある場合はボタンを無効化
>
保存
</Button>
<Button
variant="outline"
className="ml-2"
onClick={() => {
setName(""); // キャンセル時に入力をクリア
setIsEditing(false);
}}
>
キャンセル
</Button>
</div>
</div>
);
}
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
概要
変更内容
動作確認
関連するIssue
#123
備考