A full-featured CRM built with React, shadcn/ui, and Supabase.
WIP(crm): Backport Notes
Anthony Rimet74d374467562
diff --git a/src/contacts/ContactInputs.tsx b/src/contacts/ContactInputs.tsxindex a56eca2a..4ca40b9b 100644--- a/src/contacts/ContactInputs.tsx+++ b/src/contacts/ContactInputs.tsx@@ -179,7 +179,7 @@ const ContactMiscInputs = () => {source="sales_id"sort={{ field: 'last_name', order: 'ASC' }}filter={{- disabled_neq: true,+ 'disabled@neq': true,}}><SelectInputdiff --git a/src/contacts/ContactListFilter.tsx b/src/contacts/ContactListFilter.tsxindex 1b3b8021..b40ebdcf 100644--- a/src/contacts/ContactListFilter.tsx+++ b/src/contacts/ContactListFilter.tsx@@ -38,36 +38,36 @@ export const ContactListFilter = () => {<FilterListItemlabel="Today"value={{- last_seen_gte: endOfYesterday().toISOString(),- last_seen_lte: undefined,+ 'last_seen@gte': endOfYesterday().toISOString(),+ 'last_seen@lte': undefined,}}/><FilterListItemlabel="This week"value={{- last_seen_gte: startOfWeek(new Date()).toISOString(),- last_seen_lte: undefined,+ 'last_seen@gte': startOfWeek(new Date()).toISOString(),+ 'last_seen@lte': undefined,}}/><FilterListItemlabel="Before this week"value={{- last_seen_gte: undefined,- last_seen_lte: startOfWeek(new Date()).toISOString(),+ 'last_seen@gte': undefined,+ 'last_seen@lte': startOfWeek(new Date()).toISOString(),}}/><FilterListItemlabel="Before this month"value={{- last_seen_gte: undefined,- last_seen_lte: startOfMonth(new Date()).toISOString(),+ 'last_seen@gte': undefined,+ 'last_seen@lte': startOfMonth(new Date()).toISOString(),}}/><FilterListItemlabel="Before last month"value={{- last_seen_gte: undefined,- last_seen_lte: subMonths(+ 'last_seen@gte': undefined,+ 'last_seen@lte': subMonths(startOfMonth(new Date()),1).toISOString(),diff --git a/src/dataProvider.ts b/src/dataProvider.tsindex 24f30ede..17e09b10 100644--- a/src/dataProvider.ts+++ b/src/dataProvider.ts@@ -67,6 +67,9 @@ async function processContactAvatar(params: CreateParams<Contact> | UpdateParams<Contact>): Promise<CreateParams<Contact> | UpdateParams<Contact>> {const { data } = params;+ if (!data.avatar && !data.email) {+ return params;+ }const avatarUrl = await getContactAvatar(data);// Clone the data and modify the clonediff --git a/src/notes/Note.tsx b/src/notes/Note.tsxindex 9cef8e2b..3330b344 100644--- a/src/notes/Note.tsx+++ b/src/notes/Note.tsx@@ -7,27 +7,28 @@ import {Tooltip,Typography,} from '@mui/material';-import * as React from 'react';import { useState } from 'react';import {- DateField,Form,ReferenceField,- TextField,useDelete,useNotify,useResourceContext,useUpdate,+ WithRecord,} from 'react-admin';-import TrashIcon from '@mui/icons-material/Delete';import EditIcon from '@mui/icons-material/Edit';import { FieldValues, SubmitHandler } from 'react-hook-form';+import { Avatar } from '../contacts/Avatar';import { Status } from '../misc/Status';import { ContactNote, DealNote } from '../types';import { NoteAttachments } from './NoteAttachments';import { NoteInputs } from './NoteInputs';+import { SaleName } from '../sales/SaleName';+import { RelativeDate } from '../misc/RelativeDate';+import { CompanyAvatar } from '../companies/CompanyAvatar';export const Note = ({showStatus,@@ -85,52 +86,63 @@ export const Note = ({<BoxonMouseEnter={() => setHover(true)}onMouseLeave={() => setHover(false)}+ pb={1}>- <Stack- direction="row"- justifyContent="space-between"- alignItems="center"- >- <Box color="text.secondary">+ <Stack direction="row" spacing={1} alignItems="center" width="100%">+ {resource === 'contactNote' ? (+ <Avatar width={20} height={20} />+ ) : (+ <ReferenceField+ source="company_id"+ reference="companies"+ link="show"+ >+ <CompanyAvatar width={20} height={20} />+ </ReferenceField>+ )}+ <Typography color="text.secondary" variant="body2"><ReferenceFieldrecord={note}resource={resource}source="sales_id"reference="sales"+ link={false}>- <TextField source="first_name" variant="body2" />+ <WithRecord+ render={record => <SaleName sale={record} />}+ /></ReferenceField>{' '}- <Typography component="span" variant="body2">- added a note on{' '}- </Typography>- <DateField- source="date"- record={note}- variant="body2"- showTime- options={{- dateStyle: 'full',- timeStyle: 'short',+ added a note {showStatus && <Status status={note.status} />}+ <Box+ component="span"+ sx={{+ ml: 2,+ visibility: isHover ? 'visible' : 'hidden',}}- />{' '}- {showStatus && <Status status={note.status} />}- </Box>- <Box- sx={{- visibility: isHover ? 'visible' : 'hidden',- }}+ >+ <Tooltip title="Edit note">+ <IconButton+ size="small"+ onClick={handleEnterEditMode}+ >+ <EditIcon />+ </IconButton>+ </Tooltip>+ <Tooltip title="Delete note">+ <IconButton size="small" onClick={handleDelete}>+ <TrashIcon />+ </IconButton>+ </Tooltip>+ </Box>+ </Typography>+ <Box flex={1}></Box>+ <Typography+ color="textSecondary"+ variant="body2"+ component="span">- <Tooltip title="Edit note">- <IconButton size="small" onClick={handleEnterEditMode}>- <EditIcon />- </IconButton>- </Tooltip>- <Tooltip title="Delete note">- <IconButton size="small" onClick={handleDelete}>- <TrashIcon />- </IconButton>- </Tooltip>- </Box>+ <RelativeDate date={note.date} />+ </Typography></Stack>{isEditing ? (<Form onSubmit={handleNoteUpdate} record={note}>@@ -146,7 +158,7 @@ export const Note = ({Update Note</Button><Button- sx={{ mr: 1 }}+ sx={{ ml: 1 }}onClick={handleCancelEdit}color="primary">@@ -165,7 +177,6 @@ export const Note = ({<Boxflex={1}sx={{- maxWidth: '80%','& p:first-of-type': {marginTop: 0,},diff --git a/src/notes/NoteAttachments.tsx b/src/notes/NoteAttachments.tsxindex 10ffd4ca..16c53ea4 100644--- a/src/notes/NoteAttachments.tsx+++ b/src/notes/NoteAttachments.tsx@@ -1,17 +1,13 @@import { ImageList, ImageListItem, Stack } from '@mui/material';import { AttachmentNote, ContactNote, DealNote } from '../types';-import AttachFileIcon from '@mui/icons-material/AttachFile';import { FileField } from 'react-admin';+import AttachFileIcon from '@mui/icons-material/AttachFile';export const NoteAttachments = ({ note }: { note: ContactNote | DealNote }) => {if (!note.attachments || note.attachments.length === 0) {return null;}- if (!note.attachments) {- return null;- }-const imageAttachments = note.attachments.filter((attachment: AttachmentNote) => isImage(attachment.src));diff --git a/src/notes/NoteCreate.tsx b/src/notes/NoteCreate.tsxindex 45bd5b8e..d6a9923e 100644--- a/src/notes/NoteCreate.tsx+++ b/src/notes/NoteCreate.tsx@@ -16,7 +16,7 @@ import { useFormContext } from 'react-hook-form';import { Stack } from '@mui/material';import { NoteInputs } from './NoteInputs';-import { getCurrentDate } from './note';+import { getCurrentDate } from './utils';const foreignKeyMapping = {contacts: 'contact_id',@@ -63,7 +63,15 @@ const NoteCreateButton = ({if (!record || !identity) return null;const handleSuccess = (data: any) => {- reset();+ reset(+ {+ date: getCurrentDate(),+ text: null,+ status: 'warm',+ attachments: null,+ },+ { keepValues: false }+ );refetch();update(reference, {id: (record && record.id) as unknown as Identifier,diff --git a/src/notes/NoteInputs.tsx b/src/notes/NoteInputs.tsxindex c3b28b4f..80f4af4c 100644--- a/src/notes/NoteInputs.tsx+++ b/src/notes/NoteInputs.tsx@@ -8,7 +8,7 @@ import {} from 'react-admin';import { useConfigurationContext } from '../root/ConfigurationContext';import { useState } from 'react';-import { getCurrentDate } from './note';+import { formatNoteDate, getCurrentDate } from './utils';import { Status } from '../misc/Status';export const NoteInputs = ({@@ -67,6 +67,7 @@ export const NoteInputs = ({label="Date"defaultValue={getCurrentDate()}helperText={false}+ parse={formatNoteDate}/></Stack><FileInput source="attachments" multiple>diff --git a/src/notes/NotesIterator.tsx b/src/notes/NotesIterator.tsxindex 459acef9..2029d00a 100644--- a/src/notes/NotesIterator.tsx+++ b/src/notes/NotesIterator.tsx@@ -18,16 +18,16 @@ export const NotesIterator = ({<Box mt={4}><NoteCreate showStatus={showStatus} reference={reference} />{data && (- <Stack mt={4} gap={3}>+ <Stack mt={2} gap={1}>{data.map((note, index) => (<React.Fragment key={index}>- <Divider /><Notenote={note}isLast={index === data.length - 1}showStatus={showStatus}key={index}/>+ {index < data.length - 1 && <Divider />}</React.Fragment>))}</Stack>diff --git a/src/notes/note.ts b/src/notes/note.tsdeleted file mode 100644index b63ef8f1..00000000--- a/src/notes/note.ts+++ /dev/null@@ -1,7 +0,0 @@-export const getCurrentDate = () => {- const now = new Date();- now.setMinutes(now.getMinutes() - now.getTimezoneOffset());- now.setSeconds(0);- now.setMilliseconds(0);- return now.toISOString().slice(0, -1);-};diff --git a/src/notes/utils.ts b/src/notes/utils.tsnew file mode 100644index 00000000..834d3eb3--- /dev/null+++ b/src/notes/utils.ts@@ -0,0 +1,13 @@+export const getCurrentDate = () => {+ const now = new Date();+ now.setSeconds(0);+ now.setMilliseconds(0);+ return now.toISOString();+};++export const formatNoteDate = (dateString: string) => {+ const date = new Date(dateString);+ date.setSeconds(0);+ date.setMilliseconds(0);+ return date.toISOString();+};diff --git a/src/sales/SaleName.tsx b/src/sales/SaleName.tsxnew file mode 100644index 00000000..9bef5237--- /dev/null+++ b/src/sales/SaleName.tsx@@ -0,0 +1,10 @@+import { useGetIdentity } from 'react-admin';+import { Sale } from '../types';++export const SaleName = ({ sale }: { sale: Sale }) => {+ const { identity, isPending } = useGetIdentity();+ if (isPending) return null;+ return sale.id === identity?.id+ ? 'You'+ : `${sale.first_name} ${sale.last_name}`;+};diff --git a/supabase/migrations/20240730075029_init_db.sql b/supabase/migrations/20240730075029_init_db.sqlindex d9ccd2b8..7447f0dc 100644--- a/supabase/migrations/20240730075029_init_db.sql+++ b/supabase/migrations/20240730075029_init_db.sql@@ -104,7 +104,8 @@ create table "public"."sales" ("email" text not null,"administrator" boolean not null,"user_id" uuid not null,- "avatar" jsonb+ "avatar" jsonb,+ "disabled" boolean);