Skip to content

Commit c9bc973

Browse files
committed
CR fixes
1 parent a404cde commit c9bc973

File tree

17 files changed

+222
-306
lines changed

17 files changed

+222
-306
lines changed

src/components/v5/common/ActionSidebar/partials/forms/PaymentBuilderForm/hooks.ts

Lines changed: 117 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,120 @@ import {
2626
getPaymentBuilderPayload,
2727
} from './utils.ts';
2828

29+
export const useGetPaymentsValidationSchema = (
30+
networkInverseFee: string | undefined,
31+
) => {
32+
const { colony } = useColonyContext();
33+
const tokenLockStatesMap = useTokenLockStates();
34+
35+
return useMemo(
36+
() =>
37+
array()
38+
.of(
39+
object()
40+
.shape({
41+
recipient: string()
42+
.required(({ path }) => {
43+
const index = getLastIndexFromPath(path);
44+
if (index === undefined) {
45+
return formatText({ id: 'errors.recipient.required' });
46+
}
47+
return formatText(
48+
{ id: 'errors.recipient.requiredIn' },
49+
{ paymentIndex: index + 1 },
50+
);
51+
})
52+
.address(),
53+
slotId: number(),
54+
amount: string()
55+
.required(formatText({ id: 'errors.amount' }))
56+
.test(
57+
'more-than-zero',
58+
({ path }) => {
59+
const index = getLastIndexFromPath(path);
60+
if (index === undefined) {
61+
return formatText({
62+
id: 'errors.amount.greaterThanZero',
63+
});
64+
}
65+
return formatText(
66+
{ id: 'errors.amount.greaterThanZeroIn' },
67+
{ paymentIndex: index + 1 },
68+
);
69+
},
70+
(value, context) =>
71+
amountGreaterThanZeroValidation({
72+
value,
73+
context,
74+
colony,
75+
}),
76+
)
77+
.test('tokens-sum-exceeded', '', (value, context) =>
78+
allTokensAmountValidation({
79+
value,
80+
context,
81+
colony,
82+
networkInverseFee,
83+
}),
84+
),
85+
tokenAddress: string()
86+
.required()
87+
.test(
88+
'token-unlocked',
89+
formatText({ id: 'errors.amount.tokenIsLocked' }) || '',
90+
(value) =>
91+
!shouldPreventPaymentsWithTokenInColony(
92+
value || '',
93+
colony,
94+
tokenLockStatesMap,
95+
),
96+
),
97+
delay: string()
98+
.test(
99+
'is-bigger-than-max',
100+
({ path }) => {
101+
const index = getLastIndexFromPath(path);
102+
103+
return formatText(
104+
{ id: 'errors.delay.max' },
105+
{
106+
paymentIndex: index === undefined ? 1 : index + 1,
107+
max: CLAIM_DELAY_MAX_VALUE,
108+
},
109+
);
110+
},
111+
(value) => {
112+
if (!value) {
113+
return true;
114+
}
115+
116+
const unformattedValue = unformatNumeral(value);
117+
118+
return BigNumber.from(moveDecimal(unformattedValue, 4)).lte(
119+
moveDecimal(CLAIM_DELAY_MAX_VALUE, 4),
120+
);
121+
},
122+
)
123+
.required(({ path }) => {
124+
const index = getLastIndexFromPath(path);
125+
if (index === undefined) {
126+
return formatText({ id: 'errors.delay.empty' });
127+
}
128+
return formatText(
129+
{ id: 'errors.delay.emptyIndex' },
130+
{ paymentIndex: index + 1 },
131+
);
132+
}),
133+
})
134+
.defined()
135+
.required(),
136+
)
137+
.defined()
138+
.required(),
139+
[colony, networkInverseFee, tokenLockStatesMap],
140+
);
141+
};
142+
29143
export const useValidationSchema = (networkInverseFee: string | undefined) => {
30144
const { colony } = useColonyContext();
31145
const colonyTokens = useMemo(
@@ -35,7 +149,7 @@ export const useValidationSchema = (networkInverseFee: string | undefined) => {
35149
.map((colonyToken) => colonyToken.token) || [],
36150
[colony.tokens?.items],
37151
);
38-
const tokenLockStatesMap = useTokenLockStates();
152+
const paymentsValidation = useGetPaymentsValidationSchema(networkInverseFee);
39153

40154
return useMemo(
41155
() =>
@@ -46,107 +160,7 @@ export const useValidationSchema = (networkInverseFee: string | undefined) => {
46160
),
47161
decisionMethod: string().defined(),
48162
createdIn: number().defined(),
49-
payments: array()
50-
.of(
51-
object()
52-
.shape({
53-
recipient: string()
54-
.required(({ path }) => {
55-
const index = getLastIndexFromPath(path);
56-
if (index === undefined) {
57-
return formatText({ id: 'errors.recipient.required' });
58-
}
59-
return formatText(
60-
{ id: 'errors.recipient.requiredIn' },
61-
{ paymentIndex: index + 1 },
62-
);
63-
})
64-
.address(),
65-
amount: string()
66-
.required(formatText({ id: 'errors.amount' }))
67-
.test(
68-
'more-than-zero',
69-
({ path }) => {
70-
const index = getLastIndexFromPath(path);
71-
if (index === undefined) {
72-
return formatText({
73-
id: 'errors.amount.greaterThanZero',
74-
});
75-
}
76-
return formatText(
77-
{ id: 'errors.amount.greaterThanZeroIn' },
78-
{ paymentIndex: index + 1 },
79-
);
80-
},
81-
(value, context) =>
82-
amountGreaterThanZeroValidation({
83-
value,
84-
context,
85-
colony,
86-
}),
87-
)
88-
.test('tokens-sum-exceeded', '', (value, context) =>
89-
allTokensAmountValidation({
90-
value,
91-
context,
92-
colony,
93-
networkInverseFee,
94-
}),
95-
),
96-
tokenAddress: string()
97-
.required()
98-
.test(
99-
'token-unlocked',
100-
formatText({ id: 'errors.amount.tokenIsLocked' }) || '',
101-
(value) =>
102-
!shouldPreventPaymentsWithTokenInColony(
103-
value || '',
104-
colony,
105-
tokenLockStatesMap,
106-
),
107-
),
108-
delay: string()
109-
.test(
110-
'is-bigger-than-max',
111-
({ path }) => {
112-
const index = getLastIndexFromPath(path);
113-
114-
return formatText(
115-
{ id: 'errors.delay.max' },
116-
{
117-
paymentIndex: index === undefined ? 1 : index + 1,
118-
max: CLAIM_DELAY_MAX_VALUE,
119-
},
120-
);
121-
},
122-
(value) => {
123-
if (!value) {
124-
return true;
125-
}
126-
127-
const unformattedValue = unformatNumeral(value);
128-
129-
return BigNumber.from(
130-
moveDecimal(unformattedValue, 4),
131-
).lte(moveDecimal(CLAIM_DELAY_MAX_VALUE, 4));
132-
},
133-
)
134-
.required(({ path }) => {
135-
const index = getLastIndexFromPath(path);
136-
if (index === undefined) {
137-
return formatText({ id: 'errors.delay.empty' });
138-
}
139-
return formatText(
140-
{ id: 'errors.delay.emptyIndex' },
141-
{ paymentIndex: index + 1 },
142-
);
143-
}),
144-
})
145-
.defined()
146-
.required(),
147-
)
148-
.defined()
149-
.required(),
163+
payments: paymentsValidation,
150164
})
151165
.test(
152166
'is-in-colony',
@@ -168,7 +182,7 @@ export const useValidationSchema = (networkInverseFee: string | undefined) => {
168182
)
169183
.defined()
170184
.concat(ACTION_BASE_VALIDATION_SCHEMA),
171-
[colony, colonyTokens, networkInverseFee, tokenLockStatesMap],
185+
[colonyTokens, paymentsValidation],
172186
);
173187
};
174188

