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: persist asset selection #143

Merged
Merged
4 changes: 2 additions & 2 deletions src/features/swap/SwapConfirm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { toastToYourSuccess } from 'src/components/TxSuccessToast'
import { Button3D } from 'src/components/buttons/3DButton'
import { TokenId, Tokens } from 'src/config/tokens'
import { useAppDispatch, useAppSelector } from 'src/features/store/hooks'
import { setFormValues } from 'src/features/swap/swapSlice'
import { setConfirmView, setFormValues } from 'src/features/swap/swapSlice'
import { SwapFormValues } from 'src/features/swap/types'
import { useApproveTransaction } from 'src/features/swap/useApproveTransaction'
import { useSwapQuote } from 'src/features/swap/useSwapQuote'
Expand Down Expand Up @@ -142,7 +142,7 @@ export function SwapConfirmCard({ formValues }: Props) {
}, [isApproveTxSuccess, isSwapTxLoading, isSwapTxSuccess, sendSwapTx, chainId, dispatch])

const onClickBack = () => {
dispatch(setFormValues(null))
dispatch(setConfirmView(false))
}

const onClickRefresh = () => {
Expand Down
7 changes: 5 additions & 2 deletions src/features/swap/SwapForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ import { reset as blockReset } from 'src/features/blocks/blockSlice'
import { resetTokenPrices } from 'src/features/chart/tokenPriceSlice'
import { useAppDispatch, useAppSelector } from 'src/features/store/hooks'
import { SettingsMenu } from 'src/features/swap/SettingsMenu'
import { setFormValues, reset as swapReset } from 'src/features/swap/swapSlice'
import { setConfirmView, setFormValues, reset as swapReset } from 'src/features/swap/swapSlice'
import { SwapDirection, SwapFormValues } from 'src/features/swap/types'
import { useFormValidator } from 'src/features/swap/useFormValidator'
import { useSwapQuote } from 'src/features/swap/useSwapQuote'
Expand Down Expand Up @@ -66,12 +66,15 @@ function SwapForm() {
const dispatch = useAppDispatch()
const onSubmit = (values: SwapFormValues) => {
dispatch(setFormValues(values))
dispatch(setConfirmView(true)) // Switch to confirm view
}
const validateForm = useFormValidator(balances)
const storedFormValues = useAppSelector((s) => s.swap.formValues) // Get stored form values
const initialFormValues = storedFormValues || initialValues // Use stored values if they exist

return (
<Formik<SwapFormValues>
initialValues={initialValues}
initialValues={initialFormValues}
onSubmit={onSubmit}
validate={validateForm}
validateOnChange={true}
Expand Down
8 changes: 7 additions & 1 deletion src/features/swap/swapSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ export interface SwapState {
toCeloRates: ToCeloRates
showSlippage: boolean
showChart: boolean
confirmView: boolean
}

const initialState: SwapState = {
formValues: null,
toCeloRates: {},
showSlippage: false,
showChart: false,
confirmView: false,
}

export const swapSlice = createSlice({
Expand All @@ -29,8 +31,12 @@ export const swapSlice = createSlice({
state.showChart = action.payload
},
reset: () => initialState,
setConfirmView(state, action) {
state.confirmView = action.payload
},
},
})

export const { setFormValues, setShowSlippage, setShowChart, reset } = swapSlice.actions
export const { setFormValues, setShowSlippage, setShowChart, reset, setConfirmView } =
swapSlice.actions
export const swapReducer = swapSlice.reducer
8 changes: 6 additions & 2 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import { SwapConfirmCard } from 'src/features/swap/SwapConfirm'
import { SwapFormCard } from 'src/features/swap/SwapForm'

export default function SwapPage() {
const { formValues, showChart } = useAppSelector((state) => state.swap)
const { formValues, showChart, confirmView } = useAppSelector((state) => state.swap)
return (
<div className="flex justify-center items-center h-full flex-wrap w-full">
<div className="mb-6 w-full max-w-md">
{!formValues ? <SwapFormCard /> : <SwapConfirmCard formValues={formValues} />}
{!formValues || !confirmView ? (
<SwapFormCard />
) : (
<SwapConfirmCard formValues={formValues} />
)}{' '}
</div>
{config.showPriceChart && showChart && (
<div className="mb-6 md:ml-10">
Expand Down
Loading