Skip to content

Commit

Permalink
Merge pull request #54 from AAISS/feat/discount-code
Browse files Browse the repository at this point in the history
feat: added offer code integration to my account page
  • Loading branch information
AlirezaYousefpourM authored Dec 4, 2023
2 parents 34bfb64 + b779563 commit 892b758
Show file tree
Hide file tree
Showing 3 changed files with 60 additions and 7 deletions.
11 changes: 11 additions & 0 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 13 additions & 1 deletion frontend/src/pages/my-account/MyAccount.jsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import React, { useState } from 'react';
import { Box, Button, Chip, CircularProgress, Divider, Stack, Tab, Tabs, Typography } from '@mui/material';
import {Box, Button, Chip, CircularProgress, Divider, Stack, Tab, Tabs, TextField, Typography} from '@mui/material';
import ItemCard from '../../components/item-card/item-card.jsx';
import Toast from '../../components/toast/Toast.jsx';
import useMyAccount from './useMyAccount.js';
Expand All @@ -18,6 +18,8 @@ const MyAccount = () => {
removeFromCartHandler,
toastData,
openToast,
offCode,
handleOffCodeInputHandler,
setOpenToast,
} = useMyAccount();
const [tabValue, setTabValue] = useState(TAB_ITEMS[2]);
Expand Down Expand Up @@ -171,6 +173,16 @@ const MyAccount = () => {
<>
<Divider sx={{ my: 2 }} />
<Stack alignItems="center" gap={1}>
<br />
Have a Discount Code?
<TextField
id={"off-input-field"}
label={"Discount Code Here!"}
variant={"outlined"}
onChange={handleOffCodeInputHandler}
value={offCode}
/>
<br />
{renderTotalPrice()}
<Button
onClick={handleBuyCart}
Expand Down
42 changes: 36 additions & 6 deletions frontend/src/pages/my-account/useMyAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export default function useMyAccount() {
const [openToast, setOpenToast] = useState()
const [toastData, setToastData] = useState()
const [buyButtonLoading, setBuyButtonLoading] = useState(false)
const [offCode, setOffCode] = useState("")

const removeFromCartHandler = useCallback(({id, type}) => {
removeFromUserCart({
Expand All @@ -48,20 +49,47 @@ export default function useMyAccount() {
}, [removeFromUserCart])

const handleBuyCart = useCallback(() => {
postPaymentData({call_back: 'https://aaiss.ir/callback'});
if (offCode === "") {
postPaymentData({
call_back: 'https://aaiss.ir/callback',
});
} else {
postPaymentData({
call_back: 'https://aaiss.ir/callback',
discount_code: offCode
});
}
setBuyButtonLoading(true)
}, [postPaymentData]);
}, [offCode, postPaymentData]);

const handleOffCodeInputHandler = useCallback(e => {
setOffCode(e.target.value)
}, [])

useEffect(() => {
if (!paymentData)
return

if (paymentData.status !== 200 || paymentData.data.status !== 200) {
setOpenToast(true)
setToastData({
message: "There was an Error Regarding Payment! Please Try Again Later",
alertType: "error"
})
if (paymentData.status !== 200) {
if (paymentData.data?.message?.split(" ")[0] === "Discount") {
setToastData({
message: "Invalid Offer Code! Please Try Again",
alertType: "error"
})
} else {
setToastData({
message: "There was an Error Regarding Your Payment! Please Try Again Later",
alertType: "error"
})
}
} else {
setToastData({
message: "Invalid Offer Code! Please Try Again",
alertType: "error"
})
}
setBuyButtonLoading(false)
return
}
Expand Down Expand Up @@ -224,5 +252,7 @@ export default function useMyAccount() {
setOpenToast,
buyButtonLoading,
handleBuyCart,
offCode,
handleOffCodeInputHandler,
};
}

0 comments on commit 892b758

Please sign in to comment.