New: Create URL Redirection component & Ability to use Connect-Mutations
All checks were successful
Build and Push Docker Image / build (push) Successful in 2m16s
All checks were successful
Build and Push Docker Image / build (push) Successful in 2m16s
This commit is contained in:
67
src/components/shortener/createRedirectionForm.tsx
Normal file
67
src/components/shortener/createRedirectionForm.tsx
Normal file
@@ -0,0 +1,67 @@
|
||||
import { useConnectMutation } from "~/integrations/connect-query/solid";
|
||||
import { createURLRedirection } from "~/gen/proto/shorten/v1/shorten-ShortenService_connectquery";
|
||||
import useAppForm from "../form/appform";
|
||||
import { Button } from "../ui/button";
|
||||
|
||||
const CreateRedirectionForm = () => {
|
||||
const mutation = useConnectMutation(createURLRedirection);
|
||||
|
||||
const form = useAppForm(() => ({
|
||||
defaultValues: {
|
||||
url: "",
|
||||
slug: "",
|
||||
},
|
||||
onSubmit: async (values) => {
|
||||
await mutation.mutateAsync({
|
||||
url: values.value.url,
|
||||
shortCode: values.value.slug,
|
||||
});
|
||||
},
|
||||
}));
|
||||
return (
|
||||
<form
|
||||
onSubmit={(e) => {
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
form.handleSubmit();
|
||||
}}
|
||||
>
|
||||
<div class="bg-card p-2">
|
||||
<h2 class="p-2 my-2">Create New Redirection</h2>
|
||||
<div class="flex gap-x-2 p-2">
|
||||
<div class="w-1/2">
|
||||
<form.AppField
|
||||
name="slug"
|
||||
children={(field) => (
|
||||
<field.TextField
|
||||
name="slug"
|
||||
label="Slug"
|
||||
description={"The slug to redirect to"}
|
||||
placeholder="https://google.com"
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
<div class="w-full">
|
||||
<form.AppField
|
||||
name="url"
|
||||
children={(field) => (
|
||||
<field.TextField
|
||||
name="url"
|
||||
label="URL"
|
||||
description={"The URL to redirect to"}
|
||||
placeholder="https://google.com"
|
||||
/>
|
||||
)}
|
||||
/>
|
||||
</div>
|
||||
<Button type="submit" class="my-auto">
|
||||
Create
|
||||
</Button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
);
|
||||
};
|
||||
|
||||
export default CreateRedirectionForm;
|
||||
27
src/components/shortener/listRedirections.tsx
Normal file
27
src/components/shortener/listRedirections.tsx
Normal file
@@ -0,0 +1,27 @@
|
||||
import { For, Match, Switch } from "solid-js";
|
||||
import { listURLRedirections } from "~/gen/proto/shorten/v1/shorten-ShortenService_connectquery";
|
||||
import { useConnectQuery } from "~/integrations/connect-query/solid";
|
||||
|
||||
const ListRedirections = () => {
|
||||
const query = useConnectQuery(listURLRedirections);
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Switch fallback={<div>Loading...</div>}>
|
||||
<Match when={query.isError}>
|
||||
<div>Error: {query.error?.message}</div>
|
||||
</Match>
|
||||
<Match when={query.isLoading}>
|
||||
<div>Loading...</div>
|
||||
</Match>
|
||||
<Match when={query.data}>
|
||||
<For each={query.data?.urlRedirections}>
|
||||
{(item) => <div>{item.url}</div>}
|
||||
</For>
|
||||
</Match>
|
||||
</Switch>
|
||||
</div>
|
||||
);
|
||||
};
|
||||
|
||||
export default ListRedirections;
|
||||
@@ -1,10 +1,20 @@
|
||||
import { createContext, useContext } from "solid-js";
|
||||
import type { JSX } from "solid-js";
|
||||
import type { Transport, ConnectError } from "@connectrpc/connect";
|
||||
import { createQuery } from "@tanstack/solid-query";
|
||||
import type { CreateQueryResult, CreateQueryOptions } from "@tanstack/solid-query";
|
||||
import { createQueryOptions } from "@connectrpc/connect-query";
|
||||
import type { DescMethodUnary, DescMessage, MessageInitShape, MessageShape } from "@bufbuild/protobuf";
|
||||
import { createQuery, createMutation } from "@tanstack/solid-query";
|
||||
import type {
|
||||
CreateQueryResult,
|
||||
CreateQueryOptions,
|
||||
CreateMutationResult,
|
||||
CreateMutationOptions,
|
||||
} from "@tanstack/solid-query";
|
||||
import { createQueryOptions, callUnaryMethod } from "@connectrpc/connect-query";
|
||||
import type {
|
||||
DescMethodUnary,
|
||||
DescMessage,
|
||||
MessageInitShape,
|
||||
MessageShape,
|
||||
} from "@bufbuild/protobuf";
|
||||
|
||||
const TransportContext = createContext<Transport>();
|
||||
|
||||
@@ -31,15 +41,15 @@ export function useTransport() {
|
||||
return transport;
|
||||
}
|
||||
|
||||
export function useConnectQuery<
|
||||
I extends DescMessage,
|
||||
O extends DescMessage
|
||||
>(
|
||||
export function useConnectQuery<I extends DescMessage, O extends DescMessage>(
|
||||
schema: DescMethodUnary<I, O>,
|
||||
input?: MessageInitShape<I>,
|
||||
options?: Omit<CreateQueryOptions<MessageShape<O>, ConnectError>, "queryKey" | "queryFn"> & {
|
||||
options?: Omit<
|
||||
CreateQueryOptions<MessageShape<O>, ConnectError>,
|
||||
"queryKey" | "queryFn"
|
||||
> & {
|
||||
transport?: Transport;
|
||||
}
|
||||
},
|
||||
): CreateQueryResult<MessageShape<O>, ConnectError> {
|
||||
const contextTransport = useTransport();
|
||||
|
||||
@@ -52,3 +62,29 @@ export function useConnectQuery<
|
||||
} as any;
|
||||
});
|
||||
}
|
||||
|
||||
export function useConnectMutation<
|
||||
I extends DescMessage,
|
||||
O extends DescMessage,
|
||||
Ctx = unknown
|
||||
>(
|
||||
schema: DescMethodUnary<I, O>,
|
||||
options?: Omit<
|
||||
CreateMutationOptions<MessageShape<O>, ConnectError, MessageInitShape<I>, Ctx>,
|
||||
"mutationFn"
|
||||
> & {
|
||||
transport?: Transport;
|
||||
},
|
||||
): CreateMutationResult<MessageShape<O>, ConnectError, MessageInitShape<I>, Ctx> {
|
||||
const contextTransport = useTransport();
|
||||
|
||||
return createMutation(() => {
|
||||
const transport = options?.transport ?? contextTransport;
|
||||
return {
|
||||
...options,
|
||||
mutationFn: async (input: MessageInitShape<I>) => {
|
||||
return callUnaryMethod(transport, schema, input);
|
||||
},
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
@@ -2,29 +2,19 @@ import { useConnectQuery } from "~/integrations/connect-query/solid";
|
||||
import { createFileRoute } from "@tanstack/solid-router";
|
||||
import { listURLRedirections } from "~/gen/proto/shorten/v1/shorten-ShortenService_connectquery";
|
||||
import { For, Match, Switch } from "solid-js";
|
||||
import ListRedirections from "~/components/shortener/listRedirections";
|
||||
import CreateRedirectionForm from "~/components/shortener/createRedirectionForm";
|
||||
|
||||
export const Route = createFileRoute("/shorten")({
|
||||
component: RouteComponent,
|
||||
});
|
||||
|
||||
function RouteComponent() {
|
||||
const query = useConnectQuery(listURLRedirections, {});
|
||||
|
||||
return (
|
||||
<div>
|
||||
<Switch fallback={<div>Loading...</div>}>
|
||||
<Match when={query.isError}>
|
||||
<div>Error: {query.error?.message}</div>
|
||||
</Match>
|
||||
<Match when={query.isLoading}>
|
||||
<div>Loading...</div>
|
||||
</Match>
|
||||
<Match when={query.data}>
|
||||
<For each={query.data?.urlRedirections}>
|
||||
{(item) => <div>{item.url}</div>}
|
||||
</For>
|
||||
</Match>
|
||||
</Switch>
|
||||
<div class="p-4 flex flex-col gap-y-4">
|
||||
<h1 class="text-xl text-bold">URL Redirections</h1>
|
||||
<CreateRedirectionForm />
|
||||
<ListRedirections />
|
||||
</div>
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user