Preped for adding routes
This commit is contained in:
parent
84d66c598f
commit
9b809cf991
5 changed files with 26 additions and 18 deletions
|
|
@ -13,7 +13,7 @@ import { RouteDialog } from '@/components/RouteDialog';
|
||||||
|
|
||||||
export default function Home() {
|
export default function Home() {
|
||||||
|
|
||||||
const [routeDialog, setRouteDialog] = useState(false);
|
const [route, setRoute] = useState<Route>();
|
||||||
const { data: routes, error, isLoading } = useRoutes();
|
const { data: routes, error, isLoading } = useRoutes();
|
||||||
let routesMap: Route[] = routes as Route[];
|
let routesMap: Route[] = routes as Route[];
|
||||||
|
|
||||||
|
|
@ -22,7 +22,7 @@ export default function Home() {
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<main>
|
<main>
|
||||||
{routeDialog && <RouteDialog route={routes as Route} />}
|
{route && <RouteDialog route={route} routeMap={routesMap} />}
|
||||||
<div className='flex items-center justify-center p-6'>
|
<div className='flex items-center justify-center p-6'>
|
||||||
<Card className='flex'>
|
<Card className='flex'>
|
||||||
<CardHeader>
|
<CardHeader>
|
||||||
|
|
@ -35,7 +35,7 @@ export default function Home() {
|
||||||
</div>
|
</div>
|
||||||
</CardContent>
|
</CardContent>
|
||||||
<CardFooter>
|
<CardFooter>
|
||||||
<Button onClick={() => setRouteDialog(!routeDialog)} className='translate-y-2.5'>Create</Button>
|
<Button onClick={() => setRoute(route)} className='translate-y-2.5'>Create</Button>
|
||||||
</CardFooter>
|
</CardFooter>
|
||||||
</Card>
|
</Card>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
|
|
@ -32,12 +32,13 @@ export default function RouteCards({ routes }: { routes: Route[] }) {
|
||||||
|
|
||||||
|
|
||||||
let editRoute = (route: Route) => {
|
let editRoute = (route: Route) => {
|
||||||
|
console.log(routes)
|
||||||
setRoute(route);
|
setRoute(route);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
{route && <RouteDialog route={route} />}
|
{route && <RouteDialog route={route} routesMap={routes} />}
|
||||||
{
|
{
|
||||||
routes.map((route, index) => (
|
routes.map((route, index) => (
|
||||||
<div key={index} className='p-2'>
|
<div key={index} className='p-2'>
|
||||||
|
|
|
||||||
|
|
@ -4,14 +4,14 @@ import { useState } from "react";
|
||||||
import useSWRMutation from "swr/mutation";
|
import useSWRMutation from "swr/mutation";
|
||||||
import { toast } from "./ui/use-toast";
|
import { toast } from "./ui/use-toast";
|
||||||
import { useFieldArray, useForm } from "react-hook-form";
|
import { useFieldArray, useForm } from "react-hook-form";
|
||||||
import { set } from "lodash";
|
import { get, set } from "lodash";
|
||||||
import { Dialog, DialogContent } from "./ui/dialog";
|
import { Dialog, DialogContent } from "./ui/dialog";
|
||||||
import { Form, FormControl, FormField, FormItem, FormLabel } from "./ui/form";
|
import { Form, FormControl, FormField, FormItem, FormLabel } from "./ui/form";
|
||||||
import { Input } from "./ui/input";
|
import { Input } from "./ui/input";
|
||||||
import { Button } from "./ui/button";
|
import { Button } from "./ui/button";
|
||||||
|
|
||||||
export function RouteDialog({ route }: { route: Route }) {
|
export function RouteDialog({ route, routesMap }: { route: Route, routesMap: Route[] }) {
|
||||||
let index = getHosts(route).indexOf(route.match[0].host[0]);
|
let index = routesMap.indexOf(route);
|
||||||
|
|
||||||
const [modifiedRoute, setRoute] = useState(route);
|
const [modifiedRoute, setRoute] = useState(route);
|
||||||
const { trigger: pushUpdate } = useSWRMutation(`/api/caddy/routes/${index}`, updateRoute, {
|
const { trigger: pushUpdate } = useSWRMutation(`/api/caddy/routes/${index}`, updateRoute, {
|
||||||
|
|
@ -36,9 +36,9 @@ export function RouteDialog({ route }: { route: Route }) {
|
||||||
|
|
||||||
const form = useForm({
|
const form = useForm({
|
||||||
values: {
|
values: {
|
||||||
handler: route.handle[0].routes[0].handle[0].handler,
|
handler: route.handle[0].routes[0].handle[0].handler ?? "",
|
||||||
upstreams: validUpstreams,
|
upstreams: validUpstreams ?? [],
|
||||||
hosts: validHosts,
|
hosts: validHosts ?? [],
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -53,7 +53,13 @@ export function RouteDialog({ route }: { route: Route }) {
|
||||||
})
|
})
|
||||||
|
|
||||||
let onSubmit = (() => {
|
let onSubmit = (() => {
|
||||||
pushUpdate(modifiedRoute);
|
let update = {
|
||||||
|
route: modifiedRoute,
|
||||||
|
index: index,
|
||||||
|
type: index == -1 ? 'PUT' : 'PATCH'
|
||||||
|
}
|
||||||
|
if (index == -1) update.index = routesMap.length + 1;
|
||||||
|
pushUpdate(update);
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -48,15 +48,16 @@ export async function useConfig() {
|
||||||
}).then((res) => res.json());
|
}).then((res) => res.json());
|
||||||
}
|
}
|
||||||
|
|
||||||
export function updateRoute(url: any, { arg }: { arg: Route }) {
|
export function updateRoute(url: any, { arg }: { arg: { route: Route, index: number, type: string } }) {
|
||||||
|
console.log(arg.index)
|
||||||
return fetch(url, {
|
return fetch(url, {
|
||||||
method: 'PATCH',
|
method: arg.type,
|
||||||
headers: {
|
headers: {
|
||||||
'Content-Type': 'application/json'
|
'Content-Type': 'application/json'
|
||||||
},
|
},
|
||||||
cache: 'no-cache',
|
cache: 'no-cache',
|
||||||
body: JSON.stringify(arg)
|
body: JSON.stringify(arg.route)
|
||||||
}).then((res) => res.json());
|
}).then((res) => res.json()).catch((err) => console.log(err));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ interface Match {
|
||||||
interface Handle {
|
interface Handle {
|
||||||
handler: string;
|
handler: string;
|
||||||
upstreams: Upstream[];
|
upstreams: Upstream[];
|
||||||
routes?: Route[];
|
routes: Route[];
|
||||||
}
|
}
|
||||||
|
|
||||||
interface Upstream {
|
interface Upstream {
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue