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 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
46 changes: 31 additions & 15 deletions examples/crm/src/deals/DealCreate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import {
useRedirect,
useDataProvider,
useGetIdentity,
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 @@ -28,26 +29,41 @@ export const DealCreate = ({ open }: { open: boolean }) => {
redirect('/deals');
};

const onSuccess = (deal: Deal) => {
const queryClient = useQueryClient();

const onSuccess = async (deal: Deal) => {
redirect('/deals');
// 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', {
const deals = await dataProvider.getList('deals', {
pagination: { page: 1, perPage: 50 },
sort: { field: 'id', order: 'ASC' },
filter: { stage: deal.stage },
});

const updatedDeals = await Promise.all(
deals.data
.filter(oldDeal => oldDeal.id !== deal.id)
.map(async oldDeal => {
return (
await dataProvider.update('deals', {
id: oldDeal.id,
data: { index: oldDeal.index + 1 },
previousData: oldDeal,
})
)
)
);
).data;
})
);

const now = Date.now();

queryClient.setQueriesData<GetListResult | undefined>(
['deals', 'getList'],
res => {
if (!res) return res;
return { ...res, data: updatedDeals };
fzaninotto marked this conversation as resolved.
Show resolved Hide resolved
},
{ updatedAt: now }
);
};

const { identity } = useGetIdentity();
Expand Down