Skip to content

Commit

Permalink
fix: call apis to get user data
Browse files Browse the repository at this point in the history
  • Loading branch information
KimiaMontazeri committed Nov 27, 2023
1 parent d247aac commit f7342c3
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 111 deletions.
3 changes: 1 addition & 2 deletions frontend/src/pages/my-account/MyAccount.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,7 @@ const MyAccount = () => {
};

const List = ({ type, items }) => {
if (items == null) return;
return items.map((item, index) => (
return items?.map((item, index) => (
<ItemCard
key={index}
isWorkshop={type === 'Workshops'}
Expand Down
219 changes: 110 additions & 109 deletions frontend/src/pages/my-account/useMyAccount.js
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,
};
}

0 comments on commit f7342c3

Please sign in to comment.