-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add ability to rename projects (#306)
* Add ability to rename projects * Added user feedback notifiaction and convert PUT to POST * change to router.refresh * add close dialog after rename
- Loading branch information
Showing
3 changed files
with
162 additions
and
0 deletions.
There are no files selected for viewing
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
'use client'; | ||
|
||
import { Edit,Loader2 } from 'lucide-react'; | ||
import { useRouter } from "next/navigation"; | ||
import { useState } from 'react'; | ||
|
||
import { | ||
Dialog, | ||
DialogContent, | ||
DialogFooter, | ||
DialogHeader, | ||
DialogTitle, | ||
DialogTrigger | ||
} from '@/components/ui/dialog'; | ||
import { useProjectContext } from '@/contexts/project-context'; | ||
import { useToast } from '@/lib/hooks/use-toast'; | ||
import { cn } from '@/lib/utils'; | ||
|
||
import { Button } from '../ui/button'; | ||
import { Input } from '../ui/input'; | ||
import { Label } from '../ui/label'; | ||
|
||
interface RenameProjectProps {} | ||
|
||
export default function RenameProject({}: RenameProjectProps) { | ||
const { projectId, projectName } = useProjectContext(); | ||
const router = useRouter(); | ||
|
||
const [newProjectName, setNewProjectName] = useState<string>(''); | ||
const [isRenameDialogOpen, setIsRenameDialogOpen] = useState(false); | ||
const [isLoading, setIsLoading] = useState<boolean>(false); | ||
const { toast } = useToast(); | ||
|
||
const renameProject = async () => { | ||
setIsLoading(true); | ||
|
||
const res = await fetch(`/api/projects/${projectId}`, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ | ||
name: newProjectName, | ||
}), | ||
}); | ||
|
||
if (res.ok) { | ||
toast({ | ||
title: 'Project Renamed', | ||
description: `Project renamed successfully!.`, | ||
}); | ||
router.refresh(); | ||
setIsRenameDialogOpen(false); | ||
} else { | ||
toast({ | ||
title: 'Error', | ||
description: 'Something went wrong. Please try again later.', | ||
}); | ||
} | ||
|
||
setIsLoading(false); | ||
}; | ||
|
||
return ( | ||
<div> | ||
<div className="flex flex-col items-start space-y-4"> | ||
<h1 className="text-lg">Rename project</h1> | ||
<Label className="text-sm text-secondary-foreground"> | ||
Update the name of your project. Changes will take effect immediately. | ||
</Label> | ||
<Dialog | ||
open={isRenameDialogOpen} | ||
onOpenChange={() => { | ||
setIsRenameDialogOpen(!isRenameDialogOpen); | ||
setNewProjectName(''); | ||
}} | ||
> | ||
<DialogTrigger asChild> | ||
<Button | ||
onClick={() => { | ||
setIsRenameDialogOpen(true); | ||
}} | ||
variant="outline" | ||
className="h-8 max-w-80" | ||
> | ||
<Edit className="w-4 mr-1" /> | ||
Rename project | ||
</Button> | ||
</DialogTrigger> | ||
<DialogContent className="sm:max-w-[425px]"> | ||
<DialogHeader> | ||
<DialogTitle>Rename project</DialogTitle> | ||
</DialogHeader> | ||
<div className="grid gap-4 py-4"> | ||
<Label>Enter new project name</Label> | ||
<Input | ||
autoFocus | ||
placeholder={projectName} | ||
value={newProjectName} | ||
onChange={(e) => setNewProjectName(e.target.value)} | ||
/> | ||
</div> | ||
<DialogFooter> | ||
<Button | ||
disabled={!newProjectName.trim() || isLoading} | ||
onClick={renameProject} | ||
handleEnter={true} | ||
> | ||
<Loader2 | ||
className={cn( | ||
'mr-2 hidden', | ||
isLoading ? 'animate-spin block' : '' | ||
)} | ||
size={16} | ||
/> | ||
Rename | ||
</Button> | ||
</DialogFooter> | ||
</DialogContent> | ||
</Dialog> | ||
</div> | ||
</div> | ||
); | ||
} |
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