108 lines
4.1 KiB
TypeScript
108 lines
4.1 KiB
TypeScript
// R-P6 (P6 CRM core, Variante A) — organisation browser route
|
|
// (`/contacts/companies`). Same server-built KontaktListVM as `/contacts`, but the
|
|
// type filter is pinned to `organisation`. Module off -> locked empty; missing
|
|
// crm.read_own -> 404. UI is the AD-28 interim KontaktListPage (companies variant).
|
|
|
|
import {
|
|
type ActionFunctionArgs,
|
|
type LoaderFunctionArgs,
|
|
useActionData,
|
|
useLoaderData,
|
|
useFetcher,
|
|
useNavigate,
|
|
useSubmit,
|
|
} from "react-router";
|
|
import { handleKontaktListAction, loadKontaktListData, type KontaktListLoaderData } from "~/utils/integrations/crm/crmListRoute.server";
|
|
import { kontaktListQueryToParams, type KontaktListQuery } from "~/utils/integrations/crm/kontaktListQuery";
|
|
import { CRM_MODULE_KEY } from "~/utils/integrations/crm/crmRoutePaths";
|
|
import { KontaktListPage } from "~/components/screens/crm/KontaktListPage";
|
|
import type { KontaktNewDuplicates } from "~/components/screens/crm/KontaktNewPage";
|
|
import ServerError from "~/components/ui/errors/ServerError";
|
|
import { useSaveToast } from "~/hooks/use-save-toast";
|
|
|
|
export const loader = async ({ request, params }: LoaderFunctionArgs) => {
|
|
return Response.json(await loadKontaktListData({ request, params, variant: "companies" }));
|
|
};
|
|
|
|
export const action = async ({ request, params }: ActionFunctionArgs) => {
|
|
return handleKontaktListAction({ request, params, variant: "companies" });
|
|
};
|
|
|
|
export default function CompaniesIndexRoute() {
|
|
const data = useLoaderData<KontaktListLoaderData>();
|
|
const actionData = useActionData<{ error?: string }>();
|
|
const navigate = useNavigate();
|
|
// Live duplicate check for the create dialog — posts to the create route, which
|
|
// owns the `intent=checkDuplicates` branch (same contract as the standalone page).
|
|
const dupFetcher = useFetcher<KontaktNewDuplicates>();
|
|
const submit = useSubmit();
|
|
useSaveToast(actionData);
|
|
|
|
const toHref = (query: KontaktListQuery): string => {
|
|
const qs = kontaktListQueryToParams(query).toString();
|
|
return qs ? `${data.baseHref}?${qs}` : data.baseHref;
|
|
};
|
|
|
|
return (
|
|
<KontaktListPage
|
|
vm={data.vm}
|
|
variant="companies"
|
|
moduleActive={data.moduleActive}
|
|
query={data.query}
|
|
tagOptions={data.tagOptions}
|
|
ownerOptions={data.ownerOptions}
|
|
canWrite={data.canWrite}
|
|
canAdmin={data.canAdmin}
|
|
nav={{
|
|
contactsHref: data.contactsHref,
|
|
companiesHref: data.companiesHref,
|
|
customFieldsHref: data.customFieldsHref,
|
|
tagsHref: data.tagsHref,
|
|
duplicatesHref: data.duplicatesHref,
|
|
}}
|
|
newHref={data.newHref}
|
|
defaultOwnerId={data.defaultOwnerId}
|
|
duplicates={dupFetcher.data ?? null}
|
|
errorKey={actionData?.error ?? null}
|
|
actions={{
|
|
onApplyFilter: (query) => navigate(toHref(query)),
|
|
onGoToPage: (page) => navigate(toHref({ ...data.query, page })),
|
|
onBulk: (intent, ids) => {
|
|
const form = new FormData();
|
|
form.set("intent", intent);
|
|
ids.forEach((id) => form.append("kontaktId", id));
|
|
submit(form, { method: "post" });
|
|
},
|
|
onCheckDuplicates: (fields) => {
|
|
const form = new FormData();
|
|
form.set("intent", "checkDuplicates");
|
|
for (const [key, value] of Object.entries(fields)) form.set(key, value);
|
|
dupFetcher.submit(form, { method: "post", action: data.newHref });
|
|
},
|
|
onExport: () => window.location.assign(data.exportHref),
|
|
onNavigate: (href) => navigate(href),
|
|
onAssignTag: (kontaktId, tagId) => {
|
|
const form = new FormData();
|
|
form.set("intent", "assignTag");
|
|
form.set("kontaktId", kontaktId);
|
|
form.set("tagId", tagId);
|
|
submit(form, { method: "post" });
|
|
},
|
|
onRemoveTag: (kontaktId, tagId) => {
|
|
const form = new FormData();
|
|
form.set("intent", "removeTag");
|
|
form.set("kontaktId", kontaktId);
|
|
form.set("tagId", tagId);
|
|
submit(form, { method: "post" });
|
|
},
|
|
}}
|
|
/>
|
|
);
|
|
}
|
|
|
|
export function ErrorBoundary() {
|
|
return <ServerError />;
|
|
}
|
|
|
|
export const handle = { moduleKey: CRM_MODULE_KEY };
|