-
Notifications
You must be signed in to change notification settings - Fork 43
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
Feature/sep 6 deposit #163
Merged
Merged
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
d14b899
add Sep-6 deposit flow
piyalbasu b0e8751
Merge commit 'd4e703008ce1164329d997fc590cec7ddf80dfb5' into feature/…
piyalbasu 327a391
fix typo
piyalbasu d0c8c0b
adding bad input handling for deposit choice
piyalbasu 2cfc317
reorder import
piyalbasu edb5e6e
rm unneeded eslint disable
piyalbasu ed760c5
memoize depositTypes choices
piyalbasu 5528beb
PR comments
piyalbasu 14d9733
Merge commit '15ca900835868b83e5cf29cfa5267a8ad8d23e56' into feature/…
piyalbasu d5077a0
Merge commit '619dbcc19e4460e36e804b229adf2b519f5c878f' into feature/…
piyalbasu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,200 @@ | ||
import React, { useEffect, useMemo, useState } from "react"; | ||
import { useDispatch } from "react-redux"; | ||
import { Button, InfoBlock, Input, Select } from "@stellar/design-system"; | ||
import { Heading2 } from "components/Heading"; | ||
import { Modal } from "components/Modal"; | ||
import { TextLink } from "components/TextLink"; | ||
import { resetActiveAssetAction } from "ducks/activeAsset"; | ||
import { | ||
resetSep6DepositAction, | ||
submitSep6DepositFields, | ||
sep6DepositAction, | ||
} from "ducks/sep6DepositAsset"; | ||
import { useRedux } from "hooks/useRedux"; | ||
import { ActionStatus } from "types/types.d"; | ||
|
||
export const Sep6Send = () => { | ||
const { sep6DepositAsset } = useRedux("sep6DepositAsset"); | ||
const { depositResponse } = sep6DepositAsset; | ||
|
||
interface FormData { | ||
depositType: { | ||
type: string; | ||
}; | ||
fields: { | ||
[key: string]: string; | ||
}; | ||
} | ||
|
||
const formInitialState: FormData = { | ||
depositType: { | ||
type: "", | ||
}, | ||
fields: {}, | ||
}; | ||
|
||
const [formData, setFormData] = useState<FormData>(formInitialState); | ||
const dispatch = useDispatch(); | ||
|
||
const depositTypeChoices = useMemo( | ||
() => sep6DepositAsset.data.depositTypes?.type?.choices || [], | ||
[sep6DepositAsset], | ||
); | ||
|
||
useEffect(() => { | ||
if (sep6DepositAsset.status === ActionStatus.NEEDS_INPUT) { | ||
setFormData({ | ||
depositType: { | ||
type: depositTypeChoices[0], | ||
}, | ||
fields: {}, | ||
}); | ||
} | ||
}, [sep6DepositAsset.status, depositTypeChoices, dispatch]); | ||
|
||
useEffect(() => { | ||
if (sep6DepositAsset.status === ActionStatus.CAN_PROCEED) { | ||
dispatch(sep6DepositAction()); | ||
} | ||
}, [sep6DepositAsset.status, dispatch]); | ||
|
||
const resetLocalState = () => { | ||
setFormData(formInitialState); | ||
}; | ||
|
||
const handleClose = () => { | ||
dispatch(resetSep6DepositAction()); | ||
dispatch(resetActiveAssetAction()); | ||
resetLocalState(); | ||
}; | ||
|
||
const handleDepositTypeChange = ( | ||
event: React.ChangeEvent<HTMLSelectElement>, | ||
) => { | ||
const { id, value } = event.target; | ||
|
||
const updatedState = { | ||
...formData, | ||
depositType: { | ||
...formData.depositType, | ||
[id]: value, | ||
}, | ||
}; | ||
|
||
setFormData(updatedState); | ||
}; | ||
|
||
const handleFieldChange = (event: React.ChangeEvent<HTMLInputElement>) => { | ||
const { id, value } = event.target; | ||
|
||
const updatedState = { | ||
...formData, | ||
fields: { | ||
...formData.fields, | ||
[id]: value, | ||
}, | ||
}; | ||
|
||
setFormData(updatedState); | ||
}; | ||
|
||
const handleSubmit = ( | ||
event: React.MouseEvent<HTMLButtonElement, MouseEvent>, | ||
) => { | ||
event.preventDefault(); | ||
dispatch(submitSep6DepositFields({ ...formData })); | ||
}; | ||
|
||
if (sep6DepositAsset.status === ActionStatus.NEEDS_INPUT) { | ||
return ( | ||
<Modal visible={true} onClose={handleClose}> | ||
<Heading2 | ||
className="ModalHeading" | ||
tooltipText={ | ||
<> | ||
These are the fields the receiving anchor requires. The sending | ||
client obtains them from the /customer endpoint.{" "} | ||
<TextLink | ||
href="https://github.com/stellar/stellar-protocol/blob/master/ecosystem/sep-0012.md#customer-get" | ||
isExternal | ||
> | ||
Learn more | ||
</TextLink> | ||
</> | ||
} | ||
> | ||
SEP-6 Required Info | ||
</Heading2> | ||
<div className="ModalBody"> | ||
{Object.entries(sep6DepositAsset.data.depositTypes || {}).map( | ||
([id, input]) => ( | ||
<> | ||
<Select | ||
label={input.description} | ||
id={id} | ||
key={id} | ||
onChange={handleDepositTypeChange} | ||
> | ||
{depositTypeChoices.map((choice: string) => ( | ||
<option key={choice} value={choice}> | ||
{choice} | ||
</option> | ||
))} | ||
</Select> | ||
</> | ||
), | ||
)} | ||
{Object.entries(sep6DepositAsset.data.fields || {}).map( | ||
([id, input]) => ( | ||
<Input | ||
key={id} | ||
id={id} | ||
label={input.description} | ||
required | ||
onChange={handleFieldChange} | ||
/> | ||
), | ||
)} | ||
</div> | ||
|
||
<div className="ModalButtonsFooter"> | ||
<Button onClick={handleSubmit}>Submit</Button> | ||
</div> | ||
</Modal> | ||
); | ||
} | ||
|
||
if (sep6DepositAsset.status === ActionStatus.SUCCESS) { | ||
return ( | ||
<Modal visible={true} onClose={handleClose}> | ||
<Heading2 className="ModalHeading">SEP-6 Deposit Info</Heading2> | ||
|
||
<div className="ModalBody"> | ||
<InfoBlock>{depositResponse.how}</InfoBlock> | ||
|
||
{depositResponse.extra_info?.message && ( | ||
<InfoBlock>{depositResponse.extra_info.message}</InfoBlock> | ||
)} | ||
|
||
{depositResponse.max_amount && ( | ||
<InfoBlock> | ||
<strong>Max Amount: </strong> | ||
|
||
{depositResponse.max_amount} | ||
</InfoBlock> | ||
)} | ||
|
||
{depositResponse.min_amount && ( | ||
<InfoBlock> | ||
<strong>Min Amount: </strong> | ||
|
||
{depositResponse.min_amount} | ||
</InfoBlock> | ||
)} | ||
</div> | ||
</Modal> | ||
); | ||
} | ||
|
||
return null; | ||
}; |
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.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this PR covers SEP-6 deposit of a trusted asset. A follow up PR will cover untrusted assets using claimable balances