|
| 1 | +"use client"; |
| 2 | + |
| 3 | +import { useState, useEffect } from "react"; |
| 4 | +import { createClient } from "../lib/supabase/client"; |
| 5 | +import { Pencil, Check, X } from "lucide-react"; |
| 6 | + |
| 7 | +// Map enum values to display names |
| 8 | +const CLASS_OPTIONS = [ |
| 9 | + { value: 'warrior', label: 'Python Warrior', comingSoon: false }, |
| 10 | + { value: 'mage', label: 'Java Mage', comingSoon: true }, |
| 11 | + { value: 'rogue', label: 'C++ Rogue', comingSoon: true }, |
| 12 | +] as const; |
| 13 | + |
| 14 | +interface EditClassProps { |
| 15 | + currentClass: string; |
| 16 | + currentClassEnum: string | null; |
| 17 | + userId: string; |
| 18 | +} |
| 19 | + |
| 20 | +export default function EditClass({ currentClass, currentClassEnum, userId }: EditClassProps) { |
| 21 | + const [isEditing, setIsEditing] = useState(false); |
| 22 | + // Default to 'warrior' (Python Warrior) which is available |
| 23 | + const [selectedClassEnum, setSelectedClassEnum] = useState<string>(currentClassEnum || 'warrior'); |
| 24 | + const [isSaving, setIsSaving] = useState(false); |
| 25 | + const supabase = createClient(); |
| 26 | + |
| 27 | + useEffect(() => { |
| 28 | + // If current class is coming soon or null, default to warrior (Python Warrior) |
| 29 | + const currentOption = CLASS_OPTIONS.find(opt => opt.value === currentClassEnum); |
| 30 | + if (!currentClassEnum || currentOption?.comingSoon) { |
| 31 | + setSelectedClassEnum('warrior'); |
| 32 | + } else { |
| 33 | + setSelectedClassEnum(currentClassEnum); |
| 34 | + } |
| 35 | + }, [currentClassEnum]); |
| 36 | + |
| 37 | + const handleSave = async () => { |
| 38 | + setIsSaving(true); |
| 39 | + try { |
| 40 | + // Use upsert to insert if profile doesn't exist, or update if it does |
| 41 | + const { data, error } = await supabase |
| 42 | + .from('profiles') |
| 43 | + .upsert({ |
| 44 | + id: userId, |
| 45 | + class: selectedClassEnum |
| 46 | + }, { |
| 47 | + onConflict: 'id' |
| 48 | + }) |
| 49 | + .select(); |
| 50 | + |
| 51 | + if (error) { |
| 52 | + console.error("Supabase error:", error); |
| 53 | + throw error; |
| 54 | + } |
| 55 | + |
| 56 | + console.log("Class saved successfully:", data); |
| 57 | + setIsEditing(false); |
| 58 | + // Refresh the page to show updated class |
| 59 | + window.location.reload(); |
| 60 | + } catch (error) { |
| 61 | + console.error("Error updating class:", error); |
| 62 | + alert(`Failed to update class: ${error instanceof Error ? error.message : 'Unknown error'}`); |
| 63 | + } finally { |
| 64 | + setIsSaving(false); |
| 65 | + } |
| 66 | + }; |
| 67 | + |
| 68 | + const handleCancel = () => { |
| 69 | + // Reset to current class or default to warrior if coming soon |
| 70 | + const currentOption = CLASS_OPTIONS.find(opt => opt.value === currentClassEnum); |
| 71 | + if (!currentClassEnum || currentOption?.comingSoon) { |
| 72 | + setSelectedClassEnum('warrior'); |
| 73 | + } else { |
| 74 | + setSelectedClassEnum(currentClassEnum); |
| 75 | + } |
| 76 | + setIsEditing(false); |
| 77 | + }; |
| 78 | + |
| 79 | + if (isEditing) { |
| 80 | + return ( |
| 81 | + <div className="flex items-start gap-3 mb-4 relative"> |
| 82 | + <div |
| 83 | + className="absolute left-0 top-0 bottom-0 rounded" |
| 84 | + style={{ |
| 85 | + backgroundColor: "#be9661", |
| 86 | + width: "4px", |
| 87 | + marginTop: "-2px", |
| 88 | + marginBottom: "-2px", |
| 89 | + }} |
| 90 | + /> |
| 91 | + <div className="pl-3 flex-1"> |
| 92 | + <span className="text-sm block mb-2" style={{ color: "#A0A0A0" }}> |
| 93 | + Class |
| 94 | + </span> |
| 95 | + <select |
| 96 | + value={selectedClassEnum} |
| 97 | + onChange={(e) => setSelectedClassEnum(e.target.value)} |
| 98 | + className="w-full px-3 py-2 rounded-lg mb-2" |
| 99 | + style={{ |
| 100 | + backgroundColor: "#232331", |
| 101 | + color: "#E0E0E0", |
| 102 | + border: "0.5px solid rgba(190, 150, 97, 0.3)", |
| 103 | + }} |
| 104 | + > |
| 105 | + {CLASS_OPTIONS.map((option) => ( |
| 106 | + <option key={option.value} value={option.value}> |
| 107 | + {option.label}{option.comingSoon ? ' - Coming Soon' : ''} |
| 108 | + </option> |
| 109 | + ))} |
| 110 | + </select> |
| 111 | + <div className="flex gap-2"> |
| 112 | + <button |
| 113 | + onClick={handleSave} |
| 114 | + disabled={isSaving} |
| 115 | + className="px-3 py-1 rounded-lg flex items-center gap-1" |
| 116 | + style={{ |
| 117 | + backgroundColor: "#be9661", |
| 118 | + color: "#191922", |
| 119 | + border: "none", |
| 120 | + }} |
| 121 | + > |
| 122 | + <Check size={14} /> |
| 123 | + <span>{isSaving ? "Saving..." : "Save"}</span> |
| 124 | + </button> |
| 125 | + <button |
| 126 | + onClick={handleCancel} |
| 127 | + disabled={isSaving} |
| 128 | + className="px-3 py-1 rounded-lg flex items-center gap-1" |
| 129 | + style={{ |
| 130 | + backgroundColor: "#232331", |
| 131 | + color: "#E0E0E0", |
| 132 | + border: "0.5px solid rgba(190, 150, 97, 0.3)", |
| 133 | + }} |
| 134 | + > |
| 135 | + <X size={14} /> |
| 136 | + <span>Cancel</span> |
| 137 | + </button> |
| 138 | + </div> |
| 139 | + </div> |
| 140 | + </div> |
| 141 | + ); |
| 142 | + } |
| 143 | + |
| 144 | + return ( |
| 145 | + <div className="flex items-start gap-3 mb-4 relative"> |
| 146 | + <div |
| 147 | + className="absolute left-0 top-0 bottom-0 rounded" |
| 148 | + style={{ |
| 149 | + backgroundColor: "#be9661", |
| 150 | + width: "4px", |
| 151 | + marginTop: "-2px", |
| 152 | + marginBottom: "-2px", |
| 153 | + }} |
| 154 | + /> |
| 155 | + <div className="pl-3 flex-1"> |
| 156 | + <div className="flex items-center justify-between"> |
| 157 | + <div> |
| 158 | + <span className="text-sm block mb-1" style={{ color: "#A0A0A0" }}> |
| 159 | + Class |
| 160 | + </span> |
| 161 | + <span className="text-base block" style={{ color: "#E0E0E0" }}> |
| 162 | + {currentClass || 'Not set'} |
| 163 | + </span> |
| 164 | + </div> |
| 165 | + <button |
| 166 | + onClick={() => setIsEditing(true)} |
| 167 | + className="p-1 rounded transition-opacity hover:opacity-80" |
| 168 | + style={{ color: "#be9661" }} |
| 169 | + title="Edit class" |
| 170 | + > |
| 171 | + <Pencil size={16} /> |
| 172 | + </button> |
| 173 | + </div> |
| 174 | + </div> |
| 175 | + </div> |
| 176 | + ); |
| 177 | +} |
0 commit comments