2024-01-04 15:54:31 -08:00
|
|
|
import React, { useEffect, useRef, useState } from "react";
|
|
|
|
import illustration from "@/media/illustrations/create-workspace.png";
|
|
|
|
import paths from "@/utils/paths";
|
|
|
|
import showToast from "@/utils/toast";
|
|
|
|
import { useNavigate } from "react-router-dom";
|
|
|
|
import Workspace from "@/models/workspace";
|
2024-06-19 14:48:19 -07:00
|
|
|
import { useTranslation } from "react-i18next";
|
2024-01-04 15:54:31 -08:00
|
|
|
|
|
|
|
export default function CreateWorkspace({
|
|
|
|
setHeader,
|
|
|
|
setForwardBtn,
|
|
|
|
setBackBtn,
|
|
|
|
}) {
|
2025-02-11 15:46:08 -08:00
|
|
|
const { t } = useTranslation();
|
2024-01-04 15:54:31 -08:00
|
|
|
const [workspaceName, setWorkspaceName] = useState("");
|
|
|
|
const navigate = useNavigate();
|
|
|
|
const createWorkspaceRef = useRef();
|
2025-02-11 15:46:08 -08:00
|
|
|
const TITLE = t("onboarding.workspace.title");
|
|
|
|
const DESCRIPTION = t("onboarding.workspace.description");
|
2024-01-04 15:54:31 -08:00
|
|
|
|
|
|
|
useEffect(() => {
|
|
|
|
setHeader({ title: TITLE, description: DESCRIPTION });
|
|
|
|
setBackBtn({ showing: false, disabled: false, onClick: handleBack });
|
|
|
|
}, []);
|
|
|
|
|
|
|
|
useEffect(() => {
|
2024-05-15 01:11:45 +09:30
|
|
|
if (workspaceName.length > 0) {
|
2024-01-04 15:54:31 -08:00
|
|
|
setForwardBtn({ showing: true, disabled: false, onClick: handleForward });
|
|
|
|
} else {
|
|
|
|
setForwardBtn({ showing: true, disabled: true, onClick: handleForward });
|
|
|
|
}
|
|
|
|
}, [workspaceName]);
|
|
|
|
|
|
|
|
const handleCreate = async (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
const form = new FormData(e.target);
|
|
|
|
const { workspace, error } = await Workspace.new({
|
|
|
|
name: form.get("name"),
|
|
|
|
onboardingComplete: true,
|
|
|
|
});
|
|
|
|
if (!!workspace) {
|
|
|
|
showToast(
|
|
|
|
"Workspace created successfully! Taking you to home...",
|
|
|
|
"success"
|
|
|
|
);
|
|
|
|
await new Promise((resolve) => setTimeout(resolve, 1000));
|
|
|
|
navigate(paths.home());
|
|
|
|
} else {
|
|
|
|
showToast(`Failed to create workspace: ${error}`, "error");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
function handleForward() {
|
|
|
|
createWorkspaceRef.current.click();
|
|
|
|
}
|
|
|
|
|
|
|
|
function handleBack() {
|
|
|
|
navigate(paths.onboarding.survey());
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
|
|
|
<form
|
|
|
|
onSubmit={handleCreate}
|
|
|
|
className="w-full flex items-center justify-center flex-col gap-y-2"
|
|
|
|
>
|
|
|
|
<img src={illustration} alt="Create workspace" />
|
|
|
|
<div className="flex flex-col gap-y-4 w-full max-w-[600px]">
|
|
|
|
{" "}
|
|
|
|
<div className="w-full mt-4">
|
|
|
|
<label
|
|
|
|
htmlFor="name"
|
|
|
|
className="block mb-3 text-sm font-medium text-white"
|
|
|
|
>
|
2024-06-19 14:48:19 -07:00
|
|
|
{t("common.workspaces-name")}
|
2024-01-04 15:54:31 -08:00
|
|
|
</label>
|
|
|
|
<input
|
|
|
|
name="name"
|
|
|
|
type="text"
|
2024-11-25 21:07:23 -08:00
|
|
|
className="border-none bg-theme-settings-input-bg text-white focus:outline-primary-button active:outline-primary-button placeholder:text-theme-settings-input-placeholder outline-none text-sm rounded-lg block w-full p-2.5"
|
2024-01-04 15:54:31 -08:00
|
|
|
placeholder="My Workspace"
|
|
|
|
required={true}
|
|
|
|
autoComplete="off"
|
|
|
|
onChange={(e) => setWorkspaceName(e.target.value)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
<button
|
|
|
|
type="submit"
|
|
|
|
ref={createWorkspaceRef}
|
|
|
|
hidden
|
|
|
|
aria-hidden="true"
|
|
|
|
></button>
|
|
|
|
</form>
|
|
|
|
);
|
|
|
|
}
|