|
| 1 | +import React, { useState } from 'react'; |
| 2 | +import axios from 'axios'; |
| 3 | +import { |
| 4 | + AlertDialog, |
| 5 | + AlertDialogCancel, |
| 6 | + AlertDialogContent, |
| 7 | + AlertDialogDescription, |
| 8 | + AlertDialogHeader, |
| 9 | + AlertDialogTitle, |
| 10 | + AlertDialogTrigger, |
| 11 | +} from "@/components/ui/alert-dialog"; |
| 12 | +import { Cross1Icon } from "@radix-ui/react-icons"; |
| 13 | +import { Input } from '@/components/ui/input'; |
| 14 | +import TagInput from '@/components/MultiSelect/TagInput'; |
| 15 | +import { toast } from 'sonner'; |
| 16 | +import { Button } from "@/components/ui/button"; |
| 17 | +import { Icons } from "@/components/ui/icons"; |
| 18 | +import { Textarea } from '../ui/textarea'; |
| 19 | + |
| 20 | +const backendUrl = import.meta.env.VITE_BACKEND_URL || 'http://localhost:5000'; |
| 21 | + |
| 22 | +interface Project { |
| 23 | + title: string; |
| 24 | + description: string; |
| 25 | + repoLink: string; |
| 26 | + tags: Tag[]; |
| 27 | +} |
| 28 | + |
| 29 | +interface Tag { |
| 30 | + value: string; |
| 31 | + label: string; |
| 32 | +} |
| 33 | + |
| 34 | +const AddProject: React.FC<{ onProjectChange: () => void }> = ({ onProjectChange }) => { |
| 35 | + const [newProject, setNewProject] = useState<Project>({ |
| 36 | + title: '', |
| 37 | + description: '', |
| 38 | + repoLink: '', |
| 39 | + tags: [], |
| 40 | + }); |
| 41 | + |
| 42 | + const username = localStorage.getItem('devhub_username'); |
| 43 | + const [isLoading, setIsLoading] = useState(false); |
| 44 | + |
| 45 | + const handleProjectChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => { |
| 46 | + const { name, value } = e.target; |
| 47 | + setNewProject((prevProject) => ({ |
| 48 | + ...prevProject, |
| 49 | + [name]: value, |
| 50 | + })); |
| 51 | + }; |
| 52 | + |
| 53 | + const handleAddProject = async () => { |
| 54 | + if (!username) { |
| 55 | + toast.error('Username not found'); |
| 56 | + return; |
| 57 | + } |
| 58 | + |
| 59 | + setIsLoading(true); |
| 60 | + |
| 61 | + try { |
| 62 | + // Convert tags array to a comma-separated string |
| 63 | + const tagsString = newProject.tags.map((tag) => tag.value).join(','); |
| 64 | + |
| 65 | + const response = await axios.post( |
| 66 | + `${backendUrl}/profile/${username}/projects`, |
| 67 | + { |
| 68 | + title: newProject.title, |
| 69 | + description: newProject.description, |
| 70 | + repo_link: newProject.repoLink, |
| 71 | + tags: tagsString, // Send as comma-separated string |
| 72 | + }, |
| 73 | + { withCredentials: true }, |
| 74 | + ); |
| 75 | + |
| 76 | + if (response.status === 200 || response.status === 201) { |
| 77 | + toast.success('Project added successfully'); |
| 78 | + setNewProject({ title: '', description: '', repoLink: '', tags: [] }); // Reset form |
| 79 | + onProjectChange(); |
| 80 | + } else { |
| 81 | + toast.error('Failed to add project'); |
| 82 | + } |
| 83 | + } catch (error) { |
| 84 | + console.error('Failed to add project:', error); |
| 85 | + toast.error('An error occurred while adding the project'); |
| 86 | + } finally { |
| 87 | + setIsLoading(false); // Reset loading state |
| 88 | + } |
| 89 | + }; |
| 90 | + |
| 91 | + |
| 92 | + |
| 93 | + |
| 94 | + return ( |
| 95 | + <AlertDialog> |
| 96 | + <AlertDialogTrigger className="w-[120px] ml-5 mt-5 mb-3"> |
| 97 | + <Button variant="outline" className="h-[50px]">Add Project</Button> |
| 98 | + </AlertDialogTrigger> |
| 99 | + <AlertDialogContent> |
| 100 | + <div className='flex'> |
| 101 | + <AlertDialogHeader className='text-2xl'>Add New Project</AlertDialogHeader> |
| 102 | + <div className='flex-grow'></div> |
| 103 | + <AlertDialogCancel><Cross1Icon className='h-3 w-3'/></AlertDialogCancel> |
| 104 | + </div> |
| 105 | + <AlertDialogDescription> |
| 106 | + <AlertDialogTitle></AlertDialogTitle> |
| 107 | + <div className="grid gap-6 sm:w-80"> |
| 108 | + <div> |
| 109 | + <Input |
| 110 | + id="newProjectTitle" |
| 111 | + name="title" |
| 112 | + value={newProject.title} |
| 113 | + onChange={handleProjectChange} |
| 114 | + disabled={isLoading} |
| 115 | + placeholder="Project title" |
| 116 | + className="mt-2" |
| 117 | + /> |
| 118 | + </div> |
| 119 | + <div> |
| 120 | + <Textarea |
| 121 | + id="newProjectDescription" |
| 122 | + name="description" |
| 123 | + value={newProject.description} |
| 124 | + onChange={handleProjectChange} |
| 125 | + disabled={isLoading} |
| 126 | + placeholder="# Project description" |
| 127 | + className="" |
| 128 | + /> |
| 129 | + </div> |
| 130 | + <div> |
| 131 | + <TagInput |
| 132 | + selectedTags={newProject.tags} |
| 133 | + onTagsChange={(tags) => setNewProject({ ...newProject, tags })} |
| 134 | + /> |
| 135 | + </div> |
| 136 | + <div> |
| 137 | + |
| 138 | + <Input |
| 139 | + id="newProjectRepoLink" |
| 140 | + name="repoLink" |
| 141 | + value={newProject.repoLink} |
| 142 | + onChange={handleProjectChange} |
| 143 | + disabled={isLoading} |
| 144 | + placeholder="Repository link" |
| 145 | + className="" |
| 146 | + /> |
| 147 | + </div> |
| 148 | + <Button onClick={handleAddProject} disabled={isLoading} className="w-full mt-4"> |
| 149 | + {isLoading ? ( |
| 150 | + <> |
| 151 | + <Icons.spinner className="mr-2 h-4 w-4 animate-spin" /> |
| 152 | + Adding... |
| 153 | + </> |
| 154 | + ) : ( |
| 155 | + 'Add Project' |
| 156 | + )} |
| 157 | + </Button> |
| 158 | + </div> |
| 159 | + |
| 160 | + </AlertDialogDescription> |
| 161 | + </AlertDialogContent> |
| 162 | + </AlertDialog> |
| 163 | + ); |
| 164 | +}; |
| 165 | + |
| 166 | +export default AddProject; |
0 commit comments