Skip to content

Commit

Permalink
enhancement(ExpenseForm): sort payee options (#8922)
Browse files Browse the repository at this point in the history
  • Loading branch information
Betree authored May 30, 2023
1 parent 179f258 commit 0634d11
Showing 1 changed file with 29 additions and 25 deletions.
54 changes: 29 additions & 25 deletions components/expenses/ExpenseFormPayeeStep.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,53 +140,57 @@ const refreshPayoutProfile = (formik, payoutProfiles) => {
formik.setValues({ ...formik.values, draft: omit(formik.values.draft, ['payee']), payee });
};

const getPayeeOptions = (intl, payoutProfiles) => {
const profileOptions = payoutProfiles.map(value => ({
value,
label: value.name,
[FLAG_COLLECTIVE_PICKER_COLLECTIVE]: true,
}));

const profilesByType = groupBy(profileOptions, p => p.value.type);

const myself = profilesByType[INDIVIDUAL] || [];
const myOrganizations = profilesByType[ORGANIZATION] || [];
const sortProfiles = profiles => {
return profiles?.sort((a, b) => a.slug.localeCompare(b.slug)) || [];
};

myOrganizations.push({
label: null,
value: null,
isDisabled: true,
[FLAG_NEW_COLLECTIVE]: true,
types: [CollectiveType.ORGANIZATION],
__background__: 'white',
});
const getPayeeOptions = (intl, payoutProfiles) => {
const profilesByType = groupBy(payoutProfiles, p => p.type);
const getOption = profile => ({ value: profile, label: profile.name, [FLAG_COLLECTIVE_PICKER_COLLECTIVE]: true });
const getProfileOptions = type => sortProfiles(profilesByType[type]).map(getOption);

const payeeOptions = [
{ options: myself, label: intl.formatMessage({ defaultMessage: 'Myself' }) },
{ options: myOrganizations, label: intl.formatMessage({ id: 'organization', defaultMessage: 'My Organizations' }) },
{
label: intl.formatMessage({ defaultMessage: 'Myself' }),
options: getProfileOptions(INDIVIDUAL),
},
{
label: intl.formatMessage({ id: 'organization', defaultMessage: 'My Organizations' }),
options: [
...getProfileOptions(ORGANIZATION),
{
label: null,
value: null,
isDisabled: true,
[FLAG_NEW_COLLECTIVE]: true,
types: [ORGANIZATION],
__background__: 'white',
},
],
},
];

if (profilesByType[COLLECTIVE]?.length) {
payeeOptions.push({
options: profilesByType[COLLECTIVE],
options: getProfileOptions(COLLECTIVE),
label: intl.formatMessage({ id: 'collective', defaultMessage: 'My Collectives' }),
});
}
if (profilesByType[FUND]?.length) {
payeeOptions.push({
options: profilesByType[FUND],
options: getProfileOptions(FUND),
label: intl.formatMessage({ id: 'funds', defaultMessage: 'My Funds' }),
});
}
if (profilesByType[PROJECT]?.length) {
payeeOptions.push({
options: profilesByType[PROJECT],
options: getProfileOptions(PROJECT),
label: intl.formatMessage({ defaultMessage: 'My Projects' }),
});
}
if (profilesByType[EVENT]?.length) {
payeeOptions.push({
options: profilesByType[EVENT],
options: getProfileOptions(EVENT),
label: intl.formatMessage({ id: 'events', defaultMessage: 'My Events' }),
});
}
Expand Down

2 comments on commit 0634d11

@vercel
Copy link

@vercel vercel bot commented on 0634d11 May 30, 2023

Choose a reason for hiding this comment

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

@vercel
Copy link

@vercel vercel bot commented on 0634d11 May 30, 2023

Choose a reason for hiding this comment

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

Please sign in to comment.