Skip to content

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

WIP(crm): Update Activity and Dashboard

Anthony Rimetc97e7fd92484
diff --git a/src/activity/ActivityLog.tsx b/src/activity/ActivityLog.tsx
index 4d348e08..bf808ee5 100644
--- a/src/activity/ActivityLog.tsx
+++ b/src/activity/ActivityLog.tsx
@@ -1,15 +1,22 @@
import { Alert, Divider, Skeleton, Stack } from '@mui/material';
import { useQuery } from '@tanstack/react-query';
import { Identifier, useDataProvider } from 'react-admin';
+
import { CustomDataProvider } from '../dataProvider';
+import { ActivityLogContext } from './ActivityLogContext';
import { ActivityLogIterator } from './ActivityLogIterator';
type ActivityLogProps = {
companyId?: Identifier;
pageSize?: number;
+ context?: 'company' | 'contact' | 'deal' | 'all';
};
-export function ActivityLog({ companyId, pageSize = 20 }: ActivityLogProps) {
+export function ActivityLog({
+ companyId,
+ pageSize = 20,
+ context = 'all',
+}: ActivityLogProps) {
const dataProvider = useDataProvider<CustomDataProvider>();
const { data, isPending, error } = useQuery({
queryKey: ['activityLog', companyId],
@@ -18,7 +25,7 @@ export function ActivityLog({ companyId, pageSize = 20 }: ActivityLogProps) {
if (isPending) {
return (
- <Stack p={2}>
+ <Stack mt={0.5}>
{Array.from({ length: 5 }).map((_, index) => (
<Stack spacing={2} sx={{ mt: 1 }} key={index}>
<Stack
@@ -42,8 +49,12 @@ export function ActivityLog({ companyId, pageSize = 20 }: ActivityLogProps) {
}
if (error) {
- return <Alert severity="error">Failed to load acticity log</Alert>;
+ return <Alert severity="error">Failed to load activity log</Alert>;
}
- return <ActivityLogIterator activities={data} pageSize={pageSize} />;
+ return (
+ <ActivityLogContext.Provider value={context}>
+ <ActivityLogIterator activities={data} pageSize={pageSize} />
+ </ActivityLogContext.Provider>
+ );
}
diff --git a/src/activity/ActivityLogCompanyCreated.tsx b/src/activity/ActivityLogCompanyCreated.tsx
index a42dbf63..477097e2 100644
--- a/src/activity/ActivityLogCompanyCreated.tsx
+++ b/src/activity/ActivityLogCompanyCreated.tsx
@@ -1,10 +1,11 @@
-import ListItem from '@mui/material/ListItem';
-import Stack from '@mui/material/Stack';
-import Typography from '@mui/material/Typography';
-import { Link, RecordContextProvider } from 'react-admin';
+import { ListItem, Stack, Typography } from '@mui/material';
+import { Link } from 'react-admin';
+
import { CompanyAvatar } from '../companies/CompanyAvatar';
+import { SaleName } from '../sales/SaleName';
import type { ActivityCompanyCreated } from '../types';
-import { ActivityLogDate } from './ActivityLogDate';
+import { RelativeDate } from '../misc/RelativeDate';
+import { useActivityLogContext } from './ActivityLogContext';
type ActivityLogCompanyCreatedProps = {
activity: ActivityCompanyCreated;
@@ -13,39 +14,38 @@ type ActivityLogCompanyCreatedProps = {
export function ActivityLogCompanyCreated({
activity: { sale, company },
}: ActivityLogCompanyCreatedProps) {
+ const context = useActivityLogContext();
return (
- <RecordContextProvider value={company}>
- <ListItem disableGutters>
- <Stack
- direction="row"
- spacing={2}
- sx={{
- alignItems: 'center',
- width: '100%',
- }}
+ <ListItem disableGutters>
+ <Stack direction="row" spacing={1} alignItems="center" width="100%">
+ <CompanyAvatar width={20} height={20} record={company} />
+ <Typography
+ component="p"
+ variant="body2"
+ color="text.secondary"
+ flexGrow={1}
>
- <CompanyAvatar width={20} height={20} />
+ <SaleName sale={sale} /> added company{' '}
+ <Link to={`/companies/${company.id}/show`}>
+ {company.name}
+ </Link>
+ {context === 'all' && (
+ <>
+ {' '}
+ <RelativeDate date={company.created_at} />
+ </>
+ )}
+ </Typography>
+ {context === 'company' && (
<Typography
- component="p"
- sx={{
- flexGrow: 1,
- }}
+ color="textSecondary"
variant="body2"
- color="text.secondary"
+ component="span"
>
- {sale.first_name} {sale.last_name} added company{' '}
- <Link
- component={Link}
- to={`/companies/${company.id}/show`}
- variant="body2"
- >
- {company.name}
- </Link>
+ <RelativeDate date={company.created_at} />
</Typography>
-
- <ActivityLogDate date={company.created_at} />
- </Stack>
- </ListItem>
- </RecordContextProvider>
+ )}
+ </Stack>
+ </ListItem>
);
}
diff --git a/src/activity/ActivityLogContactCreated.tsx b/src/activity/ActivityLogContactCreated.tsx
index 4f9ff121..7b8592b9 100644
--- a/src/activity/ActivityLogContactCreated.tsx
+++ b/src/activity/ActivityLogContactCreated.tsx
@@ -1,10 +1,11 @@
-import ListItem from '@mui/material/ListItem';
-import Stack from '@mui/material/Stack';
-import Typography from '@mui/material/Typography';
-import { Link, RecordContextProvider } from 'react-admin';
+import { ListItem, Stack, Typography } from '@mui/material';
+import { Link } from 'react-admin';
+
import { Avatar } from '../contacts/Avatar';
import type { ActivityContactCreated } from '../types';
-import { ActivityLogDate } from './ActivityLogDate';
+import { SaleName } from '../sales/SaleName';
+import { RelativeDate } from '../misc/RelativeDate';
+import { useActivityLogContext } from './ActivityLogContext';
type ActivityLogContactCreatedProps = {
activity: ActivityContactCreated;
@@ -13,47 +14,41 @@ type ActivityLogContactCreatedProps = {
export function ActivityLogContactCreated({
activity: { sale, contact, company },
}: ActivityLogContactCreatedProps) {
+ const context = useActivityLogContext();
return (
- <RecordContextProvider value={contact}>
- <ListItem disableGutters>
- <Stack
- direction="row"
- spacing={2}
- sx={{
- alignItems: 'center',
- width: '100%',
- }}
+ <ListItem disableGutters>
+ <Stack direction="row" spacing={1} alignItems="center" width="100%">
+ <Avatar width={20} height={20} record={contact} />
+ <Typography
+ component="p"
+ variant="body2"
+ color="text.secondary"
+ flexGrow={1}
>
- <Avatar width={20} height={20} />
+ <SaleName sale={sale} /> added{' '}
+ <Link to={`/contacts/${contact.id}/show`}>
+ {contact.first_name} {contact.last_name}
+ </Link>{' '}
+ {context !== 'company' && (
+ <>
+ to{' '}
+ <Link to={`/companies/${contact.company_id}/show`}>
+ {company.name}
+ </Link>{' '}
+ <RelativeDate date={contact.first_seen} />
+ </>
+ )}
+ </Typography>
+ {context === 'company' && (
<Typography
- component="p"
- sx={{
- flexGrow: 1,
- }}
+ color="textSecondary"
variant="body2"
- color="text.secondary"
+ component="span"
>
- {sale.first_name} {sale.last_name} added{' '}
- <Link
- component={Link}
- to={`/contacts/${contact.id}/show`}
- variant="body2"
- >
- {contact.first_name} {contact.last_name}
- </Link>{' '}
- to{' '}
- <Link
- component={Link}
- to={`/companies/${contact.company_id}/show`}
- variant="body2"
- >
- {company.name}
- </Link>
+ <RelativeDate date={contact.first_seen} />
</Typography>
-
- <ActivityLogDate date={contact.first_seen} />
- </Stack>
- </ListItem>
- </RecordContextProvider>
+ )}
+ </Stack>
+ </ListItem>
);
}
diff --git a/src/activity/ActivityLogContactNoteCreated.tsx b/src/activity/ActivityLogContactNoteCreated.tsx
index fd09fbc4..65b3190d 100644
--- a/src/activity/ActivityLogContactNoteCreated.tsx
+++ b/src/activity/ActivityLogContactNoteCreated.tsx
@@ -1,9 +1,12 @@
-import Typography from '@mui/material/Typography';
-import { Link, RecordContextProvider } from 'react-admin';
+import { Typography } from '@mui/material';
+import { Link } from 'react-admin';
+
import { Avatar } from '../contacts/Avatar';
import type { ActivityContactNoteCreated } from '../types';
-import { ActivityLogDate } from './ActivityLogDate';
+import { SaleName } from '../sales/SaleName';
import { ActivityLogNote } from './ActivityLogNote';
+import { RelativeDate } from '../misc/RelativeDate';
+import { useActivityLogContext } from './ActivityLogContext';
type ActivityLogContactNoteCreatedProps = {
activity: ActivityContactNoteCreated;
@@ -12,44 +15,44 @@ type ActivityLogContactNoteCreatedProps = {
export function ActivityLogContactNoteCreated({
activity: { sale, contact, contactNote, company },
}: ActivityLogContactNoteCreatedProps) {
+ const context = useActivityLogContext();
return (
- <RecordContextProvider value={contact}>
- <ActivityLogNote
- header={
- <>
- <Avatar width={20} height={20} />
+ <ActivityLogNote
+ header={
+ <>
+ <Avatar width={20} height={20} record={contact} />
+ <Typography
+ component="p"
+ variant="body2"
+ color="text.secondary"
+ flexGrow={1}
+ >
+ <SaleName sale={sale} /> added a note about{' '}
+ <Link to={`/contacts/${contact.id}/show`}>
+ {contact.first_name} {contact.last_name}
+ </Link>
+ {context !== 'company' && (
+ <>
+ {' from '}
+ <Link to={`/companies/${company.id}/show`}>
+ {company.name}
+ </Link>{' '}
+ <RelativeDate date={contactNote.date} />
+ </>
+ )}
+ </Typography>
+ {context === 'company' && (
<Typography
- component="p"
- sx={{
- flexGrow: 1,
- }}
+ color="textSecondary"
variant="body2"
- color="text.secondary"
+ component="span"
>
- {sale.first_name} {sale.last_name} added note about{' '}
- <Link
- to={`/contacts/${contact.id}/show`}
- variant="body2"
- >
- {contact.first_name} {contact.last_name}
- </Link>{' '}
- from{' '}
- <Link
- component={Link}
- to={`/companies/${company.id}/show`}
- variant="body2"
- >
- {company.name}
- </Link>
+ <RelativeDate date={contactNote.date} />
</Typography>
-
- <ActivityLogDate
- date={contactNote.date.toISOString()}
- />
- </>
- }
- text={contactNote.text}
- />
- </RecordContextProvider>
+ )}
+ </>
+ }
+ text={contactNote.text}
+ />
);
}
diff --git a/src/activity/ActivityLogContext.tsx b/src/activity/ActivityLogContext.tsx
new file mode 100644
index 00000000..e43f5561
--- /dev/null
+++ b/src/activity/ActivityLogContext.tsx
@@ -0,0 +1,11 @@
+import { createContext, useContext } from 'react';
+
+export type activityLogContextValue = 'company' | 'contact' | 'deal' | 'all';
+
+export const ActivityLogContext = createContext<activityLogContextValue>('all');
+
+export const useActivityLogContext = () => {
+ const context = useContext(ActivityLogContext);
+
+ return context;
+};
diff --git a/src/activity/ActivityLogDate.tsx b/src/activity/ActivityLogDate.tsx
deleted file mode 100644
index 4362b2b9..00000000
--- a/src/activity/ActivityLogDate.tsx
+++ /dev/null
@@ -1,28 +0,0 @@
-import Typography from '@mui/material/Typography';
-
-export function ActivityLogDate({ date }: { date: string }) {
- const now = new Date();
- const d = new Date(date);
-
- if (
- d.getDay() === now.getDay() &&
- d.getMonth() === now.getMonth() &&
- d.getFullYear() === now.getFullYear()
- ) {
- return (
- <Typography color="textSecondary" variant="body2">
- {new Intl.DateTimeFormat('en-US', {
- timeStyle: 'short',
- }).format(d)}
- </Typography>
- );
- }
-
- return (
- <Typography color="textSecondary" variant="body2">
- {new Intl.DateTimeFormat('en-US', {
- dateStyle: 'short',
- }).format(d)}
- </Typography>
- );
-}
diff --git a/src/activity/ActivityLogDealCreated.tsx b/src/activity/ActivityLogDealCreated.tsx
index 1614b3f3..ef8157fe 100644
--- a/src/activity/ActivityLogDealCreated.tsx
+++ b/src/activity/ActivityLogDealCreated.tsx
@@ -1,10 +1,11 @@
-import ListItem from '@mui/material/ListItem';
-import Stack from '@mui/material/Stack';
-import Typography from '@mui/material/Typography';
-import { Link, RecordContextProvider } from 'react-admin';
+import { ListItem, Stack, Typography } from '@mui/material';
+import { Link } from 'react-admin';
+
import { CompanyAvatar } from '../companies/CompanyAvatar';
import type { ActivityDealCreated } from '../types';
-import { ActivityLogDate } from './ActivityLogDate';
+import { SaleName } from '../sales/SaleName';
+import { RelativeDate } from '../misc/RelativeDate';
+import { useActivityLogContext } from './ActivityLogContext';
type ActivityLogDealCreatedProps = {
activity: ActivityDealCreated;
@@ -13,42 +14,39 @@ type ActivityLogDealCreatedProps = {
export function ActivityLogDealCreated({
activity: { sale, deal, company },
}: ActivityLogDealCreatedProps) {
+ const context = useActivityLogContext();
return (
- <RecordContextProvider value={company}>
- <ListItem disableGutters>
- <Stack
- direction="row"
- spacing={2}
- sx={{
- alignItems: 'center',
- width: '100%',
- }}
+ <ListItem disableGutters>
+ <Stack direction="row" spacing={1} alignItems="center" width="100%">
+ <CompanyAvatar width={20} height={20} record={company} />
+ <Typography
+ component="p"
+ variant="body2"
+ color="text.secondary"
+ flexGrow={1}
>
- <CompanyAvatar width={20} height={20} />
+ <SaleName sale={sale} /> added deal{' '}
+ <Link to={`/deals/${deal.id}/show`}>{deal.name}</Link>{' '}
+ {context !== 'company' && (
+ <>
+ to company{' '}
+ <Link to={`/companies/${company.id}/show`}>
+ {company.name}
+ </Link>{' '}
+ <RelativeDate date={deal.created_at} />
+ </>
+ )}
+ </Typography>
+ {context === 'company' && (
<Typography
- component="p"
- sx={{
- flexGrow: 1,
- }}
+ color="textSecondary"
variant="body2"
- color="text.secondary"
+ component="span"
>
- {sale.first_name} {sale.last_name} added deal{' '}
- <Link to={`/deals/${deal.id}/show`} variant="body2">
- {deal.name}
- </Link>{' '}
- to company{' '}
- <Link
- to={`/companies/${company.id}/show`}
- variant="body2"
- >
- {company.name}
- </Link>
+ <RelativeDate date={deal.created_at} />
</Typography>
-
- <ActivityLogDate date={deal.created_at} />
- </Stack>
- </ListItem>
- </RecordContextProvider>
+ )}
+ </Stack>
+ </ListItem>
);
}
diff --git a/src/activity/ActivityLogDealNoteCreated.tsx b/src/activity/ActivityLogDealNoteCreated.tsx
index ee40e1c4..8d8db5a3 100644
--- a/src/activity/ActivityLogDealNoteCreated.tsx
+++ b/src/activity/ActivityLogDealNoteCreated.tsx
@@ -1,9 +1,12 @@
import Typography from '@mui/material/Typography';
-import { Link, RecordContextProvider } from 'react-admin';
+import { Link } from 'react-admin';
+
import { CompanyAvatar } from '../companies/CompanyAvatar';
import type { ActivityDealNoteCreated } from '../types';
-import { ActivityLogDate } from './ActivityLogDate';
+import { SaleName } from '../sales/SaleName';
+import { RelativeDate } from '../misc/RelativeDate';
import { ActivityLogNote } from './ActivityLogNote';
+import { useActivityLogContext } from './ActivityLogContext';
type ActivityLogDealNoteCreatedProps = {
activity: ActivityDealNoteCreated;
@@ -12,40 +15,42 @@ type ActivityLogDealNoteCreatedProps = {
export function ActivityLogDealNoteCreated({
activity: { company, sale, deal, dealNote },
}: ActivityLogDealNoteCreatedProps) {
+ const context = useActivityLogContext();
return (
- <RecordContextProvider value={company}>
- <ActivityLogNote
- header={
- <>
- <CompanyAvatar width={20} height={20} />
+ <ActivityLogNote
+ header={
+ <>
+ <CompanyAvatar width={20} height={20} record={company} />
+ <Typography
+ component="p"
+ variant="body2"
+ color="text.secondary"
+ flexGrow={1}
+ >
+ <SaleName sale={sale} /> added a note about deal{' '}
+ <Link to={`/deals/${deal.id}/show`}>{deal.name}</Link>
+ {context !== 'company' && (
+ <>
+ {' at '}
+ <Link to={`/companies/${deal.company_id}/show`}>
+ {company.name}
+ </Link>{' '}
+ <RelativeDate date={dealNote.date} />
+ </>
+ )}
+ </Typography>
+ {context === 'company' && (
<Typography
- component="p"
- sx={{
- flexGrow: 1,
- }}
+ color="textSecondary"
variant="body2"
- color="text.secondary"
+ component="span"
>
- {sale.first_name} {sale.last_name} added note about
- deal{' '}
- <Link to={`/deals/${deal.id}/show`} variant="body2">
- {deal.name}
- </Link>{' '}
- at{' '}
- <Link
- component={Link}
- to={`/companies/${deal.company_id}/show`}
- variant="body2"
- >
- {company.name}
- </Link>
+ <RelativeDate date={dealNote.date} />
</Typography>
-
- <ActivityLogDate date={dealNote.date.toISOString()} />
- </>
- }
- text={dealNote.text}
- />
- </RecordContextProvider>
+ )}
+ </>
+ }
+ text={dealNote.text}
+ />
);
}
diff --git a/src/activity/ActivityLogIterator.tsx b/src/activity/ActivityLogIterator.tsx
index 871e753e..f0654561 100644
--- a/src/activity/ActivityLogIterator.tsx
+++ b/src/activity/ActivityLogIterator.tsx
@@ -1,5 +1,5 @@
import { Button, Divider, List } from '@mui/material';
-import { useState } from 'react';
+import { useState, Fragment } from 'react';
import {
COMPANY_CREATED,
CONTACT_CREATED,
@@ -34,16 +34,16 @@ export function ActivityLogIterator({
marginTop: 0,
marginBottom: 1,
},
- '& .MuiListItem-root:not(:first-child)': {
+ '& .MuiListItem-root:not(:first-of-type)': {
marginTop: 1,
},
}}
>
- {filteredActivities.map(activity => (
- <>
+ {filteredActivities.map((activity, index) => (
+ <Fragment key={index}>
<ActivityItem key={activity.id} activity={activity} />
<Divider />
- </>
+ </Fragment>
))}
{activitiesDisplayed < activities.length && (
diff --git a/src/activity/ActivityLogNote.tsx b/src/activity/ActivityLogNote.tsx
index e5411ee2..d1afba8e 100644
--- a/src/activity/ActivityLogNote.tsx
+++ b/src/activity/ActivityLogNote.tsx
@@ -1,8 +1,5 @@
-import Link from '@mui/material/Link';
-import ListItem from '@mui/material/ListItem';
-import Stack from '@mui/material/Stack';
-import Typography from '@mui/material/Typography';
-import { MouseEventHandler, ReactNode, useState } from 'react';
+import { ListItem, Stack, Typography } from '@mui/material';
+import { ReactNode, Fragment } from 'react';
type ActivityLogContactNoteCreatedProps = {
header: ReactNode;
@@ -13,49 +10,40 @@ export function ActivityLogNote({
header,
text,
}: ActivityLogContactNoteCreatedProps) {
- const [seeMore, setSeeMore] = useState(false);
-
- const maxDisplayLength = 600;
-
- const slicedText =
- text.length > maxDisplayLength ? text.slice(0, maxDisplayLength) : null;
-
- const handleToggleSeeMore: MouseEventHandler = e => {
- e.preventDefault();
- setSeeMore(oldSeeMore => !oldSeeMore);
- };
+ if (!text) {
+ return null;
+ }
+ const paragraphs = text.split('\n');
return (
<ListItem disableGutters>
<Stack direction="column" spacing={2} sx={{ width: '100%' }}>
<Stack
direction="row"
- spacing={2}
- sx={{
- alignItems: 'center',
- width: '100%',
- }}
+ spacing={1}
+ alignItems="flex-start"
+ width="100%"
>
{header}
</Stack>
-
- <Typography variant="body2">
- {slicedText && !seeMore ? (
- <>
- {slicedText}
- ...{' '}
- <Link
- href="#"
- variant="body2"
- onClick={handleToggleSeeMore}
- >
- see more
- </Link>
- </>
- ) : (
- text
- )}
- </Typography>
+ <div>
+ <Typography
+ variant="body2"
+ sx={{
+ display: '-webkit-box',
+ WebkitLineClamp: '3',
+ WebkitBoxOrient: 'vertical',
+ overflow: 'hidden',
+ }}
+ >
+ {paragraphs.map((paragraph: string, index: number) => (
+ <Fragment key={index}>
+ {paragraph}
+ {index < paragraphs.length - 1 && <br />}
+ </Fragment>
+ ))}
+ </Typography>
+ </div>
</Stack>
</ListItem>
);
diff --git a/src/dashboard/DashboardActivityLog.tsx b/src/dashboard/DashboardActivityLog.tsx
index 0d7d5390..057820c3 100644
--- a/src/dashboard/DashboardActivityLog.tsx
+++ b/src/dashboard/DashboardActivityLog.tsx
@@ -3,21 +3,22 @@ import Box from '@mui/material/Box';
import Card from '@mui/material/Card';
import Typography from '@mui/material/Typography';
import { ActivityLog } from '../activity/ActivityLog';
+import { Stack } from '@mui/material';
export function DashboardActivityLog() {
return (
- <>
- <Box display="flex" alignItems="center" marginBottom="1em">
- <Box ml={2} mr={2} display="flex">
- <AccessTimeIcon color="disabled" fontSize="large" />
+ <Stack>
+ <Box display="flex" alignItems="center" mb={1}>
+ <Box mr={1} display="flex">
+ <AccessTimeIcon color="disabled" fontSize="medium" />
</Box>
<Typography variant="h5" color="textSecondary">
Latest Activity
</Typography>
</Box>
- <Card sx={{ mb: 2 }}>
+ <Card sx={{ mb: 2, px: 2 }}>
<ActivityLog pageSize={10} />
</Card>
- </>
+ </Stack>
);
}
diff --git a/src/dashboard/DealsChart.tsx b/src/dashboard/DealsChart.tsx
index 39d4c43b..2dc7f5a0 100644
--- a/src/dashboard/DealsChart.tsx
+++ b/src/dashboard/DealsChart.tsx
@@ -1,5 +1,5 @@
import AttachMoneyIcon from '@mui/icons-material/AttachMoney';
-import { Box } from '@mui/material';
+import { Box, Stack } from '@mui/material';
import { ResponsiveBar } from '@nivo/bar';
import { format, startOfMonth } from 'date-fns';
import { useMemo } from 'react';
@@ -14,6 +14,10 @@ const multiplier = {
delayed: 0.3,
};
+const threeMonthsAgo = new Date(
+ new Date().setMonth(new Date().getMonth() - 6)
+).toISOString();
+
export const DealsChart = () => {
const { data, isPending } = useGetList<Deal>('deals', {
pagination: { perPage: 100, page: 1 },
@@ -21,8 +25,10 @@ export const DealsChart = () => {
field: 'created_at',
order: 'ASC',
},
+ filter: {
+ 'created_at@gte': threeMonthsAgo,
+ },
});
-
const months = useMemo(() => {
if (!data) return [];
const dealsByMonth = data.reduce((acc, deal) => {
@@ -67,7 +73,6 @@ export const DealsChart = () => {
}, [data]);
if (isPending) return null; // FIXME return skeleton instead
-
const range = months.reduce(
(acc, month) => {
acc.min = Math.min(acc.min, month.lost);
@@ -76,12 +81,11 @@ export const DealsChart = () => {
},
{ min: 0, max: 0 }
);
-
return (
- <>
- <Box display="flex" alignItems="center">
- <Box ml={2} mr={2} display="flex">
- <AttachMoneyIcon color="disabled" fontSize="large" />
+ <Stack>
+ <Box display="flex" alignItems="center" mb={1}>
+ <Box mr={1} display="flex">
+ <AttachMoneyIcon color="disabled" fontSize="medium" />
</Box>
<Link
underline="none"
@@ -92,14 +96,14 @@ export const DealsChart = () => {
Upcoming Deal Revenue
</Link>
</Box>
- <Box height={500}>
+ <Box height={400}>
<ResponsiveBar
data={months}
indexBy="date"
keys={['won', 'pending', 'lost']}
colors={['#61cdbb', '#97e3d5', '#e25c3b']}
- margin={{ top: 50, right: 50, bottom: 50, left: 0 }}
- padding={0.5}
+ margin={{ top: 30, right: 50, bottom: 30, left: 0 }}
+ padding={0.3}
valueScale={{
type: 'linear',
min: range.min * 1.2,
@@ -151,6 +155,6 @@ export const DealsChart = () => {
}
/>
</Box>
- </>
+ </Stack>
);
};
diff --git a/src/dashboard/DealsPipeline.tsx b/src/dashboard/DealsPipeline.tsx
index e10d9059..dc447f05 100644
--- a/src/dashboard/DealsPipeline.tsx
+++ b/src/dashboard/DealsPipeline.tsx
@@ -27,7 +27,7 @@ export const DealsPipeline = () => {
{
pagination: { page: 1, perPage: 10 },
sort: { field: 'last_seen', order: 'DESC' },
- filter: { stage_neq: 'lost', sales_id: identity?.id },
+ filter: { 'stage@neq': 'lost', sales_id: identity?.id },
},
{ enabled: Number.isInteger(identity?.id) }
);
diff --git a/src/dashboard/HotContacts.tsx b/src/dashboard/HotContacts.tsx
index 5b122efa..5661bfb0 100644
--- a/src/dashboard/HotContacts.tsx
+++ b/src/dashboard/HotContacts.tsx
@@ -1,8 +1,7 @@
import * as React from 'react';
-import { Card, Box } from '@mui/material';
+import { Card, Box, Stack } from '@mui/material';
import ContactsIcon from '@mui/icons-material/Contacts';
import { useGetList, Link, SimpleList, useGetIdentity } from 'react-admin';
-import { formatDistance } from 'date-fns';
import { Avatar } from '../contacts/Avatar';
import { Contact } from '../types';
@@ -22,11 +21,12 @@ export const HotContacts = () => {
},
{ enabled: Number.isInteger(identity?.id) }
);
+
return (
- <>
- <Box display="flex" alignItems="center" marginBottom="1em">
- <Box ml={2} mr={2} display="flex">
- <ContactsIcon color="disabled" fontSize="large" />
+ <Stack>
+ <Box display="flex" alignItems="center" mb={1}>
+ <Box mr={1} display="flex">
+ <ContactsIcon color="disabled" fontSize="medium" />
</Box>
<Link
underline="none"
@@ -34,7 +34,7 @@ export const HotContacts = () => {
color="textSecondary"
to="/contacts"
>
- Hot contacts
+ Hot Contacts
</Link>
</Box>
<Card>
@@ -43,19 +43,19 @@ export const HotContacts = () => {
data={contactData}
total={contactTotal}
isPending={contactsLoading}
+ resource="contacts"
primaryText={contact =>
`${contact.first_name} ${contact.last_name}`
}
- resource="contacts"
- secondaryText={(contact: Contact) =>
- formatDistance(contact.last_seen, new Date(), {
- addSuffix: true,
- })
- }
+ secondaryText={contact => (
+ <>
+ {contact.title} at {contact.company_name}
+ </>
+ )}
leftAvatar={contact => <Avatar record={contact} />}
dense
/>
</Card>
- </>
+ </Stack>
);
};
diff --git a/src/dashboard/TasksList.tsx b/src/dashboard/TasksList.tsx
index 3d9a071b..df7194a2 100644
--- a/src/dashboard/TasksList.tsx
+++ b/src/dashboard/TasksList.tsx
@@ -1,21 +1,35 @@
+import * as React from 'react';
+import { Card, Box, Stack } from '@mui/material';
import AssignmentTurnedInIcon from '@mui/icons-material/AssignmentTurnedIn';
-import { Box, Button, Card } from '@mui/material';
-import {
- Link,
- ListContextProvider,
- ResourceContextProvider,
- useGetIdentity,
- useGetList,
- useList,
-} from 'react-admin';
-import { TasksIterator } from '../tasks/TasksIterator';
-
+import { useGetList, Link, useGetIdentity } from 'react-admin';
import { Contact } from '../types';
+import { AddTask } from '../tasks/AddTask';
+import { startOfToday, endOfToday, addDays } from 'date-fns';
+import { TasksListFilter } from './TasksListFilter';
+
+const today = new Date();
+const startOfTodayDateISO = startOfToday().toISOString();
+const endOfTodayDateISO = endOfToday().toISOString();
+const startOfWeekDateISO = addDays(today, 1).toISOString();
+const endOfWeekDateISO = addDays(today, 7).toISOString();
+
+const taskFilters = {
+ overdue: { 'done_date@is': null, 'due_date@lt': startOfTodayDateISO },
+ today: {
+ 'done_date@is': null,
+ 'due_date@gte': startOfTodayDateISO,
+ 'due_date@lte': endOfTodayDateISO,
+ },
+ thisWeek: {
+ 'done_date@is': null,
+ 'due_date@gte': startOfWeekDateISO,
+ 'due_date@lte': endOfWeekDateISO,
+ },
+ later: { 'done_date@is': null, 'due_date@gt': endOfWeekDateISO },
+};
export const TasksList = () => {
const { identity } = useGetIdentity();
-
- // get all the contacts for this sales
const { data: contacts, isPending: contactsLoading } = useGetList<Contact>(
'contacts',
{
@@ -25,38 +39,16 @@ export const TasksList = () => {
{ enabled: !!identity }
);
- // get the first 100 upcoming tasks for these contacts
- const { data: tasks, isPending: tasksLoading } = useGetList(
- 'tasks',
- {
- pagination: { page: 1, perPage: 100 },
- sort: { field: 'due_date', order: 'ASC' },
- filter: contacts?.length
- ? {
- 'done_date@': 'is.null',
- contact_id: contacts.map(contact => contact.id),
- }
- : {
- 'done_date@': 'is.null',
- },
- },
- { enabled: !!contacts }
- );
+ if (contactsLoading || !contacts) return null;
- const isPending = tasksLoading || contactsLoading;
-
- // limit to 10 tasks and provide the list context
- const listContext = useList({
- data: tasks,
- isPending,
- resource: 'tasks',
- perPage: 10,
- });
return (
- <>
- <Box display="flex" alignItems="center" marginBottom="1em">
- <Box ml={2} mr={2} display="flex">
- <AssignmentTurnedInIcon color="disabled" fontSize="large" />
+ <Stack>
+ <Box display="flex" alignItems="center" mb={1}>
+ <Box mr={1} display="flex">
+ <AssignmentTurnedInIcon
+ color="disabled"
+ fontSize="medium"
+ />
</Box>
<Link
underline="none"
@@ -64,26 +56,36 @@ export const TasksList = () => {
color="textSecondary"
to="/contacts"
>
- Upcoming tasks
+ Upcoming Tasks
</Link>
</Box>
- <Card sx={{ px: 2, mb: '2em' }}>
- <ResourceContextProvider value="tasks">
- <ListContextProvider value={listContext}>
- <TasksIterator showContact />
- </ListContextProvider>
- </ResourceContextProvider>
- {!isPending && (
- <Button
- onClick={() =>
- listContext.setPerPage(listContext.perPage + 10)
- }
- fullWidth
- >
- Load more
- </Button>
- )}
+ <Card sx={{ px: 2 }}>
+ <Box textAlign="center" mb={-2}>
+ <AddTask selectContact />
+ </Box>
+ <Stack mb={2}>
+ <TasksListFilter
+ title="Overdue"
+ filter={taskFilters.overdue}
+ contacts={contacts}
+ />
+ <TasksListFilter
+ title="Today"
+ filter={taskFilters.today}
+ contacts={contacts}
+ />
+ <TasksListFilter
+ title="This week"
+ filter={taskFilters.thisWeek}
+ contacts={contacts}
+ />
+ <TasksListFilter
+ title="Later"
+ filter={taskFilters.later}
+ contacts={contacts}
+ />
+ </Stack>
</Card>
- </>
+ </Stack>
);
};
diff --git a/src/dashboard/TasksListFilter.tsx b/src/dashboard/TasksListFilter.tsx
new file mode 100644
index 00000000..39d2f542
--- /dev/null
+++ b/src/dashboard/TasksListFilter.tsx
@@ -0,0 +1,71 @@
+import {
+ ListContextProvider,
+ ResourceContextProvider,
+ useGetList,
+ useList,
+} from 'react-admin';
+import { Link, Stack, Typography } from '@mui/material';
+
+import { Contact } from '../types';
+import { TasksIterator } from '../tasks/TasksIterator';
+
+export const TasksListFilter = ({
+ title,
+ filter,
+ contacts,
+}: {
+ title: string;
+ filter: any;
+ contacts: Contact[];
+}) => {
+ const {
+ data: tasks,
+ total,
+ isPending,
+ } = useGetList(
+ 'tasks',
+ {
+ pagination: { page: 1, perPage: 100 },
+ sort: { field: 'due_date', order: 'ASC' },
+ filter: {
+ ...filter,
+ 'contact_id@in': `(${contacts.map(contact => contact.id)})`,
+ },
+ },
+ { enabled: !!contacts }
+ );
+
+ const listContext = useList({
+ data: tasks,
+ isPending,
+ resource: 'tasks',
+ perPage: 5,
+ });
+
+ if (isPending || !tasks || !total) return null;
+
+ return (
+ <Stack>
+ <Typography variant="overline">{title}</Typography>
+ <ResourceContextProvider value="tasks">
+ <ListContextProvider value={listContext}>
+ <TasksIterator showContact sx={{ pt: 0, pb: 0 }} />
+ </ListContextProvider>
+ </ResourceContextProvider>
+ {total > listContext.perPage && (
+ <Stack justifyContent="flex-end" direction="row">
+ <Link
+ href="#"
+ onClick={e => {
+ listContext.setPerPage(listContext.perPage + 10);
+ e.preventDefault();
+ }}
+ variant="body2"
+ >
+ Load more
+ </Link>
+ </Stack>
+ )}
+ </Stack>
+ );
+};
diff --git a/src/dashboard/Welcome.tsx b/src/dashboard/Welcome.tsx
index b3fdd4dd..386f48cc 100644
--- a/src/dashboard/Welcome.tsx
+++ b/src/dashboard/Welcome.tsx
@@ -30,7 +30,7 @@ export const Welcome = () => (
>
<CardContent>
<Typography variant="h5" gutterBottom>
- CRM demo
+ CRM Demo
</Typography>
<Typography variant="body2" gutterBottom>
This app runs in the browser, and relies on a mock REST API.
diff --git a/src/dataProvider/activity.ts b/src/dataProvider/activity.ts
index 6099b524..6c037908 100644
--- a/src/dataProvider/activity.ts
+++ b/src/dataProvider/activity.ts
@@ -22,9 +22,6 @@ export async function getActivityLog(
) {
const sales = await getSales(dataProvider);
const companies = await getCompaniesLog(dataProvider, sales, companyId);
- if (!companies.companiesIds.length) {
- return [];
- }
const [contactsLog, dealsLog] = await Promise.all([
getContactsLog(dataProvider, sales, companies),
@@ -38,9 +35,9 @@ export async function getActivityLog(
async function getSales(dataProvider: DataProvider) {
const salesById = await dataProvider
.getList<Sale>('sales', {
- filter: {},
- pagination: { page: 1, perPage: 10_000 },
+ pagination: { page: 1, perPage: 200 },
sort: { field: 'id', order: 'ASC' },
+ filter: {},
})
.then(({ data }) =>
data.reduce((acc, sale) => {
@@ -94,8 +91,8 @@ async function getContactsLog(
const contacts = await dataProvider
.getList<Contact>('contacts', {
filter: {
- company_id: companiesIds,
- sales_id: salesIds,
+ 'company_id@in': `(${companiesIds})`,
+ 'sales_id@in': `(${salesIds})`,
},
pagination: { page: 1, perPage: 10_000 },
sort: { field: 'first_seen', order: 'DESC' },
@@ -110,8 +107,8 @@ async function getContactsLog(
const contactNotes = await dataProvider
.getList<ContactNote>('contactNotes', {
filter: {
- contact_id: contacts.map(({ id }) => id),
- sales_id: salesIds,
+ 'contact_id@in': `(${contacts.map(({ id }) => id)})`,
+ 'sales_id@in': `(${salesIds})`,
},
pagination: { page: 1, perPage: 10_000 },
sort: { field: 'date', order: 'DESC' },
@@ -139,7 +136,7 @@ async function getContactsLog(
sale: salesDict.get(contactNote.sales_id) as Sale,
contact,
contactNote,
- date: contactNote.date.toISOString(),
+ date: contactNote.date,
};
})
);
@@ -153,8 +150,8 @@ async function getDealsLog(
const deals = await dataProvider
.getList<Deal>('deals', {
filter: {
- company_id: companiesIds,
- sales_id: salesIds,
+ 'company_id@in': `(${companiesIds})`,
+ 'sales_id@in': `(${salesIds})`,
},
pagination: { page: 1, perPage: 10_000 },
sort: { field: 'created_at', order: 'DESC' },
@@ -169,8 +166,8 @@ async function getDealsLog(
const dealsNotes = await dataProvider
.getList<DealNote>('dealNotes', {
filter: {
- deal_id: deals.map(({ id }) => id),
- sales_id: salesIds,
+ 'deal_id@in': `(${deals.map(({ id }) => id)})`,
+ 'sales_id@in': `(${salesIds})`,
},
pagination: { page: 1, perPage: 10_000 },
sort: { field: 'date', order: 'DESC' },
@@ -196,7 +193,7 @@ async function getDealsLog(
sale: salesDict.get(dealNote.sales_id) as Sale,
deal,
dealNote,
- date: dealNote.date.toISOString(),
+ date: dealNote.date,
};
})
);
diff --git a/src/tasks/AddTask.tsx b/src/tasks/AddTask.tsx
index 632a97d3..e4c8c5a8 100644
--- a/src/tasks/AddTask.tsx
+++ b/src/tasks/AddTask.tsx
@@ -11,10 +11,12 @@ import {
import * as React from 'react';
import { useState } from 'react';
import {
+ AutocompleteInput,
CreateBase,
DateInput,
Form,
RecordRepresentation,
+ ReferenceInput,
SaveButton,
SelectInput,
TextInput,
@@ -24,8 +26,9 @@ import {
} from 'react-admin';
import { useConfigurationContext } from '../root/ConfigurationContext';
import { DialogCloseButton } from '../misc/DialogCloseButton';
+import { contactInputText, contactOptionText } from '../misc/ContactOption';
-export const AddTask = () => {
+export const AddTask = ({ selectContact }: { selectContact?: boolean }) => {
const { taskTypes } = useConfigurationContext();
const contact = useRecordContext();
const [open, setOpen] = useState(false);
@@ -34,7 +37,7 @@ export const AddTask = () => {
};
return (
<>
- <Box mt={1}>
+ <Box mt={2} mb={2}>
<Chip
icon={<ControlPointIcon />}
size="small"
@@ -51,6 +54,15 @@ export const AddTask = () => {
contact_id: contact?.id,
due_date: new Date().toISOString().slice(0, 10),
}}
+ transform={data => {
+ const dueDate = new Date(data.due_date);
+ dueDate.setHours(0, 0, 0, 0);
+ data.due_date = dueDate.toISOString();
+ return {
+ ...data,
+ due_date: new Date(data.due_date).toISOString(),
+ };
+ }}
mutationOptions={{ onSuccess: () => setOpen(false) }}
>
<Dialog
@@ -64,36 +76,61 @@ export const AddTask = () => {
<Form>
<DialogCloseButton onClose={() => setOpen(false)} />
<DialogTitle id="form-dialog-title">
- Create a new task for{' '}
- <RecordRepresentation
- record={contact}
- resource="contacts"
- />
+ {!selectContact
+ ? 'Create a new task for '
+ : 'Create a new task'}
+ {!selectContact && (
+ <RecordRepresentation
+ record={contact}
+ resource="contacts"
+ />
+ )}
</DialogTitle>
<DialogContent>
- <TextInput
- autoFocus
- source="text"
- label="Description"
- validate={required()}
- multiline
- helperText={false}
- />
- <Stack direction="row" spacing={1} mt={2}>
- <DateInput
- source="due_date"
- validate={required()}
- helperText={false}
- />
- <SelectInput
- source="type"
+ <Stack gap={2}>
+ <TextInput
+ autoFocus
+ source="text"
+ label="Description"
validate={required()}
- choices={taskTypes.map(type => ({
- id: type,
- name: type,
- }))}
+ multiline
+ sx={{ margin: 0 }}
helperText={false}
/>
+ {selectContact && (
+ <ReferenceInput
+ source="contact_id"
+ reference="contacts"
+ >
+ <AutocompleteInput
+ label="Contact"
+ optionText={contactOptionText}
+ inputText={contactInputText}
+ helperText={false}
+ validate={required()}
+ TextFieldProps={{
+ margin: 'none',
+ }}
+ />
+ </ReferenceInput>
+ )}
+
+ <Stack direction="row" spacing={1}>
+ <DateInput
+ source="due_date"
+ validate={required()}
+ helperText={false}
+ />
+ <SelectInput
+ source="type"
+ validate={required()}
+ choices={taskTypes.map(type => ({
+ id: type,
+ name: type,
+ }))}
+ helperText={false}
+ />
+ </Stack>
</Stack>
</DialogContent>
<DialogActions sx={{ p: 0 }}>
diff --git a/src/tasks/Task.tsx b/src/tasks/Task.tsx
index db3d5f08..b1d14832 100644
--- a/src/tasks/Task.tsx
+++ b/src/tasks/Task.tsx
@@ -57,9 +57,7 @@ export const Task = ({
update('tasks', {
id: task.id,
data: {
- done_date: task.done_date
- ? undefined
- : new Date().toISOString(),
+ done_date: task.done_date ? null : new Date().toISOString(),
},
previousData: task,
});
diff --git a/src/tasks/TasksIterator.tsx b/src/tasks/TasksIterator.tsx
index 2ed73f82..004baa77 100644
--- a/src/tasks/TasksIterator.tsx
+++ b/src/tasks/TasksIterator.tsx
@@ -1,11 +1,17 @@
import * as React from 'react';
import { useListContext } from 'react-admin';
import { isAfter } from 'date-fns';
-import { List } from '@mui/material';
+import { List, SxProps } from '@mui/material';
import { Task } from './Task';
-export const TasksIterator = ({ showContact }: { showContact?: boolean }) => {
+export const TasksIterator = ({
+ showContact,
+ sx,
+}: {
+ showContact?: boolean;
+ sx?: SxProps;
+}) => {
const { data, error, isPending } = useListContext();
if (isPending || error || data.length === 0) return null;
@@ -20,7 +26,7 @@ export const TasksIterator = ({ showContact }: { showContact?: boolean }) => {
);
return (
- <List dense>
+ <List dense sx={sx}>
{tasks.map(task => (
<Task task={task} showContact={showContact} key={task.id} />
))}
diff --git a/src/types.ts b/src/types.ts
index 2e074c9c..83f41bad 100644
--- a/src/types.ts
+++ b/src/types.ts
@@ -6,6 +6,7 @@ import {
DEAL_CREATED,
DEAL_NOTE_CREATED,
} from './consts';
+import { SvgIconComponent } from '@mui/icons-material';
export type SignUpData = {
email: string;
@@ -27,6 +28,7 @@ export interface Sale extends RaRecord {
last_name: string;
administrator: boolean;
avatar?: RAFile;
+ disabled?: boolean;
user_id: string;
// This is a copy of the user's email, to make it easier to handle by react admin
@@ -82,7 +84,7 @@ export interface Contact extends RaRecord {
export interface ContactNote extends RaRecord {
contact_id: Identifier;
text: string;
- date: Date;
+ date: string;
sales_id: Identifier;
status: string;
attachments?: AttachmentNote[];
@@ -108,7 +110,7 @@ export interface Deal extends RaRecord {
export interface DealNote extends RaRecord {
deal_id: Identifier;
text: string;
- date: Date;
+ date: string;
sales_id: Identifier;
attachments?: AttachmentNote[];
}
@@ -192,4 +194,5 @@ export interface NoteStatus {
export interface ContactGender {
value: string;
label: string;
+ icon: SvgIconComponent;
}
diff --git a/supabase/migrations/20240730075029_init_db.sql b/supabase/migrations/20240730075029_init_db.sql
index 7447f0dc..152823c3 100644
--- a/supabase/migrations/20240730075029_init_db.sql
+++ b/supabase/migrations/20240730075029_init_db.sql
@@ -123,7 +123,6 @@ alter table "public"."tags" enable row level security;
create table "public"."tasks" (
"id" bigint generated by default as identity not null,
"contact_id" bigint not null,
- "sales_id" bigint not null,
"type" text,
"text" text,
"due_date" timestamp with time zone not null,
@@ -209,10 +208,6 @@ alter table "public"."tasks" add constraint "tasks_contact_id_fkey" FOREIGN KEY
alter table "public"."tasks" validate constraint "tasks_contact_id_fkey";
-alter table "public"."tasks" add constraint "tasks_sales_id_fkey" FOREIGN KEY (sales_id) REFERENCES sales(id) ON UPDATE CASCADE ON DELETE CASCADE not valid;
-
-alter table "public"."tasks" validate constraint "tasks_sales_id_fkey";
-
set check_function_bodies = off;
grant delete on table "public"."companies" to "authenticated";