File size: 6,174 Bytes
aa120c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19d90a0
aa120c6
 
 
 
 
 
19d90a0
aa120c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19d90a0
aa120c6
 
 
 
 
 
 
 
 
 
19d90a0
aa120c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
19d90a0
aa120c6
 
 
 
 
 
 
19d90a0
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import { useNavigate } from 'react-router-dom';
import { useEffect, useState } from 'react';
import './TierList.css';

const API_BASE_URL = "/api";

const YourRanksContainer = () => {
    const navigate = useNavigate();
    const [tierLists, setTierLists] = useState([]);
    const [loading, setLoading] = useState(true);
    const [newName, setNewName] = useState('');
    const [creating, setCreating] = useState(false);

    useEffect(() => {
        fetchTierLists();
    }, []);

    const fetchTierLists = () => {
        setLoading(true);
        fetch(`${API_BASE_URL}/tierlist/read`)
            .then(async res => {
                const text = await res.text();
                try {
                    const data = JSON.parse(text);
                    setTierLists(Array.isArray(data) ? data : []);
                } catch (e) {
                    console.error('Invalid JSON from tierlist/read:', text);
                    setTierLists([]);
                }
                setLoading(false);
            })
            .catch(error => {
                console.error('Error fetching tier lists:', error);
                setLoading(false);
            });
    };

    const handleCreate = async (e) => {
        e.preventDefault();
        if (!newName.trim() || creating) return;

        setCreating(true);
        try {
            const response = await fetch(`${API_BASE_URL}/tierlist/create`, {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ name: newName.trim() }),
            });

            const text = await response.text();
            let data = null;
            try {
                data = JSON.parse(text);
            } catch (e) {
                console.error('Non-JSON response from create:', text);
                alert('Server error creating tier list: ' + text);
                setCreating(false);
                return;
            }

            console.log('Create response:', data);

            if (data && data.success && data.id) {
                setNewName('');
                await fetchTierLists();
                navigate(`/start-ranking/${data.id}`);
            } else {
                alert((data && data.message) ? data.message : 'Failed to create tier list. Please try again.');
            }
        } catch (error) {
            console.error('Error creating tier list:', error);
            alert('Error creating tier list. Check console for details.');
        } finally {
            setCreating(false);
        }
    };

    const handleDelete = async (id) => {
        if (!window.confirm('Are you sure you want to delete this tier list?')) return;
        
        try {
            const response = await fetch(`${API_BASE_URL}/tierlist/delete`, {
                method: 'POST',
                headers: { 'Content-Type': 'application/json' },
                body: JSON.stringify({ id }),
            });

            const text = await response.text();
            let data = null;
            try { data = JSON.parse(text); } catch (e) { console.error('Non-JSON response from delete:', text); }

            if (data && data.success) {
                await fetchTierLists();
            } else {
                alert((data && data.message) ? data.message : 'Failed to delete tier list. Please try again.');
            }
        } catch (error) {
            console.error('Error deleting tier list:', error);
            alert('Error deleting tier list. Please try again.');
        }
    };

    return (
        <div className="your-ranks-container">
            <div className="tier-list-header">
                <h2>Have Fun!</h2>
                <p>Create a new tier list or continue ranking your existing ones.</p>
            </div>

            <div className="create-section">
                <form onSubmit={handleCreate} className="create-list-form">
                    <input
                        type="text"
                        value={newName}
                        onChange={(e) => setNewName(e.target.value)}
                        placeholder="Enter name for new tier list"
                        disabled={creating}
                        required
                    />
                    <button type="submit" className="create-button" disabled={creating}>
                        {creating ? 'Creating...' : 'Create New Tier List'}
                    </button>
                </form>
            </div>

            <div className="existing-lists">
                <h3>Your Tier Lists</h3>
                {loading ? (
                    <div className="loading">Loading your tier lists...</div>
                ) : tierLists.length === 0 ? (
                    <div className="no-lists">
                        <p>You haven't created any tier lists yet.</p>
                        <p>Create one above to get started!</p>
                    </div>
                ) : (
                    <div className="tier-lists-grid">
                        {tierLists.map(list => (
                            <div key={list.id} className="tier-list-card">
                                <h4>{list.name}</h4>
                                <div className="tier-list-actions">
                                    <button 
                                        onClick={() => navigate(`/start-ranking/${list.id}`)}
                                        className="edit-button"
                                    >
                                        Continue Ranking
                                    </button>
                                    <button 
                                        onClick={() => handleDelete(list.id)}
                                        className="delete-button"
                                    >
                                        Delete
                                    </button>
                                </div>
                            </div>
                        ))}
                    </div>
                )}
            </div>
        </div>
    );
};

export default YourRanksContainer;