-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSuggestionsPanel.tsx
101 lines (90 loc) · 3.24 KB
/
SuggestionsPanel.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import React, { useState } from "react";
import CreateSuggestionRequest from "@/models/SuggestionModels/CreateSuggestionRequest";
import suggestionService from "@/service/suggestionService";
import { toast } from "../Toaster";
import { AxiosError } from "axios";
import { useAuth } from "@/context/useAuth";
type SuggestionsPanelProps = {
onClose: () => void;
};
const SuggestionsPanel: React.FC<SuggestionsPanelProps> = (props) => {
const {
onClose,
} = props;
const { accessToken } = useAuth();
const [error, setError] = useState<string | null>(null);
const handleSubmit = async (event: React.FormEvent) => {
event.preventDefault();
const form = event.target as HTMLFormElement;
try {
const newSuggestion = {
title: (form.elements.namedItem("title") as HTMLInputElement).value,
description: (form.elements.namedItem("description") as HTMLTextAreaElement).value,
} as CreateSuggestionRequest;
await suggestionService.createSuggestion(newSuggestion, accessToken!);
toast.success("Suggestion submitted successfully!");
onClose();
} catch (error: unknown) {
if (error instanceof AxiosError && error.response) {
setError(error.response.data.error);
} else {
setError("An unexpected error occurred.");
}
}
};
return (
<div className="fixed inset-0 z-50 flex items-center justify-center">
<div className="w-full max-w-md p-4 rounded-md shadow-lg bg-slate-100">
<div className="mb-4">
<h2 className="text-lg font-semibold text-muted-foreground">Suggestions</h2>
<p className="text-sm text-gray-600">
Suggest a quest or feature
</p>
</div>
<form className="flex flex-col" onSubmit={handleSubmit}>
<div className="p-2">
<label htmlFor="title" className="sr-only">Title</label>
<input
id="title"
name="title"
type="text"
placeholder="Title"
className="w-full p-2 border border-gray-300 rounded-md"
/>
</div>
<div className="p-2">
<label htmlFor="description" className="sr-only">Suggestion</label>
<textarea
id="description"
name="description"
placeholder="Details of Suggestion, if this is a quest, please include the quest details and potential objectives"
className="w-full p-2 border border-gray-300 rounded-md"
/>
</div>
{
error &&
<div className="text-sm text-red-500 text-end">
{error}
</div>
}
<div className="flex justify-end mt-2 space-x-2">
<button
type="button"
className="p-2 transition-colors rounded-md text-slate-500 hover:bg-slate-400 hover:text-white"
onClick={onClose}
>
Cancel
</button>
<button
type="submit"
className="p-2 text-white transition-colors rounded-md bg-slate-500 hover:bg-slate-400"
>
Submit
</button>
</div>
</form>
</div>
</div>
);
}
export default SuggestionsPanel;