From 259da570e23b1a62a2c6b0a37ad22c38b7629d01 Mon Sep 17 00:00:00 2001 From: Josh Miller Date: Thu, 29 Feb 2024 11:56:57 +0000 Subject: [PATCH] Refactor service creation logic in SpawnStakingCheck component --- .../components/Spawn/SpawnStakingCheck.tsx | 90 ++++++++++--------- 1 file changed, 49 insertions(+), 41 deletions(-) diff --git a/frontend/components/Spawn/SpawnStakingCheck.tsx b/frontend/components/Spawn/SpawnStakingCheck.tsx index 16c0174b4..b9f54e819 100644 --- a/frontend/components/Spawn/SpawnStakingCheck.tsx +++ b/frontend/components/Spawn/SpawnStakingCheck.tsx @@ -54,47 +54,51 @@ export const SpawnStakingCheck = ({ message.error('Service creation already in progress'); return; } + setIsCreating(true); - return createService({ - ...serviceTemplate, - configuration: { - ...serviceTemplate.configuration, - rpc, - use_staking: isStaking, - }, - }) - .then((service: Service) => { - setService(service); - - // Set agent funding requirements - if (service.chain_data?.instances) { - setAgentFundRequirements( - service.chain_data.instances.reduce( - (acc: FundsRequirementMap, address: Address) => ({ - ...acc, - [address]: - serviceTemplate.configuration.fund_requirements.agent, - }), - {}, - ), - ); - } - - // Set multisig funding requirements from multisig/safe - if (service.chain_data?.multisig) { - const { multisig } = service.chain_data; - const { safe } = serviceTemplate.configuration.fund_requirements; - setAgentFundRequirements((prev: FundsRequirementMap) => ({ - ...prev, - [multisig]: safe, - })); - } - return Promise.resolve(service); - }) - .catch(() => { - return Promise.reject(); - }) - .finally(() => setIsCreating(false)); + + let service: Service; + try { + service = await createService({ + ...serviceTemplate, + configuration: { + ...serviceTemplate.configuration, + rpc, + use_staking: isStaking, + }, + }); + } catch (e) { + message.error('Failed to create service'); + setIsCreating(false); + return; + } + + setService(service); + + // Set agent funding requirements + if (service.chain_data?.instances) { + setAgentFundRequirements( + service.chain_data.instances.reduce( + (acc: FundsRequirementMap, address: Address) => ({ + ...acc, + [address]: serviceTemplate.configuration.fund_requirements.agent, + }), + {}, + ), + ); + } + + // Set multisig funding requirements from multisig/safe + if (service.chain_data?.multisig) { + const { multisig } = service.chain_data; + const { safe } = serviceTemplate.configuration.fund_requirements; + setAgentFundRequirements((prev: FundsRequirementMap) => ({ + ...prev, + [multisig]: safe, + })); + } + + return service; }, [ createService, @@ -145,7 +149,9 @@ export const SpawnStakingCheck = ({ message.error(`${userPublicKey} requires more OLAS to stake`); return setButtonClicked(undefined); } + const service: Service | undefined = await create(true); + if (!service) { message.error('Failed to create service'); } else { @@ -157,14 +163,16 @@ export const SpawnStakingCheck = ({ setIsStaking(true); setSpawnScreenState(nextPage); } - setButtonClicked(undefined); } + + setButtonClicked(undefined); }; const handleNo = async () => { setButtonClicked(ButtonOptions.NO); const service: Service | undefined = await create(false); + if (!service) { message.error('Failed to create service'); } else {