File size: 2,711 Bytes
98cb11c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
```javascript
import { useState } from 'react'
import { supabase } from './supabaseClient'

export default function ProjectCard({ project, onUpdate }) {
  const [isEditing, setIsEditing] = useState(false)
  const [title, setTitle] = useState(project.title)
  const [description, setDescription] = useState(project.description)

  async function deleteProject() {
    const { error } = await supabase
      .from('user_projects')
      .delete()
      .eq('id', project.id)

    if (error) alert(error.message)
    else onUpdate()
  }

  async function updateProject() {
    const { error } = await supabase
      .from('user_projects')
      .update({ title, description })
      .eq('id', project.id)

    if (error) alert(error.message)
    else {
      onUpdate()
      setIsEditing(false)
    }
  }

  return (
    <div className="bg-gray-800 rounded-xl p-6 shadow-lg border border-gray-700 hover:border-primary/50 transition">
      {isEditing ? (
        <div className="space-y-4">
          <input
            type="text"
            value={title}
            onChange={(e) => setTitle(e.target.value)}
            className="w-full bg-gray-700 rounded-lg px-4 py-2 text-white"
          />
          <textarea
            value={description}
            onChange={(e) => setDescription(e.target.value)}
            className="w-full bg-gray-700 rounded-lg px-4 py-2 text-white"
            rows="3"
          />
          <div className="flex space-x-2">
            <button
              onClick={updateProject}
              className="bg-primary hover:bg-secondary px-3 py-1 rounded-lg text-sm"
            >
              Save
            </button>
            <button
              onClick={() => setIsEditing(false)}
              className="bg-gray-700 hover:bg-gray-600 px-3 py-1 rounded-lg text-sm"
            >
              Cancel
            </button>
          </div>
        </div>
      ) : (
        <div>
          <h3 className="text-xl font-bold mb-2">{project.title}</h3>
          <p className="text-gray-400 mb-4">{project.description}</p>
          <p className="text-xs text-gray-500">
            {new Date(project.created_at).toLocaleString()}
          </p>
          <div className="flex space-x-2 mt-4">
            <button
              onClick={() => setIsEditing(true)}
              className="bg-gray-700 hover:bg-gray-600 px-3 py-1 rounded-lg text-sm"
            >
              Edit
            </button>
            <button
              onClick={deleteProject}
              className="bg-red-900 hover:bg-red-800 px-3 py-1 rounded-lg text-sm"
            >
              Delete
            </button>
          </div>
        </div>
      )}
    </div>
  )
}
```