Skip to content

Commit d5e250f

Browse files
committed
Remove settings from flow
1 parent ecedd80 commit d5e250f

File tree

4 files changed

+52
-26
lines changed

4 files changed

+52
-26
lines changed

components/multi-step-form.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,17 @@ export function useStep () {
203203
return steps[stepIndex]
204204
}
205205

206+
export function useIsFirstStep () {
207+
const stepIndex = useStepIndex()
208+
return stepIndex === 0
209+
}
210+
211+
export function useIsLastStep () {
212+
const maxSteps = useMaxSteps()
213+
const stepIndex = useStepIndex()
214+
return stepIndex === maxSteps - 1
215+
}
216+
206217
export function useNext () {
207218
const { next } = useContext(MultiStepFormContext)
208219
return next

wallets/client/components/form/hooks.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { isTemplate, isWallet, protocolClientSchema, protocolFields, protocolFormId, walletLud16Domain } from '@/wallets/lib/util'
22
import { createContext, useContext, useEffect, useMemo, useCallback, useState } from 'react'
3+
import { useRouter } from 'next/router'
34
import { useWalletProtocolUpsert } from '@/wallets/client/hooks'
45
import { MultiStepForm, useFormState, useStep } from '@/components/multi-step-form'
56
import { parseNwcUrl } from '@/wallets/lib/validate'
@@ -133,6 +134,7 @@ export function useSaveWallet () {
133134
const wallet = useWallet()
134135
const [formState] = useFormState()
135136
const upsert = useWalletProtocolUpsert()
137+
const router = useRouter()
136138

137139
const save = useCallback(async () => {
138140
let walletId = isWallet(wallet) ? wallet.id : undefined
@@ -147,7 +149,8 @@ export function useSaveWallet () {
147149
)
148150
walletId ??= id
149151
}
150-
}, [wallet, formState, upsert])
152+
router.push('/wallets')
153+
}, [wallet, formState, upsert, router])
151154

152155
return save
153156
}

wallets/client/components/form/index.js

Lines changed: 37 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ import { Checkbox, Form, Input, PasswordInput, SubmitButton } from '@/components
77
import CancelButton from '@/components/cancel-button'
88
import Text from '@/components/text'
99
import Info from '@/components/info'
10-
import { useFormState, useMaxSteps, useNext, useStepIndex } from '@/components/multi-step-form'
11-
import { isTemplate, isWallet, protocolDisplayName, protocolFormId, protocolLogName, walletLud16Domain } from '@/wallets/lib/util'
10+
import { useIsFirstStep, useIsLastStep, useNext } from '@/components/multi-step-form'
11+
import { isTemplate, protocolDisplayName, protocolFormId, protocolLogName, walletLud16Domain } from '@/wallets/lib/util'
1212
import { WalletGuide, WalletLayout, WalletLayoutHeader, WalletLayoutImageOrName, WalletLogs } from '@/wallets/client/components'
1313
import { TemplateLogsProvider, useTestSendPayment, useWalletLogger, useTestCreateInvoice, useWalletSupport } from '@/wallets/client/hooks'
1414
import ArrowRight from '@/svgs/arrow-right-s-fill.svg'
1515
import { useFormikContext } from 'formik'
1616

17-
import { WalletMultiStepFormContextProvider, Step, useWallet, useWalletProtocols, useProtocol, useProtocolForm } from './hooks'
18-
import { Settings } from './settings'
17+
import { WalletMultiStepFormContextProvider, Step, useWallet, useWalletProtocols, useProtocol, useProtocolForm, useSaveWallet } from './hooks'
1918
import { BackButton, SkipButton } from './button'
2019

