Files
formbricks-formbricks/apps/web/components/team/CreateTeamModal.tsx
Anshuman Pandey ad42f4cc55 Dramatically improve load times when creating a new team(#614)
* fix: attempts to reduce time taken to create team

* fix: fixes long time taken in team creation

* fix: refactors prisma logic

* feat: added logic for adding demo data while signing up

* fix: adds comment

* fix: adds another logic for adding demo data

* fix: adds service for adding demo data

* fix: fixes

* fix: adds demo product creation logic in next auth options

* fix: fixes next auth options

* fix: fixes team creation logic

* refactor: clean up

* fix: moves the logic for adding demo product while creating team in bg

* fix: moves individual queries in a transaction

* refactor: service

* fix: moves api route to app-dir

* fix: fixes api calls

* fix: fixes cache

* fix: removes unused code
2023-08-05 13:59:06 +02:00

87 lines
3.0 KiB
TypeScript

import Modal from "@/components/shared/Modal";
import { changeEnvironmentByTeam } from "@/lib/environments/changeEnvironments";
import { useMemberships } from "@/lib/memberships";
import { useProfile } from "@/lib/profile";
import { Button, Input, Label } from "@formbricks/ui";
import { PlusCircleIcon } from "@heroicons/react/24/outline";
import { useRouter } from "next/navigation";
import { useState } from "react";
import { useForm } from "react-hook-form";
import toast from "react-hot-toast";
import { createTeam } from "@/app/(app)/environments/[environmentId]/actions";
interface CreateTeamModalProps {
open: boolean;
setOpen: (v: boolean) => void;
}
export default function CreateTeamModal({ open, setOpen }: CreateTeamModalProps) {
const router = useRouter();
const { profile } = useProfile();
const { mutateMemberships } = useMemberships();
const [loading, setLoading] = useState(false);
const { register, handleSubmit } = useForm();
const submitTeam = async (data) => {
setLoading(true);
const newTeam = await createTeam(data.name, (profile as any).id);
const newMemberships = await mutateMemberships();
changeEnvironmentByTeam(newTeam.id, newMemberships, router);
toast.success("Team created successfully!");
setOpen(false);
setLoading(false);
};
return (
<Modal open={open} setOpen={setOpen} noPadding closeOnOutsideClick={false}>
<div className="flex h-full flex-col rounded-lg">
<div className="rounded-t-lg bg-slate-100">
<div className="flex items-center justify-between p-6">
<div className="flex items-center space-x-2">
<div className="mr-1.5 h-10 w-10 text-slate-500">
<PlusCircleIcon />
</div>
<div>
<div className="text-xl font-medium text-slate-700">Create team</div>
<div className="text-sm text-slate-500">
Create a new team to handle a different set of products.
</div>
</div>
</div>
</div>
</div>
<form onSubmit={handleSubmit(submitTeam)}>
<div className="flex w-full justify-between space-y-4 rounded-lg p-6">
<div className="grid w-full gap-x-2">
<div>
<Label>Team Name</Label>
<Input
autoFocus
placeholder="e.g. Power Puff Girls"
{...register("name", { required: true })}
/>
</div>
</div>
</div>
<div className="flex justify-end border-t border-slate-200 p-6">
<div className="flex space-x-2">
<Button
type="button"
variant="minimal"
onClick={() => {
setOpen(false);
}}>
Cancel
</Button>
<Button variant="darkCTA" type="submit" loading={loading}>
Create team
</Button>
</div>
</div>
</form>
</div>
</Modal>
);
}