web: added the base for the web project
This commit is contained in:
153
apps/web/src/modals/AddClient.tsx
Normal file
153
apps/web/src/modals/AddClient.tsx
Normal file
@@ -0,0 +1,153 @@
|
||||
import { api, handleError } from "@/utils/api";
|
||||
import { ModalContent, ModalHeader } from "./Modal/Container";
|
||||
import { Controller, useForm } from "react-hook-form";
|
||||
import { z } from "zod";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { ButtonContainer } from "@/components/ButtonContainer";
|
||||
import { popModal } from ".";
|
||||
import { toast } from "@/components/ui/use-toast";
|
||||
import { InputWithLabel } from "@/components/forms/InputWithLabel";
|
||||
import { useRefetchActive } from "@/hooks/useRefetchActive";
|
||||
import { Combobox } from "@/components/ui/combobox";
|
||||
import { Label } from "@/components/ui/label";
|
||||
import { Copy } from "lucide-react";
|
||||
import { clipboard } from "@/utils/clipboard";
|
||||
import dynamic from "next/dynamic";
|
||||
import { useOrganizationParams } from "@/hooks/useOrganizationParams";
|
||||
|
||||
const Syntax = dynamic(import('@/components/Syntax'))
|
||||
|
||||
const validator = z.object({
|
||||
name: z.string().min(1, "Required"),
|
||||
projectId: z.string().min(1, "Required"),
|
||||
});
|
||||
|
||||
type IForm = z.infer<typeof validator>;
|
||||
|
||||
export default function CreateProject() {
|
||||
const params = useOrganizationParams()
|
||||
const refetch = useRefetchActive();
|
||||
const query = api.project.list.useQuery({
|
||||
organizationSlug: params.organization,
|
||||
});
|
||||
const mutation = api.client.create.useMutation({
|
||||
onError: handleError,
|
||||
onSuccess() {
|
||||
toast({
|
||||
title: "Success",
|
||||
description: "Client created!",
|
||||
});
|
||||
refetch();
|
||||
},
|
||||
});
|
||||
const { register, handleSubmit, formState, control } = useForm<IForm>({
|
||||
resolver: zodResolver(validator),
|
||||
defaultValues: {
|
||||
name: "",
|
||||
projectId: "",
|
||||
},
|
||||
});
|
||||
|
||||
if (mutation.isSuccess && mutation.data) {
|
||||
const { clientId, clientSecret } = mutation.data;
|
||||
return (
|
||||
<ModalContent>
|
||||
<ModalHeader title="Client created 🚀" />
|
||||
<p>
|
||||
Your client has been created! You will only see the client secret once
|
||||
so keep it safe 🫣
|
||||
</p>
|
||||
|
||||
<button className="mt-4 text-left" onClick={() => clipboard(clientId)}>
|
||||
<Label>Client ID</Label>
|
||||
<div className="flex items-center justify-between rounded bg-gray-100 p-2 px-3">
|
||||
{clientId}
|
||||
<Copy size={16} />
|
||||
</div>
|
||||
</button>
|
||||
<button
|
||||
className="mt-4 text-left"
|
||||
onClick={() => clipboard(clientSecret)}
|
||||
>
|
||||
<Label>Client Secret</Label>
|
||||
<div className="flex items-center justify-between rounded bg-gray-100 p-2 px-3">
|
||||
{clientSecret}
|
||||
<Copy size={16} />
|
||||
</div>
|
||||
</button>
|
||||
<div className="mt-4">
|
||||
<Label>Code snippet</Label>
|
||||
<div className="overflow-x-auto [&_pre]:!rounded [&_pre]:!bg-gray-100 [&_pre]:text-sm">
|
||||
<Syntax
|
||||
code={`const mixan = new Mixan({
|
||||
clientId: "${clientId}",
|
||||
clientSecret: "${clientSecret}",
|
||||
})`}
|
||||
/>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<ButtonContainer>
|
||||
<div />
|
||||
<Button onClick={() => popModal()}>Done</Button>
|
||||
</ButtonContainer>
|
||||
</ModalContent>
|
||||
);
|
||||
}
|
||||
|
||||
return (
|
||||
<ModalContent>
|
||||
<ModalHeader title="Create client" />
|
||||
<form
|
||||
onSubmit={handleSubmit((values) => {
|
||||
mutation.mutate({
|
||||
...values,
|
||||
organizationSlug: params.organization,
|
||||
});
|
||||
})}
|
||||
>
|
||||
<InputWithLabel
|
||||
label="Name"
|
||||
placeholder="Name"
|
||||
{...register("name")}
|
||||
className="mb-4"
|
||||
/>
|
||||
<Controller
|
||||
control={control}
|
||||
name="projectId"
|
||||
render={({ field }) => {
|
||||
return (
|
||||
<div>
|
||||
<Label className="mb-2 block">Project</Label>
|
||||
<Combobox
|
||||
{...field}
|
||||
onChange={(value) => {
|
||||
console.log("wtf?", value);
|
||||
|
||||
field.onChange(value);
|
||||
}}
|
||||
items={
|
||||
query.data?.map((item) => ({
|
||||
value: item.id,
|
||||
label: item.name,
|
||||
})) ?? []
|
||||
}
|
||||
placeholder="Select a project"
|
||||
/>
|
||||
</div>
|
||||
);
|
||||
}}
|
||||
/>
|
||||
<ButtonContainer>
|
||||
<Button type="button" variant="outline" onClick={() => popModal()}>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button type="submit" disabled={!formState.isDirty}>
|
||||
Create
|
||||
</Button>
|
||||
</ButtonContainer>
|
||||
</form>
|
||||
</ModalContent>
|
||||
);
|
||||
}
|
||||
55
apps/web/src/modals/AddProject.tsx
Normal file
55
apps/web/src/modals/AddProject.tsx
Normal file
@@ -0,0 +1,55 @@
|
||||
import { api, handleError } from "@/utils/api";
|
||||
import { ModalContent, ModalHeader } from "./Modal/Container";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { z } from "zod";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { ButtonContainer } from "@/components/ButtonContainer";
|
||||
import { popModal } from ".";
|
||||
import { toast } from "@/components/ui/use-toast";
|
||||
import { InputWithLabel } from "@/components/forms/InputWithLabel";
|
||||
import { useRefetchActive } from "@/hooks/useRefetchActive";
|
||||
|
||||
const validator = z.object({
|
||||
name: z.string().min(1),
|
||||
});
|
||||
|
||||
type IForm = z.infer<typeof validator>;
|
||||
|
||||
export default function AddProject() {
|
||||
const refetch = useRefetchActive()
|
||||
const mutation = api.project.create.useMutation({
|
||||
onError: handleError,
|
||||
onSuccess() {
|
||||
toast({
|
||||
title: 'Success',
|
||||
description: 'Project created! Lets create a client for it 🤘',
|
||||
})
|
||||
refetch()
|
||||
popModal()
|
||||
}
|
||||
});
|
||||
const { register, handleSubmit, formState } = useForm<IForm>({
|
||||
resolver: zodResolver(validator),
|
||||
defaultValues: {
|
||||
name: "",
|
||||
},
|
||||
});
|
||||
|
||||
return (
|
||||
<ModalContent>
|
||||
<ModalHeader title="Create project" />
|
||||
<form
|
||||
onSubmit={handleSubmit((values) => {
|
||||
mutation.mutate(values);
|
||||
})}
|
||||
>
|
||||
<InputWithLabel label="Name" placeholder="Name" {...register('name')} />
|
||||
<ButtonContainer>
|
||||
<Button type="button" variant="outline" onClick={() => popModal()}>Cancel</Button>
|
||||
<Button type="submit" disabled={!formState.isDirty}>Create</Button>
|
||||
</ButtonContainer>
|
||||
</form>
|
||||
</ModalContent>
|
||||
);
|
||||
}
|
||||
44
apps/web/src/modals/Confirm.tsx
Normal file
44
apps/web/src/modals/Confirm.tsx
Normal file
@@ -0,0 +1,44 @@
|
||||
import { ModalContent, ModalHeader } from "./Modal/Container";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { ButtonContainer } from "@/components/ButtonContainer";
|
||||
import { popModal } from ".";
|
||||
|
||||
export type ConfirmProps = {
|
||||
title: string;
|
||||
text: string;
|
||||
onConfirm: () => void;
|
||||
onCancel?: () => void;
|
||||
};
|
||||
|
||||
export default function Confirm({
|
||||
title,
|
||||
text,
|
||||
onConfirm,
|
||||
onCancel,
|
||||
}: ConfirmProps) {
|
||||
return (
|
||||
<ModalContent>
|
||||
<ModalHeader title={title} />
|
||||
<p>{text}</p>
|
||||
<ButtonContainer>
|
||||
<Button
|
||||
variant="outline"
|
||||
onClick={() => {
|
||||
popModal("Confirm");
|
||||
onCancel?.();
|
||||
}}
|
||||
>
|
||||
Cancel
|
||||
</Button>
|
||||
<Button
|
||||
onClick={() => {
|
||||
popModal("Confirm");
|
||||
onConfirm();
|
||||
}}
|
||||
>
|
||||
Yes
|
||||
</Button>
|
||||
</ButtonContainer>
|
||||
</ModalContent>
|
||||
);
|
||||
}
|
||||
70
apps/web/src/modals/EditClient.tsx
Normal file
70
apps/web/src/modals/EditClient.tsx
Normal file
@@ -0,0 +1,70 @@
|
||||
import { api, handleError } from "@/utils/api";
|
||||
import { ModalContent, ModalHeader } from "./Modal/Container";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { z } from "zod";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { useEffect } from "react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { ButtonContainer } from "@/components/ButtonContainer";
|
||||
import { popModal } from ".";
|
||||
import { toast } from "@/components/ui/use-toast";
|
||||
import { InputWithLabel } from "@/components/forms/InputWithLabel";
|
||||
import { useRefetchActive } from "@/hooks/useRefetchActive";
|
||||
|
||||
type EditClientProps = {
|
||||
id: string;
|
||||
};
|
||||
|
||||
const validator = z.object({
|
||||
id: z.string().min(1),
|
||||
name: z.string().min(1),
|
||||
});
|
||||
|
||||
type IForm = z.infer<typeof validator>;
|
||||
|
||||
export default function EditClient({ id }: EditClientProps) {
|
||||
const refetch = useRefetchActive()
|
||||
const mutation = api.client.update.useMutation({
|
||||
onError: handleError,
|
||||
onSuccess() {
|
||||
toast({
|
||||
title: 'Success',
|
||||
description: 'Client updated.',
|
||||
})
|
||||
popModal()
|
||||
refetch()
|
||||
}
|
||||
});
|
||||
const query = api.client.get.useQuery({ id });
|
||||
const data = query.data;
|
||||
const { register, handleSubmit, reset, formState } = useForm<IForm>({
|
||||
resolver: zodResolver(validator),
|
||||
defaultValues: {
|
||||
id: "",
|
||||
name: "",
|
||||
},
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (data) {
|
||||
reset(data);
|
||||
}
|
||||
}, [data, reset]);
|
||||
|
||||
return (
|
||||
<ModalContent>
|
||||
<ModalHeader title="Edit client" />
|
||||
<form
|
||||
onSubmit={handleSubmit((values) => {
|
||||
mutation.mutate(values);
|
||||
})}
|
||||
>
|
||||
<InputWithLabel label="Name" placeholder="Name" {...register('name')} />
|
||||
<ButtonContainer>
|
||||
<Button type="button" variant="outline" onClick={() => popModal()}>Cancel</Button>
|
||||
<Button type="submit" disabled={!formState.isDirty}>Save</Button>
|
||||
</ButtonContainer>
|
||||
</form>
|
||||
</ModalContent>
|
||||
);
|
||||
}
|
||||
70
apps/web/src/modals/EditProject.tsx
Normal file
70
apps/web/src/modals/EditProject.tsx
Normal file
@@ -0,0 +1,70 @@
|
||||
import { api, handleError } from "@/utils/api";
|
||||
import { ModalContent, ModalHeader } from "./Modal/Container";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { z } from "zod";
|
||||
import { zodResolver } from "@hookform/resolvers/zod";
|
||||
import { useEffect } from "react";
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { ButtonContainer } from "@/components/ButtonContainer";
|
||||
import { popModal } from ".";
|
||||
import { toast } from "@/components/ui/use-toast";
|
||||
import { InputWithLabel } from "@/components/forms/InputWithLabel";
|
||||
import { useRefetchActive } from "@/hooks/useRefetchActive";
|
||||
|
||||
type EditProjectProps = {
|
||||
id: string;
|
||||
};
|
||||
|
||||
const validator = z.object({
|
||||
id: z.string().min(1),
|
||||
name: z.string().min(1),
|
||||
});
|
||||
|
||||
type IForm = z.infer<typeof validator>;
|
||||
|
||||
export default function EditProject({ id }: EditProjectProps) {
|
||||
const refetch = useRefetchActive()
|
||||
const mutation = api.project.update.useMutation({
|
||||
onError: handleError,
|
||||
onSuccess() {
|
||||
toast({
|
||||
title: 'Success',
|
||||
description: 'Project updated.',
|
||||
})
|
||||
popModal()
|
||||
refetch()
|
||||
}
|
||||
});
|
||||
const query = api.project.get.useQuery({ id });
|
||||
const data = query.data;
|
||||
const { register, handleSubmit, reset,formState } = useForm<IForm>({
|
||||
resolver: zodResolver(validator),
|
||||
defaultValues: {
|
||||
id: "",
|
||||
name: "",
|
||||
},
|
||||
});
|
||||
|
||||
useEffect(() => {
|
||||
if (data) {
|
||||
reset(data);
|
||||
}
|
||||
}, [data, reset]);
|
||||
|
||||
return (
|
||||
<ModalContent>
|
||||
<ModalHeader title="Edit project" />
|
||||
<form
|
||||
onSubmit={handleSubmit((values) => {
|
||||
mutation.mutate(values);
|
||||
})}
|
||||
>
|
||||
<InputWithLabel label="Name" placeholder="Name" {...register('name')} />
|
||||
<ButtonContainer>
|
||||
<Button type="button" variant="outline" onClick={() => popModal()}>Cancel</Button>
|
||||
<Button type="submit" disabled={!formState.isDirty}>Save</Button>
|
||||
</ButtonContainer>
|
||||
</form>
|
||||
</ModalContent>
|
||||
);
|
||||
}
|
||||
31
apps/web/src/modals/Modal/Container.tsx
Normal file
31
apps/web/src/modals/Modal/Container.tsx
Normal file
@@ -0,0 +1,31 @@
|
||||
import { Button } from "@/components/ui/button";
|
||||
import { X } from "lucide-react";
|
||||
import { popModal } from "..";
|
||||
|
||||
type ModalContentProps = {
|
||||
children: React.ReactNode;
|
||||
};
|
||||
|
||||
export function ModalContent({ children }: ModalContentProps) {
|
||||
return (
|
||||
<div className="fixed left-[50%] top-[50%] z-50 grid w-full max-w-lg translate-x-[-50%] translate-y-[-50%] border bg-background p-6 shadow-lg duration-200 sm:rounded-lg md:w-full">
|
||||
{children}
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
type ModalHeaderProps = {
|
||||
title: string | React.ReactNode;
|
||||
};
|
||||
|
||||
export function ModalHeader({ title }: ModalHeaderProps) {
|
||||
return (
|
||||
<div className="flex items-center justify-between">
|
||||
<div className="font-medium">{title}</div>
|
||||
<Button variant="ghost" size="sm" onClick={() => popModal()}>
|
||||
<X className="h-4 w-4" />
|
||||
<span className="sr-only">Close</span>
|
||||
</Button>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
197
apps/web/src/modals/index.tsx
Normal file
197
apps/web/src/modals/index.tsx
Normal file
@@ -0,0 +1,197 @@
|
||||
import { Suspense, useEffect, useRef, useState } from 'react';
|
||||
|
||||
import { Loader } from 'lucide-react';
|
||||
import mitt from 'mitt';
|
||||
import dynamic from 'next/dynamic';
|
||||
import { useOnClickOutside } from 'usehooks-ts';
|
||||
import { type ConfirmProps } from './Confirm';
|
||||
|
||||
const Loading = () => (
|
||||
<div className='fixed top-0 z-50 flex h-screen w-screen items-center justify-center overflow-auto bg-backdrop'>
|
||||
<Loader className='mb-8 animate-spin' size={40} />
|
||||
</div>
|
||||
);
|
||||
|
||||
const modals = {
|
||||
EditProject: dynamic(() => import('./EditProject'), {
|
||||
loading: Loading,
|
||||
}),
|
||||
EditClient: dynamic(() => import('./EditClient'), {
|
||||
loading: Loading,
|
||||
}),
|
||||
AddProject: dynamic(() => import('./AddProject'), {
|
||||
loading: Loading,
|
||||
}),
|
||||
AddClient: dynamic(() => import('./AddClient'), {
|
||||
loading: Loading,
|
||||
}),
|
||||
Confirm: dynamic(() => import('./Confirm'), {
|
||||
loading: Loading,
|
||||
}),
|
||||
};
|
||||
|
||||
const emitter = mitt<{
|
||||
push: {
|
||||
name: ModalRoutes;
|
||||
props: Record<string, unknown>;
|
||||
};
|
||||
replace: {
|
||||
name: ModalRoutes;
|
||||
props: Record<string, unknown>;
|
||||
};
|
||||
pop: { name?: ModalRoutes };
|
||||
unshift: { name: ModalRoutes };
|
||||
}>();
|
||||
|
||||
type ModalRoutes = keyof typeof modals;
|
||||
|
||||
type StateItem = {
|
||||
key: string;
|
||||
name: ModalRoutes;
|
||||
props: Record<string, unknown>;
|
||||
};
|
||||
|
||||
type ModalWrapperProps = {
|
||||
children: React.ReactNode;
|
||||
name: ModalRoutes;
|
||||
isOnTop: boolean;
|
||||
};
|
||||
|
||||
function ModalWrapper({ children, name, isOnTop }: ModalWrapperProps) {
|
||||
const ref = useRef<HTMLDivElement>(null);
|
||||
|
||||
useOnClickOutside(ref, (event) => {
|
||||
const target = event.target as HTMLElement;
|
||||
const isPortal = typeof target.closest === 'function' ? !!target.closest('[data-radix-popper-content-wrapper]') : false
|
||||
|
||||
if (isOnTop && !isPortal) {
|
||||
emitter.emit('pop', {
|
||||
name,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
return (
|
||||
<div className='fixed top-0 z-50 flex h-screen w-screen items-center justify-center overflow-auto'>
|
||||
<div ref={ref} className='w-inherit m-auto py-4'>
|
||||
<Suspense>{children}</Suspense>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
export function ModalProvider() {
|
||||
const [state, setState] = useState<StateItem[]>([]);
|
||||
|
||||
useEffect(() => {
|
||||
document.addEventListener('keydown', (event) => {
|
||||
if (event.key === 'Escape' && state.length > 0) {
|
||||
setState((p) => {
|
||||
p.pop();
|
||||
return [...p];
|
||||
});
|
||||
}
|
||||
});
|
||||
}, [state]);
|
||||
|
||||
useEffect(() => {
|
||||
emitter.on('push', ({ name, props }) => {
|
||||
setState((p) => [
|
||||
...p,
|
||||
{
|
||||
key: Math.random().toString(),
|
||||
name,
|
||||
props,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
emitter.on('replace', ({ name, props }) => {
|
||||
setState([
|
||||
{
|
||||
key: Math.random().toString(),
|
||||
name,
|
||||
props,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
emitter.on('pop', ({ name }) => {
|
||||
setState((items) => {
|
||||
const match =
|
||||
name === undefined
|
||||
? // Pick last item if no name is provided
|
||||
items.length - 1
|
||||
: items.findLastIndex((item) => item.name === name);
|
||||
return items.filter((_, index) => index !== match);
|
||||
});
|
||||
});
|
||||
|
||||
emitter.on('unshift', ({ name }) => {
|
||||
setState((items) => {
|
||||
const match = items.findIndex((item) => item.name === name);
|
||||
return items.filter((_, index) => index !== match);
|
||||
});
|
||||
});
|
||||
|
||||
return () => emitter.all.clear();
|
||||
});
|
||||
return (
|
||||
<>
|
||||
{!!state.length && <div className="fixed top-0 left-0 right-0 bottom-0 bg-[rgba(0,0,0,0.2)]"></div>}
|
||||
{state.map((item, index) => {
|
||||
const Modal = modals[item.name];
|
||||
return (
|
||||
<ModalWrapper
|
||||
key={item.key}
|
||||
name={item.name}
|
||||
isOnTop={state.length - 1 === index}
|
||||
>
|
||||
{/* @ts-expect-error */}
|
||||
<Modal {...item.props} />
|
||||
</ModalWrapper>
|
||||
);
|
||||
})}
|
||||
</>
|
||||
);
|
||||
}
|
||||
|
||||
type GetComponentProps<T> = T extends
|
||||
| React.ComponentType<infer P>
|
||||
| React.Component<infer P>
|
||||
? P
|
||||
: never;
|
||||
type OrUndefined<T> = T extends Record<string, never> ? undefined : T;
|
||||
|
||||
export const pushModal = <
|
||||
T extends StateItem['name'],
|
||||
B extends OrUndefined<GetComponentProps<(typeof modals)[T]>>
|
||||
>(
|
||||
name: T,
|
||||
...rest: B extends undefined ? [] : [B]
|
||||
) =>
|
||||
emitter.emit('push', {
|
||||
name,
|
||||
props: Array.isArray(rest) && rest[0] ? rest[0] : {},
|
||||
});
|
||||
export const replaceModal = <
|
||||
T extends StateItem['name'],
|
||||
B extends OrUndefined<GetComponentProps<(typeof modals)[T]>>
|
||||
>(
|
||||
name: T,
|
||||
...rest: B extends undefined ? [] : [B]
|
||||
) =>
|
||||
emitter.emit('replace', {
|
||||
name,
|
||||
props: Array.isArray(rest) && rest[0] ? rest[0] : {},
|
||||
});
|
||||
export const popModal = (name?: StateItem['name']) =>
|
||||
emitter.emit('pop', {
|
||||
name,
|
||||
});
|
||||
export const unshiftModal = (name: StateItem['name']) =>
|
||||
emitter.emit('unshift', {
|
||||
name,
|
||||
});
|
||||
|
||||
export const showConfirm = (props: ConfirmProps) => pushModal('Confirm', props);
|
||||
Reference in New Issue
Block a user