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

Convert MergeUnusedPayeesModal.jsx to tsx #3866

Merged
merged 2 commits into from
Nov 21, 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
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
import React, { useState, useRef, useEffect } from 'react';
import React, { useState, useRef, useEffect, useCallback } from 'react';
import { useDispatch, useSelector } from 'react-redux';

import { t } from 'i18next';

import { replaceModal } from 'loot-core/src/client/actions/modals';
import { send } from 'loot-core/src/platform/client/fetch';
import { type PayeeEntity } from 'loot-core/types/models';

import { usePayees } from '../../hooks/usePayees';
import { theme } from '../../style';
Expand All @@ -17,13 +18,21 @@ import { View } from '../common/View';

const highlightStyle = { color: theme.pageTextPositive };

export function MergeUnusedPayeesModal({ payeeIds, targetPayeeId }) {
type MergeUnusedPayeesModalProps = {
payeeIds: Array<PayeeEntity['id']>;
targetPayeeId: PayeeEntity['id'];
};

export function MergeUnusedPayeesModal({
payeeIds,
targetPayeeId,
}: MergeUnusedPayeesModalProps) {
const allPayees = usePayees();
const modalStack = useSelector(state => state.modals.modalStack);
const isEditingRule = !!modalStack.find(m => m.name === 'edit-rule');
const dispatch = useDispatch();
const [shouldCreateRule, setShouldCreateRule] = useState(true);
const flashRef = useRef(null);
const flashRef = useRef<HTMLUListElement | null>(null);

useEffect(() => {
// Flash the scrollbar
Expand All @@ -41,40 +50,46 @@ export function MergeUnusedPayeesModal({ payeeIds, targetPayeeId }) {
//
// TODO: I think a custom `useSelector` hook that doesn't bind would
// be nice
const [payees] = useState(() =>
payeeIds.map(id => allPayees.find(p => p.id === id)),
const [payees] = useState<PayeeEntity[]>(() =>
allPayees.filter(p => payeeIds.includes(p.id)),
);
Comment on lines +53 to 55
Copy link
Contributor

Choose a reason for hiding this comment

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

Much nicer!

const targetPayee = allPayees.find(p => p.id === targetPayeeId);

if (!targetPayee) {
return null;
}

async function onMerge() {
await send('payees-merge', {
targetId: targetPayee.id,
mergeIds: payees.map(p => p.id),
});

let ruleId;
if (shouldCreateRule && !isEditingRule) {
const id = await send('rule-add-payee-rename', {
fromNames: payees.map(p => p.name),
to: targetPayee.id,
const onMerge = useCallback(
async (targetPayee: PayeeEntity) => {
await send('payees-merge', {
targetId: targetPayee.id,
mergeIds: payees.map(payee => payee.id),
});
ruleId = id;
}

return ruleId;
}
let ruleId;
if (shouldCreateRule && !isEditingRule) {
const id = await send('rule-add-payee-rename', {
fromNames: payees.map(payee => payee.name),
to: targetPayee.id,
});
ruleId = id;
}

return ruleId;
},
[shouldCreateRule, isEditingRule, payees],
);

async function onMergeAndCreateRule() {
const ruleId = await onMerge();
const onMergeAndCreateRule = useCallback(
async (targetPayee: PayeeEntity) => {
const ruleId = await onMerge(targetPayee);

if (ruleId) {
const rule = await send('rule-get', { id: ruleId });
dispatch(replaceModal('edit-rule', { rule }));
}
if (ruleId) {
const rule = await send('rule-get', { id: ruleId });
dispatch(replaceModal('edit-rule', { rule }));
}
},
[onMerge, dispatch],
);

const targetPayee = allPayees.find(p => p.id === targetPayeeId);
if (!targetPayee) {
return null;
}

return (
Expand Down Expand Up @@ -103,9 +118,9 @@ export function MergeUnusedPayeesModal({ payeeIds, targetPayeeId }) {
overflow: 'auto',
}}
>
{payees.map(p => (
<li key={p.id}>
<Text style={highlightStyle}>{p.name}</Text>
{payees.map(payee => (
<li key={payee.id}>
<Text style={highlightStyle}>{payee.name}</Text>
</li>
))}
</ul>
Expand Down Expand Up @@ -157,7 +172,7 @@ export function MergeUnusedPayeesModal({ payeeIds, targetPayeeId }) {
autoFocus
style={{ marginRight: 10 }}
onPress={() => {
onMerge();
onMerge(targetPayee);
close();
}}
>
Expand All @@ -167,7 +182,7 @@ export function MergeUnusedPayeesModal({ payeeIds, targetPayeeId }) {
<Button
style={{ marginRight: 10 }}
onPress={() => {
onMergeAndCreateRule();
onMergeAndCreateRule(targetPayee);
close();
}}
>
Expand Down
6 changes: 6 additions & 0 deletions upcoming-release-notes/3866.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
category: Maintenance
authors: [joel-jeremy]
---

Convert MergeUnusedPayeesModal.jsx to tsx