Skip to content

Commit 7d8bb02

Browse files
committed
remove nb_task and nb_notes, fetch in database instead
1 parent b346d7f commit 7d8bb02

File tree

11 files changed

+75
-36
lines changed

11 files changed

+75
-36
lines changed

packages/demo/db-seed.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ async function main() {
110110
});
111111
const { data: persistedDeals, error: errorDeals } = await supabase
112112
.from('deals')
113-
.insert(deals.map(({ nb_notes, id, ...deal }) => deal))
113+
.insert(deals.map(({ id, ...deal }) => deal))
114114
.select();
115115

116116
if (errorDeals) {

packages/demo/src/companies/CompanyShow.tsx

+11-10
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ import { LogoField } from './LogoField';
3939
import { CompanyAside } from './CompanyAside';
4040
import { Company, Deal, Contact } from '../types';
4141
import { stageNames } from '../deals/stages';
42+
import { NbRelations } from '../misc/NbRelations';
4243

4344
export const CompanyShow = () => (
4445
<ShowBase>
@@ -185,16 +186,16 @@ const ContactsIterator = () => {
185186
secondary={
186187
<>
187188
{contact.title}
188-
{contact.nb_notes
189-
? ` - ${contact.nb_notes} note${
190-
contact.nb_notes > 1 ? 's' : ''
191-
}`
192-
: ''}
193-
{contact.nb_tasks
194-
? ` - ${contact.nb_tasks} task${
195-
contact.nb_tasks > 1 ? 's' : ''
196-
}`
197-
: ''}
189+
<NbRelations
190+
label="note"
191+
reference="contactNotes"
192+
target="contact_id"
193+
/>
194+
<NbRelations
195+
label="task"
196+
reference="tasks"
197+
target="contact_id"
198+
/>
198199
&nbsp; &nbsp;
199200
<TagsList />
200201
</>

packages/demo/src/contacts/ContactList.tsx

+11-14
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ import { Status } from '../misc/Status';
3434
import { TagsList } from './TagsList';
3535
import { ContactListFilter } from './ContactListFilter';
3636
import { Contact } from '../types';
37+
import { NbRelations } from '../misc/NbRelations';
3738

3839
const ContactListContent = () => {
3940
const {
@@ -91,20 +92,16 @@ const ContactListContent = () => {
9192
>
9293
<TextField source="name" />
9394
</ReferenceField>
94-
{contact.nb_notes
95-
? ` - ${contact.nb_notes} note${
96-
contact.nb_notes > 1
97-
? 's'
98-
: ''
99-
}`
100-
: ''}
101-
{contact.nb_tasks
102-
? ` - ${contact.nb_tasks} task${
103-
contact.nb_tasks > 1
104-
? 's'
105-
: ''
106-
}`
107-
: ''}
95+
<NbRelations
96+
label="note"
97+
reference="contactNotes"
98+
target="contact_id"
99+
/>
100+
<NbRelations
101+
label="task"
102+
reference="tasks"
103+
target="contact_id"
104+
/>
108105
&nbsp;&nbsp;
109106
<TagsList />
110107
</>

packages/demo/src/dataGenerator/contactNotes.ts

-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ export const generateContactNotes = (db: Db): ContactNote[] => {
1010
return Array.from(Array(1200).keys()).map(id => {
1111
const contact = random.arrayElement(db.contacts);
1212
const date = randomDate(new Date(contact.first_seen)).toISOString();
13-
contact.nb_notes++;
1413
contact.last_seen = date > contact.last_seen ? date : contact.last_seen;
1514
return {
1615
id,

packages/demo/src/dataGenerator/contacts.ts

-1
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ export const generateContacts = (db: Db): Contact[] => {
7676
.arrayElements(db.tags, random.arrayElement([0, 0, 0, 1, 1, 2]))
7777
.map(tag => tag.id), // finalize
7878
sales_id: company.sales_id,
79-
nb_notes: 0,
8079
};
8180
});
8281
};

packages/demo/src/dataGenerator/dealNotes.ts

-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@ export const generateDealNotes = (db: Db) => {
1212
const date = company
1313
? randomDate(new Date(company.created_at)).toISOString()
1414
: randomDate().toISOString();
15-
deal.nb_notes++;
1615
return {
1716
id,
1817
deal_id: deal.id,

packages/demo/src/dataGenerator/deals.ts

-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export const generateDeals = (db: Db): Deal[] => {
5151
).toISOString(),
5252
sales_id: company.sales_id,
5353
index: 0,
54-
nb_notes: 0,
5554
};
5655
});
5756
// compute index based on stage
+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import { Typography, CircularProgress } from '@mui/material';
2+
import {
3+
useRecordContext,
4+
useReferenceManyFieldController,
5+
useTimeout,
6+
} from 'ra-core';
7+
import ErrorIcon from '@mui/icons-material/Error';
8+
9+
export const NbRelations = (props: NbNotesProps) => {
10+
const record = useRecordContext(props);
11+
const { reference, target, label, timeout = 1000 } = props;
12+
13+
const oneSecondHasPassed = useTimeout(timeout);
14+
15+
const { isLoading, error, total } = useReferenceManyFieldController({
16+
page: 1,
17+
perPage: 1,
18+
record,
19+
reference,
20+
source: 'id',
21+
target,
22+
});
23+
24+
const body = isLoading ? (
25+
oneSecondHasPassed ? (
26+
<CircularProgress size={14} />
27+
) : (
28+
''
29+
)
30+
) : error ? (
31+
<ErrorIcon color="error" fontSize="small" titleAccess="error" />
32+
) : total ? (
33+
`- ${total} ${label}${total > 1 ? 's' : ''}`
34+
) : (
35+
''
36+
);
37+
38+
return (
39+
<Typography component="span" variant="body2">
40+
{body}
41+
</Typography>
42+
);
43+
};
44+
45+
export interface NbNotesProps {
46+
reference: string;
47+
target: string;
48+
timeout?: number;
49+
label: string;
50+
}

packages/demo/src/notes/Note.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,6 @@ export const Note = ({
5151
notify('Note deleted', { type: 'info', undoable: true });
5252
update(reference, {
5353
id: record.id,
54-
data: { nb_notes: record.nb_notes - 1 },
5554
previousData: record,
5655
});
5756
},

packages/demo/src/types.ts

-2
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ export interface Contact extends RaRecord {
3737
tags: Identifier[];
3838
gender: string;
3939
sales_id: Identifier;
40-
nb_notes: number;
4140
status: string;
4241
background: string;
4342
}
@@ -63,7 +62,6 @@ export interface Deal extends RaRecord {
6362
start_at: string;
6463
sales_id: Identifier;
6564
index: number;
66-
nb_notes: number;
6765
}
6866

6967
export interface Tag extends RaRecord {

supabase/migrations/20230126140204_init.sql

+2-4
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ create table "public"."contacts" (
4444
"has_newsletter" boolean not null,
4545
"status" character varying not null,
4646
"tags" bigint[] not null,
47-
"sales_id" bigint not null,
48-
"nb_notes" smallint not null default 0
47+
"sales_id" bigint not null
4948
);
5049

5150

@@ -72,8 +71,7 @@ create table "public"."deals" (
7271
"updated_at" timestamp without time zone not null,
7372
"start_at" timestamp without time zone not null,
7473
"sales_id" bigint not null,
75-
"index" bigint not null,
76-
"nb_notes" smallint not null default 0
74+
"index" bigint not null
7775
);
7876

7977

0 commit comments

Comments
 (0)