Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Demo] Fix new deal does not appear in the correct order in CRM #9582

Merged
merged 2 commits into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion examples/crm/src/dataGenerator/deals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const generateDeals = (db: Db): Deal[] => {
deals
.filter(deal => deal.stage === stage)
.forEach((deal, index) => {
deals[deal.id].index = index + 1;
deals[deal.id].index = index;
});
});
return deals;
Expand Down
63 changes: 43 additions & 20 deletions examples/crm/src/deals/DealCreate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ import {
useRedirect,
useDataProvider,
useGetIdentity,
useListContext,
GetListResult,
} from 'react-admin';
import { Dialog } from '@mui/material';

import { useQueryClient } from 'react-query';
import { stageChoices } from './stages';
import { typeChoices } from './types';
import { Deal } from '../types';
Expand All @@ -23,31 +25,52 @@ const validateRequired = required();
export const DealCreate = ({ open }: { open: boolean }) => {
const redirect = useRedirect();
const dataProvider = useDataProvider();
const { data: allDeals } = useListContext<Deal>();

const handleClose = () => {
redirect('/deals');
};

const onSuccess = (deal: Deal) => {
redirect('/deals');
const queryClient = useQueryClient();

const onSuccess = async (deal: Deal) => {
// increase the index of all deals in the same stage as the new deal
dataProvider
.getList('deals', {
pagination: { page: 1, perPage: 50 },
sort: { field: 'id', order: 'ASC' },
filter: { stage: deal.stage },
})
.then(({ data: deals }) =>
Promise.all(
deals.map(oldDeal =>
dataProvider.update('deals', {
id: oldDeal.id,
data: { index: oldDeal.index + 1 },
previousData: oldDeal,
})
)
)
);
// first, get the list of deals in the same stage
const deals = allDeals.filter(
(d: Deal) => d.stage === deal.stage && d.id !== deal.id
);
// update the actual deals in the database
await Promise.all(
deals.map(async oldDeal =>
dataProvider.update('deals', {
id: oldDeal.id,
data: { index: oldDeal.index + 1 },
previousData: oldDeal,
})
)
);
// refresh the list of deals in the cache as we used dataProvider.update(),
// which does not update the cache
const dealsById = deals.reduce(
(acc, d) => ({
...acc,
[d.id]: { ...d, index: d.index + 1 },
}),
{} as { [key: string]: Deal }
);
const now = Date.now();
queryClient.setQueriesData<GetListResult | undefined>(
['deals', 'getList'],
res => {
if (!res) return res;
return {
...res,
data: res.data.map((d: Deal) => dealsById[d.id] || d),
};
},
{ updatedAt: now }
);
redirect('/deals');
};

const { identity } = useGetIdentity();
Expand Down
2 changes: 1 addition & 1 deletion examples/crm/src/deals/DealList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ const DealList = () => {
component="div"
>
<DealListContent />
<DealCreate open={!!matchCreate} />
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because this way, DealCreate can grab the list of deals from the ListContext - this saves one query and makes the post save process faster.

</List>
<DealCreate open={!!matchCreate} />
<DealShow open={!!matchShow} id={matchShow?.params.id} />
</>
);
Expand Down
Loading