Skip to content

A full-featured CRM built with React, shadcn/ui, and Supabase.

Change import UI from dialog to page

fzaninotto783cd0442e52
diff --git a/src/components/atomic-crm/layout/Header.tsx b/src/components/atomic-crm/layout/Header.tsx
index ebef817a..ae68c82b 100644
--- a/src/components/atomic-crm/layout/Header.tsx
+++ b/src/components/atomic-crm/layout/Header.tsx
@@ -1,4 +1,3 @@
-import React from "react";
import { Import, Settings, User } from "lucide-react";
import { CanAccess, useUserMenu } from "ra-core";
import { Link, matchPath, useLocation } from "react-router";
@@ -8,13 +7,11 @@ import { UserMenu } from "@/components/admin/user-menu";
import { DropdownMenuItem } from "@/components/ui/dropdown-menu";
import { useConfigurationContext } from "../root/ConfigurationContext";
-import { ImportFromJsonDialog } from "../misc/ImportFromJsonDialog";
+import { ImportPage } from "../misc/ImportPage";
const Header = () => {
const { darkModeLogo, lightModeLogo, title } = useConfigurationContext();
const location = useLocation();
- const [isImportFromJsonDialogOpen, setIsImportFromJsonOpen] =
- React.useState(false);
let currentPath: string | boolean = "/";
if (matchPath("/", location.pathname)) {
@@ -80,24 +77,16 @@ const Header = () => {
<RefreshButton />
<UserMenu>
<ConfigurationMenu />
- <ImportFromJsonMenuItem
- onClick={() => setIsImportFromJsonOpen(true)}
- />
<CanAccess resource="sales" action="list">
<UsersMenu />
</CanAccess>
+ <ImportFromJsonMenuItem />
</UserMenu>
</div>
</div>
</div>
</header>
</nav>
- {/* The dialog is rendered here instead of in ImportFromJsonMenuItem because otherwise
- it would disappear when closing the menu */}
- <ImportFromJsonDialog
- open={isImportFromJsonDialogOpen}
- onOpenChange={(open) => setIsImportFromJsonOpen(open)}
- />
</>
);
};
@@ -152,20 +141,16 @@ const ConfigurationMenu = () => {
);
};
-const ImportFromJsonMenuItem = ({ onClick }: { onClick: () => void }) => {
+const ImportFromJsonMenuItem = () => {
const userMenuContext = useUserMenu();
if (!userMenuContext) {
- throw new Error("<ImportFromJsonMenuItem> must be used inside <UserMenu?");
+ throw new Error("<ImportFromJsonMenuItem> must be used inside <UserMenu>");
}
return (
- <DropdownMenuItem
- className="flex items-center gap-2 cursor-pointer"
- onClick={() => {
- userMenuContext.onClose();
- onClick();
- }}
- >
- <Import /> Import from JSON
+ <DropdownMenuItem asChild onClick={userMenuContext.onClose}>
+ <Link to={ImportPage.path} className="flex items-center gap-2">
+ <Import /> Import data
+ </Link>
</DropdownMenuItem>
);
};
diff --git a/src/components/atomic-crm/misc/ImportFromJsonDialog.tsx b/src/components/atomic-crm/misc/ImportPage.tsx
similarity index 63%
rename from src/components/atomic-crm/misc/ImportFromJsonDialog.tsx
rename to src/components/atomic-crm/misc/ImportPage.tsx
index f12f3215..507f3a98 100644
--- a/src/components/atomic-crm/misc/ImportFromJsonDialog.tsx
+++ b/src/components/atomic-crm/misc/ImportPage.tsx
@@ -1,24 +1,8 @@
-import React from "react";
import { AlertCircleIcon } from "lucide-react";
import { Form, required } from "ra-core";
-import {
- Dialog,
- DialogClose,
- DialogContent,
- DialogFooter,
- DialogHeader,
- DialogTitle,
-} from "@/components/ui/dialog";
import { Alert, AlertDescription, AlertTitle } from "@/components/ui/alert";
import { Button } from "@/components/ui/button";
-import { FileField, FileInput } from "@/components/admin";
-import {
- type ImportFromJsonErrorState,
- type ImportFromJsonFailures,
- type ImportFromJsonFunction,
- type ImportFromJsonState,
- useImportFromJson,
-} from "./useImportFromJson";
+import { Card, CardContent, CardHeader, CardTitle } from "@/components/ui/card";
import {
Table,
TableBody,
@@ -29,69 +13,67 @@ import {
TableRow,
} from "@/components/ui/table";
import { Spinner } from "@/components/ui/spinner";
+import { FileField, FileInput } from "@/components/admin";
+import {
+ type ImportFromJsonErrorState,
+ type ImportFromJsonFailures,
+ type ImportFromJsonFunction,
+ type ImportFromJsonState,
+ useImportFromJson,
+} from "./useImportFromJson";
import sampleFile from "./import-sample.json?url";
-export const ImportFromJsonDialog = ({
- onOpenChange,
- ...props
-}: React.ComponentProps<typeof Dialog>) => {
+export const ImportPage = () => {
const [importState, importFile, reset] = useImportFromJson();
return (
- <Dialog
- onOpenChange={(open) => {
- if (importState.status !== "importing") {
- // If the import isn't running, reset its state so that users
- // may import another file after either a successful or failed import.
- reset();
- if (onOpenChange) {
- onOpenChange(open);
- }
- }
- }}
- {...props}
- >
- <DialogContent>
- <DialogHeader>
- <DialogTitle>Import from JSON</DialogTitle>
- </DialogHeader>
- {importState.status === "idle" ? (
- <ImportFromJsonIdle importFile={importFile} />
- ) : importState.status === "error" ? (
- <ImportFromJsonError
- importState={importState}
- importFile={importFile}
- />
- ) : importState.status === "importing" ? (
- <ImportFromJsonStatus importState={importState} />
- ) : (
- <ImportFromJsonSuccess importState={importState} />
- )}
- </DialogContent>
- </Dialog>
+ <div className="max-w-2xl mx-auto mt-8">
+ <Card>
+ <CardHeader>
+ <CardTitle>Import Data</CardTitle>
+ </CardHeader>
+ <CardContent>
+ {importState.status === "idle" ? (
+ <ImportFromJsonIdle importFile={importFile} />
+ ) : importState.status === "error" ? (
+ <ImportFromJsonError
+ importState={importState}
+ importFile={importFile}
+ />
+ ) : importState.status === "importing" ? (
+ <ImportFromJsonStatus importState={importState} />
+ ) : (
+ <ImportFromJsonSuccess importState={importState} reset={reset} />
+ )}
+ </CardContent>
+ </Card>
+ </div>
);
};
+ImportPage.path = "/import";
+
const ImportFromJsonIdle = ({
importFile,
}: {
importFile: ImportFromJsonFunction;
}) => (
<>
- <Alert>
- <AlertTitle>Instructions</AlertTitle>
- <AlertDescription>
- Provide a JSON file containing sales, companies, contacts, notes or
- tasks
+ <div className="mb-4">
+ <p className="text-sm">
+ You can import sales, companies, contacts, companies, notes, and tasks.
+ </p>
+ <p className="text-sm">
+ Data must be in a JSON file matching the following sample:{" "}
<a
- className="font-bold hover:underline"
+ className="underline"
download="import-sample.json"
href={sampleFile}
>
- Download a sample
+ sample.json
</a>
- </AlertDescription>
- </Alert>
+ </p>
+ </div>
<ImportFromJsonForm importFile={importFile} />
</>
);
@@ -104,7 +86,7 @@ const ImportFromJsonError = ({
importState: ImportFromJsonErrorState;
}) => (
<>
- <Alert variant="destructive">
+ <Alert variant="destructive" className="mb-4">
<AlertCircleIcon />
<AlertTitle>Unable to import this file.</AlertTitle>
<AlertDescription>
@@ -128,14 +110,9 @@ const ImportFromJsonForm = ({
<FileInput className="mt-4" source="file" validate={required()}>
<FileField source="src" title="title" />
</FileInput>
- <DialogFooter>
+ <div className="flex justify-end mt-4">
<Button type="submit">Import</Button>
- <DialogClose asChild>
- <Button variant="outline" type="button">
- Cancel
- </Button>
- </DialogClose>
- </DialogFooter>
+ </div>
</Form>
);
@@ -145,44 +122,41 @@ const ImportFromJsonStatus = ({
importState: ImportFromJsonState;
}) => (
<>
- <Alert>
- <AlertTitle>
- <Spinner /> Importing data...
- </AlertTitle>
- <AlertDescription>Do not close this browser tab</AlertDescription>
- </Alert>
+ <Spinner />
+ <p className="my-4 text-sm text-center text-muted-foreground">
+ Import in progress, please don't navigate away from this page.
+ </p>
<ImportStats importState={importState} />
</>
);
const ImportFromJsonSuccess = ({
importState,
+ reset,
}: {
importState: ImportFromJsonState;
+ reset: () => void;
}) => (
<>
- <Alert>
- <AlertTitle>Imported data</AlertTitle>
- <AlertDescription>
- <p>You can now safely close this browser tab or the dialog.</p>
- {hasFailedImports(importState.failedImports) ? (
- <>
- <p className="text-destructive">Some records were not imported.</p>
- <DownloadErrorFileButton
- failedImports={importState.failedImports}
- />
- </>
- ) : null}
- </AlertDescription>
- </Alert>
+ <p className="mb-4 text-sm">
+ Import complete.{" "}
+ {hasFailedImports(importState.failedImports) ? (
+ <>
+ <span className="text-destructive">
+ Some records were not imported.
+ </span>
+ <DownloadErrorFileButton failedImports={importState.failedImports} />
+ </>
+ ) : (
+ <span>All records were imported successfully.</span>
+ )}
+ </p>
<ImportStats importState={importState} />
- <DialogFooter>
- <DialogClose asChild>
- <Button variant="outline" type="button">
- Close
- </Button>
- </DialogClose>
- </DialogFooter>
+ <div className="flex justify-end mt-4">
+ <Button variant="outline" onClick={reset}>
+ Import another file
+ </Button>
+ </div>
</>
);
diff --git a/src/components/atomic-crm/root/CRM.tsx b/src/components/atomic-crm/root/CRM.tsx
index fe3a6687..a025b028 100644
--- a/src/components/atomic-crm/root/CRM.tsx
+++ b/src/components/atomic-crm/root/CRM.tsx
@@ -18,6 +18,7 @@ import { Dashboard } from "../dashboard/Dashboard";
import deals from "../deals";
import { Layout } from "../layout/Layout";
import { SignupPage } from "../login/SignupPage";
+import { ImportPage } from "../misc/ImportPage";
import {
authProvider as defaultAuthProvider,
dataProvider as defaultDataProvider,
@@ -154,6 +155,7 @@ export const CRM = ({
<CustomRoutes>
<Route path={SettingsPage.path} element={<SettingsPage />} />
+ <Route path={ImportPage.path} element={<ImportPage />} />
</CustomRoutes>
<Resource name="deals" {...deals} />
<Resource name="contacts" {...contacts} />