-
Notifications
You must be signed in to change notification settings - Fork 65
Basic auction optimization of code and null checks #226
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,43 +11,25 @@ const WALLET_URL = `${BASE_API_URL}/wallet`; | |
|
|
||
| const HandleAuctions = () => { | ||
| const [auctionsData, setAuctionsData] = useState([]); | ||
| const [userBid, setUserBid] = useState(); | ||
| const [userBid, setUserBid] = useState(0); | ||
| const [isUserLoggedIn, setIsUserLoggedIn] = useState(false); | ||
| const [isLoading, setIsLoading] = useState(true); | ||
| const [userMoney, setUserMoney] = useState(); | ||
|
|
||
| useEffect(() => { | ||
| fetchAndSetAuctions(); | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| getUserWallet(); | ||
| }, []); | ||
|
|
||
| useEffect(() => { | ||
| fetchSelfDetails() | ||
| .then((res) => { | ||
| if (res.status === 200) { | ||
| setIsUserLoggedIn(true); | ||
| } | ||
| }) | ||
| .catch((err) => { | ||
| console.log('User is not logged in', err); | ||
| }); | ||
| }, []); | ||
| const [userMoney, setUserMoney] = useState(0); | ||
|
|
||
| const fetchAndSetAuctions = async () => { | ||
| const response = await fetchData(AUCTIONS_URL); | ||
| const json = await response.json(); | ||
| setAuctionsData(json.auctions); | ||
| if (!response) return null; | ||
| setAuctionsData( | ||
| JSON.parse(response && response.auctions ? response.auctions : 0) || 0 | ||
| ); | ||
| setIsLoading(false); | ||
| }; | ||
|
|
||
| const getUserWallet = async () => { | ||
| const response = await fetchData(WALLET_URL, 'GET', { | ||
| credentials: 'include', | ||
| }); | ||
| const { status } = await response; | ||
| const { status } = response; | ||
| if (status === 200) { | ||
| const { wallet } = await response.json(); | ||
| if (Object.keys(wallet).length === 0) return setUserMoney(0); | ||
|
|
@@ -59,9 +41,26 @@ const HandleAuctions = () => { | |
| } | ||
| } else { | ||
| setUserMoney(0); | ||
| return null; | ||
| } | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| (async () => { | ||
|
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. Please create named functions!
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. It's an IIFE ,please recheck the implementation,thanks |
||
| await fetchAndSetAuctions(); | ||
| await getUserWallet(); | ||
| await fetchSelfDetails() | ||
| .then((res) => { | ||
|
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. Please don't mix the |
||
| if (res.status === 200) { | ||
| setIsUserLoggedIn(true); | ||
| } | ||
| }) | ||
| .catch((err) => { | ||
| console.error('User is not logged in', err); | ||
| }); | ||
| })(); | ||
| }, []); | ||
|
|
||
| const handleNewBid = async (e, auctionId) => { | ||
| e.preventDefault(); | ||
| if (!isUserLoggedIn) { | ||
|
|
@@ -86,7 +85,7 @@ const HandleAuctions = () => { | |
| ); | ||
| const { status } = await response; | ||
| if (status === 201) { | ||
| fetchAndSetAuctions(); | ||
| await fetchAndSetAuctions(); | ||
| setIsLoading(false); | ||
| } | ||
| } | ||
|
|
@@ -108,78 +107,82 @@ const HandleAuctions = () => { | |
| e.target.src = '/assets/default_avatar.jpg'; | ||
| }; | ||
|
|
||
| const auctionHandler = auctionsData.map((auction) => { | ||
| const { id, seller, quantity, highest_bid, bidders } = auction; | ||
| return ( | ||
| <div className={`${styles.auctionContainer} ${id}`} key={id}> | ||
| <div className={styles.auctionSeller}> | ||
| <h2>Seller:</h2> | ||
| <img | ||
| className={styles.profilePhoto} | ||
| src={`${BASE_IMAGE_URL}/${seller}/img.png`} | ||
| onError={brokenImageHandler} | ||
| /> | ||
| </div> | ||
| <div className={styles.auctionStats}> | ||
| <div className={styles.itemInfo}> | ||
| <h1> | ||
| {quantity} x {' '} | ||
| <Image | ||
| className={styles.gemImage} | ||
| layout="fixed" | ||
| src="/assets/gem.png" | ||
| width={25} | ||
| height={25} | ||
| /> | ||
| </h1> | ||
| </div> | ||
| <div className={styles.currentStatus}> | ||
| <h2> | ||
| Current Bid: | ||
| <div className={styles.currentBidPrice}>{highest_bid}</div> | ||
| </h2> | ||
| </div> | ||
| </div> | ||
| <div> | ||
| <form | ||
| className={styles.bidOptions} | ||
| onSubmit={(e) => handleNewBid(e, id)} | ||
| > | ||
| <input | ||
| className={styles.inputBid} | ||
| type="number" | ||
| min={parseInt(highest_bid) + 1} | ||
| required={true} | ||
| onBlur={({ target: { value } }) => | ||
| validateBid(value, highest_bid) | ||
| } | ||
| /> | ||
| <button type="submit" className={styles.bidBtn}> | ||
| Bid | ||
| </button> | ||
| </form> | ||
| </div> | ||
| <div className={styles.bidders}> | ||
| {bidders.map((bidder) => { | ||
| return ( | ||
| <div | ||
| className={styles.biddersImg} | ||
| key={bidder} | ||
| data-columns={getColumns(bidders.length)} | ||
| title={bidder} | ||
| > | ||
| const auctionHandler = | ||
| auctionsData && auctionsData.length | ||
|
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. Use the |
||
| ? auctionsData.map(({ id, seller, quantity, highest_bid, bidders }) => { | ||
|
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. Please create a sub-component which you can then use in the ternary operator... |
||
| return ( | ||
| <div className={`${styles.auctionContainer} ${id}`} key={id}> | ||
| <div className={styles.auctionSeller}> | ||
| <h2>Seller:</h2> | ||
| <img | ||
| className={styles.profilePhoto} | ||
| src={`${BASE_IMAGE_URL}/${bidder}/img.png`} | ||
| onError={brokenImageHandler} | ||
| src={`${BASE_IMAGE_URL}/${seller}/img.png`} | ||
| onError={(e) => brokenImageHandler(e)} | ||
| /> | ||
| </div> | ||
| ); | ||
| })} | ||
| </div> | ||
| </div> | ||
| ); | ||
| }); | ||
| <div className={styles.auctionStats}> | ||
| <div className={styles.itemInfo}> | ||
| <h1> | ||
| {quantity} x {' '} | ||
| <Image | ||
| className={styles.gemImage} | ||
| layout="fixed" | ||
| src="/assets/gem.png" | ||
| width={25} | ||
| height={25} | ||
| /> | ||
| </h1> | ||
| </div> | ||
| <div className={styles.currentStatus}> | ||
| <h2> | ||
| Current Bid: | ||
| <div className={styles.currentBidPrice}>{highest_bid}</div> | ||
| </h2> | ||
| </div> | ||
| </div> | ||
| <div> | ||
| <form | ||
| className={styles.bidOptions} | ||
| onSubmit={(e) => handleNewBid(e, id)} | ||
| > | ||
| <input | ||
| className={styles.inputBid} | ||
| type="number" | ||
| min={parseInt(highest_bid) + 1} | ||
| required={true} | ||
| onBlur={({ target: { value } }) => | ||
| validateBid(value, highest_bid) | ||
| } | ||
| /> | ||
| <button type="submit" className={styles.bidBtn}> | ||
| Bid | ||
| </button> | ||
| </form> | ||
| </div> | ||
| <div className={styles.bidders}> | ||
| {bidders.length | ||
| ? bidders.map((bidder) => { | ||
| return ( | ||
| <div | ||
| className={styles.biddersImg} | ||
| key={bidder} | ||
| data-columns={getColumns(bidders.length)} | ||
| title={bidder} | ||
| > | ||
| <img | ||
| className={styles.profilePhoto} | ||
| src={`${BASE_IMAGE_URL}/${bidder}/img.png`} | ||
| onError={brokenImageHandler} | ||
| /> | ||
| </div> | ||
| ); | ||
| }) | ||
| : null} | ||
| </div> | ||
| </div> | ||
| ); | ||
| }) | ||
| : null; | ||
|
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. Please use the inline && operator to avoid the null condition 😄 |
||
|
|
||
| return ( | ||
| <div className={styles.mainContainer}> | ||
|
|
||
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.
You could use the new syntax here!
JSON.parse(response.auctions ?? 0)It mostly won't need the fallback
|| 0as well 😄