Skip to content

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

fix(contacts): show an error state instead of a blank page when a contact fails to load on mobile

erwanMarmelab2f16a5257dde
diff --git a/src/components/atomic-crm/contacts/ContactShow.test.tsx b/src/components/atomic-crm/contacts/ContactShow.test.tsx
index e0dafdea..befd68eb 100644
--- a/src/components/atomic-crm/contacts/ContactShow.test.tsx
+++ b/src/components/atomic-crm/contacts/ContactShow.test.tsx
@@ -62,6 +62,22 @@ describe("ContactShow", () => {
.toHaveAttribute("aria-selected", "true");
});
+ it("shows an error with a retry button when the contact cannot be loaded", async () => {
+ const screen = await render(
+ <StoryWrapper data={{ contacts: [] }}>
+ {/* An id no other test loads, so no persisted query cache can serve it */}
+ <ContactShow resource="contacts" id={404} />
+ </StoryWrapper>,
+ );
+
+ await expect
+ .element(screen.getByText("Error loading contact"))
+ .toBeVisible();
+ await expect
+ .element(screen.getByRole("button", { name: "Retry" }))
+ .toBeVisible();
+ });
+
it("updates the contact status from the aside", async () => {
mockIsMobile.mockReturnValue(false);
diff --git a/src/components/atomic-crm/contacts/ContactShow.tsx b/src/components/atomic-crm/contacts/ContactShow.tsx
index d0e7ebd9..96f35723 100644
--- a/src/components/atomic-crm/contacts/ContactShow.tsx
+++ b/src/components/atomic-crm/contacts/ContactShow.tsx
@@ -14,7 +14,7 @@ import { Card, CardContent } from "@/components/ui/card";
import { Tabs, TabsList, TabsTrigger, TabsContent } from "@/components/ui/tabs";
import { Button } from "@/components/ui/button";
import { Separator } from "@/components/ui/separator";
-import { Pencil } from "lucide-react";
+import { Pencil, RotateCcw } from "lucide-react";
import { Link, useSearchParams } from "react-router";
import MobileHeader from "../layout/MobileHeader";
@@ -63,10 +63,34 @@ const ContactShowContentMobile = () => {
const requestedTab = searchParams.get("tab");
const initialTab =
requestedTab && MOBILE_TABS.includes(requestedTab) ? requestedTab : "notes";
- const { defaultTitle, record, isPending } = useShowContext<Contact>();
+ const { defaultTitle, record, isPending, refetch } =
+ useShowContext<Contact>();
const [noteCreateOpen, setNoteCreateOpen] = useState(false);
const [editOpen, setEditOpen] = useState(false);
- if (isPending || !record) return null;
+ if (isPending) return null;
+ if (!record) {
+ // The error notification is disabled on mobile, so show the error here
+ return (
+ <>
+ <MobileHeader>
+ <MobileBackButton />
+ </MobileHeader>
+ <MobileContent>
+ <div className="p-4">
+ <div className="text-center text-muted-foreground mb-4">
+ {translate("resources.contacts.show.error_loading")}
+ </div>
+ <div className="text-center mt-2">
+ <Button onClick={() => refetch()}>
+ <RotateCcw />
+ {translate("crm.common.retry")}
+ </Button>
+ </div>
+ </div>
+ </MobileContent>
+ </>
+ );
+ }
const taskCount = record.nb_tasks ?? 0;
diff --git a/src/components/atomic-crm/providers/commons/englishCrmMessages.ts b/src/components/atomic-crm/providers/commons/englishCrmMessages.ts
index 847ab895..776f47b9 100644
--- a/src/components/atomic-crm/providers/commons/englishCrmMessages.ts
+++ b/src/components/atomic-crm/providers/commons/englishCrmMessages.ts
@@ -130,6 +130,9 @@ export const englishCrmMessages = {
list: {
error_loading: "Error loading contacts",
},
+ show: {
+ error_loading: "Error loading contact",
+ },
bulk_tag: {
action: "Tag",
back: "Back to tags",
diff --git a/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts b/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts
index 41ebc5c9..f4dc4a6e 100644
--- a/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts
+++ b/src/components/atomic-crm/providers/commons/frenchCrmMessages.ts
@@ -133,6 +133,9 @@ export const frenchCrmMessages = {
list: {
error_loading: "Erreur lors du chargement des contacts",
},
+ show: {
+ error_loading: "Erreur lors du chargement du contact",
+ },
bulk_tag: {
action: "Étiqueter",
back: "Retour aux étiquettes",