Spaces:
Sleeping
Sleeping
File size: 3,679 Bytes
f85b485 6f7ef5c aa120c6 6f7ef5c f85b485 6f7ef5c f85b485 19d90a0 f85b485 66906f8 f85b485 6f7ef5c f85b485 6f7ef5c f85b485 6f7ef5c f85b485 6f7ef5c f85b485 6f7ef5c f85b485 6f7ef5c aa120c6 6f7ef5c aa120c6 19d90a0 aa120c6 6f7ef5c aa120c6 6f7ef5c f85b485 6f7ef5c f85b485 6f7ef5c f85b485 6f7ef5c f85b485 6f7ef5c f85b485 6f7ef5c | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 | import { useEffect, useState, useCallback } from 'react';
import RankingGrid from "./RankingGrid";
import ItemCollection from "./ItemCollection";
const API_BASE_URL = "/api/item"; // Use proxy to .NET which will forward to PHP
const RankItems = ({ items, setItems, dataType, imgArr, localStorageKey, tierListId, onDeleteItem }) => {
const [reload, setReload] = useState(false);
const getDataFromApi = useCallback(() => {
let url = `${API_BASE_URL}/read`;
if (tierListId) {
url += `?item_type=${dataType}&tier_list_id=${tierListId}`;
} else if (dataType) {
url += `?item_type=${dataType}`;
}
fetch(url)
.then((results) => results.json())
.then(data => {
setItems(data);
})
.catch(error => {
setItems([]);
console.error('Error fetching items:', error);
});
}, [tierListId, dataType, setItems]);
function Reload() {
setReload(!reload);
getDataFromApi();
}
function drag(ev) {
ev.dataTransfer.setData("text", ev.target.id);
}
function allowDrop(ev) {
ev.preventDefault();
}
function drop(ev) {
ev.preventDefault();
const targetElm = ev.target;
// Don't allow dropping on images
if (targetElm.nodeName === "IMG") {
return false;
}
var data = parseInt(ev.dataTransfer.getData("text").substring(5));
// If dropped on a rank cell (has id starting with "rank-")
if (targetElm.id && targetElm.id.startsWith("rank-")) {
if (targetElm.childNodes.length === 0) {
const ranking = parseInt(targetElm.id.substring(5));
const transformedCollection = items.map((item) => (item.id === parseInt(data)) ?
{ ...item, ranking: ranking } : { ...item, ranking: item.ranking });
setItems(transformedCollection);
updateItemRanking(data, ranking);
}
}
// If dropped on unranked section or unranked items
else if (targetElm.className === "items-not-ranked" || targetElm.closest(".items-not-ranked")) {
const transformedCollection = items.map((item) => (item.id === parseInt(data)) ?
{ ...item, ranking: 0 } : { ...item, ranking: item.ranking });
setItems(transformedCollection);
updateItemRanking(data, 0);
}
}
function updateItemRanking(itemId, newRanking) {
fetch(`${API_BASE_URL}/update`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({ id: itemId, ranking: newRanking }),
})
.then(response => response.json())
.catch(error => console.error('Error updating ranking:', error));
}
useEffect(() => {
if (!items || items.length === 0) {
getDataFromApi();
}
}, [getDataFromApi, items.length]);
useEffect(() => {
if (items != null && localStorageKey) {
localStorage.setItem(localStorageKey, JSON.stringify(items));
}
}, [items, localStorageKey])
return (
(items != null) ?
<main>
<RankingGrid items={items} imgArr={imgArr} drag={drag} allowDrop={allowDrop} drop={drop} />
<ItemCollection items={items} drag={drag} imgArr={imgArr} onDelete={onDeleteItem} allowDrop={allowDrop} drop={drop} />
</main>
: <main>Loading...</main>
)
}
export default RankItems; |