-
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.
Merge pull request #29 from AAISS/fix/update-item-card-data
fix: show correct data for capacity and cost
- Loading branch information
Showing
2 changed files
with
105 additions
and
102 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,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, | ||
}; | ||
} |