Skip to content

Commit f0cbd06

Browse files
authored
Merge d0bc569 into 18a1cac
2 parents 18a1cac + d0bc569 commit f0cbd06

File tree

13 files changed

+167
-77
lines changed

13 files changed

+167
-77
lines changed

src/components/ActionsBuilder/Action/Action.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,9 @@ export const ActionRow: React.FC<ActionViewProps> = ({
177177
isOpen={isEditActionModalOpen}
178178
setIsOpen={setIsEditActionModalOpen}
179179
action={decodedAction}
180-
onAddAction={onEdit}
180+
onAddActions={null}
181+
onEditAction={onEdit}
182+
isEditing={true}
181183
/>
182184
)}
183185
</CardWrapperWithMargin>

src/components/ActionsBuilder/Option/Option.tsx

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -43,16 +43,16 @@ export const OptionRow: React.FC<OptionRowProps> = ({
4343
} = useSortable({ id: option.id });
4444
const [isActionsModalOpen, setIsActionsModalOpen] = useState(false);
4545

46-
function addAction(action: DecodedAction) {
46+
function addActions(actions: DecodedAction[]) {
4747
onChange({
4848
...option,
49-
decodedActions: [...option.decodedActions, action],
49+
decodedActions: [...option.decodedActions, ...actions],
5050
});
5151
}
5252

53-
function updateAction(index: number, action: DecodedAction) {
54-
const updatedActions = option?.decodedActions.map((a, i) =>
55-
index === i ? action : a
53+
function updateAction(action: DecodedAction) {
54+
const updatedActions = option?.decodedActions.map(a =>
55+
a.id === action.id ? action : a
5656
);
5757
onChange({ ...option, decodedActions: updatedActions });
5858
}
@@ -118,7 +118,7 @@ export const OptionRow: React.FC<OptionRowProps> = ({
118118
key={index}
119119
isEditable={true}
120120
decodedAction={action}
121-
onEdit={updatedAction => updateAction(index, updatedAction)}
121+
onEdit={updateAction}
122122
onRemove={targetAction => removeAction(targetAction)}
123123
/>
124124
);
@@ -137,10 +137,12 @@ export const OptionRow: React.FC<OptionRowProps> = ({
137137
<ActionModal
138138
isOpen={isActionsModalOpen}
139139
setIsOpen={setIsActionsModalOpen}
140-
onAddAction={action => {
141-
addAction(action);
140+
onAddActions={action => {
141+
addActions(action);
142142
setIsActionsModalOpen(false);
143143
}}
144+
onEditAction={updateAction}
145+
isEditing={false}
144146
/>
145147
</OptionWrapper>
146148
);

src/components/ActionsBuilder/SupportedActions/ERC20Transfer/ERC20TransferEditor.tsx

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -103,26 +103,30 @@ const ERC20TransferEditor: React.FC<ActionEditorProps> = ({
103103
if (values.token.type === TokenType.ERC20) {
104104
const ERC20Contract = new utils.Interface(ERC20.abi);
105105

106-
onSubmit({
107-
...decodedCall,
108-
callType: SupportedAction.ERC20_TRANSFER,
109-
to: values.token.address,
110-
value: BigNumber.from(0),
111-
function: ERC20Contract.getFunction('transfer'),
112-
args: {
113-
_value: values.amount,
114-
_to: values.recipientAddress,
106+
onSubmit([
107+
{
108+
...decodedCall,
109+
callType: SupportedAction.ERC20_TRANSFER,
110+
to: values.token.address,
111+
value: BigNumber.from(0),
112+
function: ERC20Contract.getFunction('transfer'),
113+
args: {
114+
_value: values.amount,
115+
_to: values.recipientAddress,
116+
},
115117
},
116-
});
118+
]);
117119
} else {
118-
onSubmit({
119-
...decodedCall,
120-
callType: SupportedAction.NATIVE_TRANSFER,
121-
to: values.recipientAddress,
122-
value: values.amount,
123-
function: null,
124-
args: null,
125-
});
120+
onSubmit([
121+
{
122+
...decodedCall,
123+
callType: SupportedAction.NATIVE_TRANSFER,
124+
to: values.recipientAddress,
125+
value: values.amount,
126+
function: null,
127+
args: null,
128+
},
129+
]);
126130
}
127131
};
128132

src/components/ActionsBuilder/SupportedActions/RepMint/RepMintEditor.tsx

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -63,14 +63,16 @@ export const Mint: React.FC<ActionEditorProps> = ({
6363
};
6464

6565
const submitAction = () => {
66-
onSubmit({
67-
...decodedCall,
68-
args: {
69-
...decodedCall.args,
70-
to: recipient,
71-
amount: ethers.utils.parseUnits(repAmount.toString()),
66+
onSubmit([
67+
{
68+
...decodedCall,
69+
args: {
70+
...decodedCall.args,
71+
to: recipient,
72+
amount: ethers.utils.parseUnits(repAmount.toString()),
73+
},
7274
},
73-
});
75+
]);
7476
};
7577

7678
return (

src/components/ActionsBuilder/SupportedActions/SetPermissions/SetPermissionsEditor.tsx

Lines changed: 64 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@ import { Controller, useForm } from 'react-hook-form';
66
import { FiChevronDown } from 'react-icons/fi';
77

88
import { ParsedDataInterface, TABS } from './types';
9-
import { ANY_FUNC_SIGNATURE, ERC20_TRANSFER_SIGNATURE } from 'utils';
9+
import {
10+
ANY_FUNC_SIGNATURE,
11+
ERC20_TRANSFER_SIGNATURE,
12+
ERC20_APPROVE_SIGNATURE,
13+
} from 'utils';
1014
import { resolveUri } from 'utils/url';
1115
import { ActionEditorProps } from '..';
1216
import { useTokenList } from 'hooks/Guilds/tokens/useTokenList';
@@ -45,6 +49,7 @@ interface FormValues {
4549
const Permissions: React.FC<ActionEditorProps> = ({
4650
decodedCall,
4751
onSubmit,
52+
isEdit,
4853
}) => {
4954
const parsedData = useMemo<ParsedDataInterface>(() => {
5055
if (!decodedCall) return null;
@@ -111,28 +116,76 @@ const Permissions: React.FC<ActionEditorProps> = ({
111116
updateFunctionSignature(value);
112117
};
113118

114-
const submitAction = (values: FormValues) => {
115-
const isAssetTransferCall = activeTab === TABS.ASSET_TRANSFER;
119+
const submitAssetTransfer = (values: FormValues) => {
120+
const baseCall = {
121+
...decodedCall,
122+
args: {
123+
...decodedCall.args,
124+
to: values.tokenAddress,
125+
valueAllowed: BigNumber.from(0),
126+
// "from" field set by default previously as guild id
127+
},
128+
optionalProps: {
129+
asset: values.tokenAddress,
130+
tab: TABS.ASSET_TRANSFER,
131+
},
132+
};
133+
const newAssetTransferCall: DecodedCall = {
134+
...baseCall,
135+
args: {
136+
...baseCall.args,
137+
functionSignature: ERC20_TRANSFER_SIGNATURE,
138+
},
139+
optionalProps: {
140+
...baseCall.optionalProps,
141+
functionName: '',
142+
},
143+
};
116144

145+
const newApprovalAssetCall = {
146+
...decodedCall,
147+
args: {
148+
...decodedCall.args,
149+
functionSignature: ERC20_APPROVE_SIGNATURE,
150+
},
151+
optionalProps: {
152+
...baseCall.optionalProps,
153+
functionName: 'approve(address,uint256)',
154+
},
155+
};
156+
157+
if (isEdit) {
158+
// in case of edit mode we submit only one action that is being edited
159+
return parsedData?.functionSignature === ERC20_APPROVE_SIGNATURE
160+
? onSubmit([newApprovalAssetCall])
161+
: onSubmit([newAssetTransferCall]);
162+
}
163+
return onSubmit([newAssetTransferCall, newApprovalAssetCall]);
164+
};
165+
166+
const submitFunctionCall = (values: FormValues) => {
117167
const newCall: DecodedCall = {
118168
...decodedCall,
119169
args: {
120170
...decodedCall.args,
121-
to: isAssetTransferCall ? values.tokenAddress : values.toAddress,
171+
to: values.toAddress,
122172
// native value allowed
123-
valueAllowed: isAssetTransferCall ? BigNumber.from(0) : values.amount,
124-
functionSignature: isAssetTransferCall
125-
? ERC20_TRANSFER_SIGNATURE
126-
: values.functionSignature,
173+
valueAllowed: values.amount,
174+
functionSignature: values.functionSignature,
127175
// "from" field set by default previously as guild id
128176
},
129177
optionalProps: {
130-
functionName: isAssetTransferCall ? '' : values.functionName,
131-
asset: isAssetTransferCall ? values.tokenAddress : '',
178+
functionName: values.functionName,
179+
asset: '',
132180
tab: activeTab,
133181
},
134182
};
135-
onSubmit(newCall);
183+
onSubmit([newCall]);
184+
};
185+
186+
const submitAction = (values: FormValues) => {
187+
const isAssetTransferCall = activeTab === TABS.ASSET_TRANSFER;
188+
(isAssetTransferCall ? submitAssetTransfer : submitFunctionCall)(values);
136189
};
137190

138191
const tabArray = [

src/components/ActionsBuilder/SupportedActions/SetPermissions/SetPermissionsInfoLine.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import { Flex } from 'components/primitives/Layout';
99
import { useNetwork } from 'wagmi';
1010
import { useTokenList } from 'hooks/Guilds/tokens/useTokenList';
1111
import { resolveUri } from 'utils/url';
12-
import { shortenAddress } from 'utils';
12+
import {
13+
shortenAddress,
14+
ERC20_TRANSFER_SIGNATURE,
15+
ERC20_APPROVE_SIGNATURE,
16+
} from 'utils';
1317

1418
const SetPermissionsInfoLine: React.FC<ActionViewProps> = ({
1519
decodedCall,
@@ -48,7 +52,15 @@ const SetPermissionsInfoLine: React.FC<ActionViewProps> = ({
4852
<Segment>
4953
<BiCheckShield size={16} />
5054
</Segment>
51-
<Segment>{t('setPermissionsFor')}</Segment>
55+
<Segment>
56+
{t(
57+
parsedData?.functionSignature === ERC20_APPROVE_SIGNATURE
58+
? 'setApprovalPermissionsFor'
59+
: parsedData?.functionSignature === ERC20_TRANSFER_SIGNATURE
60+
? 'setTransferPermissionsFor'
61+
: 'setPermissionsFor'
62+
)}
63+
</Segment>
5264
<Segment>
5365
{currentToken && currentToken.address ? (
5466
<>

src/components/ActionsBuilder/SupportedActions/UpdateENSContent/UpdateENSContentEditor.tsx

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -56,20 +56,22 @@ const UpdateENSContentEditor: React.FC<ActionEditorProps> = ({
5656
const { nameHash } = convertToNameHash(fullEnsName);
5757
const { contentHash } = convertToContentHash(values.ipfsHash);
5858

59-
onSubmit({
60-
...decodedCall,
61-
to: resolver?.address,
62-
args: {
63-
...decodedCall.args,
64-
node: nameHash,
65-
hash: contentHash,
59+
onSubmit([
60+
{
61+
...decodedCall,
62+
to: resolver?.address,
63+
args: {
64+
...decodedCall.args,
65+
node: nameHash,
66+
hash: contentHash,
67+
},
68+
optionalProps: {
69+
...decodedCall.optionalProps,
70+
ensName: values.ensName,
71+
ipfsHash: values.ipfsHash,
72+
},
6673
},
67-
optionalProps: {
68-
...decodedCall.optionalProps,
69-
ensName: values.ensName,
70-
ipfsHash: values.ipfsHash,
71-
},
72-
});
74+
]);
7375
};
7476

7577
if (chain.id === LOCALHOST_ID)

src/components/ActionsBuilder/SupportedActions/index.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,8 @@ export interface ActionViewProps {
3232

3333
export interface ActionEditorProps extends ActionViewProps {
3434
updateCall?: (updatedCall: DecodedCall) => void;
35-
onSubmit: (decodedCall: DecodedCall) => void;
35+
onSubmit: (decodedCall: DecodedCall[]) => void;
36+
isEdit?: boolean;
3637
}
3738

3839
type SupportedActionViews = {

src/components/ActionsModal/ActionsModal.test.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ describe('ActionsModal', () => {
3737
props = {
3838
isOpen: false,
3939
setIsOpen: jest.fn(),
40-
onAddAction: jest.fn(), // (action: DecodedAction) => void;
40+
onAddActions: jest.fn(), // (actions: DecodedAction[]) => void;
4141
};
4242
});
4343

src/components/ActionsModal/ActionsModal.tsx

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,9 @@ const ActionModal: React.FC<ActionModalProps> = ({
3232
action,
3333
isOpen,
3434
setIsOpen,
35-
onAddAction,
35+
onAddActions,
36+
onEditAction,
37+
isEditing,
3638
}) => {
3739
const { t } = useTranslation();
3840
const { guildId } = useTypedParams();
@@ -112,8 +114,11 @@ const ActionModal: React.FC<ActionModalProps> = ({
112114
fn={fn}
113115
defaultValues={data?.args}
114116
onSubmit={args => {
115-
onAddAction({
116-
id: `action-${Math.random()}`,
117+
const submitAction = {
118+
id:
119+
isEditing && !!action?.id
120+
? action.id
121+
: `action-${Math.random()}`,
117122
contract: contractInterface,
118123
decodedCall: {
119124
callType: SupportedAction.GENERIC_CALL,
@@ -136,7 +141,9 @@ const ActionModal: React.FC<ActionModalProps> = ({
136141
...payableFnData,
137142
},
138143
}),
139-
});
144+
};
145+
if (isEditing && onEditAction) onEditAction(submitAction);
146+
else if (onAddActions) onAddActions([submitAction]);
140147
handleClose();
141148
}}
142149
/>
@@ -160,7 +167,8 @@ const ActionModal: React.FC<ActionModalProps> = ({
160167
<Editor
161168
decodedCall={data}
162169
updateCall={setData}
163-
onSubmit={saveSupportedAction}
170+
onSubmit={handleEditorSubmit}
171+
isEdit={isEditing}
164172
/>
165173
</EditorWrapper>
166174
);
@@ -207,20 +215,22 @@ const ActionModal: React.FC<ActionModalProps> = ({
207215
setSelectedAction(action);
208216
}
209217

210-
function saveSupportedAction(call?: DecodedCall) {
211-
const decodedCall = call ?? data;
212-
218+
function buildAction(decodedCall: DecodedCall): DecodedAction {
219+
if (!decodedCall) return null;
213220
const defaultDecodedAction = defaultValues[decodedCall.callType];
214221

215-
if (!selectedAction || !decodedCall) return;
216-
217222
const decodedAction: DecodedAction = {
218-
id: `action-${Math.random()}`,
223+
id: isEditing && !!action?.id ? action.id : `action-${Math.random()}`, // Mantain id if is edit mode & id is exists
219224
decodedCall,
220225
contract: defaultDecodedAction.contract,
221226
};
227+
return decodedAction;
228+
}
222229

223-
onAddAction(decodedAction);
230+
function handleEditorSubmit(calls?: DecodedCall[]) {
231+
if (!calls) return;
232+
if (isEditing && onEditAction) onEditAction(buildAction(calls[0]));
233+
else onAddActions(calls.map(buildAction));
224234
handleClose();
225235
}
226236

0 commit comments

Comments
 (0)