Skip to content

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

fix(contacts): keep showing a cached contact when its refetch fails on mobile

erwanMarmelab3e68ef5c6858
diff --git a/src/components/atomic-crm/contacts/ContactShow.test.tsx b/src/components/atomic-crm/contacts/ContactShow.test.tsx
index ccaa40d5..86d01baf 100644
--- a/src/components/atomic-crm/contacts/ContactShow.test.tsx
+++ b/src/components/atomic-crm/contacts/ContactShow.test.tsx
@@ -4,8 +4,10 @@ import {
useDataProvider,
type DataProvider,
} from "ra-core";
+import { useQueryClient } from "@tanstack/react-query";
import { render } from "vitest-browser-react";
-import { buildContact, StoryWrapper } from "@/test/StoryWrapper";
+import { createDataProvider } from "@/components/atomic-crm/providers/fakerest";
+import { buildContact, createCrmDb, StoryWrapper } from "@/test/StoryWrapper";
import { ContactAside } from "./ContactAside";
import { ContactShow } from "./ContactShow";
import { MobileSuccess } from "./ContactShow.mobile.stories";
@@ -75,6 +77,50 @@ describe("ContactShow", () => {
.toBeVisible();
});
+ it("keeps showing a loaded contact when refreshing it fails", async () => {
+ const contact = buildContact({ id: 405 });
+ const baseDataProvider = createDataProvider({
+ db: createCrmDb({ contacts: [contact] }),
+ silent: true,
+ });
+ let isOffline = false;
+ let refetchQueries: (() => Promise<void>) | null = null;
+
+ const QueryClientListener = () => {
+ const queryClient = useQueryClient();
+ refetchQueries = () => queryClient.refetchQueries();
+ return null;
+ };
+
+ const screen = await render(
+ <StoryWrapper
+ data={{ contacts: [contact] }}
+ dataProvider={{
+ getOne: (resource, params) =>
+ isOffline && resource === "contacts"
+ ? Promise.reject(new Error("Failed to fetch"))
+ : baseDataProvider.getOne(resource, params),
+ }}
+ >
+ <QueryClientListener />
+ <ContactShow resource="contacts" id={contact.id} />
+ </StoryWrapper>,
+ );
+ await expect
+ .element(screen.getByRole("tab", { name: "0 tasks" }))
+ .toBeVisible();
+
+ isOffline = true;
+ await refetchQueries!();
+
+ await expect
+ .element(screen.getByText("Error loading contact"))
+ .not.toBeInTheDocument();
+ await expect
+ .element(screen.getByRole("tab", { name: "0 tasks" }))
+ .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 0470c128..ca467d7f 100644
--- a/src/components/atomic-crm/contacts/ContactShow.tsx
+++ b/src/components/atomic-crm/contacts/ContactShow.tsx
@@ -47,8 +47,6 @@ export const ContactShow = (props: ShowBaseProps = {}) => {
}
: undefined,
}}
- // The error notification is disabled on mobile, so show the error here
- error={isMobile ? <ContactShowErrorMobile /> : undefined}
{...props}
>
{isMobile ? <ContactShowContentMobile /> : <ContactShowContent />}
@@ -81,10 +79,13 @@ 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, error } = useShowContext<Contact>();
const [noteCreateOpen, setNoteCreateOpen] = useState(false);
const [editOpen, setEditOpen] = useState(false);
- if (isPending || !record) return null;
+ if (isPending) return null;
+ // The error notification is disabled on mobile, so show the error here,
+ // unless a cached record is still available (e.g. offline refetch failure)
+ if (!record) return error ? <ContactShowErrorMobile /> : null;
const taskCount = record.nb_tasks ?? 0;