File size: 7,680 Bytes
2a60e5d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import React, { useState } from 'react';
import { X, Search, Bitcoin, BarChart3 } from 'lucide-react';
import { marketAPI } from '../utils/api';

const AddHoldingModal = ({ isOpen, onClose, onAdd }) => {
  const [formData, setFormData] = useState({
    symbol: '',
    name: '',
    asset_type: 'stock',
    quantity: '',
    buy_price: '',
    buy_date: new Date().toISOString().split('T')[0],
    notes: ''
  });
  const [searchResults, setSearchResults] = useState([]);
  const [isSearching, setIsSearching] = useState(false);
  const [isSubmitting, setIsSubmitting] = useState(false);
  const [error, setError] = useState('');

  const handleSearch = async (query) => {
    if (query.length < 1) {
      setSearchResults([]);
      return;
    }
    
    setIsSearching(true);
    try {
      const response = await marketAPI.search(query, formData.asset_type);
      setSearchResults(response.data.slice(0, 5));
    } catch (err) {
      console.error('Search error:', err);
    } finally {
      setIsSearching(false);
    }
  };

  const selectAsset = (asset) => {
    setFormData({
      ...formData,
      symbol: asset.symbol,
      name: asset.name
    });
    setSearchResults([]);
  };

  const handleSubmit = async (e) => {
    e.preventDefault();
    setError('');
    
    if (!formData.symbol || !formData.quantity || !formData.buy_price) {
      setError('Please fill in all required fields');
      return;
    }
    
    setIsSubmitting(true);
    try {
      const success = await onAdd({
        ...formData,
        quantity: parseFloat(formData.quantity),
        buy_price: parseFloat(formData.buy_price),
        buy_date: new Date(formData.buy_date).toISOString()
      });
      
      if (success) {
        onClose();
        setFormData({
          symbol: '',
          name: '',
          asset_type: 'stock',
          quantity: '',
          buy_price: '',
          buy_date: new Date().toISOString().split('T')[0],
          notes: ''
        });
      } else {
        setError('Failed to add holding');
      }
    } catch (err) {
      setError(err.message);
    } finally {
      setIsSubmitting(false);
    }
  };

  if (!isOpen) return null;

  return (
    <div className="fixed inset-0 bg-black/70 flex items-center justify-center z-50 p-4">
      <div className="glass-card w-full max-w-md animate-fade-in">
        <div className="flex items-center justify-between p-6 border-b border-white/10">
          <h2 className="text-xl font-bold">Add Holding</h2>
          <button 
            onClick={onClose}
            className="p-2 text-gray-400 hover:text-white hover:bg-white/10 rounded-lg transition-colors"
          >
            <X className="w-5 h-5" />
          </button>
        </div>

        <form onSubmit={handleSubmit} className="p-6 space-y-4">
          {/* Asset Type Toggle */}
          <div className="flex gap-2">
            <button
              type="button"
              onClick={() => setFormData({ ...formData, asset_type: 'stock' })}
              className={`flex-1 flex items-center justify-center gap-2 py-3 rounded-xl border transition-all ${
                formData.asset_type === 'stock'
                  ? 'bg-primary-500/20 border-primary-500 text-primary-400'
                  : 'border-white/10 text-gray-400 hover:border-white/20'
              }`}
            >
              <BarChart3 className="w-5 h-5" />
              <span>Stock</span>
            </button>
            <button
              type="button"
              onClick={() => setFormData({ ...formData, asset_type: 'crypto' })}
              className={`flex-1 flex items-center justify-center gap-2 py-3 rounded-xl border transition-all ${
                formData.asset_type === 'crypto'
                  ? 'bg-orange-500/20 border-orange-500 text-orange-400'
                  : 'border-white/10 text-gray-400 hover:border-white/20'
              }`}
            >
              <Bitcoin className="w-5 h-5" />
              <span>Crypto</span>
            </button>
          </div>

          {/* Symbol Search */}
          <div className="relative">
            <label className="block text-sm text-gray-400 mb-2">Symbol *</label>
            <div className="relative">
              <Search className="absolute left-3 top-1/2 -translate-y-1/2 w-5 h-5 text-gray-400" />
              <input
                type="text"
                value={formData.symbol}
                onChange={(e) => {
                  setFormData({ ...formData, symbol: e.target.value.toUpperCase() });
                  handleSearch(e.target.value);
                }}
                placeholder="Search symbol..."
                className="input-field pl-10"
              />
            </div>
            
            {/* Search Results Dropdown */}
            {searchResults.length > 0 && (
              <div className="absolute w-full mt-2 glass-card py-2 z-10">
                {searchResults.map((result) => (
                  <button
                    key={result.symbol}
                    type="button"
                    onClick={() => selectAsset(result)}
                    className="w-full px-4 py-2 text-left hover:bg-white/10 transition-colors"
                  >
                    <span className="font-medium">{result.symbol}</span>
                    <span className="text-gray-400 ml-2">{result.name}</span>
                  </button>
                ))}
              </div>
            )}
          </div>

          {/* Quantity & Price */}
          <div className="grid grid-cols-2 gap-4">
            <div>
              <label className="block text-sm text-gray-400 mb-2">Quantity *</label>
              <input
                type="number"
                step="any"
                value={formData.quantity}
                onChange={(e) => setFormData({ ...formData, quantity: e.target.value })}
                placeholder="0.00"
                className="input-field"
              />
            </div>
            <div>
              <label className="block text-sm text-gray-400 mb-2">Buy Price (USD) *</label>
              <input
                type="number"
                step="any"
                value={formData.buy_price}
                onChange={(e) => setFormData({ ...formData, buy_price: e.target.value })}
                placeholder="0.00"
                className="input-field"
              />
            </div>
          </div>

          {/* Buy Date */}
          <div>
            <label className="block text-sm text-gray-400 mb-2">Buy Date</label>
            <input
              type="date"
              value={formData.buy_date}
              onChange={(e) => setFormData({ ...formData, buy_date: e.target.value })}
              className="input-field"
            />
          </div>

          {/* Notes */}
          <div>
            <label className="block text-sm text-gray-400 mb-2">Notes (optional)</label>
            <textarea
              value={formData.notes}
              onChange={(e) => setFormData({ ...formData, notes: e.target.value })}
              placeholder="Add notes..."
              rows={2}
              className="input-field resize-none"
            />
          </div>

          {error && (
            <p className="text-danger-400 text-sm">{error}</p>
          )}

          {/* Submit Button */}
          <button
            type="submit"
            disabled={isSubmitting}
            className="btn-primary w-full disabled:opacity-50"
          >
            {isSubmitting ? 'Adding...' : 'Add Holding'}
          </button>
        </form>
      </div>
    </div>
  );
};

export default AddHoldingModal;