Skip to content

Commit

Permalink
fix: show current delegated tracks
Browse files Browse the repository at this point in the history
  • Loading branch information
tuul-wq committed Jan 9, 2025
1 parent 4e4428e commit d903bf8
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -157,13 +157,11 @@ export const delegateDetailsModel = {
$delegate,
$activeAccounts,
$activeTracks,
$uniqueTracks: $activeTracks.map((tracks) =>
uniq(
Object.values(tracks)
.map((tracks) => [...tracks])
.flat(),
),
),
$uniqueTracks: $activeTracks.map((tracks) => {
const flatTracks = Object.values(tracks).flatMap((tracks) => [...tracks]);

return uniq(flatTracks);
}),
$activeDelegations,

$isAddAvailable,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,10 @@ const $api = combine(
walletData: $walletData,
},
({ apis, walletData }) => {
return walletData?.chain ? apis[walletData.chain.chainId] : undefined;
if (!walletData.chain) return null;

return apis[walletData.chain.chainId] || null;
},
{ skipVoid: false },
);

const $transactions = combine(
Expand All @@ -104,7 +105,7 @@ const $transactions = combine(
txWrappers: $txWrappers,
},
({ api, walletData, coreTxs, txWrappers }) => {
if (!api || !walletData.chain) return undefined;
if (!api || !walletData.chain) return null;

return coreTxs.map((tx) =>
transactionService.getWrappedTransaction({
Expand All @@ -115,7 +116,6 @@ const $transactions = combine(
}),
);
},
{ skipVoid: false },
);

// Transaction & Form
Expand Down Expand Up @@ -475,17 +475,14 @@ sample({
return Boolean(walletData.wallet) && Boolean(coreTxs) && Boolean(txWrappers);
},
fn: ({ walletData, coreTxs, txWrappers }) => {
const txs = coreTxs!.map(
(coreTx) =>
({
initiatorWallet: walletData.wallet!.id,
coreTx,
txWrappers,
groupId: Date.now(),
}) as BasketTransaction,
);

return txs;
return coreTxs!.map((coreTx) => {
return {
initiatorWallet: walletData.wallet!.id,
coreTx,
txWrappers,
groupId: Date.now(),
} as BasketTransaction;
});
},
target: basketModel.events.transactionsCreated,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ const tracksSelected = createEvent<number[]>();
const accountsChanged = createEvent<Account[]>();

const $tracks = createStore<number[]>([]).reset(formInitiated);
const $delegatedTracks = createStore<string[]>([]).reset(formInitiated);
const $votedTracks = createStore<string[]>([]).reset(formInitiated);
const $delegatedTracks = createStore<string[]>([]).reset(formInitiated);
const $votesToRemove = createStore<VotesToRemove[]>([]).reset(formInitiated);

const $accounts = createStore<Account[]>([]);
Expand Down Expand Up @@ -122,27 +122,34 @@ sample({
sample({
clock: [votingAggregate.$activeWalletVotes, $addresses],
source: {
tracks: $tracks,
votes: votingAggregate.$activeWalletVotes,
addresses: $addresses,
delegate: $delegate,
},
fn: ({ addresses, votes, delegate }) => {
fn: ({ tracks, addresses, votes, delegate }) => {
const activeTracks = new Set<string>();
const delegatedTracks = new Set<string>();
const otherDelegatedTracks = new Set<string>();
const currentDelegatedTracks = new Set<number>();
const votesToRemove = [];

for (const [address, voteList] of Object.entries(votes)) {
if (!addresses.includes(address)) continue;
for (const [track, vote] of Object.entries(voteList)) {
const isNotCurrentDelegate =
votingService.isDelegating(vote) && delegate && toAccountId(delegate.accountId) !== toAccountId(vote.target);
const isDelegateExist = votingService.isDelegating(vote) && delegate;
const isCurrentDelegate = isDelegateExist && toAccountId(delegate.accountId) === toAccountId(vote.target);
const isOtherDelegate = isDelegateExist && toAccountId(delegate.accountId) !== toAccountId(vote.target);

if ((votingService.isCasting(vote) && !votingService.isUnlockingDelegation(vote)) || isNotCurrentDelegate) {
if ((votingService.isCasting(vote) && !votingService.isUnlockingDelegation(vote)) || isOtherDelegate) {
activeTracks.add(track);
}

if (isNotCurrentDelegate) {
delegatedTracks.add(track);
if (isOtherDelegate) {
otherDelegatedTracks.add(track);
}

if (isCurrentDelegate) {
currentDelegatedTracks.add(Number(track));
}

if (votingService.isCasting(vote) && !votingService.isUnlockingDelegation(vote)) {
Expand All @@ -154,12 +161,14 @@ sample({
}

return {
tracks: [...tracks, ...currentDelegatedTracks],
votedTracks: [...activeTracks],
delegatedTracks: [...delegatedTracks],
delegatedTracks: [...otherDelegatedTracks],
votesToRemove: [...votesToRemove],
};
},
target: spread({
tracks: $tracks,
votedTracks: $votedTracks,
delegatedTracks: $delegatedTracks,
votesToRemove: $votesToRemove,
Expand Down Expand Up @@ -191,7 +200,10 @@ sample({

sample({
clock: tracksSelected,
source: { tracks: $tracks, votedTracks: $votedTracks },
source: {
tracks: $tracks,
votedTracks: $votedTracks,
},
fn: ({ tracks, votedTracks }, newTracks) => {
const resultArray = newTracks.filter((num) => !votedTracks.includes(num.toString()));

Expand Down
34 changes: 15 additions & 19 deletions src/renderer/widgets/EditDelegationModal/ui/EditDelegation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useUnit } from 'effector-react';

import { useI18n } from '@/shared/i18n';
import { useModalClose } from '@/shared/lib/hooks';
import { Step, isStep } from '@/shared/lib/utils';
import { Step, isStep, nullable } from '@/shared/lib/utils';
import { BaseModal, Button } from '@/shared/ui';
import { OperationTitle } from '@/entities/chain';
import { SignButton } from '@/entities/operations';
Expand Down Expand Up @@ -32,12 +32,18 @@ export const EditDelegation = () => {
editDelegationModel.output.flowFinished,
);

if (!walletData) {
return null;
if (isStep(step, Step.INIT)) {
return (
<DelegateForm
isOpen={isStep(step, Step.INIT)}
onClose={closeModal}
onGoBack={() => editDelegationModel.events.stepChanged(Step.SELECT_TRACK)}
/>
);
}

if (isStep(step, Step.SUBMIT)) {
return <OperationSubmit isOpen={isModalOpen} onClose={closeModal} />;
if (isStep(step, Step.SELECT_TRACK)) {
return <SelectTrackForm isOpen={isStep(step, Step.SELECT_TRACK)} onClose={closeModal} />;
}

if (isStep(step, Step.BASKET)) {
Expand All @@ -52,21 +58,11 @@ export const EditDelegation = () => {
);
}

if (isStep(step, Step.SELECT_TRACK)) {
return <SelectTrackForm isOpen={isStep(step, Step.SELECT_TRACK)} onClose={closeModal} />;
}

if (isStep(step, Step.INIT)) {
return (
<DelegateForm
isOpen={isStep(step, Step.INIT)}
onClose={closeModal}
onGoBack={() => editDelegationModel.events.stepChanged(Step.SELECT_TRACK)}
/>
);
if (isStep(step, Step.SUBMIT)) {
return <OperationSubmit isOpen={isModalOpen} onClose={closeModal} />;
}

if (transactions === undefined) {
if (nullable(transactions)) {
return null;
}

Expand Down Expand Up @@ -109,7 +105,7 @@ export const EditDelegation = () => {
</div>
}
>
{transactions?.map((t, index) => (
{transactions.map((_, index) => (
<ConfirmSlider.Item key={index}>
<Confirmation id={index} hideSignButton />
</ConfirmSlider.Item>
Expand Down

0 comments on commit d903bf8

Please sign in to comment.