File size: 4,351 Bytes
aa120c6
 
 
 
 
 
 
 
 
 
 
 
 
 
f85b485
aa120c6
 
 
 
 
 
19d90a0
aa120c6
 
 
 
 
 
66906f8
aa120c6
 
 
 
 
 
 
 
 
 
 
 
 
 
66906f8
aa120c6
 
 
 
 
 
 
 
19d90a0
aa120c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f85b485
 
19d90a0
f85b485
 
 
 
 
 
 
 
 
 
 
 
aa120c6
 
f85b485
aa120c6
f85b485
aa120c6
 
 
 
f85b485
 
 
 
 
 
 
 
 
 
 
 
 
 
aa120c6
 
 
 
 
 
 
 
 
 
 
 
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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
import { useState, useEffect } from 'react';
import { useParams, useNavigate } from 'react-router-dom';
import RankItems from './RankItems';
import ItemUpload from './ItemUpload';
import './TierList.css';

const API_BASE_URL = "/api";

const StartRankingContainer = () => {
    const { tierListId } = useParams();
    const navigate = useNavigate();
    const [items, setItems] = useState([]);
    const [loading, setLoading] = useState(true);               
    const [tierListName, setTierListName] = useState('');
    const itemType = 1; // Always use type 1 for tier list items

    useEffect(() => {
        // Fetch tier list details and items
        const fetchData = async () => {
            try {
                // Fetch tier list details
                const listResponse = await fetch(`${API_BASE_URL}/tierlist/read?id=${tierListId}`);
                const listData = await listResponse.json();
                if (listData && listData.name) {
                    setTierListName(listData.name);
                }

                // Fetch items for this tier list
                const itemsResponse = await fetch(`${API_BASE_URL}/item/read?item_type=${itemType}&tier_list_id=${tierListId}`);
                const itemsData = await itemsResponse.json();
                setItems(Array.isArray(itemsData) ? itemsData : []);
            } catch (error) {
                console.error('Error fetching data:', error);
            } finally {
                setLoading(false);
            }
        };

        fetchData();
    }, [tierListId]);

    const handleUploadComplete = () => {
        // Refresh items after upload
        fetch(`${API_BASE_URL}/item/read?item_type=${itemType}&tier_list_id=${tierListId}`)
            .then(res => res.json())
            .then(data => setItems(Array.isArray(data) ? data : []));
    };

    const handleReset = () => {
        if (window.confirm('Are you sure you want to reset all rankings?')) {
            // Reset all items to ranking 0
            const resetPromises = items.map(item =>
                fetch(`${API_BASE_URL}/item/update`, {
                    method: 'POST',
                    headers: { 'Content-Type': 'application/json' },
                    body: JSON.stringify({ id: item.id, ranking: 0 }),
                })
            );

            Promise.all(resetPromises)
                .then(() => handleUploadComplete())
                .catch(error => console.error('Error resetting rankings:', error));
        }
    };

    const handleBack = () => {
        navigate('/your-ranks');
    };

    if (loading) {
        return <div>Loading...</div>;
    }

    const handleDelete = async (itemId) => {
        try {
            const response = await fetch(`${API_BASE_URL}/item/delete`, {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ id: itemId }),
            });
            if (response.ok) {
                handleUploadComplete();
            }
        } catch (error) {
            console.error('Error deleting item:', error);
        }
    };

    return (
        <div className="ranking-container">
            {/* Header with back button and title */}
            <div className="ranking-header">
                <button onClick={handleBack} className="back-button">← Back to Lists</button>
                <h2>{tierListName || 'Ranking'}</h2>
                <button onClick={handleReset} className="reset-button">Reset Rankings</button>
            </div>

            {/* Grid */}
            <div className="ranking-grid-section">
            <RankItems
                items={items}
                setItems={setItems}
                dataType={itemType}
                tierListId={tierListId}
                localStorageKey={`tierlist-${tierListId}`}
                onRefresh={handleUploadComplete}
                onDeleteItem={handleDelete}
            />
            </div>

            {/* Upload section at the bottom */}
            <div className="upload-section">
                <ItemUpload
                    tierListId={tierListId}
                    itemType={itemType}
                    onUploadComplete={handleUploadComplete}
                />
            </div>
        </div>
    );
};

export default StartRankingContainer;