Skip to content

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

Feat(task): Add task edit support

Jonathan ARNAULTf5e8b85761d5
diff --git a/src/contacts/ContactListContent.tsx b/src/contacts/ContactListContent.tsx
index edc8798d..15901652 100644
--- a/src/contacts/ContactListContent.tsx
+++ b/src/contacts/ContactListContent.tsx
@@ -1,31 +1,30 @@
/* eslint-disable import/no-anonymous-default-export */
-import * as React from 'react';
-import {
- RecordContextProvider,
- ReferenceField,
- SimpleListLoading,
- TextField,
- useListContext,
-} from 'react-admin';
+import type { Theme } from '@mui/material';
import {
+ Checkbox,
List,
ListItem,
ListItemAvatar,
ListItemIcon,
ListItemSecondaryAction,
ListItemText,
- Checkbox,
Typography,
useMediaQuery,
} from '@mui/material';
-import type { Theme } from '@mui/material';
-import { Link } from 'react-router-dom';
import { formatRelative } from 'date-fns';
+import {
+ RecordContextProvider,
+ ReferenceField,
+ SimpleListLoading,
+ TextField,
+ useListContext,
+} from 'react-admin';
+import { Link } from 'react-router-dom';
-import { Avatar } from './Avatar';
import { Status } from '../misc/Status';
-import { TagsList } from './TagsList';
import { Contact } from '../types';
+import { Avatar } from './Avatar';
+import { TagsList } from './TagsList';
export const ContactListContent = () => {
const {
diff --git a/src/contacts/TagsListEdit.tsx b/src/contacts/TagsListEdit.tsx
index cb375fab..b1e1e953 100644
--- a/src/contacts/TagsListEdit.tsx
+++ b/src/contacts/TagsListEdit.tsx
@@ -1,41 +1,24 @@
+import ControlPointIcon from '@mui/icons-material/ControlPoint';
+import EditIcon from '@mui/icons-material/Edit';
+import { Box, Chip, Menu, MenuItem } from '@mui/material';
import * as React from 'react';
-import { useState, FormEvent } from 'react';
+import { useState } from 'react';
import {
- useGetMany,
- useCreate,
- useUpdate,
- useGetList,
Identifier,
+ useGetList,
+ useGetMany,
useRecordContext,
- Toolbar,
+ useUpdate,
} from 'react-admin';
-import {
- Chip,
- Box,
- Button,
- Dialog,
- DialogTitle,
- DialogContent,
- DialogActions,
- TextField,
- MenuItem,
- Menu,
-} from '@mui/material';
-import ControlPointIcon from '@mui/icons-material/ControlPoint';
-import EditIcon from '@mui/icons-material/Edit';
-import { colors } from '../tags/colors';
+import { TagChip } from '../tags/TagChip';
+import { TagCreateModal } from '../tags/TagCreateModal';
import { Contact, Tag } from '../types';
-import { DialogCloseButton } from '../misc/DialogCloseButton';
-import ContentSave from '@mui/icons-material/Save';
export const TagsListEdit = () => {
const record = useRecordContext<Contact>();
const [open, setOpen] = useState(false);
- const [newTagName, setNewTagName] = useState('');
- const [newTagColor, setNewTagColor] = useState(colors[0]);
const [anchorEl, setAnchorEl] = useState<null | HTMLElement>(null);
- const [disabled, setDisabled] = useState(false);
const { data: allTags, isPending: isPendingAllTags } = useGetList<Tag>(
'tags',
@@ -50,100 +33,86 @@ export const TagsListEdit = () => {
{ enabled: record && record.tags && record.tags.length > 0 }
);
const [update] = useUpdate<Contact>();
- const [create] = useCreate<Tag>();
const unselectedTags =
allTags &&
record &&
allTags.filter(tag => !record.tags.includes(tag.id));
- const handleOpen = (event: React.MouseEvent<HTMLDivElement>) => {
+ const handleMenuOpen = (event: React.MouseEvent<HTMLDivElement>) => {
setAnchorEl(event.currentTarget);
};
- const handleClose = () => {
+ const handleMenuClose = () => {
setAnchorEl(null);
};
- const handleDeleteTag = (id: Identifier) => {
+ const handleTagAdd = (id: Identifier) => {
if (!record) {
throw new Error('No contact record found');
}
- const tags = record.tags.filter(tagId => tagId !== id);
+ const tags = [...record.tags, id];
update('contacts', {
id: record.id,
data: { tags },
previousData: record,
});
+ setAnchorEl(null);
};
- const handleAddTag = (id: Identifier) => {
+ const handleTagDelete = async (id: Identifier) => {
if (!record) {
throw new Error('No contact record found');
}
- const tags = [...record.tags, id];
- update('contacts', {
+ const tags = record.tags.filter(tagId => tagId !== id);
+ await update('contacts', {
id: record.id,
data: { tags },
previousData: record,
});
- setAnchorEl(null);
};
- const handleOpenCreateDialog = () => {
+ const openTagCreateDialog = () => {
setOpen(true);
setAnchorEl(null);
- setDisabled(false);
};
- const handleNewTagNameChange = (
- event: React.ChangeEvent<HTMLInputElement>
- ) => {
- setNewTagName(event.target.value);
+ const handleTagCreateClose = () => {
+ setOpen(false);
};
- const handleCreateTag = (event: FormEvent<HTMLFormElement>) => {
- event.preventDefault();
- if (!record) {
- throw new Error('No contact record found');
- }
- setDisabled(true);
- create(
- 'tags',
- { data: { name: newTagName, color: newTagColor } },
- {
- onSuccess: tag => {
- update(
- 'contacts',
- {
- id: record.id,
- data: { tags: [...record.tags, tag.id] },
- previousData: record,
- },
- {
- onSuccess: () => {
- setNewTagName('');
- setNewTagColor(colors[0]);
- setOpen(false);
- },
- }
- );
- },
+ const handleTagCreated = React.useCallback(
+ async (tag: Tag) => {
+ if (!record) {
+ throw new Error('No contact record found');
}
- );
- };
+
+ await update(
+ 'contacts',
+ {
+ id: record.id,
+ data: { tags: [...record.tags, tag.id] },
+ previousData: record,
+ },
+ {
+ onSuccess: () => {
+ setOpen(false);
+ },
+ }
+ );
+ },
+ [update, record]
+ );
if (isPendingRecordTags || isPendingAllTags) return null;
return (
<>
{tags?.map(tag => (
<Box mt={1} mb={1} key={tag.id}>
- <Chip
- size="small"
- variant="outlined"
- onDelete={() => handleDeleteTag(tag.id)}
- label={tag.name}
- style={{ backgroundColor: tag.color, border: 0 }}
+ <TagChip
+ tag={tag}
+ onUnlink={() => handleTagDelete(tag.id)}
+ key={tag.id}
/>
</Box>
))}
@@ -152,18 +121,18 @@ export const TagsListEdit = () => {
icon={<ControlPointIcon />}
size="small"
variant="outlined"
- onClick={handleOpen}
+ onClick={handleMenuOpen}
label="Add tag"
color="primary"
/>
</Box>
<Menu
open={Boolean(anchorEl)}
- onClose={handleClose}
+ onClose={handleMenuClose}
anchorEl={anchorEl}
>
{unselectedTags?.map(tag => (
- <MenuItem key={tag.id} onClick={() => handleAddTag(tag.id)}>
+ <MenuItem key={tag.id} onClick={() => handleTagAdd(tag.id)}>
<Chip
size="small"
variant="outlined"
@@ -172,93 +141,26 @@ export const TagsListEdit = () => {
backgroundColor: tag.color,
border: 0,
}}
- onClick={() => handleAddTag(tag.id)}
+ onClick={() => handleTagAdd(tag.id)}
/>
</MenuItem>
))}
- <MenuItem onClick={handleOpenCreateDialog}>
+ <MenuItem onClick={openTagCreateDialog}>
<Chip
icon={<EditIcon />}
size="small"
variant="outlined"
- onClick={handleOpenCreateDialog}
+ onClick={openTagCreateDialog}
color="primary"
label="Create new tag"
/>
</MenuItem>
</Menu>
- <Dialog
+ <TagCreateModal
open={open}
- onClose={() => setOpen(false)}
- aria-labelledby="form-dialog-title"
- >
- <form onSubmit={handleCreateTag}>
- <DialogCloseButton onClose={() => setOpen(false)} />
- <DialogTitle id="form-dialog-title">
- Create a new tag
- </DialogTitle>
- <DialogContent>
- <TextField
- autoFocus
- label="Tag name"
- value={newTagName}
- onChange={handleNewTagNameChange}
- sx={{ mt: 1 }}
- />
- <Box display="flex" flexWrap="wrap" width={230} mt={2}>
- {colors.map(color => (
- <RoundButton
- key={color}
- color={color}
- selected={color === newTagColor}
- handleClick={() => {
- setNewTagColor(color);
- }}
- />
- ))}
- </Box>
- </DialogContent>
- <DialogActions
- sx={{
- justifyContent: 'flex-start',
- p: 0,
- }}
- >
- <Toolbar
- sx={{
- width: '100%',
- }}
- >
- <Button
- type="submit"
- color="primary"
- disabled={disabled}
- variant="contained"
- startIcon={<ContentSave />}
- >
- Save
- </Button>
- </Toolbar>
- </DialogActions>
- </form>
- </Dialog>
+ onClose={handleTagCreateClose}
+ onSuccess={handleTagCreated}
+ />
</>
);
};
-
-const RoundButton = ({ color, handleClick, selected }: any) => (
- <Box
- component="button"
- type="button"
- sx={{
- bgcolor: color,
- width: 30,
- height: 30,
- borderRadius: 15,
- border: selected ? '2px solid grey' : 'none',
- display: 'inline-block',
- margin: 1,
- }}
- onClick={handleClick}
- />
-);
diff --git a/src/tags/RoundButton.tsx b/src/tags/RoundButton.tsx
new file mode 100644
index 00000000..ebb9020d
--- /dev/null
+++ b/src/tags/RoundButton.tsx
@@ -0,0 +1,18 @@
+import { Box } from '@mui/material';
+
+export const RoundButton = ({ color, handleClick, selected }: any) => (
+ <Box
+ component="button"
+ type="button"
+ sx={{
+ bgcolor: color,
+ width: 30,
+ height: 30,
+ borderRadius: 15,
+ border: selected ? '2px solid grey' : 'none',
+ display: 'inline-block',
+ margin: 1,
+ }}
+ onClick={handleClick}
+ />
+);
diff --git a/src/tags/TagChip.tsx b/src/tags/TagChip.tsx
new file mode 100644
index 00000000..a0bf8b01
--- /dev/null
+++ b/src/tags/TagChip.tsx
@@ -0,0 +1,36 @@
+import { Chip } from '@mui/material';
+import { useState } from 'react';
+import { Tag } from '../types';
+import { TagEditModal } from './TagEditModal';
+
+type TagChipProps = {
+ tag: Tag;
+
+ onUnlink: () => Promise<void>;
+};
+
+export function TagChip({ tag, onUnlink }: TagChipProps) {
+ const [open, setOpen] = useState(false);
+
+ const handleClose = () => {
+ setOpen(false);
+ };
+
+ const handleClick = () => {
+ setOpen(true);
+ };
+
+ return (
+ <>
+ <Chip
+ size="small"
+ variant="outlined"
+ onDelete={onUnlink}
+ label={tag.name}
+ style={{ backgroundColor: tag.color, border: 0 }}
+ onClick={handleClick}
+ />
+ <TagEditModal tag={tag} open={open} onClose={handleClose} />
+ </>
+ );
+}
diff --git a/src/tags/TagCreateModal.tsx b/src/tags/TagCreateModal.tsx
new file mode 100644
index 00000000..d7a36b65
--- /dev/null
+++ b/src/tags/TagCreateModal.tsx
@@ -0,0 +1,38 @@
+import { useCreate } from 'react-admin';
+import { Tag } from '../types';
+import { TagDialog } from './TagDialog';
+
+type TagCreateModalProps = {
+ open: boolean;
+ onClose(): void;
+ onSuccess?(tag: Tag): Promise<void>;
+};
+
+export function TagCreateModal({
+ open,
+ onClose,
+ onSuccess,
+}: TagCreateModalProps) {
+ const [create] = useCreate<Tag>();
+
+ const handleCreateTag = async (data: Pick<Tag, 'name' | 'color'>) => {
+ await create(
+ 'tags',
+ { data },
+ {
+ onSuccess: async tag => {
+ await onSuccess?.(tag);
+ },
+ }
+ );
+ };
+
+ return (
+ <TagDialog
+ open={open}
+ title="Create a new tag"
+ onClose={onClose}
+ onSubmit={handleCreateTag}
+ />
+ );
+}
diff --git a/src/tags/TagDialog.tsx b/src/tags/TagDialog.tsx
new file mode 100644
index 00000000..a21090e3
--- /dev/null
+++ b/src/tags/TagDialog.tsx
@@ -0,0 +1,121 @@
+import ContentSave from '@mui/icons-material/Save';
+import {
+ Box,
+ Button,
+ Dialog,
+ DialogActions,
+ DialogContent,
+ DialogTitle,
+ TextField,
+} from '@mui/material';
+import { FormEvent, useEffect, useState } from 'react';
+import { Toolbar } from 'react-admin';
+import { DialogCloseButton } from '../misc/DialogCloseButton';
+import { Tag } from '../types';
+import { colors } from './colors';
+import { RoundButton } from './RoundButton';
+
+type TagDialogProps = {
+ open: boolean;
+ tag?: Pick<Tag, 'name' | 'color'>;
+ title: string;
+ onSubmit(tag: Pick<Tag, 'name' | 'color'>): Promise<void>;
+ onClose(): void;
+};
+
+export function TagDialog({
+ open,
+ tag,
+ title,
+ onClose,
+ onSubmit,
+}: TagDialogProps) {
+ const [newTagName, setNewTagName] = useState('');
+ const [newTagColor, setNewTagColor] = useState(colors[0]);
+ const [disabled, setDisabled] = useState(false);
+
+ const handleNewTagNameChange = (
+ event: React.ChangeEvent<HTMLInputElement>
+ ) => {
+ setNewTagName(event.target.value);
+ };
+
+ const handleClose = () => {
+ setDisabled(false);
+ onClose();
+ };
+
+ const handleSubmit = async (event: FormEvent<HTMLFormElement>) => {
+ event.preventDefault();
+
+ await onSubmit({ name: newTagName, color: newTagColor });
+
+ setDisabled(true);
+ setNewTagName('');
+ setNewTagColor(colors[0]);
+
+ handleClose();
+ };
+
+ useEffect(() => {
+ console.log('tag.updated');
+ setNewTagName(tag?.name ?? '');
+ setNewTagColor(tag?.color ?? colors[0]);
+ }, [tag]);
+
+ return (
+ <Dialog
+ open={open}
+ onClose={handleClose}
+ aria-labelledby="form-dialog-title"
+ >
+ <form onSubmit={handleSubmit}>
+ <DialogCloseButton onClose={handleClose} />
+ <DialogTitle id="form-dialog-title">{title}</DialogTitle>
+ <DialogContent>
+ <TextField
+ autoFocus
+ label="Tag name"
+ value={newTagName}
+ onChange={handleNewTagNameChange}
+ sx={{ mt: 1 }}
+ />
+ <Box display="flex" flexWrap="wrap" width={230} mt={2}>
+ {colors.map(color => (
+ <RoundButton
+ key={color}
+ color={color}
+ selected={color === newTagColor}
+ handleClick={() => {
+ setNewTagColor(color);
+ }}
+ />
+ ))}
+ </Box>
+ </DialogContent>
+ <DialogActions
+ sx={{
+ justifyContent: 'flex-start',
+ p: 0,
+ }}
+ >
+ <Toolbar
+ sx={{
+ width: '100%',
+ }}
+ >
+ <Button
+ type="submit"
+ color="primary"
+ disabled={disabled}
+ variant="contained"
+ startIcon={<ContentSave />}
+ >
+ Save
+ </Button>
+ </Toolbar>
+ </DialogActions>
+ </form>
+ </Dialog>
+ );
+}
diff --git a/src/tags/TagEditModal.tsx b/src/tags/TagEditModal.tsx
new file mode 100644
index 00000000..9aacfcd2
--- /dev/null
+++ b/src/tags/TagEditModal.tsx
@@ -0,0 +1,43 @@
+import { useUpdate } from 'react-admin';
+import { Tag } from '../types';
+import { TagDialog } from './TagDialog';
+
+type TagEditModalProps = {
+ tag: Tag;
+ open: boolean;
+ onClose(): void;
+ onSuccess?(tag: Tag): Promise<void>;
+};
+
+export function TagEditModal({
+ tag,
+ open,
+ onClose,
+ onSuccess,
+}: TagEditModalProps) {
+ const [update] = useUpdate<Tag>();
+
+ const handleEditTag = async (data: Pick<Tag, 'name' | 'color'>) => {
+ console.log('handleEditTag', data);
+ await update(
+ 'tags',
+ { id: tag.id, data, previousData: tag },
+ {
+ onSuccess: async tag => {
+ console.log('update.tag.onSuccess', tag);
+ await onSuccess?.(tag);
+ },
+ }
+ );
+ };
+
+ return (
+ <TagDialog
+ open={open}
+ title={`Update ${tag.name} tag`}
+ onClose={onClose}
+ onSubmit={handleEditTag}
+ tag={tag}
+ />
+ );
+}
diff --git a/supabase/migrations/20240813084010_tags_policy.sql b/supabase/migrations/20240813084010_tags_policy.sql
new file mode 100644
index 00000000..a1790fc7
--- /dev/null
+++ b/supabase/migrations/20240813084010_tags_policy.sql
@@ -0,0 +1,18 @@
+create policy "Enable delete for authenticated users only"
+on "public"."tags"
+as permissive
+for delete
+to authenticated
+using (true);
+
+
+create policy "Enable update for authenticated users only"
+on "public"."tags"
+as permissive
+for update
+to authenticated
+using (true);
+
+
+
+