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

feat: Allow changing network name before submitting a "Add network" FE-1183 #1725

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions .changeset/hip-beds-do.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"fuels-wallet": patch
---

Allow editing network name when adding.
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,17 @@ export function NetworkForm({
}: NetworkFormProps) {
const [isFirstShownTestConnectionBtn, setIsFirstShownTestConnectionBtn] =
useState(false);
const { control, formState } = form;
const { control, formState, setValue } = form;

const url = useWatch({ control, name: 'url' });
const chainId = useWatch({ control, name: 'chainId' });
const customName = useWatch({ control, name: 'name' });

useEffect(() => {
if (isReviewing && chainName) {
setValue('name', chainName);
}
}, [isReviewing, chainName, setValue]);

useEffect(() => {
if (isValid && chainId) {
Expand All @@ -54,12 +61,40 @@ export function NetworkForm({
return (
<Box.Stack css={{ width: '100%' }} gap="$4">
{isReviewing && (
<NetworkReviewCard
headerText="You're adding this network"
name={chainName || ''}
chainId={chainId}
url={url}
/>
<>
<NetworkReviewCard
headerText="You're adding this network"
name={customName || chainName || ''}
chainId={chainId}
url={url}
/>
<ControlledField
control={control}
name="name"
label={
<HelperIcon message="Customize the network name to avoid conflicts">
Network Name
</HelperIcon>
}
isRequired
isInvalid={Boolean(formState.errors?.name)}
render={({ field }) => (
<MotionInput {...animations.slideInTop()}>
<Input.Field
{...field}
id="network-name"
aria-label="Network name"
placeholder={chainName || 'Enter network name'}
/>
</MotionInput>
)}
/>
{formState.errors?.name && (
<Form.ErrorMessage>
{formState.errors.name.message}
</Form.ErrorMessage>
)}
</>
)}
{!isReviewing && (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,8 +146,11 @@ export const networksMachine = createMachine(
},
onDone: [
{
target: 'idle',
target: 'waitingAddNetwork',
cond: FetchMachine.hasError,
actions: assign({
error: (_, ev) => ev.data,
}),
},
{
actions: [
Expand Down
30 changes: 20 additions & 10 deletions packages/app/src/systems/Network/pages/AddNetwork/AddNetwork.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export function AddNetwork() {
const form = useNetworkForm({ context });
const url = useWatch({ control: form.control, name: 'url' });
const chainId = useWatch({ control: form.control, name: 'chainId' });
const name = useWatch({ control: form.control, name: 'name' });
const isValid =
form.formState.isDirty &&
form.formState.isValid &&
Expand Down Expand Up @@ -61,15 +62,24 @@ export function AddNetwork() {
});
}

function onAddNetwork() {
const name = chainInfoToAdd?.name || '';
handlers.addNetwork({
data: {
chainId: Number(chainId),
name,
url,
},
});
async function onAddNetwork() {
if (!name) return;
try {
await handlers.addNetwork({
data: {
chainId: Number(chainId),
name,
url,
},
});
} catch (error) {
if (error instanceof Error && error.message.includes('already exists')) {
form.setError('name', {
type: 'manual',
message: 'A network with this name already exists',
});
}
}
}

return (
Expand Down Expand Up @@ -101,7 +111,7 @@ export function AddNetwork() {
<Button
onPress={onAddNetwork}
intent="primary"
isDisabled={!isReviewingAddNetwork}
isDisabled={!isReviewingAddNetwork || !name}
isLoading={isLoading}
leftIcon={<Icon icon="Plus" />}
aria-label="Add new network"
Expand Down
8 changes: 6 additions & 2 deletions packages/app/src/systems/Network/services/network.ts
Original file line number Diff line number Diff line change
Expand Up @@ -353,8 +353,12 @@ export class NetworkService {
name,
url,
});
if (network) {
throw new Error('Network with Name or URL already exists');

if (network?.url === url) {
throw new Error('Network with URL already exists');
}
if (network?.name === name) {
throw new Error('Network with Name already exists');
}
}

Expand Down
Loading