-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
CHE-65 Created CreateThread component and updated ThreadsDisplay to s…
…upport functionality
- Loading branch information
1 parent
175d75b
commit 37bd4b8
Showing
3 changed files
with
116 additions
and
1 deletion.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
client/src/components/Forums/CreateThread/CreateThread.tsx
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
import React, { useState, ChangeEvent, FormEvent } from "react"; | ||
import axios from "axios"; | ||
|
||
interface CreateThreadProps { | ||
forumId: string; | ||
onClose: () => void; | ||
} | ||
|
||
const CreateThread: React.FC<CreateThreadProps> = ({ forumId, onClose }) => { | ||
const [formData, setFormData] = useState<{ title: string; content: string }>({ | ||
title: "", | ||
content: "", | ||
}); | ||
|
||
const { title, content } = formData; | ||
|
||
const handleChange = ( | ||
e: ChangeEvent<HTMLInputElement | HTMLTextAreaElement> | ||
) => { | ||
setFormData({ ...formData, [e.target.name]: e.target.value }); | ||
}; | ||
|
||
const handleSubmit = async (e: FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
try { | ||
const response = await axios.post( | ||
`/api/forums/${forumId}/threads`, | ||
formData, | ||
{ | ||
withCredentials: true, | ||
} | ||
); | ||
console.log("Thread created:", response.data); | ||
onClose(); | ||
} catch (error) { | ||
console.error("Failed to create thread:", error); | ||
//TODO userfeedback with errors | ||
} | ||
}; | ||
|
||
return ( | ||
<div className="w-full max-w-lg"> | ||
<form | ||
className="bg-white shadow-md rounded px-8 pt-6 pb-8 mb-4" | ||
onSubmit={handleSubmit} | ||
> | ||
<div className="mb-4"> | ||
<label | ||
className="block text-gray-700 text-sm font-bold mb-2" | ||
htmlFor="title" | ||
> | ||
Title | ||
</label> | ||
<input | ||
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" | ||
id="title" | ||
name="title" | ||
type="text" | ||
placeholder="Thread Title" | ||
value={title} | ||
onChange={handleChange} | ||
required | ||
/> | ||
</div> | ||
<div className="mb-6"> | ||
<label | ||
className="block text-gray-700 text-sm font-bold mb-2" | ||
htmlFor="content" | ||
> | ||
Content | ||
</label> | ||
<textarea | ||
className="shadow appearance-none border rounded w-full py-2 px-3 text-gray-700 leading-tight focus:outline-none focus:shadow-outline" | ||
id="content" | ||
name="content" | ||
rows={4} | ||
placeholder="Write something..." | ||
value={content} | ||
onChange={handleChange} | ||
required | ||
/> | ||
</div> | ||
<div className="flex items-center justify-between"> | ||
<button | ||
className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded focus:outline-none focus:shadow-outline" | ||
type="submit" | ||
> | ||
Create Thread | ||
</button> | ||
</div> | ||
</form> | ||
</div> | ||
); | ||
}; | ||
|
||
export default CreateThread; |
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
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