Skip to content

Commit

Permalink
refactor(fb): selected lockdate state when going back to 1st step fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
barrytra committed Aug 27, 2024
1 parent 26f71e6 commit 9194c8b
Show file tree
Hide file tree
Showing 5 changed files with 12 additions and 5 deletions.
1 change: 1 addition & 0 deletions src/components/fb2/CreateFidelityBond.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,7 @@ const CreateFidelityBond2 = ({ otherFidelityBondExists, wallet, walletInfo, onDo
<SelectDate
description={t('earn.fidelity_bond.select_date.description')}
yearsRange={yearsRange}
lockdate={lockDate}
onChange={(date) => setLockDate(date)}
/>
{bondWithSelectedLockDateAlreadyExists && (
Expand Down
4 changes: 2 additions & 2 deletions src/components/fb2/FidelityBondSteps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -43,13 +43,13 @@ interface ConfirmationProps {
timelockedAddress: Api.BitcoinAddress
}

const SelectDate = ({ description, yearsRange, disabled, onChange }: SelectDateProps) => {
const SelectDate = ({ description, yearsRange, lockdate, disabled, onChange }: SelectDateProps) => {
return (
<div className="d-flex gap-4">
<Sprite symbol="clock" width="24" height="24" />
<div className="d-flex flex-column gap-4 w-100">
<div className={styles.stepDescription}>{description}</div>
<LockdateForm yearsRange={yearsRange} onChange={onChange} disabled={disabled} />
<LockdateForm yearsRange={yearsRange} lockdate={lockdate} onChange={onChange} disabled={disabled} />
</div>
</div>
)
Expand Down
3 changes: 2 additions & 1 deletion src/components/fb2/LockdateForm.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,11 @@ import LockdateForm, { _minMonth, _selectableMonths, _selectableYears } from './

describe('<LockdateForm />', () => {
const now = new Date(Date.UTC(2009, 0, 3))
const lockdate = null
const setup = (onChange: (lockdate: Api.Lockdate | null) => void) => {
render(
<I18nextProvider i18n={i18n}>
<LockdateForm onChange={onChange} now={now} />
<LockdateForm onChange={onChange} lockdate={lockdate} now={now} />
</I18nextProvider>,
)
}
Expand Down
8 changes: 6 additions & 2 deletions src/components/fb2/LockdateForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,21 @@ export const _selectableYears = (yearsRange: fb.YearsRange, now = new Date()): n

export interface LockdateFormProps {
onChange: (lockdate: Api.Lockdate | null) => void
lockdate: Api.Lockdate | null
yearsRange?: fb.YearsRange
now?: Date
disabled?: boolean
}

const LockdateForm = ({ onChange, now, yearsRange, disabled }: LockdateFormProps) => {
const LockdateForm = ({ onChange, now, lockdate, yearsRange, disabled }: LockdateFormProps) => {
const { i18n } = useTranslation()
const _now = useMemo<Date>(() => now || new Date(), [now])
const _yearsRange = useMemo<fb.YearsRange>(() => yearsRange || fb.DEFAULT_TIMELOCK_YEARS_RANGE, [yearsRange])

const initialValue = useMemo<Api.Lockdate>(() => fb.lockdate.initial(_now, _yearsRange), [_now, _yearsRange])
const initialValue = useMemo<Api.Lockdate>(() => {
if (lockdate) return lockdate
return fb.lockdate.initial(_now, _yearsRange)
}, [_now, _yearsRange, lockdate])
const initialDate = useMemo(() => new Date(fb.lockdate.toTimestamp(initialValue)), [initialValue])
const initialYear = useMemo(() => initialDate.getUTCFullYear(), [initialDate])
const initialMonth = useMemo(() => (initialDate.getUTCMonth() + 1) as Month, [initialDate])
Expand Down
1 change: 1 addition & 0 deletions src/components/fb2/SpendFidelityBondModal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -372,6 +372,7 @@ const RenewFidelityBondModal = ({
<SelectDate
description={t('earn.fidelity_bond.select_date.description')}
yearsRange={yearsRange}
lockdate={null}
disabled={isLoading}
onChange={onSelectedDateChanged}
/>
Expand Down

0 comments on commit 9194c8b

Please sign in to comment.