-
Notifications
You must be signed in to change notification settings - Fork 21
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3f361f0
commit 9855825
Showing
19 changed files
with
713 additions
and
100 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
import { FC, FormEvent, ReactNode, useState } from 'react'; | ||
import { Token } from 'libs/tokens'; | ||
import { Link, useNavigate, useParams } from 'libs/routing'; | ||
import { Button } from 'components/common/button'; | ||
import { cn } from 'utils/helpers'; | ||
import { BaseOrder } from 'components/strategies/common/types'; | ||
import { useWagmi } from 'libs/wagmi'; | ||
import { lsService } from 'services/localeStorage'; | ||
import style from 'components/strategies/common/form.module.css'; | ||
import { buttonStyles } from 'components/common/button/buttonStyles'; | ||
|
||
interface FormProps { | ||
base: Token; | ||
quote: Token; | ||
order0: BaseOrder; | ||
order1: BaseOrder; | ||
approvalText?: string; | ||
children: ReactNode; | ||
} | ||
|
||
export const EditStrategyCartForm: FC<FormProps> = (props) => { | ||
const { base, quote, order0, order1, children } = props; | ||
const { strategyId } = useParams({ from: '/cart/edit/$strategyId' }); | ||
const { user } = useWagmi(); | ||
const nav = useNavigate(); | ||
const [hasChanged, setHasChanged] = useState(false); | ||
|
||
const isDisabled = (form: HTMLFormElement) => { | ||
if (!form.checkValidity()) return true; | ||
if (!!form.querySelector('.loading-message')) return true; | ||
if (!!form.querySelector('.error-message')) return true; | ||
const warnings = form.querySelector('.warning-message'); | ||
if (!warnings) return false; | ||
return !form.querySelector<HTMLInputElement>('#approve-warnings')?.checked; | ||
}; | ||
|
||
const edit = (e: FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
if (isDisabled(e.currentTarget)) return; | ||
if (!user) return; | ||
const current = lsService.getItem('carts') || {}; | ||
const strategies = current[user]; | ||
if (!strategies?.length) return; | ||
const index = strategies.findIndex(({ id }) => id === strategyId); | ||
if (index === -1) return; | ||
current[user][index] = { | ||
id: strategyId, | ||
base: base.address, | ||
quote: quote.address, | ||
order0: { ...order0, marginalPrice: order0.marginalPrice ?? '' }, | ||
order1: { ...order1, marginalPrice: order1.marginalPrice ?? '' }, | ||
}; | ||
lsService.setItem('carts', current); | ||
nav({ to: '/cart' }); | ||
}; | ||
|
||
return ( | ||
<form | ||
onChange={() => setHasChanged(true)} | ||
onSubmit={edit} | ||
className={cn(style.form, 'flex flex-1 flex-col gap-20')} | ||
data-testid="create-strategy-form" | ||
> | ||
{children} | ||
<label | ||
htmlFor="approve-warnings" | ||
className={cn( | ||
style.approveWarnings, | ||
'rounded-10 bg-background-900 text-14 font-weight-500 flex items-center gap-8 p-20 text-white/60' | ||
)} | ||
> | ||
<input | ||
id="approve-warnings" | ||
type="checkbox" | ||
className="size-18" | ||
data-testid="approve-warnings" | ||
/> | ||
{props.approvalText ?? | ||
"I've reviewed the warning(s) but choose to proceed."} | ||
</label> | ||
|
||
<Button | ||
type="submit" | ||
variant="success" | ||
size="lg" | ||
fullWidth | ||
data-testid="edit-submit" | ||
> | ||
Confirm Change | ||
</Button> | ||
<Link | ||
to="/cart" | ||
className={buttonStyles({ variant: 'secondary', size: 'lg' })} | ||
> | ||
Cancel | ||
</Link> | ||
</form> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.