Skip to content

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

move pendingPlaceHolder logic into TasksListByDueDate to avoid one request

ThieryMichel0fe96730433e
diff --git a/src/components/atomic-crm/contacts/ContactTasksList.tsx b/src/components/atomic-crm/contacts/ContactTasksList.tsx
index 357c8a2e..5e42ed68 100644
--- a/src/components/atomic-crm/contacts/ContactTasksList.tsx
+++ b/src/components/atomic-crm/contacts/ContactTasksList.tsx
@@ -1,5 +1,5 @@
import { useState } from "react";
-import { useRecordContext, useGetList, useTimeout } from "ra-core";
+import { useRecordContext } from "ra-core";
import { Skeleton } from "@/components/ui/skeleton";
import { Button } from "@/components/ui/button";
@@ -10,49 +10,34 @@ import type { Contact } from "../types";
export const ContactTasksList = () => {
const record = useRecordContext<Contact>();
const [taskCreateOpen, setTaskCreateOpen] = useState(false);
- const oneSecondHasPassed = useTimeout(1000);
- const { total, isPending } = useGetList("tasks", {
- filter: {
- "contact_id@eq": record?.id,
- "done_date@is": null,
- },
- pagination: { page: 1, perPage: 1 },
- sort: { field: "due_date", order: "ASC" },
- });
if (!record) return null;
- const hasActiveTasks = total != null && total > 0;
-
- if (isPending) {
- if (!oneSecondHasPassed) {
- return null;
- }
- return (
- <div className="flex flex-col gap-4">
- {Array.from({ length: 3 }).map((_, index) => (
- <Skeleton className="w-full h-10" key={index} />
- ))}
- </div>
- );
- }
- if (!hasActiveTasks) {
- return (
- <>
- <TaskCreateSheet
- open={taskCreateOpen}
- onOpenChange={setTaskCreateOpen}
- contact_id={record.id}
- />
- <div className="flex flex-col items-center justify-center py-8 text-center">
- <p className="text-muted-foreground mb-4">No tasks yet</p>
- <Button variant="outline" onClick={() => setTaskCreateOpen(true)}>
- Add task
- </Button>
+ return (
+ <TasksListByDueDate
+ filterByContact={record.id}
+ emptyPlaceholder={
+ <>
+ <TaskCreateSheet
+ open={taskCreateOpen}
+ onOpenChange={setTaskCreateOpen}
+ contact_id={record.id}
+ />
+ <div className="flex flex-col items-center justify-center py-8 text-center">
+ <p className="text-muted-foreground mb-4">No tasks yet</p>
+ <Button variant="outline" onClick={() => setTaskCreateOpen(true)}>
+ Add task
+ </Button>
+ </div>
+ </>
+ }
+ pendingPlaceholder={
+ <div className="flex flex-col gap-4">
+ {Array.from({ length: 3 }).map((_, index) => (
+ <Skeleton className="w-full h-10" key={index} />
+ ))}
</div>
- </>
- );
- }
-
- return <TasksListByDueDate filterByContact={record.id} />;
+ }
+ />
+ );
};
diff --git a/src/components/atomic-crm/dashboard/TasksListByDueDate.tsx b/src/components/atomic-crm/dashboard/TasksListByDueDate.tsx
index c29a1f98..40a1ce5b 100644
--- a/src/components/atomic-crm/dashboard/TasksListByDueDate.tsx
+++ b/src/components/atomic-crm/dashboard/TasksListByDueDate.tsx
@@ -1,4 +1,9 @@
-import { type Identifier, useGetIdentity, useGetList } from "ra-core";
+import {
+ type Identifier,
+ useGetIdentity,
+ useGetList,
+ useTimeout,
+} from "ra-core";
import { useIsMobile } from "@/hooks/use-mobile";
import { TaskListFilter } from "./TasksListFilter";
@@ -16,18 +21,16 @@ import { useMemo } from "react";
export const TasksListByDueDate = ({
filterByContact,
emptyPlaceholder,
+ pendingPlaceholder,
}: {
filterByContact?: Identifier;
emptyPlaceholder?: React.ReactNode;
+ pendingPlaceholder?: React.ReactNode;
}) => {
const { identity } = useGetIdentity();
const isMobile = useIsMobile();
- const {
- data: tasks,
- total,
- isPending,
- } = useGetList(
+ const { data: tasks, isPending } = useGetList(
"tasks",
{
pagination: { page: 1, perPage: 1000 },
@@ -79,11 +82,17 @@ export const TasksListByDueDate = ({
[ongoingTasks],
);
- if (!tasks || !total || isPending) {
+ const oneSecondHasPassed = useTimeout(1000);
+
+ if (isPending && oneSecondHasPassed) {
+ return pendingPlaceholder ?? null;
+ }
+
+ if (isPending) {
return null;
}
- if (!total) {
+ if (!ongoingTasks.length) {
return emptyPlaceholder ?? null;
}