-
Notifications
You must be signed in to change notification settings - Fork 0
charging contest owner when selecting winner and fix hardcode creater… #121
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
Open
banh0006
wants to merge
5
commits into
dev
Choose a base branch
from
charging-on-select-winner
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9e47aa2
charging contest owner when selecting winner and fix hardcode creater…
banh0006 f99e857
added checkout form and call confirmCardPayment method
banh0006 bbb9df3
fixed minor issues from CR
banh0006 c76ed9f
hide submit button when contest is over
banh0006 313649e
Merge remote-tracking branch 'origin/dev' into charging-on-select-winner
edyhtan 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 hidden or 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 hidden or 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,154 @@ | ||
| import React, { useState } from 'react' | ||
| import { CardNumberElement, CardExpiryElement, CardCvcElement, useElements, useStripe} from '@stripe/react-stripe-js' | ||
| import { makeStyles } from "@material-ui/core/styles" | ||
| import RequestError, { setContestWinner } from '../apiCalls' | ||
|
|
||
| const CARD_ELEMENT_OPTIONS = { | ||
| showIcon: true, | ||
| style: { | ||
| base: { | ||
| color: "#32325d", | ||
| fontFamily: '"Helvetica Neue", Helvetica, sans-serif', | ||
| fontSmoothing: "antialiased", | ||
| fontSize: "16px", | ||
| "::placeholder": { | ||
| color: "#aab7c4", | ||
| }, | ||
| }, | ||
| invalid: { | ||
| color: "#fa755a", | ||
| iconColor: "#fa755a", | ||
| }, | ||
| }, | ||
| } | ||
|
|
||
| const useStyles = makeStyles(theme => ({ | ||
| root: { | ||
| alignItems: "center", | ||
| }, | ||
| container: { | ||
| width:300, | ||
| marginTop: theme.spacing(2), | ||
| '& .StripeElement': { | ||
| minWidth: '400px' | ||
| } | ||
| }, | ||
| centered: { | ||
| display: "flex", | ||
| alignItems: "center", | ||
| justifyContent: "center", | ||
| }, | ||
| button: { | ||
| marginTop: theme.spacing(5), | ||
| padding: '0.5rem' | ||
| } | ||
| })) | ||
|
|
||
| export default function CheckoutForm(props) { | ||
| const [numberError, setNumberError] = useState(null); | ||
| const [expiryError, setExpiryError] = useState(null); | ||
| const [cvcError, setCVCError] = useState(null); | ||
| const stripe = useStripe() | ||
| const elements = useElements() | ||
| const classes = useStyles() | ||
|
|
||
| const handleSubmit = async (event) => { | ||
| event.preventDefault() | ||
|
|
||
| if (!stripe || !elements) { | ||
| // Stripe.js has not yet loaded. | ||
| // Make sure to disable form submission until Stripe.js has loaded. | ||
| return | ||
| } | ||
|
|
||
| const result = await stripe.confirmCardPayment(props.clientSecret, { | ||
| payment_method: { | ||
| card: elements.getElement(CardNumberElement), | ||
| billing_details: { | ||
| name: 'Jenny Rosen', | ||
| }, | ||
| } | ||
| }) | ||
|
|
||
| if (result.error) { | ||
| // Show error to your customer (e.g., insufficient funds) | ||
| console.log(result.error.message) | ||
| } else { | ||
| // The payment has been processed! | ||
| if (result.paymentIntent.status === 'succeeded') { | ||
| setContestWinner(props.contestId, props.winningSubmission, data => { | ||
| console.log('Winner Successfully Declared') | ||
| alert('Winner Successfully Declared') | ||
| }, error => { | ||
| if (error instanceof RequestError && error.status === 400) { | ||
| console.log(error.body) | ||
| } else { | ||
| console.log("unexpected error") | ||
| } | ||
| }) | ||
| } else { | ||
| alert('Winner Successfully Declared') | ||
| } | ||
| } | ||
| props.setOpenPaymentDialog(false) | ||
| } | ||
|
|
||
| const onChangeNumber = (event) => { | ||
| if (event.error) { | ||
| setNumberError(event.error.message); | ||
| } else { | ||
| setNumberError(null); | ||
| } | ||
| } | ||
|
|
||
| const onChangeExpiry = (event) => { | ||
| if (event.error) { | ||
| setExpiryError(event.error.message); | ||
| } else { | ||
| setExpiryError(null); | ||
| } | ||
| } | ||
|
|
||
| const onChangeCVC = (event) => { | ||
| if (event.error) { | ||
| setCVCError(event.error.message); | ||
| } else { | ||
| setCVCError(null); | ||
| } | ||
| } | ||
|
|
||
| return ( | ||
| <form onSubmit={handleSubmit} className={classes.root}> | ||
| <div className={classes.container}> | ||
| <label> | ||
| Card number | ||
| <CardNumberElement | ||
| onChange={onChangeNumber} | ||
| options={CARD_ELEMENT_OPTIONS} | ||
| /> | ||
| </label> | ||
| </div> | ||
| <div className={classes.container}> | ||
| <label> | ||
| Expiration date | ||
| <CardExpiryElement | ||
| onChange={onChangeExpiry} | ||
| options={CARD_ELEMENT_OPTIONS} | ||
| /> | ||
| </label> | ||
| </div> | ||
| <div className={classes.container}> | ||
| <label> | ||
| CVC | ||
| <CardCvcElement | ||
| onChange={onChangeCVC} | ||
| options={CARD_ELEMENT_OPTIONS} | ||
| /> | ||
| </label> | ||
| </div> | ||
| <div className={classes.centered}> | ||
| <button className={classes.button}>Confirm order</button> | ||
| </div> | ||
| </form> | ||
| ) | ||
| } |
This file contains hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -3,6 +3,9 @@ import { Link } from 'react-router-dom' | |
| import { useHistory } from 'react-router-dom' | ||
| import { makeStyles } from '@material-ui/core/styles' | ||
| import TabPanel from '../components/TabPanel' | ||
| import CheckoutForm from '../components/CheckoutForm' | ||
| import { loadStripe } from '@stripe/stripe-js' | ||
| import { Elements } from '@stripe/react-stripe-js' | ||
| import { | ||
| Button, | ||
| Typography, | ||
|
|
@@ -21,9 +24,10 @@ import { | |
| Radio, | ||
| } from '@material-ui/core' | ||
|
|
||
| import RequestError, { getContestDetails, setContestWinner } from '../apiCalls' | ||
| import RequestError, { getContestDetails, checkStripeIDExists, createPayment } from '../apiCalls' | ||
| import { UserContext } from '../App' | ||
|
|
||
| const stripePromise = loadStripe("pk_test_51IH8tAGt0nFBLozrVPDB6mwr6yEw9QOAVWhTQK0hErAcLv7F278Dz2f3P637jEaboOp7lJbAVnZNbQTxQUoqD91z00VxrG6s3T") | ||
| const useStyles = makeStyles((theme) => ({ | ||
| pageContainer: { | ||
| height: 'calc(100vh - 100px)', | ||
|
|
@@ -127,6 +131,17 @@ const useStyles = makeStyles((theme) => ({ | |
| dialogImage: { | ||
| width: '100%', | ||
| height: '100%' | ||
| }, | ||
| contestOverText: { | ||
| color: 'red', | ||
| backgroundColor: '#1f1f1f', | ||
| fontSize: '1.5rem', | ||
| padding: '1rem' | ||
| }, | ||
| paymentDialog: { | ||
| '& .MuiDialog-paper': { | ||
| minWidth: '600px' | ||
| } | ||
| } | ||
| })) | ||
|
|
||
|
|
@@ -135,6 +150,7 @@ export default function ContestDetails(props) { | |
| const {user, setUser} = useContext(UserContext) | ||
| const [activeTab, setActiveTab] = useState(0) | ||
| const [openDesignDialog, setOpenDesignDialog] = useState(false) | ||
| const [openPaymentDialog, setOpenPaymentDialog] = useState(false) | ||
| const [openInspirationalDialog, setOpenInspirationalDialog] = useState(false) | ||
| const [contest, setContest] = useState(null) | ||
| const [designOpening, setDesignOpening] = useState({}) | ||
|
|
@@ -143,6 +159,7 @@ export default function ContestDetails(props) { | |
| const [inspirationalGridListItems, setInspirationalGridListItems] = useState(null) | ||
| const [submitButton, setSubmitButton] = useState(null) | ||
| const [winningSubmission, setWinningSubmission] = useState(null) | ||
| const [clientSecret, setClientSecret] = useState("") | ||
| const contestId = props.match.params.id | ||
| const history = useHistory() | ||
|
|
||
|
|
@@ -155,21 +172,36 @@ export default function ContestDetails(props) { | |
| setOpenInspirationalDialog(false); | ||
| } | ||
|
|
||
| const handlePaymentDialogClose = () => { | ||
| setOpenPaymentDialog(false) | ||
| } | ||
|
|
||
| const handleWinnerChange = e => { | ||
| setWinningSubmission(parseInt(e.target.value)) | ||
| } | ||
|
|
||
| const onWinnerSubmit = e => { | ||
| setContestWinner(contestId, winningSubmission, data => { | ||
| console.log('Winner Successfully Declared') | ||
| }, error => { | ||
| if (error instanceof RequestError && error.status === 400) { | ||
| console.log(error.body) | ||
| } else { | ||
| console.log("unexpected error") | ||
| const onWinnerSubmit = async(e) => { | ||
| checkStripeIDExists( // check if payment is set up before allow user submit a winner | ||
| (data) => { | ||
| let intentSecret = data["intent_id"] | ||
| if (intentSecret != null){ | ||
| const amount = contest.prize_contest | ||
| const currency = 'usd' | ||
|
|
||
| createPayment(amount, currency, data => { | ||
| setClientSecret(data.client_secret) | ||
| setOpenPaymentDialog(true) | ||
| }, error => { | ||
| console.log("unexpected error") | ||
| }) | ||
| } else { | ||
| alert("Please set up your payment before selecting a winner.") | ||
| } | ||
| }, | ||
| (error) => { | ||
| alert("Please set up your payment before selecting a winner.") | ||
| } | ||
| }) | ||
| getContestInfo(contestId) | ||
| ) | ||
| } | ||
|
|
||
| const onBackButtonClick = e => { | ||
|
|
@@ -198,7 +230,6 @@ export default function ContestDetails(props) { | |
|
|
||
| const getContestInfo = contestId => { | ||
| getContestDetails(contestId, (data) => { | ||
| console.log(data) | ||
| data.title ? setContest(data) : setContest(null) | ||
| }, (error) => { | ||
| if (error instanceof RequestError && error.status === 400) { | ||
|
|
@@ -214,8 +245,17 @@ export default function ContestDetails(props) { | |
| // eslint-disable-next-line | ||
| }, []) | ||
|
|
||
| useEffect(() => { | ||
| //when payment dialog close, should fetch new data | ||
| getContestInfo(contestId) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. are you getting a warning that
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yep, there was a warning, I just add contestId into the dependency array |
||
| }, [openPaymentDialog, contestId]) | ||
|
|
||
| useEffect(() => { | ||
| if (contest) { | ||
| // Hide submit button when contest is over | ||
| if (contest.winner !== null) { | ||
| setSubmitButton(null) | ||
| } | ||
| var newGridListItems = null | ||
| const newInspirationalGridListItems = contest.attached_inspirational_images.map((design, index) => ( | ||
| <GridListTile key={index}> | ||
|
|
@@ -258,7 +298,9 @@ export default function ContestDetails(props) { | |
| } else { | ||
| newGridListItems = <div>There is no submitted designs</div> | ||
| } | ||
| createSubmitDesignButton() | ||
| if (Date.parse(contest.deadline_date) < Date.now() && contest.winner == null) { | ||
| createSubmitDesignButton() | ||
| } | ||
| } | ||
| setDesignGridListItems(newGridListItems) | ||
| } | ||
|
|
@@ -286,6 +328,7 @@ export default function ContestDetails(props) { | |
|
|
||
| return ( | ||
| contest !== null ? ( | ||
| <Elements stripe={stripePromise}> | ||
| <div className={classes.pageContainer}> | ||
| <div className={classes.containerWrapper}> | ||
| <Dialog open={openDesignDialog} onClose={handleClose} className={classes.dialog}> | ||
|
|
@@ -299,13 +342,27 @@ export default function ContestDetails(props) { | |
| <Button onClick={handleClose} color="primary">Close</Button> | ||
| </DialogActions> | ||
| </Dialog> | ||
| <Dialog open={openPaymentDialog} onClose={handlePaymentDialogClose} className={classes.paymentDialog}> | ||
| <DialogTitle> | ||
| Checkout Form | ||
| </DialogTitle> | ||
| <DialogContent> | ||
| <CheckoutForm | ||
| clientSecret={clientSecret} | ||
| contestId={contestId} | ||
| winningSubmission={winningSubmission} | ||
| setOpenPaymentDialog={setOpenPaymentDialog} | ||
| /> | ||
| </DialogContent> | ||
| </Dialog> | ||
|
|
||
| <Dialog open={openInspirationalDialog} onClose={handleClose} className={classes.dialog}> | ||
| <DialogContent> | ||
| <img src={inspirationalOpening} className={classes.dialogImage} /> | ||
| <img src={inspirationalOpening} className={classes.dialogImage} alt="" /> | ||
| </DialogContent> | ||
| <DialogActions> | ||
| <Button onClick={handleClose} color="primary">Close</Button> | ||
| </DialogActions> | ||
| </DialogActions> | ||
| </Dialog> | ||
| <div className={classes.backButtonDiv}> | ||
| <div className={classes.backButton} onClick={onBackButtonClick}> | ||
|
|
@@ -332,6 +389,7 @@ export default function ContestDetails(props) { | |
| </Grid> | ||
| </Grid> | ||
| </div> | ||
| {contest.winner !== null ? <div className={classes.contestOverText}>This contest is over</div> : <div></div>} | ||
| <div className={classes.contestDesigns}> | ||
| <Paper> | ||
| <Tabs | ||
|
|
@@ -363,6 +421,7 @@ export default function ContestDetails(props) { | |
| </div> | ||
| </div> | ||
| </div> | ||
| </Elements> | ||
| ) : ( | ||
| <div className={classes.pageContainer}> | ||
| <div className={classes.backButtonDiv}> | ||
|
|
||
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.
need to change this to a config var so that @edyhtan can replace accordingly