forked from Nova-reward/Nova-Rewards
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRedemptionModal.js
More file actions
180 lines (164 loc) · 6.7 KB
/
Copy pathRedemptionModal.js
File metadata and controls
180 lines (164 loc) · 6.7 KB
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
'use client';
import { useState } from 'react';
import Image from 'next/image';
/**
* Multi-step redemption modal:
* 1. Review – show reward details & select amount
* 2. Sign – prompt wallet signature
* 3. Status – real-time transaction tracking (pending → success/error)
*/
export default function RedemptionModal({
isOpen,
reward,
currentPoints,
onConfirm, // async fn(amount) → throws on error
onCancel,
isLoading,
txStatus, // 'idle' | 'pending' | 'success' | 'error'
txHash,
txError,
}) {
const [amount, setAmount] = useState(1);
if (!isOpen || !reward) return null;
const maxAmount = reward.stock > 0 ? Math.min(reward.stock, Math.floor(currentPoints / reward.cost)) : 0;
const totalCost = reward.cost * amount;
const pointsAfter = currentPoints - totalCost;
const canConfirm = amount >= 1 && amount <= maxAmount && !isLoading;
// Determine which step to show
const step = txStatus === 'idle' ? 'review' : txStatus;
const handleConfirm = () => {
onConfirm(amount);
};
return (
<div className="modal-overlay" onClick={txStatus === 'idle' ? onCancel : undefined}>
<div className="modal-content redemption-modal" onClick={(e) => e.stopPropagation()}>
{/* ── Step: Review & select amount ── */}
{step === 'review' && (
<>
<h2>Confirm Redemption</h2>
<div className="redemption-details">
{reward.image_url && (
<div style={{ position: 'relative', width: '100%', height: '160px', borderRadius: '8px', overflow: 'hidden' }}>
<Image src={reward.image_url} alt={reward.name} fill sizes="(max-width: 600px) 100vw, 480px" style={{ objectFit: 'cover' }} className="redemption-image" />
</div>
)}
<div className="redemption-info">
<h3>{reward.name}</h3>
{reward.description && (
<p className="redemption-description">{reward.description}</p>
)}
{reward.category && (
<span className="badge badge-gray">{reward.category}</span>
)}
</div>
</div>
{/* Amount selector */}
{maxAmount > 1 && (
<div className="redemption-amount-row">
<label className="label" htmlFor="redeem-amount">Quantity</label>
<div className="amount-stepper">
<button
className="btn btn-secondary stepper-btn"
onClick={() => setAmount((a) => Math.max(1, a - 1))}
disabled={amount <= 1}
aria-label="Decrease quantity"
>−</button>
<input
id="redeem-amount"
type="number"
className="input stepper-input"
min={1}
max={maxAmount}
value={amount}
onChange={(e) => {
const v = Math.max(1, Math.min(maxAmount, Number(e.target.value)));
setAmount(v);
}}
/>
<button
className="btn btn-secondary stepper-btn"
onClick={() => setAmount((a) => Math.min(maxAmount, a + 1))}
disabled={amount >= maxAmount}
aria-label="Increase quantity"
>+</button>
</div>
</div>
)}
<div className="redemption-points">
<div className="points-row">
<span className="points-label">Your Points</span>
<span className="points-value">{currentPoints.toLocaleString()}</span>
</div>
<div className="points-row points-deduction">
<span className="points-label">Cost ({amount} × {reward.cost})</span>
<span className="points-value">−{totalCost.toLocaleString()}</span>
</div>
<div className="points-row points-total">
<span className="points-label">Points After</span>
<span className="points-value">{pointsAfter.toLocaleString()}</span>
</div>
</div>
<p className="redemption-wallet-note">
🔐 You will be asked to sign this transaction with your wallet.
</p>
<div className="modal-actions">
<button className="btn btn-secondary" onClick={onCancel} disabled={isLoading}>
Cancel
</button>
<button
className="btn btn-primary"
onClick={handleConfirm}
disabled={!canConfirm}
>
Sign & Redeem
</button>
</div>
</>
)}
{/* ── Step: Pending (waiting for signature / broadcast) ── */}
{step === 'pending' && (
<div className="tx-status-panel">
<div className="tx-spinner" aria-label="Processing" />
<h3>Waiting for Wallet Signature…</h3>
<p className="tx-status-msg">Please approve the transaction in your Freighter wallet.</p>
</div>
)}
{/* ── Step: Success ── */}
{step === 'success' && (
<div className="tx-status-panel">
<div className="tx-icon tx-icon-success" aria-hidden="true">✓</div>
<h3>Redemption Successful!</h3>
<p className="tx-status-msg">
You redeemed <strong>{reward.name}</strong>.
</p>
{txHash && (
<a
className="tx-hash-link"
href={`https://stellar.expert/explorer/testnet/tx/${txHash}`}
target="_blank"
rel="noopener noreferrer"
>
View on Stellar Explorer ↗
</a>
)}
<div className="modal-actions">
<button className="btn btn-primary" onClick={onCancel}>Done</button>
</div>
</div>
)}
{/* ── Step: Error ── */}
{step === 'error' && (
<div className="tx-status-panel">
<div className="tx-icon tx-icon-error" aria-hidden="true">✕</div>
<h3>Redemption Failed</h3>
<p className="tx-status-msg error">{txError || 'An unexpected error occurred.'}</p>
<div className="modal-actions">
<button className="btn btn-secondary" onClick={onCancel}>Close</button>
<button className="btn btn-primary" onClick={() => onConfirm(amount)}>Retry</button>
</div>
</div>
)}
</div>
</div>
);
}