Skip to content

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

Refactor ImportFromJsonDialog for clarity

React-Admin CIa48b2025c1e8
diff --git a/src/components/atomic-crm/layout/Header.tsx b/src/components/atomic-crm/layout/Header.tsx
index 97c6c9b9..62118fc4 100644
--- a/src/components/atomic-crm/layout/Header.tsx
+++ b/src/components/atomic-crm/layout/Header.tsx
@@ -1,12 +1,11 @@
import React from "react";
import { Import, Settings, User } from "lucide-react";
-import { CanAccess } from "ra-core";
+import { CanAccess, useUserMenu } from "ra-core";
import { Link, matchPath, useLocation } from "react-router";
import { RefreshButton } from "@/components/admin/refresh-button";
import { ThemeModeToggle } from "@/components/admin/theme-mode-toggle";
import { UserMenu } from "@/components/admin/user-menu";
import { DropdownMenuItem } from "@/components/ui/dropdown-menu";
-import { useUserMenu } from "@/hooks/user-menu-context";
import { useConfigurationContext } from "../root/ConfigurationContext";
import { ImportFromJsonDialog } from "../misc/ImportFromJsonDialog";
@@ -81,7 +80,7 @@ const Header = () => {
<RefreshButton />
<UserMenu>
<ConfigurationMenu />
- <ImportFromJsonButton
+ <ImportFromJsonMenuItem
onClick={() => setIsImportFromJsonOpen(true)}
/>
<CanAccess resource="sales" action="list">
@@ -93,6 +92,8 @@ const Header = () => {
</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)}
@@ -123,9 +124,12 @@ const NavigationTab = ({
);
const UsersMenu = () => {
- const { onClose } = useUserMenu() ?? {};
+ const userMenuContext = useUserMenu();
+ if (!userMenuContext) {
+ throw new Error("<UsersMenu> must be used inside <UserMenu?");
+ }
return (
- <DropdownMenuItem asChild onClick={onClose}>
+ <DropdownMenuItem asChild onClick={userMenuContext.onClose}>
<Link to="/sales" className="flex items-center gap-2">
<User /> Users
</Link>
@@ -134,9 +138,12 @@ const UsersMenu = () => {
};
const ConfigurationMenu = () => {
- const { onClose } = useUserMenu() ?? {};
+ const userMenuContext = useUserMenu();
+ if (!userMenuContext) {
+ throw new Error("<ConfigurationMenu> must be used inside <UserMenu?");
+ }
return (
- <DropdownMenuItem asChild onClick={onClose}>
+ <DropdownMenuItem asChild onClick={userMenuContext.onClose}>
<Link to="/settings" className="flex items-center gap-2">
<Settings />
My info
@@ -145,13 +152,16 @@ const ConfigurationMenu = () => {
);
};
-const ImportFromJsonButton = ({ onClick }: { onClick: () => void }) => {
- const { onClose } = useUserMenu() ?? { onClose: () => {} };
+const ImportFromJsonMenuItem = ({ onClick }: { onClick: () => void }) => {
+ const userMenuContext = useUserMenu();
+ if (!userMenuContext) {
+ throw new Error("<ImportFromJsonMenuItem> must be used inside <UserMenu?");
+ }
return (
<DropdownMenuItem
className="flex items-center gap-2 cursor-pointer"
onClick={() => {
- onClose();
+ userMenuContext.onClose();
onClick();
}}
>
diff --git a/src/components/atomic-crm/misc/ImportFromJsonDialog.tsx b/src/components/atomic-crm/misc/ImportFromJsonDialog.tsx
index 117fe180..298a07aa 100644
--- a/src/components/atomic-crm/misc/ImportFromJsonDialog.tsx
+++ b/src/components/atomic-crm/misc/ImportFromJsonDialog.tsx
@@ -13,8 +13,10 @@ 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 ImportFromJsonStats,
+ type ImportFromJsonFunction,
+ type ImportFromJsonState,
useImportFromJson,
} from "./useImportFromJson";
import {
@@ -33,12 +35,14 @@ export const ImportFromJsonDialog = ({
onOpenChange,
...props
}: React.ComponentProps<typeof Dialog>) => {
- const [importStatus, importFile, reset] = useImportFromJson();
+ const [importState, importFile, reset] = useImportFromJson();
return (
<Dialog
onOpenChange={(open) => {
- if (importStatus.status !== "importing") {
+ 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);
@@ -51,99 +55,137 @@ export const ImportFromJsonDialog = ({
<DialogHeader>
<DialogTitle>Import from JSON</DialogTitle>
</DialogHeader>
- {importStatus.status === "idle" || importStatus.status === "error" ? (
- <Form
- onSubmit={(values: any) => {
- importFile(values.file.rawFile);
- }}
- >
- {importStatus.status === "error" ? (
- <Alert variant="destructive">
- <AlertCircleIcon />
- <AlertTitle>Unable to import this file.</AlertTitle>
- <AlertDescription>
- <p>{importStatus.error.message}</p>
- </AlertDescription>
- </Alert>
- ) : (
- <Alert>
- <AlertTitle>Instructions</AlertTitle>
- <AlertDescription>
- Provide a JSON file containing sales, companies, contacts,
- notes or tasks
- <a
- className="font-bold hover:underline"
- download="import-sample.json"
- href={sampleFile}
- >
- Download a sample
- </a>
- </AlertDescription>
- </Alert>
- )}
- <FileInput className="mt-4" source="file" validate={required()}>
- <FileField source="src" title="title" />
- </FileInput>
- <DialogFooter>
- <Button type="submit">Import</Button>
- <DialogClose asChild>
- <Button variant="outline" type="button">
- Cancel
- </Button>
- </DialogClose>
- </DialogFooter>
- </Form>
+ {importState.status === "idle" ? (
+ <ImportFromJsonIdle importFile={importFile} />
+ ) : importState.status === "error" ? (
+ <ImportFromJsonError
+ importState={importState}
+ importFile={importFile}
+ />
+ ) : importState.status === "importing" ? (
+ <ImportFromJsonStatus importState={importState} />
) : (
- <>
- {importStatus.status === "importing" ? (
- <Alert>
- <AlertTitle>
- <Spinner /> Importing data...
- </AlertTitle>
- <AlertDescription>
- Do not close this browser tab
- </AlertDescription>
- </Alert>
- ) : (
- <Alert>
- <AlertTitle>Imported data</AlertTitle>
- <AlertDescription>
- <p>
- You can now safely close this browser tab or the dialog.
- </p>
- {hasFailedImports(importStatus.failedImports) ? (
- <>
- <p className="text-destructive">
- Some records were not imported.
- </p>
- <DownloadErrorFileButton
- failedImports={importStatus.failedImports}
- />
- </>
- ) : null}
- </AlertDescription>
- </Alert>
- )}
- <ImportStats
- stats={importStatus.stats}
- failedImports={importStatus.failedImports}
- />
- {importStatus.status === "success" ? (
- <DialogFooter>
- <DialogClose asChild>
- <Button variant="outline" type="button">
- Close
- </Button>
- </DialogClose>
- </DialogFooter>
- ) : null}
- </>
+ <ImportFromJsonSuccess importState={importState} />
)}
</DialogContent>
</Dialog>
);
};
+const ImportFromJsonIdle = ({
+ importFile,
+}: {
+ importFile: ImportFromJsonFunction;
+}) => (
+ <>
+ <Alert>
+ <AlertTitle>Instructions</AlertTitle>
+ <AlertDescription>
+ Provide a JSON file containing sales, companies, contacts, notes or
+ tasks
+ <a
+ className="font-bold hover:underline"
+ download="import-sample.json"
+ href={sampleFile}
+ >
+ Download a sample
+ </a>
+ </AlertDescription>
+ </Alert>
+ <ImportFromJsonForm importFile={importFile} />
+ </>
+);
+
+const ImportFromJsonError = ({
+ importState,
+ importFile,
+}: {
+ importFile: ImportFromJsonFunction;
+ importState: ImportFromJsonErrorState;
+}) => (
+ <>
+ <Alert variant="destructive">
+ <AlertCircleIcon />
+ <AlertTitle>Unable to import this file.</AlertTitle>
+ <AlertDescription>
+ <p>{importState.error.message}</p>
+ </AlertDescription>
+ </Alert>
+ <ImportFromJsonForm importFile={importFile} />
+ </>
+);
+
+const ImportFromJsonForm = ({
+ importFile,
+}: {
+ importFile: ImportFromJsonFunction;
+}) => (
+ <Form
+ onSubmit={(values: any) => {
+ importFile(values.file.rawFile);
+ }}
+ >
+ <FileInput className="mt-4" source="file" validate={required()}>
+ <FileField source="src" title="title" />
+ </FileInput>
+ <DialogFooter>
+ <Button type="submit">Import</Button>
+ <DialogClose asChild>
+ <Button variant="outline" type="button">
+ Cancel
+ </Button>
+ </DialogClose>
+ </DialogFooter>
+ </Form>
+);
+
+const ImportFromJsonStatus = ({
+ importState,
+}: {
+ importState: ImportFromJsonState;
+}) => (
+ <>
+ <Alert>
+ <AlertTitle>
+ <Spinner /> Importing data...
+ </AlertTitle>
+ <AlertDescription>Do not close this browser tab</AlertDescription>
+ </Alert>
+ <ImportStats importState={importState} />
+ </>
+);
+
+const ImportFromJsonSuccess = ({
+ importState,
+}: {
+ importState: ImportFromJsonState;
+}) => (
+ <>
+ <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>
+ <ImportStats importState={importState} />
+ <DialogFooter>
+ <DialogClose asChild>
+ <Button variant="outline" type="button">
+ Close
+ </Button>
+ </DialogClose>
+ </DialogFooter>
+ </>
+);
+
const hasFailedImports = (failedImports: ImportFromJsonFailures) => {
return (
failedImports.sales.length > 0 ||
@@ -171,11 +213,9 @@ const DownloadErrorFileButton = ({
};
const ImportStats = ({
- stats,
- failedImports,
+ importState: { stats, failedImports },
}: {
- stats: ImportFromJsonStats;
- failedImports: ImportFromJsonFailures;
+ importState: ImportFromJsonState;
}) => {
const data = [
{