Skip to content

Commit

Permalink
Merge pull request #29 from AAISS/fix/update-item-card-data
Browse files Browse the repository at this point in the history
fix: show correct data for capacity and cost
  • Loading branch information
KimiaMontazeri authored Nov 27, 2023
2 parents d4cfaac + 2f017cb commit 8727bb5
Show file tree
Hide file tree
Showing 2 changed files with 105 additions and 102 deletions.
2 changes: 2 additions & 0 deletions frontend/src/pages/workshops/WorkshopsPage.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ export default function WorkshopsPage() {
description={item.desc && Helper.omitLongString(item.desc, 50)}
purchaseState={0}
presenterName={item.presenters.join(', ')}
cost={item.cost}
capacity={item.capacity}
startDate={new Date(item.start_date).toLocaleString()}
endDate={new Date(item.end_date).toLocaleString()}
level={item.level}
Expand Down
205 changes: 103 additions & 102 deletions frontend/src/pages/workshops/useWorkshopsPage.js
Original file line number Diff line number Diff line change
@@ -1,115 +1,116 @@
import {useCallback, useEffect, useState} from "react";
import {useAPI} from "../../providers/APIProvider/APIProvider.jsx";
import { useCallback, useEffect, useState } from 'react';
import { useAPI } from '../../providers/APIProvider/APIProvider.jsx';

export default function useWorkshopsPage() {
const {
getWorkshopsData,
workshopsData,
getPresentationsData,
presentationsData,
addToCartResponse,
addItemToCart,
teachersData,
getTeachersData,
presenterData,
getPresenterData,
setAddToCartResponse,
} = useAPI()
const {
getWorkshopsData,
workshopsData,
getPresentationsData,
presentationsData,
addToCartResponse,
addItemToCart,
teachersData,
getTeachersData,
presenterData,
getPresenterData,
setAddToCartResponse,
} = useAPI();

const [parsedItemsList, setParsedItemsList] = useState()
const [gridTemplateColumnsValue, setGridTemplateColumnsValue] = useState("")
const [toastData, setToastData] = useState(false)
const [openToast, setOpenToast] = useState(false)
const [parsedItemsList, setParsedItemsList] = useState();
const [gridTemplateColumnsValue, setGridTemplateColumnsValue] = useState('');
const [toastData, setToastData] = useState(false);
const [openToast, setOpenToast] = useState(false);

useEffect(() => {
const func = () => {
setGridTemplateColumnsValue("1fr ".repeat(Math.floor(window.innerWidth / 400)))
}
func()
window.addEventListener("resize", func);
}, [])
useEffect(() => {
const func = () => {
setGridTemplateColumnsValue('1fr '.repeat(Math.floor(window.innerWidth / 400)));
};
func();
window.addEventListener('resize', func);
}, []);

useEffect(() => {
if (addToCartResponse == null) return
useEffect(() => {
if (addToCartResponse == null) return;

const toastDataTemp = {}
switch (addToCartResponse.status) {
case 200:
toastDataTemp.type = "success"
toastDataTemp.message = "Item Successfully Added to Cart!"
break;
case 400:
toastDataTemp.type = "error"
toastDataTemp.message = "Failed to Add Item to Cart: Item Is Already In Your Cart!"
break;
case 401:
toastDataTemp.type = "error"
toastDataTemp.message = "Failed to Add Item to Cart: You Should Login First!"
}
setToastData(toastDataTemp)
setOpenToast(true)
setAddToCartResponse(null)
}, [addToCartResponse])
const toastDataTemp = {};
switch (addToCartResponse.status) {
case 200:
toastDataTemp.type = 'success';
toastDataTemp.message = 'Item Successfully Added to Cart!';
break;
case 400:
toastDataTemp.type = 'error';
toastDataTemp.message = 'Failed to Add Item to Cart: Item Is Already In Your Cart!';
break;
case 401:
toastDataTemp.type = 'error';
toastDataTemp.message = 'Failed to Add Item to Cart: You Should Login First!';
}
setToastData(toastDataTemp);
setOpenToast(true);
setAddToCartResponse(null);
}, [addToCartResponse]);

useEffect(() => {
getWorkshopsData()
getPresentationsData()
getTeachersData()
getPresenterData()
}, [getPresentationsData, getWorkshopsData])
useEffect(() => {
getWorkshopsData();
getPresentationsData();
getTeachersData();
getPresenterData();
}, [getPresentationsData, getWorkshopsData]);

useEffect(() => {
if (workshopsData == null
|| presentationsData == null
|| teachersData == null
|| presenterData == null) return
useEffect(() => {
if (workshopsData == null || presentationsData == null || teachersData == null || presenterData == null) return;

const parsedData = workshopsData.concat(presentationsData).map(workshop => {
if ("is_full" in workshop && !("id" in workshop)) return null
if (workshop.year < 2023)
return
const item = {}
const parsedData = workshopsData
.concat(presentationsData)
.map((workshop) => {
if ('is_full' in workshop && !('id' in workshop)) return null;
if (workshop.year < 2023) return;
const item = {};

const presenters = []
if (workshop.teachers) {
workshop.teachers.forEach(item => {
presenters.push(teachersData.filter(el => el.id === item)[0].name);
})
} else {
workshop.presenters.forEach(item => {
presenters.push(presenterData.filter(el => el.id === item)[0].name);
})
}
item.presenters = presenters
const presenters = [];
if (workshop.teachers) {
workshop.teachers.forEach((item) => {
presenters.push(teachersData.filter((el) => el.id === item)[0].name);
});
} else {
workshop.presenters.forEach((item) => {
presenters.push(presenterData.filter((el) => el.id === item)[0].name);
});
}
item.presenters = presenters;

item["id"] = workshop.id
item["name"] = workshop.name
item["start_date"] = workshop.start_date
item["end_date"] = workshop.end_date
item["level"] = workshop.level
item["desc"] = workshop.desc
item["isWorkshop"] = !("presenters" in workshop)
return item
}).filter(e => e != null)
setParsedItemsList(parsedData)
}, [workshopsData, presentationsData, presenterData, teachersData])
item['id'] = workshop.id;
item['name'] = workshop.name;
item['start_date'] = workshop.start_date;
item['end_date'] = workshop.end_date;
item['level'] = workshop.level;
item['desc'] = workshop.desc;
item['cost'] = workshop.cost;
item['capacity'] = workshop.capacity;
item['isWorkshop'] = !('presenters' in workshop);
return item;
})
.filter((e) => e != null);
setParsedItemsList(parsedData);
}, [workshopsData, presentationsData, presenterData, teachersData]);

const addToCart = useCallback(({
id,
type
}) => {
addItemToCart({
id,
type,
})
}, [addItemToCart])
const addToCart = useCallback(
({ id, type }) => {
addItemToCart({
id,
type,
});
},
[addItemToCart],
);

return {
parsedItemsList,
addToCart,
gridTemplateColumnsValue,
toastData,
setOpenToast,
openToast,
}
}
return {
parsedItemsList,
addToCart,
gridTemplateColumnsValue,
toastData,
setOpenToast,
openToast,
};
}

0 comments on commit 8727bb5

Please sign in to comment.