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

[Wave Collect] [Xero] Default select options #42360

Merged
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ function XeroAdvancedPage({policy}: WithPolicyConnectionsProps) {
const getSelectedAccountName = useMemo(
() => (accountID: string) => {
const selectedAccount = (bankAccounts ?? []).find((bank) => bank.id === accountID);
return selectedAccount?.name ?? '';
return selectedAccount?.name ?? bankAccounts?.[0]?.name ?? '';
},
[bankAccounts],
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ function XeroBillPaymentAccountSelectorPage({policy}: WithPolicyConnectionsProps

const {reimbursementAccountID, syncReimbursedReports} = policy?.connections?.xero?.config.sync ?? {};

const xeroSelectorOptions = useMemo<SelectorType[]>(
() =>
(bankAccounts ?? []).map(({id, name}) => ({
value: id,
text: name,
keyForList: id,
isSelected: reimbursementAccountID === id,
})),
[reimbursementAccountID, bankAccounts],
);
const xeroSelectorOptions = useMemo<SelectorType[]>(() => {
const isMatchFound = bankAccounts?.some(({id}) => id === reimbursementAccountID);

return (bankAccounts ?? []).map(({id, name}, index) => ({
value: id,
text: name,
keyForList: id,
isSelected: isMatchFound ? reimbursementAccountID === id : index === 0,
}));
}, [reimbursementAccountID, bankAccounts]);
Copy link
Contributor

Choose a reason for hiding this comment

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

This code is repeated 3 times, in 3 different files. Can you move it to an utility function?


const listHeaderComponent = useMemo(
() => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ function XeroInvoiceAccountSelectorPage({policy}: WithPolicyConnectionsProps) {

const {invoiceCollectionsAccountID, syncReimbursedReports} = policy?.connections?.xero?.config.sync ?? {};

const xeroSelectorOptions = useMemo<SelectorType[]>(
() =>
(bankAccounts ?? []).map(({id, name}) => ({
value: id,
text: name,
keyForList: id,
isSelected: invoiceCollectionsAccountID === id,
})),
[invoiceCollectionsAccountID, bankAccounts],
);
const xeroSelectorOptions = useMemo<SelectorType[]>(() => {
const isMatchFound = bankAccounts?.some(({id}) => id === invoiceCollectionsAccountID);

return (bankAccounts ?? []).map(({id, name}, index) => ({
value: id,
text: name,
keyForList: id,
isSelected: isMatchFound ? invoiceCollectionsAccountID === id : index === 0,
}));
}, [invoiceCollectionsAccountID, bankAccounts]);

const listHeaderComponent = useMemo(
() => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,16 @@ function XeroBankAccountSelectPage({policy}: WithPolicyConnectionsProps) {

const {nonReimbursableAccount: nonReimbursableAccountID} = policy?.connections?.xero?.config.export ?? {};

const xeroSelectorOptions = useMemo<SelectorType[]>(
() =>
(bankAccounts ?? []).map(({id, name}) => ({
value: id,
text: name,
keyForList: id,
isSelected: nonReimbursableAccountID === id,
})),
[nonReimbursableAccountID, bankAccounts],
);
const xeroSelectorOptions = useMemo<SelectorType[]>(() => {
const isMatchFound = bankAccounts?.some(({id}) => id === nonReimbursableAccountID);

return (bankAccounts ?? []).map(({id, name}, index) => ({
value: id,
text: name,
keyForList: id,
isSelected: isMatchFound ? nonReimbursableAccountID === id : index === 0,
}));
}, [nonReimbursableAccountID, bankAccounts]);

const listHeaderComponent = useMemo(
() => (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ function XeroExportConfigurationPage({policy}: WithPolicyConnectionsProps) {
const {bankAccounts} = policy?.connections?.xero?.data ?? {};
const selectedBankAccountName = useMemo(() => {
const selectedAccount = (bankAccounts ?? []).find((bank) => bank.id === exportConfiguration?.nonReimbursableAccount);
return selectedAccount?.name ?? '';
return selectedAccount?.name ?? bankAccounts?.[0]?.name ?? '';
}, [bankAccounts, exportConfiguration?.nonReimbursableAccount]);

const currentXeroOrganizationName = useMemo(() => getCurrentXeroOrganizationName(policy ?? undefined), [policy]);
Expand Down
Loading