-
Notifications
You must be signed in to change notification settings - Fork 0
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
KimiaMontazeri
committed
Nov 27, 2023
1 parent
d247aac
commit f7342c3
Showing
2 changed files
with
111 additions
and
111 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 |
---|---|---|
@@ -1,125 +1,126 @@ | ||
import {useCallback, useEffect, useState} from "react"; | ||
import {useAPI} from "../../providers/APIProvider/APIProvider.jsx"; | ||
import {useConfig} from "../../providers/config-provider/ConfigProvider.jsx"; | ||
import { useCallback, useEffect, useState } from 'react'; | ||
import { useAPI } from '../../providers/APIProvider/APIProvider.jsx'; | ||
import { useConfig } from '../../providers/config-provider/ConfigProvider.jsx'; | ||
|
||
export default function useMyAccount() { | ||
const { | ||
getUserWorkshops, | ||
getUserPresentations, | ||
userPresentationsData, | ||
userWorkshopsData, | ||
getWorkshopsData, | ||
getPresentationsData, | ||
workshopsData, | ||
presentationsData, | ||
removeFromUserCart, | ||
removeFromCartResponse, | ||
setRemoveFromCartResponse, | ||
setWorkshopsData, | ||
setPresentationsData, | ||
} = useAPI(); | ||
|
||
const { | ||
getUserWorkshops, | ||
getUserPresentations, | ||
userPresentationsData, | ||
userWorkshopsData, | ||
getWorkshopsData, | ||
getPresentationsData, | ||
workshopsData, | ||
presentationsData, | ||
removeFromUserCart, | ||
removeFromCartResponse, | ||
setRemoveFromCartResponse, | ||
setWorkshopsData, | ||
setPresentationsData, | ||
} = useAPI() | ||
const { accessToken } = useConfig(); | ||
|
||
const { | ||
accessToken | ||
} = useConfig() | ||
const [talks, setTalks] = useState(); | ||
const [workshops, setWorkshops] = useState(); | ||
const [cart, setCart] = useState(); | ||
const [openToast, setOpenToast] = useState(); | ||
const [toastData, setToastData] = useState(); | ||
|
||
const [talks, setTalks] = useState() | ||
const [workshops, setWorkshops] = useState() | ||
const [cart, setCart] = useState() | ||
const [openToast, setOpenToast] = useState() | ||
const [toastData, setToastData] = useState() | ||
const removeFromCartHandler = useCallback( | ||
({ id, type }) => { | ||
removeFromUserCart({ | ||
id: id, | ||
type: type, | ||
}); | ||
}, | ||
[removeFromUserCart], | ||
); | ||
|
||
const removeFromCartHandler = useCallback(({id, type}) => { | ||
removeFromUserCart({ | ||
id: id, | ||
type: type, | ||
}) | ||
}, [removeFromUserCart]) | ||
useEffect(() => { | ||
const toastDataTemp = {}; | ||
if (removeFromCartResponse) { | ||
switch (removeFromCartResponse.status) { | ||
case 200: | ||
toastDataTemp.message = 'Item Removed Successfully'; | ||
toastDataTemp.alertType = 'success'; | ||
break; | ||
case 400: | ||
toastDataTemp.message = 'Failed to Remove Item'; | ||
toastDataTemp.alertType = 'error'; | ||
break; | ||
default: | ||
toastDataTemp.message = 'Unknown Error. Please Try Again Later'; | ||
toastDataTemp.alertType = 'error'; | ||
break; | ||
} | ||
setToastData(toastDataTemp); | ||
setOpenToast(true); | ||
} | ||
|
||
useEffect(() => { | ||
if (removeFromCartResponse == null) | ||
return | ||
getUserWorkshops(); | ||
getUserPresentations(); | ||
}, [ | ||
getUserPresentations, | ||
getUserWorkshops, | ||
removeFromCartResponse, | ||
setPresentationsData, | ||
setRemoveFromCartResponse, | ||
setWorkshopsData, | ||
]); | ||
|
||
const toastDataTemp = {} | ||
switch (removeFromCartResponse.status) { | ||
case 200: | ||
toastDataTemp.message = "Item Removed Successfully" | ||
toastDataTemp.alertType = "success" | ||
break; | ||
case 400: | ||
toastDataTemp.message = "Failed to Remove Item" | ||
toastDataTemp.alertType = "error" | ||
break; | ||
default: | ||
toastDataTemp.message = "Unknown Error. Please Try Again Later" | ||
toastDataTemp.alertType = "error" | ||
break; | ||
} | ||
|
||
setToastData(toastDataTemp) | ||
setOpenToast(true) | ||
getUserWorkshops() | ||
getUserPresentations() | ||
}, [getUserPresentations, getUserWorkshops, removeFromCartResponse, setPresentationsData, setRemoveFromCartResponse, setWorkshopsData]) | ||
|
||
useEffect(() => { | ||
if (!accessToken) | ||
return | ||
useEffect(() => { | ||
if (!accessToken) return; | ||
|
||
getWorkshopsData() | ||
getPresentationsData() | ||
getUserWorkshops() | ||
getUserPresentations() | ||
}, [accessToken, getPresentationsData, getUserPresentations, getUserWorkshops, getWorkshopsData]) | ||
getWorkshopsData(); | ||
getPresentationsData(); | ||
getUserWorkshops(); | ||
getUserPresentations(); | ||
}, [accessToken, getPresentationsData, getUserPresentations, getUserWorkshops, getWorkshopsData]); | ||
|
||
useEffect(() => { | ||
if (!userPresentationsData | ||
|| !userWorkshopsData | ||
|| !workshopsData | ||
|| !presentationsData) | ||
return | ||
useEffect(() => { | ||
if (!userPresentationsData || !userWorkshopsData || !workshopsData || !presentationsData) return; | ||
|
||
const userTempPresentations = [] | ||
const userTempWorkshops = [] | ||
const userTempCart = [] | ||
for (const presentation of presentationsData) { | ||
for (const userPresentation of userPresentationsData.data) { | ||
if (presentation.id === userPresentation.id) { | ||
presentation.type = "presentation" | ||
if (userPresentation.status !== "AWAITING_PAYMENT") { | ||
userTempPresentations.push(presentation) | ||
} else { | ||
userTempCart.push(presentation) | ||
} | ||
} | ||
} | ||
const userTempPresentations = []; | ||
const userTempWorkshops = []; | ||
const userTempCart = []; | ||
for (const presentation of presentationsData) { | ||
for (const userPresentation of userPresentationsData.data) { | ||
if (presentation.id === userPresentation.id) { | ||
presentation.type = 'presentation'; | ||
if (userPresentation.status !== 'AWAITING_PAYMENT') { | ||
userTempPresentations.push(presentation); | ||
} else { | ||
userTempCart.push(presentation); | ||
} | ||
} | ||
for (const workshop of workshopsData) { | ||
for (const userWorkshop of userWorkshopsData.data) { | ||
if (workshop.id === userWorkshop.id) { | ||
workshop.type = "workshop" | ||
if (userWorkshop.status !== "AWAITING_PAYMENT") { | ||
userTempWorkshops.push(workshop) | ||
} else { | ||
userTempCart.push(workshop) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
for (const workshop of workshopsData) { | ||
for (const userWorkshop of userWorkshopsData.data) { | ||
if (workshop.id === userWorkshop.id) { | ||
workshop.type = 'workshop'; | ||
if (userWorkshop.status !== 'AWAITING_PAYMENT') { | ||
userTempWorkshops.push(workshop); | ||
} else { | ||
userTempCart.push(workshop); | ||
} | ||
} | ||
} | ||
} | ||
|
||
setTalks(userTempPresentations) | ||
setWorkshops(userTempWorkshops) | ||
setCart(userTempCart) | ||
}, [userPresentationsData, workshopsData, presentationsData, userWorkshopsData]) | ||
setTalks(userTempPresentations); | ||
setWorkshops(userTempWorkshops); | ||
setCart(userTempCart); | ||
}, [userPresentationsData, workshopsData, presentationsData, userWorkshopsData]); | ||
|
||
return { | ||
talks, | ||
workshops, | ||
removeFromCartHandler, | ||
cart, | ||
toastData, | ||
openToast, | ||
setOpenToast | ||
}; | ||
return { | ||
talks, | ||
workshops, | ||
removeFromCartHandler, | ||
cart, | ||
toastData, | ||
openToast, | ||
setOpenToast, | ||
}; | ||
} |