2120
export function WalletMultiStepForm ({ wallet }) {
@@ -33,8 +32,7 @@ export function WalletMultiStepForm ({ wallet }) {
3332
const steps = useMemo(() =>
3433
[
3534
support.send && Step.SEND,
36-
support.receive && Step.RECEIVE,
37-
Step.SETTINGS
35+
support.receive && Step.RECEIVE
3836
].filter(Boolean),
3937
[support])
4038

@@ -51,7 +49,7 @@ export function WalletMultiStepForm ({ wallet }) {
5149
// and can thus render a different form for send vs. receive
5250
if (step === Step.SEND) return <WalletForm key={step} />
5351
if (step === Step.RECEIVE) return <WalletForm key={step} />
54-
return <Settings key={step} />
52+
return null
5553
})}
5654
</WalletMultiStepFormContextProvider>
5755
</div>
@@ -92,12 +90,21 @@ function WalletProtocolSelector () {
9290
function WalletProtocolForm () {
9391
const wallet = useWallet()
9492
const [protocol] = useProtocol()
95-
const next = useNext()
93+
94+
// on the last step, we save the wallet, otherwise we just go to the next step
95+
const isLastStep = useIsLastStep()
96+
const formNext = useNext()
97+
const formSave = useSaveWallet()
98+
9699
const testSendPayment = useTestSendPayment(protocol)
97100
const testCreateInvoice = useTestCreateInvoice(protocol)
98101
const logger = useWalletLogger(protocol)
99102
const [{ fields, initial, schema }, setFormState] = useProtocolForm(protocol)
100103

104+
const next = useCallback(() => {
105+
isLastStep ? formSave() : formNext()
106+
}, [isLastStep, formSave, formNext])
107+
101108
// create a copy of values to avoid mutating the original
102109
const onSubmit = useCallback(async ({ ...values }) => {
103110
const lud16Domain = walletLud16Domain(wallet.name)
@@ -152,25 +159,31 @@ function WalletProtocolForm () {
152159
}
153160

154161
function WalletProtocolFormNavigator () {
155-
const wallet = useWallet()
156-
const stepIndex = useStepIndex()
157-
const maxSteps = useMaxSteps()
158-
const [formState] = useFormState()
159-
160-
// was something already configured or was something configured just now?
161-
const configExists = (isWallet(wallet) && wallet.protocols.length > 0) || Object.keys(formState).length > 0
162-
163-
// don't allow going to settings as last step with nothing configured
164-
const hideSkip = stepIndex === maxSteps - 2 && !configExists
162+
// show 'cancel' in the first step
163+
const showCancel = useIsFirstStep()
164+
// show 'save' instead of 'next' in the last step
165+
const isLastStep = useIsLastStep()
166+
// show 'skip' if there's a next step
167+
const showSkip = !isLastStep
165168

166169
return (
167170
<div className='d-flex justify-content-end align-items-center'>
168-
{stepIndex === 0 ? <CancelButton>cancel</CancelButton> : <BackButton />}
169-
{!hideSkip ? <SkipButton /> : <div className='ms-auto' />}
170-
<SubmitButton variant='primary' className='ps-3 pe-2 d-flex align-items-center'>
171-
next
172-
<ArrowRight width={24} height={24} />
173-
</SubmitButton>
171+
{showCancel ? <CancelButton>cancel</CancelButton> : <BackButton />}
172+
{showSkip ? <SkipButton /> : <div className='ms-auto' />}
173+
{
174+
isLastStep
175+
? (
176+
<SubmitButton variant='primary' className='d-flex align-items-center'>
177+
save
178+
</SubmitButton>
179+
)
180+
: (
181+
<SubmitButton variant='primary' className='ps-3 pe-2 d-flex align-items-center'>
182+
next
183+
<ArrowRight width={24} height={24} />
184+
</SubmitButton>
185+
)
186+
}
174187
</div>
175188
)
176189
}

wallets/client/components/form/settings.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ export function Settings () {
4545
})
4646
}
4747
})
48-
router.push('/wallets')
4948
} catch (err) {
5049
console.error(err)
5150
toaster.danger('failed to save wallet')

0 commit comments

Comments
 (0)