src/components/v5/common/ActionSidebar/partials/forms/PaymentBuilderForm/partials/PaymentBuilderRecipientsField/PaymentBuilderRecipientsField.tsx

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,6 @@ const PaymentBuilderRecipientsField: FC<PaymentBuilderRecipientsFieldProps> = ({
184184
isFullSize={isMobile}
185185
onClick={() => {
186186
fieldArrayMethods.append({
187-
// @TODO: Add slotId (find highest existing slotId and add 1)
188187
recipient: '',
189188
amount: '',
190189
tokenAddress: nativeToken?.tokenAddress || '',

src/components/v5/common/CompletedAction/partials/EditModeModal/EditModeModal.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@ const MSG = defineMessages({
6767

6868
interface EditModeModalProps extends ModalProps {
6969
expenditure: Expenditure;
70+
editValues: ExpenditurePayoutFieldValue[] | undefined;
7071
}
7172

7273
interface EditModeModalContentProps {
@@ -178,12 +179,13 @@ const EditModeModal: FC<EditModeModalProps> = ({
178179
isOpen,
179180
onClose,
180181
expenditure,
182+
editValues,
181183
...rest
182184
}) => {
183185
const { colony } = useColonyContext();
184186
const { user } = useAppContext();
185187
const { networkInverseFee = '0' } = useNetworkInverseFee();
186-
const { editValues, setIsEditMode } = useActionSidebarContext();
188+
const { setIsEditMode } = useActionSidebarContext();
187189

188190
const editLockedExpenditureMotion = useAsyncFunction({
189191
submit: ActionTypes.MOTION_EDIT_LOCKED_EXPENDITURE,
@@ -196,7 +198,7 @@ const EditModeModal: FC<EditModeModalProps> = ({
196198
success: ActionTypes.EXPENDITURE_EDIT_SUCCESS,
197199
});
198200

199-
const handleEditExpenditureViaMotion = async ({ decisionMethod }) => {
201+
const handleEditExpenditure = async ({ decisionMethod }) => {
200202
try {
201203
if (!expenditure) {
202204
return;
@@ -243,7 +245,7 @@ const EditModeModal: FC<EditModeModalProps> = ({
243245
>
244246
<Form
245247
className="flex h-full flex-col"
246-
onSubmit={handleEditExpenditureViaMotion}
248+
onSubmit={handleEditExpenditure}
247249
validationSchema={validationSchema}
248250
defaultValues={{ decisionMethod: {} }}
249251
>

src/components/v5/common/CompletedAction/partials/PaymentBuilder/PaymentBuilder.tsx

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,9 +148,7 @@ const PaymentBuilder = ({ action }: PaymentBuilderProps) => {
148148
(user?.walletAddress === initiatorUser?.walletAddress || hasPermissions);
149149

150150
const showEditOption =
151-
expenditure?.status !== ExpenditureStatus.Cancelled &&
152-
expenditure?.status !== ExpenditureStatus.Draft &&
153-
expenditure?.status !== ExpenditureStatus.Finalized &&
151+
expenditure?.status === ExpenditureStatus.Locked &&
154152
(user?.walletAddress === initiatorUser?.walletAddress || hasPermissions);
155153

156154
const expenditureMeatballOptions: MeatBallMenuItem[] = [

src/components/v5/common/CompletedAction/partials/PaymentBuilder/partials/EditStep/partials/EditRequests/hooks.ts

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const useCountChanges = (
77
let changes = 0;
88

99
if (newSlots.length !== oldSlots.length) {
10-
changes += newSlots.length - oldSlots.length;
10+
changes += Math.abs(newSlots.length - oldSlots.length);
1111
}
1212

1313
const sortedOldSlots = oldSlots
@@ -30,17 +30,18 @@ const useCountChanges = (
3030
changes += 1;
3131
}
3232

33-
newSlot?.payouts?.forEach((newPayout, payoutIndex) => {
34-
const oldPayout = oldSlot?.payouts?.[payoutIndex];
33+
const oldPayout = oldSlot?.payouts?.[0];
34+
const newPayout = newSlot?.payouts?.[0];
3535

36-
if (newPayout.tokenAddress !== oldPayout?.tokenAddress) {
36+
if (oldPayout && newPayout) {
37+
if (newPayout.tokenAddress !== oldPayout.tokenAddress) {
3738
changes += 1;
3839
}
3940

40-
if (newPayout.amount !== oldPayout?.amount) {
41+
if (newPayout.amount !== oldPayout.amount) {
4142
changes += 1;
4243
}
43-
});
44+
}
4445
});
4546

4647
return changes;

src/components/v5/common/CompletedAction/partials/PaymentBuilder/partials/PaymentBuilderTable/PaymentBuilderTable.tsx

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -310,7 +310,8 @@ const PaymentBuilderTable: FC<PaymentBuilderTableProps> = ({
310310
oldValues: oldSlot,
311311
};
312312
})
313-
.sort((a, b) => a.id - b.id);
313+
.sort((a, b) => a.id - b.id)
314+
.filter((item) => BigNumber.from(item.amount).gt(0));
314315

315316
const unchangedData = data
316317
.filter((item) => !changedSlots.map((slot) => slot.id).includes(item.id))
@@ -322,10 +323,9 @@ const PaymentBuilderTable: FC<PaymentBuilderTableProps> = ({
322323
const filteredData = dataWithChanges.filter((item) =>
323324
BigNumber.from(item.amount).gt(0),
324325
);
325-
const tableData = isEditStepActive ? dataWithChanges : filteredData;
326326

327327
const columns = useGetPaymentBuilderColumns({
328-
data: tableData,
328+
data: filteredData,
329329
status,
330330
slots: items,
331331
finalizedTimestamp,
@@ -447,7 +447,7 @@ const PaymentBuilderTable: FC<PaymentBuilderTableProps> = ({
447447
)}
448448
renderSubComponent={renderSubComponent}
449449
data={
450-
!tableData.length
450+
!filteredData.length
451451
? [
452452
{
453453
recipient: '0x000',
@@ -477,7 +477,7 @@ const PaymentBuilderTable: FC<PaymentBuilderTableProps> = ({
477477
isLoading: true,
478478
},
479479
]
480-
: tableData
480+
: filteredData
481481
}
482482
columns={columns}
483483
renderCellWrapper={(_, content) => content}

0 commit comments

Comments
 (0)