2022-10-20 05:09:52 +00:00
|
|
|
import { useAutoAnimate } from "@formkit/auto-animate/react";
|
2023-01-04 13:30:46 +00:00
|
|
|
import Link from "next/link";
|
2022-11-23 02:55:25 +00:00
|
|
|
import React, { useCallback, useState } from "react";
|
2023-01-07 15:50:28 +00:00
|
|
|
import { Query, Builder, Utils as QbUtils } from "react-awesome-query-builder";
|
2022-07-14 12:40:53 +00:00
|
|
|
// types
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { JsonTree, ImmutableTree, BuilderProps } from "react-awesome-query-builder";
|
2022-07-14 12:40:53 +00:00
|
|
|
|
2023-01-10 15:39:29 +00:00
|
|
|
import Shell from "@calcom/features/shell/Shell";
|
2023-02-20 10:07:58 +00:00
|
|
|
import { useLocale } from "@calcom/lib/hooks/useLocale";
|
2022-07-22 17:27:06 +00:00
|
|
|
import { trpc } from "@calcom/trpc/react";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type { inferSSRProps } from "@calcom/types/inferSSRProps";
|
2022-11-23 02:55:25 +00:00
|
|
|
import {
|
2023-01-04 13:30:46 +00:00
|
|
|
SelectField,
|
2022-11-23 02:55:25 +00:00
|
|
|
FormCard,
|
|
|
|
SelectWithValidation as Select,
|
|
|
|
TextArea,
|
|
|
|
TextField,
|
2023-01-04 13:30:46 +00:00
|
|
|
Badge,
|
2023-04-05 18:14:46 +00:00
|
|
|
Divider,
|
2022-11-23 02:55:25 +00:00
|
|
|
} from "@calcom/ui";
|
2022-07-14 12:40:53 +00:00
|
|
|
|
2022-11-23 02:55:25 +00:00
|
|
|
import SingleForm, {
|
|
|
|
getServerSidePropsForSingleFormView as getServerSideProps,
|
|
|
|
} from "../../components/SingleForm";
|
2022-07-14 12:40:53 +00:00
|
|
|
import "../../components/react-awesome-query-builder/styles.css";
|
2023-01-07 15:50:28 +00:00
|
|
|
import { RoutingPages } from "../../lib/RoutingPages";
|
2023-01-04 13:30:46 +00:00
|
|
|
import { createFallbackRoute } from "../../lib/createFallbackRoute";
|
2023-01-07 15:50:28 +00:00
|
|
|
import { getQueryBuilderConfig } from "../../lib/getQueryBuilderConfig";
|
2023-01-04 13:30:46 +00:00
|
|
|
import isRouter from "../../lib/isRouter";
|
2023-02-16 22:39:57 +00:00
|
|
|
import type {
|
|
|
|
GlobalRoute,
|
|
|
|
LocalRoute,
|
|
|
|
QueryBuilderUpdatedConfig,
|
|
|
|
SerializableRoute,
|
|
|
|
} from "../../types/types";
|
2022-07-14 12:40:53 +00:00
|
|
|
|
2022-11-11 09:57:44 +00:00
|
|
|
export { getServerSideProps };
|
|
|
|
|
2023-01-04 13:30:46 +00:00
|
|
|
const hasRules = (route: Route) => {
|
|
|
|
if (isRouter(route)) return false;
|
2022-08-13 11:04:57 +00:00
|
|
|
route.queryValue.children1 && Object.keys(route.queryValue.children1).length;
|
2023-01-04 13:30:46 +00:00
|
|
|
};
|
|
|
|
const getEmptyRoute = (): Exclude<SerializableRoute, GlobalRoute> => {
|
2022-07-14 12:40:53 +00:00
|
|
|
const uuid = QbUtils.uuid();
|
|
|
|
return {
|
|
|
|
id: uuid,
|
|
|
|
action: {
|
|
|
|
type: "eventTypeRedirectUrl",
|
|
|
|
value: "",
|
|
|
|
},
|
|
|
|
queryValue: { id: uuid, type: "group" },
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2023-01-04 13:30:46 +00:00
|
|
|
type Route =
|
|
|
|
| (LocalRoute & {
|
|
|
|
// This is what's persisted
|
|
|
|
queryValue: JsonTree;
|
|
|
|
// `queryValue` is parsed to create state
|
|
|
|
state: {
|
|
|
|
tree: ImmutableTree;
|
|
|
|
config: QueryBuilderUpdatedConfig;
|
|
|
|
};
|
|
|
|
})
|
|
|
|
| GlobalRoute;
|
2022-07-14 12:40:53 +00:00
|
|
|
|
|
|
|
const Route = ({
|
|
|
|
route,
|
|
|
|
routes,
|
|
|
|
setRoute,
|
|
|
|
config,
|
|
|
|
setRoutes,
|
|
|
|
moveUp,
|
|
|
|
moveDown,
|
2023-01-04 13:30:46 +00:00
|
|
|
appUrl,
|
|
|
|
disabled = false,
|
2022-07-14 12:40:53 +00:00
|
|
|
}: {
|
|
|
|
route: Route;
|
|
|
|
routes: Route[];
|
|
|
|
setRoute: (id: string, route: Partial<Route>) => void;
|
|
|
|
config: QueryBuilderUpdatedConfig;
|
|
|
|
setRoutes: React.Dispatch<React.SetStateAction<Route[]>>;
|
|
|
|
moveUp?: { fn: () => void; check: () => boolean } | null;
|
|
|
|
moveDown?: { fn: () => void; check: () => boolean } | null;
|
2023-01-04 13:30:46 +00:00
|
|
|
appUrl: string;
|
|
|
|
disabled?: boolean;
|
2022-07-14 12:40:53 +00:00
|
|
|
}) => {
|
|
|
|
const index = routes.indexOf(route);
|
2022-11-10 12:58:07 +00:00
|
|
|
|
2022-11-10 23:40:01 +00:00
|
|
|
const { data: eventTypesByGroup } = trpc.viewer.eventTypes.getByViewer.useQuery();
|
2022-07-14 12:40:53 +00:00
|
|
|
|
|
|
|
const eventOptions: { label: string; value: string }[] = [];
|
|
|
|
eventTypesByGroup?.eventTypeGroups.forEach((group) => {
|
|
|
|
group.eventTypes.forEach((eventType) => {
|
|
|
|
const uniqueSlug = `${group.profile.slug}/${eventType.slug}`;
|
|
|
|
eventOptions.push({
|
|
|
|
label: uniqueSlug,
|
|
|
|
value: uniqueSlug,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
const onChange = (route: Route, immutableTree: ImmutableTree, config: QueryBuilderUpdatedConfig) => {
|
|
|
|
const jsonTree = QbUtils.getTree(immutableTree);
|
|
|
|
setRoute(route.id, {
|
|
|
|
state: { tree: immutableTree, config: config },
|
|
|
|
queryValue: jsonTree,
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
const renderBuilder = useCallback(
|
|
|
|
(props: BuilderProps) => (
|
|
|
|
<div className="query-builder-container">
|
|
|
|
<div className="query-builder qb-lite">
|
|
|
|
<Builder {...props} />
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
),
|
|
|
|
[]
|
|
|
|
);
|
|
|
|
|
2023-01-04 13:30:46 +00:00
|
|
|
if (isRouter(route)) {
|
|
|
|
return (
|
|
|
|
<div>
|
|
|
|
<FormCard
|
|
|
|
moveUp={moveUp}
|
|
|
|
moveDown={moveDown}
|
|
|
|
deleteField={{
|
|
|
|
check: () => routes.length !== 1,
|
|
|
|
fn: () => {
|
|
|
|
const newRoutes = routes.filter((r) => r.id !== route.id);
|
|
|
|
setRoutes(newRoutes);
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
label={
|
|
|
|
<div>
|
|
|
|
<span className="mr-2">{`Route ${index + 1}`}</span>
|
|
|
|
</div>
|
|
|
|
}
|
|
|
|
className="mb-6">
|
|
|
|
<div className="-mt-3">
|
|
|
|
<Link href={`/${appUrl}/route-builder/${route.id}`}>
|
2023-01-06 12:13:56 +00:00
|
|
|
<Badge variant="gray">
|
|
|
|
<span className="font-semibold">{route.name}</span>
|
|
|
|
</Badge>
|
2023-01-04 13:30:46 +00:00
|
|
|
</Link>
|
2023-04-05 18:14:46 +00:00
|
|
|
<p className="text-subtle mt-2 text-sm">
|
2023-01-04 13:30:46 +00:00
|
|
|
Fields available in <span className="font-bold">{route.name}</span> will be added to this form.
|
|
|
|
</p>
|
|
|
|
</div>
|
|
|
|
</FormCard>
|
|
|
|
</div>
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-07-14 12:40:53 +00:00
|
|
|
return (
|
2022-09-02 19:00:41 +00:00
|
|
|
<FormCard
|
|
|
|
className="mb-6"
|
|
|
|
moveUp={moveUp}
|
|
|
|
moveDown={moveDown}
|
|
|
|
label={route.isFallback ? "Fallback Route" : `Route ${index + 1}`}
|
|
|
|
deleteField={{
|
|
|
|
check: () => routes.length !== 1 && !route.isFallback,
|
2022-09-14 10:10:52 +00:00
|
|
|
fn: () => {
|
|
|
|
const newRoutes = routes.filter((r) => r.id !== route.id);
|
|
|
|
setRoutes(newRoutes);
|
|
|
|
},
|
2022-09-02 19:00:41 +00:00
|
|
|
}}>
|
|
|
|
<div className="-mx-4 mb-4 flex w-full items-center sm:mx-0">
|
2022-07-15 11:46:05 +00:00
|
|
|
<div className="cal-query-builder w-full ">
|
2022-07-14 12:40:53 +00:00
|
|
|
<div>
|
2023-04-05 18:14:46 +00:00
|
|
|
<div className="text-emphasis flex w-full items-center text-sm">
|
2022-07-14 12:40:53 +00:00
|
|
|
<div className="flex flex-grow-0 whitespace-nowrap">
|
2022-09-02 19:00:41 +00:00
|
|
|
<span>Send Booker to</span>
|
2022-07-14 12:40:53 +00:00
|
|
|
</div>
|
|
|
|
<Select
|
2023-01-04 13:30:46 +00:00
|
|
|
isDisabled={disabled}
|
2022-07-14 12:40:53 +00:00
|
|
|
className="block w-full flex-grow px-2"
|
|
|
|
required
|
2023-01-04 13:30:46 +00:00
|
|
|
value={RoutingPages.find((page) => page.value === route.action?.type)}
|
2022-07-14 12:40:53 +00:00
|
|
|
onChange={(item) => {
|
|
|
|
if (!item) {
|
|
|
|
return;
|
|
|
|
}
|
2023-01-04 13:30:46 +00:00
|
|
|
const action: LocalRoute["action"] = {
|
2022-07-14 12:40:53 +00:00
|
|
|
type: item.value,
|
|
|
|
value: "",
|
|
|
|
};
|
|
|
|
|
|
|
|
if (action.type === "customPageMessage") {
|
|
|
|
action.value = "We are not ready for you yet :(";
|
|
|
|
} else {
|
|
|
|
action.value = "";
|
|
|
|
}
|
|
|
|
|
|
|
|
setRoute(route.id, { action });
|
|
|
|
}}
|
|
|
|
options={RoutingPages}
|
|
|
|
/>
|
2023-01-04 13:30:46 +00:00
|
|
|
{route.action?.type ? (
|
|
|
|
route.action?.type === "customPageMessage" ? (
|
2022-09-02 19:00:41 +00:00
|
|
|
<TextArea
|
2022-07-14 12:40:53 +00:00
|
|
|
required
|
2023-01-04 13:30:46 +00:00
|
|
|
disabled={disabled}
|
2022-09-02 19:00:41 +00:00
|
|
|
name="customPageMessage"
|
2023-04-05 18:14:46 +00:00
|
|
|
className="border-default flex w-full flex-grow"
|
2022-07-14 12:40:53 +00:00
|
|
|
value={route.action.value}
|
|
|
|
onChange={(e) => {
|
|
|
|
setRoute(route.id, { action: { ...route.action, value: e.target.value } });
|
|
|
|
}}
|
|
|
|
/>
|
2023-01-04 13:30:46 +00:00
|
|
|
) : route.action?.type === "externalRedirectUrl" ? (
|
2022-09-02 19:00:41 +00:00
|
|
|
<TextField
|
2023-01-04 13:30:46 +00:00
|
|
|
disabled={disabled}
|
2022-09-02 19:00:41 +00:00
|
|
|
name="externalRedirectUrl"
|
2023-04-05 18:14:46 +00:00
|
|
|
className="border-default flex w-full flex-grow text-sm"
|
2022-09-17 02:00:22 +00:00
|
|
|
containerClassName="w-full mt-2"
|
2023-04-24 14:12:50 +00:00
|
|
|
type="url"
|
2022-07-14 12:40:53 +00:00
|
|
|
required
|
2022-09-17 02:00:22 +00:00
|
|
|
labelSrOnly
|
2022-07-14 12:40:53 +00:00
|
|
|
value={route.action.value}
|
|
|
|
onChange={(e) => {
|
|
|
|
setRoute(route.id, { action: { ...route.action, value: e.target.value } });
|
|
|
|
}}
|
2023-04-24 14:12:50 +00:00
|
|
|
placeholder="https://example.com"
|
2022-07-14 12:40:53 +00:00
|
|
|
/>
|
|
|
|
) : (
|
|
|
|
<div className="block w-full">
|
|
|
|
<Select
|
|
|
|
required
|
2023-01-04 13:30:46 +00:00
|
|
|
isDisabled={disabled}
|
2022-07-14 12:40:53 +00:00
|
|
|
options={eventOptions}
|
|
|
|
onChange={(option) => {
|
|
|
|
if (!option) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setRoute(route.id, { action: { ...route.action, value: option.value } });
|
|
|
|
}}
|
|
|
|
value={eventOptions.find((eventOption) => eventOption.value === route.action.value)}
|
|
|
|
/>
|
|
|
|
</div>
|
|
|
|
)
|
|
|
|
) : null}
|
|
|
|
</div>
|
2022-08-13 11:04:57 +00:00
|
|
|
|
|
|
|
{((route.isFallback && hasRules(route)) || !route.isFallback) && (
|
|
|
|
<>
|
2023-04-05 18:14:46 +00:00
|
|
|
<Divider className="mt-3 mb-6" />
|
2022-08-13 11:04:57 +00:00
|
|
|
<Query
|
|
|
|
{...config}
|
|
|
|
value={route.state.tree}
|
|
|
|
onChange={(immutableTree, config) => {
|
|
|
|
onChange(route, immutableTree, config as QueryBuilderUpdatedConfig);
|
|
|
|
}}
|
|
|
|
renderBuilder={renderBuilder}
|
|
|
|
/>
|
|
|
|
</>
|
|
|
|
)}
|
2022-07-14 12:40:53 +00:00
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
2022-09-02 19:00:41 +00:00
|
|
|
</FormCard>
|
2022-07-14 12:40:53 +00:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2023-01-04 13:30:46 +00:00
|
|
|
const deserializeRoute = (
|
|
|
|
route: Exclude<SerializableRoute, GlobalRoute>,
|
|
|
|
config: QueryBuilderUpdatedConfig
|
|
|
|
): Route => {
|
2022-07-14 12:40:53 +00:00
|
|
|
return {
|
|
|
|
...route,
|
|
|
|
state: {
|
|
|
|
tree: QbUtils.checkTree(QbUtils.loadTree(route.queryValue), config),
|
|
|
|
config: config,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
const Routes = ({
|
|
|
|
form,
|
2022-09-02 19:00:41 +00:00
|
|
|
hookForm,
|
2023-01-04 13:30:46 +00:00
|
|
|
appUrl,
|
2022-07-14 12:40:53 +00:00
|
|
|
}: {
|
|
|
|
form: inferSSRProps<typeof getServerSideProps>["form"];
|
2022-09-02 19:00:41 +00:00
|
|
|
// Figure out the type
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
|
|
hookForm: any;
|
2023-01-04 13:30:46 +00:00
|
|
|
appUrl: string;
|
2022-07-14 12:40:53 +00:00
|
|
|
}) => {
|
|
|
|
const { routes: serializedRoutes } = form;
|
2023-02-20 10:07:58 +00:00
|
|
|
const { t } = useLocale();
|
2023-01-04 13:30:46 +00:00
|
|
|
|
2022-07-14 12:40:53 +00:00
|
|
|
const config = getQueryBuilderConfig(form);
|
|
|
|
const [routes, setRoutes] = useState(() => {
|
|
|
|
const transformRoutes = () => {
|
|
|
|
const _routes = serializedRoutes || [getEmptyRoute()];
|
|
|
|
_routes.forEach((r) => {
|
2023-01-04 13:30:46 +00:00
|
|
|
if (isRouter(r)) return;
|
2022-07-14 12:40:53 +00:00
|
|
|
if (!r.queryValue?.id) {
|
|
|
|
r.queryValue = { id: QbUtils.uuid(), type: "group" };
|
|
|
|
}
|
|
|
|
});
|
|
|
|
return _routes;
|
|
|
|
};
|
|
|
|
|
2023-01-04 13:30:46 +00:00
|
|
|
return transformRoutes().map((route) => {
|
|
|
|
if (isRouter(route)) return route;
|
|
|
|
return deserializeRoute(route, config);
|
|
|
|
});
|
2022-07-14 12:40:53 +00:00
|
|
|
});
|
2023-01-04 13:30:46 +00:00
|
|
|
const { data: allForms } = trpc.viewer.appRoutingForms.forms.useQuery();
|
|
|
|
|
|
|
|
const availableRouters =
|
|
|
|
allForms
|
|
|
|
?.filter((router) => {
|
|
|
|
return router.id !== form.id;
|
|
|
|
})
|
|
|
|
.map((router) => {
|
|
|
|
return {
|
|
|
|
value: router.id,
|
|
|
|
label: router.name,
|
|
|
|
name: router.name,
|
|
|
|
description: router.description,
|
|
|
|
isDisabled: false,
|
|
|
|
};
|
|
|
|
}) || [];
|
|
|
|
|
|
|
|
const isConnectedForm = (id: string) => form.connectedForms.map((f) => f.id).includes(id);
|
|
|
|
|
|
|
|
const routerOptions = (
|
|
|
|
[
|
|
|
|
{
|
|
|
|
label: "Create a New Route",
|
|
|
|
value: "newRoute",
|
|
|
|
name: null,
|
|
|
|
description: null,
|
|
|
|
},
|
|
|
|
] as {
|
|
|
|
label: string;
|
|
|
|
value: string;
|
|
|
|
name: string | null;
|
|
|
|
description: string | null;
|
|
|
|
isDisabled?: boolean;
|
|
|
|
}[]
|
|
|
|
).concat(
|
|
|
|
availableRouters.map((r) => {
|
|
|
|
// Reset disabled state
|
|
|
|
r.isDisabled = false;
|
|
|
|
|
|
|
|
// Can't select a form as router that is already a connected form. It avoids cyclic dependency
|
|
|
|
if (isConnectedForm(r.value)) {
|
|
|
|
r.isDisabled = true;
|
|
|
|
}
|
|
|
|
// A route that's already used, can't be reselected
|
|
|
|
if (routes.find((route) => route.id === r.value)) {
|
|
|
|
r.isDisabled = true;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
})
|
|
|
|
);
|
2022-09-22 17:23:43 +00:00
|
|
|
|
2022-10-20 05:09:52 +00:00
|
|
|
const [animationRef] = useAutoAnimate<HTMLDivElement>();
|
2022-07-14 12:40:53 +00:00
|
|
|
|
2023-01-04 13:30:46 +00:00
|
|
|
const mainRoutes = routes.filter((route) => {
|
|
|
|
if (isRouter(route)) return true;
|
|
|
|
return !route.isFallback;
|
|
|
|
});
|
|
|
|
|
|
|
|
let fallbackRoute = routes.find((route) => {
|
|
|
|
if (isRouter(route)) return false;
|
|
|
|
return route.isFallback;
|
|
|
|
});
|
|
|
|
|
2022-07-14 12:40:53 +00:00
|
|
|
if (!fallbackRoute) {
|
|
|
|
fallbackRoute = deserializeRoute(createFallbackRoute(), config);
|
|
|
|
setRoutes((routes) => {
|
|
|
|
// Even though it's obvious that fallbackRoute is defined here but TypeScript just can't figure it out.
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
return [...routes, fallbackRoute!];
|
|
|
|
});
|
|
|
|
return null;
|
|
|
|
} else if (routes.indexOf(fallbackRoute) !== routes.length - 1) {
|
|
|
|
// Ensure fallback is last
|
|
|
|
setRoutes((routes) => {
|
|
|
|
// Even though it's obvious that fallbackRoute is defined here but TypeScript just can't figure it out.
|
|
|
|
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
|
|
|
|
return [...routes.filter((route) => route.id !== fallbackRoute!.id), fallbackRoute!];
|
|
|
|
});
|
|
|
|
}
|
2023-01-04 13:30:46 +00:00
|
|
|
|
2022-07-14 12:40:53 +00:00
|
|
|
const setRoute = (id: string, route: Partial<Route>) => {
|
|
|
|
const index = routes.findIndex((route) => route.id === id);
|
|
|
|
const newRoutes = [...routes];
|
|
|
|
newRoutes[index] = { ...routes[index], ...route };
|
|
|
|
setRoutes(newRoutes);
|
|
|
|
};
|
|
|
|
|
|
|
|
const swap = (from: number, to: number) => {
|
|
|
|
setRoutes((routes) => {
|
|
|
|
const newRoutes = [...routes];
|
|
|
|
const routeToSwap = newRoutes[from];
|
|
|
|
newRoutes[from] = newRoutes[to];
|
|
|
|
newRoutes[to] = routeToSwap;
|
|
|
|
return newRoutes;
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2023-01-04 13:30:46 +00:00
|
|
|
const routesToSave = routes.map((route) => {
|
|
|
|
if (isRouter(route)) {
|
|
|
|
return route;
|
|
|
|
}
|
|
|
|
return {
|
|
|
|
id: route.id,
|
|
|
|
action: route.action,
|
|
|
|
isFallback: route.isFallback,
|
|
|
|
queryValue: route.queryValue,
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
2022-09-02 19:00:41 +00:00
|
|
|
hookForm.setValue("routes", routesToSave);
|
2023-01-04 13:30:46 +00:00
|
|
|
|
2022-07-14 12:40:53 +00:00
|
|
|
return (
|
|
|
|
<div className="flex flex-col-reverse md:flex-row">
|
2022-09-22 17:23:43 +00:00
|
|
|
<div ref={animationRef} className="w-full ltr:mr-2 rtl:ml-2">
|
2022-07-14 12:40:53 +00:00
|
|
|
{mainRoutes.map((route, key) => {
|
|
|
|
return (
|
|
|
|
<Route
|
2023-01-04 13:30:46 +00:00
|
|
|
appUrl={appUrl}
|
2022-08-13 11:04:57 +00:00
|
|
|
key={route.id}
|
2022-07-14 12:40:53 +00:00
|
|
|
config={config}
|
|
|
|
route={route}
|
|
|
|
moveUp={{
|
|
|
|
check: () => key !== 0,
|
|
|
|
fn: () => {
|
|
|
|
swap(key, key - 1);
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
moveDown={{
|
|
|
|
// routes.length - 1 is fallback route always. So, routes.length - 2 is the last item that can be moved down
|
|
|
|
check: () => key !== routes.length - 2,
|
|
|
|
fn: () => {
|
|
|
|
swap(key, key + 1);
|
|
|
|
},
|
|
|
|
}}
|
|
|
|
routes={routes}
|
|
|
|
setRoute={setRoute}
|
|
|
|
setRoutes={setRoutes}
|
|
|
|
/>
|
|
|
|
);
|
|
|
|
})}
|
2023-01-04 13:30:46 +00:00
|
|
|
<SelectField
|
2023-02-20 10:07:58 +00:00
|
|
|
placeholder={t("select_a_router")}
|
2023-01-04 13:30:46 +00:00
|
|
|
containerClassName="mb-6 data-testid-select-router"
|
|
|
|
isOptionDisabled={(option) => !!option.isDisabled}
|
2023-02-20 10:07:58 +00:00
|
|
|
label={t("add_a_new_route")}
|
2023-01-04 13:30:46 +00:00
|
|
|
options={routerOptions}
|
|
|
|
key={mainRoutes.length}
|
|
|
|
onChange={(option) => {
|
|
|
|
if (!option) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const router = option.value;
|
|
|
|
if (router === "newRoute") {
|
|
|
|
const newEmptyRoute = getEmptyRoute();
|
|
|
|
const newRoutes = [
|
|
|
|
...routes,
|
|
|
|
{
|
|
|
|
...newEmptyRoute,
|
|
|
|
state: {
|
|
|
|
tree: QbUtils.checkTree(QbUtils.loadTree(newEmptyRoute.queryValue), config),
|
|
|
|
config,
|
|
|
|
},
|
2022-07-14 12:40:53 +00:00
|
|
|
},
|
2023-01-04 13:30:46 +00:00
|
|
|
];
|
|
|
|
|
|
|
|
setRoutes(newRoutes);
|
|
|
|
} else {
|
|
|
|
const routerId = router;
|
|
|
|
if (!routerId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
setRoutes([
|
|
|
|
...routes,
|
|
|
|
{
|
|
|
|
isRouter: true,
|
|
|
|
id: routerId,
|
|
|
|
name: option.name,
|
|
|
|
description: option.description,
|
|
|
|
} as Route,
|
|
|
|
]);
|
|
|
|
}
|
|
|
|
}}
|
|
|
|
/>
|
|
|
|
|
2022-07-14 12:40:53 +00:00
|
|
|
<div>
|
|
|
|
<Route
|
|
|
|
config={config}
|
|
|
|
route={fallbackRoute}
|
|
|
|
routes={routes}
|
|
|
|
setRoute={setRoute}
|
|
|
|
setRoutes={setRoutes}
|
2023-01-04 13:30:46 +00:00
|
|
|
appUrl={appUrl}
|
2022-07-14 12:40:53 +00:00
|
|
|
/>
|
|
|
|
</div>
|
2022-09-02 19:00:41 +00:00
|
|
|
</div>
|
2022-07-14 12:40:53 +00:00
|
|
|
</div>
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default function RouteBuilder({
|
|
|
|
form,
|
|
|
|
appUrl,
|
|
|
|
}: inferSSRProps<typeof getServerSideProps> & { appUrl: string }) {
|
|
|
|
return (
|
2022-09-02 19:00:41 +00:00
|
|
|
<SingleForm
|
|
|
|
form={form}
|
2022-07-22 20:51:38 +00:00
|
|
|
appUrl={appUrl}
|
2022-09-02 19:00:41 +00:00
|
|
|
Page={({ hookForm, form }) => (
|
|
|
|
<div className="route-config">
|
2023-01-04 13:30:46 +00:00
|
|
|
<Routes hookForm={hookForm} appUrl={appUrl} form={form} />
|
2022-09-02 19:00:41 +00:00
|
|
|
</div>
|
|
|
|
)}
|
|
|
|
/>
|
2022-07-14 12:40:53 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-09-02 19:00:41 +00:00
|
|
|
RouteBuilder.getLayout = (page: React.ReactElement) => {
|
2022-09-24 11:55:50 +00:00
|
|
|
return (
|
|
|
|
<Shell backPath="/apps/routing-forms/forms" withoutMain={true}>
|
|
|
|
{page}
|
|
|
|
</Shell>
|
|
|
|
);
|
2022-09-02 19:00:41 +00:00
|
|
|
};
|