2023-07-12 14:25:08 +00:00
|
|
|
import React, { Dispatch, SetStateAction, useEffect, useState } from "react";
|
2023-03-11 11:53:23 +00:00
|
|
|
|
|
|
|
import { mutate } from "swr";
|
|
|
|
|
|
|
|
// react-hook-form
|
|
|
|
import { Controller, useForm } from "react-hook-form";
|
|
|
|
// services
|
|
|
|
import workspaceService from "services/workspace.service";
|
|
|
|
// hooks
|
|
|
|
import useToast from "hooks/use-toast";
|
|
|
|
// ui
|
2023-07-18 09:50:05 +00:00
|
|
|
import { CustomSelect, Input, PrimaryButton } from "components/ui";
|
2023-03-11 11:53:23 +00:00
|
|
|
// types
|
2023-06-06 16:06:00 +00:00
|
|
|
import { ICurrentUserResponse, IWorkspace } from "types";
|
2023-03-11 11:53:23 +00:00
|
|
|
// fetch-keys
|
|
|
|
import { USER_WORKSPACES } from "constants/fetch-keys";
|
|
|
|
// constants
|
2023-07-12 14:25:08 +00:00
|
|
|
import { ORGANIZATION_SIZE } from "constants/workspace";
|
2023-03-11 11:53:23 +00:00
|
|
|
|
|
|
|
type Props = {
|
2023-07-12 14:25:08 +00:00
|
|
|
onSubmit?: (res: IWorkspace) => Promise<void>;
|
2023-03-31 10:33:35 +00:00
|
|
|
defaultValues: {
|
|
|
|
name: string;
|
|
|
|
slug: string;
|
2023-07-12 14:25:08 +00:00
|
|
|
organization_size: string;
|
2023-03-31 10:33:35 +00:00
|
|
|
};
|
|
|
|
setDefaultValues: Dispatch<SetStateAction<any>>;
|
2023-06-06 16:06:00 +00:00
|
|
|
user: ICurrentUserResponse | undefined;
|
2023-07-12 14:25:08 +00:00
|
|
|
secondaryButton?: React.ReactNode;
|
2023-07-18 09:50:05 +00:00
|
|
|
primaryButtonText?: {
|
|
|
|
loading: string;
|
|
|
|
default: string;
|
|
|
|
};
|
2023-03-11 11:53:23 +00:00
|
|
|
};
|
|
|
|
|
2023-04-03 18:43:21 +00:00
|
|
|
const restrictedUrls = [
|
2023-04-21 19:27:16 +00:00
|
|
|
"api",
|
2023-06-16 13:30:04 +00:00
|
|
|
"installations",
|
|
|
|
"404",
|
2023-04-03 18:43:21 +00:00
|
|
|
"create-workspace",
|
|
|
|
"error",
|
|
|
|
"invitations",
|
|
|
|
"magic-sign-in",
|
|
|
|
"onboarding",
|
2023-07-28 08:09:42 +00:00
|
|
|
"profile",
|
2023-06-06 16:06:13 +00:00
|
|
|
"reset-password",
|
2023-06-16 13:30:04 +00:00
|
|
|
"sign-up",
|
2023-04-21 19:27:16 +00:00
|
|
|
"workspace-member-invitation",
|
2023-04-03 18:43:21 +00:00
|
|
|
];
|
|
|
|
|
2023-03-31 10:33:35 +00:00
|
|
|
export const CreateWorkspaceForm: React.FC<Props> = ({
|
|
|
|
onSubmit,
|
|
|
|
defaultValues,
|
|
|
|
setDefaultValues,
|
2023-06-06 16:06:00 +00:00
|
|
|
user,
|
2023-07-12 14:25:08 +00:00
|
|
|
secondaryButton,
|
2023-07-18 09:50:05 +00:00
|
|
|
primaryButtonText = {
|
|
|
|
loading: "Creating...",
|
|
|
|
default: "Create Workspace",
|
|
|
|
},
|
2023-03-31 10:33:35 +00:00
|
|
|
}) => {
|
2023-03-11 11:53:23 +00:00
|
|
|
const [slugError, setSlugError] = useState(false);
|
2023-04-13 10:16:25 +00:00
|
|
|
const [invalidSlug, setInvalidSlug] = useState(false);
|
2023-03-11 11:53:23 +00:00
|
|
|
|
|
|
|
const { setToastAlert } = useToast();
|
|
|
|
|
|
|
|
const {
|
|
|
|
register,
|
|
|
|
handleSubmit,
|
|
|
|
control,
|
|
|
|
setValue,
|
2023-03-31 10:33:35 +00:00
|
|
|
getValues,
|
2023-07-18 09:50:05 +00:00
|
|
|
formState: { errors, isSubmitting, isValid },
|
2023-07-18 06:38:22 +00:00
|
|
|
} = useForm<IWorkspace>({ defaultValues, mode: "onChange" });
|
2023-03-11 11:53:23 +00:00
|
|
|
|
|
|
|
const handleCreateWorkspace = async (formData: IWorkspace) => {
|
|
|
|
await workspaceService
|
|
|
|
.workspaceSlugCheck(formData.slug)
|
|
|
|
.then(async (res) => {
|
2023-04-03 18:43:21 +00:00
|
|
|
if (res.status === true && !restrictedUrls.includes(formData.slug)) {
|
2023-03-11 11:53:23 +00:00
|
|
|
setSlugError(false);
|
2023-07-12 14:25:08 +00:00
|
|
|
|
2023-03-11 11:53:23 +00:00
|
|
|
await workspaceService
|
2023-06-06 16:06:00 +00:00
|
|
|
.createWorkspace(formData, user)
|
2023-07-12 14:25:08 +00:00
|
|
|
.then(async (res) => {
|
2023-03-11 11:53:23 +00:00
|
|
|
setToastAlert({
|
|
|
|
type: "success",
|
|
|
|
title: "Success!",
|
|
|
|
message: "Workspace created successfully.",
|
|
|
|
});
|
2023-07-12 14:25:08 +00:00
|
|
|
|
|
|
|
mutate<IWorkspace[]>(
|
|
|
|
USER_WORKSPACES,
|
|
|
|
(prevData) => [res, ...(prevData ?? [])],
|
|
|
|
false
|
|
|
|
);
|
|
|
|
if (onSubmit) await onSubmit(res);
|
2023-03-11 11:53:23 +00:00
|
|
|
})
|
2023-07-12 14:25:08 +00:00
|
|
|
.catch(() =>
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Workspace could not be created. Please try again.",
|
|
|
|
})
|
|
|
|
);
|
2023-03-11 11:53:23 +00:00
|
|
|
} else setSlugError(true);
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
setToastAlert({
|
|
|
|
type: "error",
|
|
|
|
title: "Error!",
|
|
|
|
message: "Some error occurred while creating workspace. Please try again.",
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-03-31 10:33:35 +00:00
|
|
|
useEffect(
|
|
|
|
() => () => {
|
|
|
|
// when the component unmounts set the default values to whatever user typed in
|
|
|
|
setDefaultValues(getValues());
|
|
|
|
},
|
|
|
|
[getValues, setDefaultValues]
|
|
|
|
);
|
2023-03-11 11:53:23 +00:00
|
|
|
|
|
|
|
return (
|
2023-07-12 14:25:08 +00:00
|
|
|
<form className="space-y-6 sm:space-y-9" onSubmit={handleSubmit(handleCreateWorkspace)}>
|
|
|
|
<div className="space-y-6 sm:space-y-7">
|
|
|
|
<div className="space-y-1 text-sm">
|
|
|
|
<label htmlFor="workspaceName">Workspace Name</label>
|
|
|
|
<Input
|
|
|
|
id="workspaceName"
|
|
|
|
name="name"
|
|
|
|
register={register}
|
|
|
|
autoComplete="off"
|
|
|
|
onChange={(e) =>
|
|
|
|
setValue("slug", e.target.value.toLocaleLowerCase().trim().replace(/ /g, "-"))
|
|
|
|
}
|
|
|
|
validations={{
|
|
|
|
required: "Workspace name is required",
|
|
|
|
validate: (value) =>
|
|
|
|
/^[\w\s-]*$/.test(value) ||
|
|
|
|
`Name can only contain (" "), ( - ), ( _ ) & alphanumeric characters.`,
|
|
|
|
}}
|
|
|
|
placeholder="Enter workspace name..."
|
|
|
|
error={errors.name}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
<div className="space-y-1 text-sm">
|
|
|
|
<label htmlFor="workspaceUrl">Workspace URL</label>
|
2023-07-17 10:58:23 +00:00
|
|
|
<div className="flex w-full items-center rounded-md border border-custom-border-200 px-3">
|
2023-07-12 14:25:08 +00:00
|
|
|
<span className="whitespace-nowrap text-sm text-custom-text-200">
|
|
|
|
{window && window.location.host}/
|
|
|
|
</span>
|
2023-05-30 13:44:35 +00:00
|
|
|
<Input
|
2023-07-12 14:25:08 +00:00
|
|
|
id="workspaceUrl"
|
|
|
|
mode="trueTransparent"
|
2023-05-30 13:44:35 +00:00
|
|
|
autoComplete="off"
|
2023-07-12 14:25:08 +00:00
|
|
|
name="slug"
|
|
|
|
register={register}
|
|
|
|
className="block w-full rounded-md bg-transparent py-2 !px-0 text-sm"
|
2023-05-30 13:44:35 +00:00
|
|
|
validations={{
|
2023-07-12 14:25:08 +00:00
|
|
|
required: "Workspace URL is required",
|
2023-05-30 13:44:35 +00:00
|
|
|
}}
|
2023-07-12 14:25:08 +00:00
|
|
|
onChange={(e) =>
|
|
|
|
/^[a-zA-Z0-9_-]+$/.test(e.target.value)
|
|
|
|
? setInvalidSlug(false)
|
|
|
|
: setInvalidSlug(true)
|
|
|
|
}
|
2023-05-30 13:44:35 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2023-07-12 14:25:08 +00:00
|
|
|
{slugError && (
|
|
|
|
<span className="-mt-3 text-sm text-red-500">Workspace URL is already taken!</span>
|
|
|
|
)}
|
|
|
|
{invalidSlug && (
|
|
|
|
<span className="text-sm text-red-500">{`URL can only contain ( - ), ( _ ) & alphanumeric characters.`}</span>
|
|
|
|
)}
|
2023-05-30 13:44:35 +00:00
|
|
|
</div>
|
2023-07-12 14:25:08 +00:00
|
|
|
<div className="space-y-1 text-sm">
|
|
|
|
<span>What size is your organization?</span>
|
2023-05-30 13:44:35 +00:00
|
|
|
<div className="w-full">
|
|
|
|
<Controller
|
2023-07-12 14:25:08 +00:00
|
|
|
name="organization_size"
|
2023-05-30 13:44:35 +00:00
|
|
|
control={control}
|
|
|
|
rules={{ required: "This field is required" }}
|
|
|
|
render={({ field: { value, onChange } }) => (
|
|
|
|
<CustomSelect
|
|
|
|
value={value}
|
|
|
|
onChange={onChange}
|
|
|
|
label={
|
2023-07-12 14:25:08 +00:00
|
|
|
ORGANIZATION_SIZE.find((c) => c === value) ?? (
|
|
|
|
<span className="text-custom-text-200">Select organization size</span>
|
2023-05-30 13:44:35 +00:00
|
|
|
)
|
|
|
|
}
|
|
|
|
input
|
|
|
|
width="w-full"
|
|
|
|
>
|
2023-07-12 14:25:08 +00:00
|
|
|
{ORGANIZATION_SIZE.map((item) => (
|
|
|
|
<CustomSelect.Option key={item} value={item}>
|
|
|
|
{item}
|
2023-05-30 13:44:35 +00:00
|
|
|
</CustomSelect.Option>
|
|
|
|
))}
|
|
|
|
</CustomSelect>
|
2023-03-30 08:25:50 +00:00
|
|
|
)}
|
2023-05-30 13:44:35 +00:00
|
|
|
/>
|
2023-07-12 14:25:08 +00:00
|
|
|
{errors.organization_size && (
|
|
|
|
<span className="text-sm text-red-500">{errors.organization_size.message}</span>
|
2023-05-30 13:44:35 +00:00
|
|
|
)}
|
2023-03-11 11:53:23 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2023-05-30 13:44:35 +00:00
|
|
|
|
2023-07-12 14:25:08 +00:00
|
|
|
<div className="flex items-center gap-4">
|
|
|
|
{secondaryButton}
|
2023-07-18 09:50:05 +00:00
|
|
|
<PrimaryButton type="submit" size="md" disabled={!isValid} loading={isSubmitting}>
|
|
|
|
{isSubmitting ? primaryButtonText.loading : primaryButtonText.default}
|
2023-05-30 13:44:35 +00:00
|
|
|
</PrimaryButton>
|
|
|
|
</div>
|
2023-03-11 11:53:23 +00:00
|
|
|
</form>
|
|
|
|
);
|
|
|
|
};
|