feat: update workspace name
This commit is contained in:
@@ -4,13 +4,14 @@ import ContentEditable from "react-contenteditable";
|
|||||||
interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {
|
interface InputProps extends React.InputHTMLAttributes<HTMLInputElement> {
|
||||||
contentEditable?: boolean;
|
contentEditable?: boolean;
|
||||||
value?: string;
|
value?: string;
|
||||||
|
errorMessage?: string;
|
||||||
onChange?: (
|
onChange?: (
|
||||||
e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
|
e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>,
|
||||||
) => void;
|
) => void;
|
||||||
}
|
}
|
||||||
|
|
||||||
const Input = forwardRef<HTMLInputElement, InputProps>(
|
const Input = forwardRef<HTMLInputElement, InputProps>(
|
||||||
({ contentEditable, value, onChange, ...props }, ref) => {
|
({ contentEditable, errorMessage, value, onChange, ...props }, ref) => {
|
||||||
if (contentEditable) {
|
if (contentEditable) {
|
||||||
return (
|
return (
|
||||||
<ContentEditable
|
<ContentEditable
|
||||||
@@ -23,11 +24,17 @@ const Input = forwardRef<HTMLInputElement, InputProps>(
|
|||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<input
|
<div className="flex w-full flex-col gap-1">
|
||||||
ref={ref}
|
<input
|
||||||
className="block w-full rounded-md border-0 bg-dark-300 bg-white/5 py-1.5 shadow-sm ring-1 ring-inset ring-light-600 placeholder:text-dark-800 focus:ring-2 focus:ring-inset focus:ring-light-700 dark:text-dark-1000 dark:ring-dark-700 dark:focus:ring-dark-700 sm:text-sm sm:leading-6"
|
ref={ref}
|
||||||
{...props}
|
onChange={onChange}
|
||||||
/>
|
className="block w-full rounded-md border-0 bg-dark-300 bg-white/5 py-1.5 shadow-sm ring-1 ring-inset ring-light-600 placeholder:text-dark-800 focus:ring-2 focus:ring-inset focus:ring-light-700 dark:text-dark-1000 dark:ring-dark-700 dark:focus:ring-dark-700 sm:text-sm sm:leading-6"
|
||||||
|
{...props}
|
||||||
|
/>
|
||||||
|
{errorMessage && (
|
||||||
|
<div className="text-xs text-red-500">{errorMessage}</div>
|
||||||
|
)}
|
||||||
|
</div>
|
||||||
);
|
);
|
||||||
},
|
},
|
||||||
);
|
);
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
import { WorkspaceProvider } from "~/providers/workspace";
|
import { WorkspaceProvider } from "~/providers/workspace";
|
||||||
import Dashboard from "~/components/dashboard";
|
import Dashboard from "~/components/dashboard";
|
||||||
import SettingsView from "~/views/settings";
|
import SettingsView from "~/views/settings";
|
||||||
|
import Popup from "~/components/Popup";
|
||||||
|
|
||||||
export default function SettingsPage() {
|
export default function SettingsPage() {
|
||||||
return (
|
return (
|
||||||
@@ -8,6 +9,7 @@ export default function SettingsPage() {
|
|||||||
<Dashboard>
|
<Dashboard>
|
||||||
<SettingsView />
|
<SettingsView />
|
||||||
</Dashboard>
|
</Dashboard>
|
||||||
|
<Popup />
|
||||||
</WorkspaceProvider>
|
</WorkspaceProvider>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -55,6 +55,22 @@ export const workspaceRouter = createTRPCRouter({
|
|||||||
code: "INTERNAL_SERVER_ERROR",
|
code: "INTERNAL_SERVER_ERROR",
|
||||||
});
|
});
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}),
|
||||||
|
update: protectedProcedure
|
||||||
|
.input(
|
||||||
|
z.object({
|
||||||
|
workspacePublicId: z.string().min(12),
|
||||||
|
name: z.string().min(3).max(24),
|
||||||
|
}),
|
||||||
|
)
|
||||||
|
.mutation(async ({ ctx, input }) => {
|
||||||
|
const result = await workspaceRepo.update(
|
||||||
|
ctx.db,
|
||||||
|
input.workspacePublicId,
|
||||||
|
input.name,
|
||||||
|
);
|
||||||
|
|
||||||
return result;
|
return result;
|
||||||
}),
|
}),
|
||||||
delete: protectedProcedure
|
delete: protectedProcedure
|
||||||
|
|||||||
@@ -38,6 +38,20 @@ export const create = async (
|
|||||||
return newWorkspace;
|
return newWorkspace;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
export const update = async (
|
||||||
|
db: SupabaseClient<Database>,
|
||||||
|
workspacePublicId: string,
|
||||||
|
name: string,
|
||||||
|
) => {
|
||||||
|
const { data, error } = await db
|
||||||
|
.from("workspace")
|
||||||
|
.update({ name })
|
||||||
|
.eq("publicId", workspacePublicId)
|
||||||
|
.is("deletedAt", null);
|
||||||
|
|
||||||
|
return data;
|
||||||
|
};
|
||||||
|
|
||||||
export const getByPublicId = async (
|
export const getByPublicId = async (
|
||||||
db: SupabaseClient<Database>,
|
db: SupabaseClient<Database>,
|
||||||
workspacePublicId: string,
|
workspacePublicId: string,
|
||||||
|
|||||||
79
src/views/settings/components/UpdateWorkspaceNameForm.tsx
Normal file
79
src/views/settings/components/UpdateWorkspaceNameForm.tsx
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
import { useForm } from "react-hook-form";
|
||||||
|
import { zodResolver } from "@hookform/resolvers/zod";
|
||||||
|
import { z } from "zod";
|
||||||
|
import Input from "~/components/Input";
|
||||||
|
import Button from "~/components/Button";
|
||||||
|
import { usePopup } from "~/providers/popup";
|
||||||
|
import { api } from "~/utils/api";
|
||||||
|
|
||||||
|
const schema = z.object({
|
||||||
|
name: z
|
||||||
|
.string()
|
||||||
|
.min(3, { message: "Workspace name must be at least 3 characters long" })
|
||||||
|
.max(24, { message: "Workspace name cannot exceed 24 characters" }),
|
||||||
|
});
|
||||||
|
|
||||||
|
type FormValues = z.infer<typeof schema>;
|
||||||
|
|
||||||
|
const UpdateWorkspaceNameForm = ({
|
||||||
|
workspacePublicId,
|
||||||
|
workspaceName,
|
||||||
|
}: {
|
||||||
|
workspacePublicId: string;
|
||||||
|
workspaceName: string;
|
||||||
|
}) => {
|
||||||
|
const utils = api.useUtils();
|
||||||
|
const { showPopup } = usePopup();
|
||||||
|
const {
|
||||||
|
register,
|
||||||
|
handleSubmit,
|
||||||
|
formState: { isDirty, errors },
|
||||||
|
} = useForm<FormValues>({
|
||||||
|
resolver: zodResolver(schema),
|
||||||
|
values: {
|
||||||
|
name: workspaceName,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const updateWorkspaceName = api.workspace.update.useMutation({
|
||||||
|
onSuccess: async () => {
|
||||||
|
try {
|
||||||
|
await utils.workspace.all.refetch();
|
||||||
|
} catch (e) {
|
||||||
|
console.error(e);
|
||||||
|
throw e;
|
||||||
|
}
|
||||||
|
},
|
||||||
|
onError: () => {
|
||||||
|
showPopup({
|
||||||
|
header: "Error updating workspace name",
|
||||||
|
message: "Please try again later, or contact customer support.",
|
||||||
|
});
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const onSubmit = (data: FormValues) => {
|
||||||
|
updateWorkspaceName.mutate({
|
||||||
|
workspacePublicId,
|
||||||
|
name: data.name,
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
|
return (
|
||||||
|
<>
|
||||||
|
<div className="mb-4 flex max-w-[350px] items-center gap-2">
|
||||||
|
<Input {...register("name")} errorMessage={errors.name?.message} />
|
||||||
|
</div>
|
||||||
|
<Button
|
||||||
|
onClick={handleSubmit(onSubmit)}
|
||||||
|
variant="primary"
|
||||||
|
disabled={!isDirty || updateWorkspaceName.isPending}
|
||||||
|
isLoading={updateWorkspaceName.isPending}
|
||||||
|
>
|
||||||
|
Update
|
||||||
|
</Button>
|
||||||
|
</>
|
||||||
|
);
|
||||||
|
};
|
||||||
|
|
||||||
|
export default UpdateWorkspaceNameForm;
|
||||||
@@ -1,10 +1,17 @@
|
|||||||
import { useModal } from "~/providers/modal";
|
import { useForm } from "react-hook-form";
|
||||||
|
|
||||||
import Modal from "~/components/modal";
|
import Modal from "~/components/modal";
|
||||||
import Button from "~/components/Button";
|
import Button from "~/components/Button";
|
||||||
|
|
||||||
import { DeleteWorkspaceConfirmation } from "./components/DeleteWorkspaceConfirmation";
|
import { DeleteWorkspaceConfirmation } from "./components/DeleteWorkspaceConfirmation";
|
||||||
|
import UpdateWorkspaceNameForm from "./components/UpdateWorkspaceNameForm";
|
||||||
|
|
||||||
|
import { useModal } from "~/providers/modal";
|
||||||
|
import { useWorkspace } from "~/providers/workspace";
|
||||||
|
|
||||||
export default function SettingsPage() {
|
export default function SettingsPage() {
|
||||||
const { modalContentType, openModal } = useModal();
|
const { modalContentType, openModal } = useModal();
|
||||||
|
const { workspace } = useWorkspace();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className="px-28 py-12">
|
<div className="px-28 py-12">
|
||||||
@@ -14,6 +21,16 @@ export default function SettingsPage() {
|
|||||||
</h1>
|
</h1>
|
||||||
</div>
|
</div>
|
||||||
|
|
||||||
|
<div className="mb-8 border-t border-light-300 dark:border-dark-300">
|
||||||
|
<h2 className="mb-4 mt-8 text-[14px] text-neutral-900 dark:text-dark-1000">
|
||||||
|
Workspace name
|
||||||
|
</h2>
|
||||||
|
<UpdateWorkspaceNameForm
|
||||||
|
workspacePublicId={workspace?.publicId}
|
||||||
|
workspaceName={workspace?.name}
|
||||||
|
/>
|
||||||
|
</div>
|
||||||
|
|
||||||
<div className="border-t border-light-300 dark:border-dark-300">
|
<div className="border-t border-light-300 dark:border-dark-300">
|
||||||
<h2 className="mt-8 text-[14px] text-neutral-900 dark:text-dark-1000">
|
<h2 className="mt-8 text-[14px] text-neutral-900 dark:text-dark-1000">
|
||||||
Delete workspace
|
Delete workspace
|
||||||
|
|||||||
Reference in New Issue
Block a user