import React, { useCallback, useRef, useState } from "react"; import type { BuilderProps, Config, ImmutableTree, JsonLogicResult, JsonTree, } from "react-awesome-query-builder"; import { Builder, Query, Utils as QbUtils } from "react-awesome-query-builder"; import Shell from "@calcom/features/shell/Shell"; import { classNames } from "@calcom/lib"; import { useLocale } from "@calcom/lib/hooks/useLocale"; import { trpc } from "@calcom/trpc/react"; import type { inferSSRProps } from "@calcom/types/inferSSRProps"; import { Button } from "@calcom/ui"; import { useInViewObserver } from "@lib/hooks/useInViewObserver"; import SingleForm, { getServerSidePropsForSingleFormView as getServerSideProps, } from "../../components/SingleForm"; import type QueryBuilderInitialConfig from "../../components/react-awesome-query-builder/config/config"; import "../../components/react-awesome-query-builder/styles.css"; import type { JsonLogicQuery } from "../../jsonLogicToPrisma"; import { getQueryBuilderConfig } from "../../lib/getQueryBuilderConfig"; export { getServerSideProps }; type QueryBuilderUpdatedConfig = typeof QueryBuilderInitialConfig & { fields: Config["fields"] }; const Result = ({ formId, jsonLogicQuery }: { formId: string; jsonLogicQuery: JsonLogicQuery | null }) => { const { t } = useLocale(); const { isLoading, status, data, isFetching, error, isFetchingNextPage, hasNextPage, fetchNextPage } = trpc.viewer.appRoutingForms.report.useInfiniteQuery( { formId: formId, // Send jsonLogicQuery only if it's a valid logic, otherwise send a logic with no query. jsonLogicQuery: jsonLogicQuery?.logic ? jsonLogicQuery : { logic: {}, }, }, { getNextPageParam: (lastPage) => lastPage.nextCursor, } ); const buttonInView = useInViewObserver(() => { if (!isFetching && hasNextPage && status === "success") { fetchNextPage(); } }); const headers = useRef(null); if (!isLoading && !data) { return
Error loading report {error?.message}
; } headers.current = (data?.pages && data?.pages[0]?.headers) || headers.current; return (
{headers.current?.map((header, index) => ( ))} {!isLoading && data?.pages.map((page) => { return page.responses?.map((responses, rowIndex) => { const isLastRow = page.responses.length - 1 === rowIndex; return ( {responses.map((r, columnIndex) => { const isLastColumn = columnIndex === responses.length - 1; return ( ); })} ); }); })}
{header}
{r}
{isLoading ?
{t("loading")}
: ""} {hasNextPage && ( )}
); }; const getInitialQuery = (config: ReturnType) => { const uuid = QbUtils.uuid(); const queryValue: JsonTree = { id: uuid, type: "group" } as JsonTree; const tree = QbUtils.checkTree(QbUtils.loadTree(queryValue), config); return { state: { tree, config }, queryValue, }; }; const Reporter = ({ form }: { form: inferSSRProps["form"] }) => { const config = getQueryBuilderConfig(form, true); const [query, setQuery] = useState(getInitialQuery(config)); const [jsonLogicQuery, setJsonLogicQuery] = useState(null); const onChange = (immutableTree: ImmutableTree, config: QueryBuilderUpdatedConfig) => { const jsonTree = QbUtils.getTree(immutableTree); setQuery(() => { const newValue = { state: { tree: immutableTree, config: config }, queryValue: jsonTree, }; setJsonLogicQuery(QbUtils.jsonLogicFormat(newValue.state.tree, config)); return newValue; }); }; const renderBuilder = useCallback( (props: BuilderProps) => (
), [] ); return (
{ onChange(immutableTree, config as QueryBuilderUpdatedConfig); }} renderBuilder={renderBuilder} />
); }; export default function ReporterWrapper({ form, appUrl, }: inferSSRProps & { appUrl: string }) { return ( (
)} /> ); } ReporterWrapper.getLayout = (page: React.ReactElement) => { return ( {page} ); };