CBV01 commited on
Commit
de8d8fa
·
1 Parent(s): 683df00

Separate dropdown and approve button logic correctly

Browse files
Files changed (1) hide show
  1. frontend/src/app/admin/page.tsx +41 -16
frontend/src/app/admin/page.tsx CHANGED
@@ -984,21 +984,32 @@ export default function AdminDashboard() {
984
  value={u.plan}
985
  onChange={async (e) => {
986
  const newPlan = e.target.value;
987
- // Optimistically update local state FIRST so UI doesn't snap back
 
 
 
988
  setMonetizationUsers(prev => prev.map(mu =>
989
- mu.id === u.id ? { ...mu, plan: newPlan, is_approved: newPlan === 'free' ? 0 : 1 } : mu
 
 
 
 
 
 
990
  ));
 
991
  const res = await apiFetch(`/api/admin/monetization/users/${u.id}/vitals`, {
992
  method: 'POST',
993
  body: JSON.stringify({ plan: newPlan, is_approved: newPlan === 'free' ? 0 : 1 })
994
  });
995
  if (res.ok) {
996
- setSuccessMsg(`Plan set to ${newPlan.toUpperCase()} for ${u.username}`);
997
  setTimeout(() => setSuccessMsg(''), 4000);
998
  } else {
999
  setErrorMsg('Failed to update plan. Try again.');
1000
  setTimeout(() => setErrorMsg(''), 4000);
1001
  }
 
1002
  fetchMonetizationData();
1003
  }}
1004
  className={`bg-white/5 border border-white/10 rounded-lg px-3 py-1.5 text-[10px] font-black uppercase transition-all ${u.plan !== 'free' ? 'text-indigo-400 border-indigo-400/30' : 'text-foreground/20'}`}
@@ -1009,12 +1020,12 @@ export default function AdminDashboard() {
1009
  <option value="premium">Premium</option>
1010
  <option value="unlimited">Unlimited (Admin Only)</option>
1011
  </select>
1012
- {u.plan_activated_at && (
1013
  <div className="text-[9px] font-bold text-foreground/40 mt-1 uppercase tracking-tighter">
1014
  Start: {new Date(u.plan_activated_at).toLocaleDateString()}
1015
  </div>
1016
  )}
1017
- {u.plan_expires_at && (
1018
  <div className="text-[9px] font-bold text-red-400 mt-0.5 uppercase tracking-tighter">
1019
  End: {new Date(u.plan_expires_at).toLocaleDateString()}
1020
  </div>
@@ -1058,29 +1069,37 @@ export default function AdminDashboard() {
1058
  )}
1059
  </td>
1060
  <td className="px-6 py-4">
1061
- {u.is_approved === 0 && (
 
1062
  <button
1063
  onClick={async () => {
1064
- // Use the plan currently selected in the dropdown (default basic if still free)
1065
- const planToApprove = u.plan === 'free' ? 'basic' : u.plan;
1066
- if (confirm(`Approve "${planToApprove.toUpperCase()}" plan for ${u.username}?`)) {
1067
- // 1. Optimistically update local state right away so UI reflects change
 
 
 
1068
  setMonetizationUsers(prev => prev.map(mu =>
1069
- mu.id === u.id ? { ...mu, plan: planToApprove, is_approved: 1 } : mu
 
 
 
 
 
 
1070
  ));
1071
- // 2. Send to backend
1072
  const res = await apiFetch(`/api/admin/monetization/users/${u.id}/vitals`, {
1073
  method: 'POST',
1074
  body: JSON.stringify({ plan: planToApprove, is_approved: 1 })
1075
  });
1076
  if (res.ok) {
1077
- setSuccessMsg(`✅ Approved ${u.username} for ${planToApprove}!`);
1078
- setTimeout(() => setSuccessMsg(''), 4000);
1079
  } else {
1080
- setErrorMsg('Approval failed. Check server logs.');
1081
  setTimeout(() => setErrorMsg(''), 4000);
1082
  }
1083
- // 3. Re-fetch to verify backend committed the change
1084
  fetchMonetizationData();
1085
  }
1086
  }}
@@ -1089,6 +1108,12 @@ export default function AdminDashboard() {
1089
  <CheckCircle2 size={12} /> Approve
1090
  </button>
1091
  )}
 
 
 
 
 
 
1092
  </td>
1093
  </tr>
1094
  ))}
 
984
  value={u.plan}
985
  onChange={async (e) => {
986
  const newPlan = e.target.value;
987
+ // 1. Optimistically update local state immediately so dropdown doesn't snap back
988
+ const now = new Date();
989
+ const expires = new Date(now);
990
+ expires.setDate(expires.getDate() + 30);
991
  setMonetizationUsers(prev => prev.map(mu =>
992
+ mu.id === u.id ? {
993
+ ...mu,
994
+ plan: newPlan,
995
+ is_approved: newPlan === 'free' ? 0 : 1,
996
+ plan_activated_at: newPlan === 'free' ? null : now.toISOString(),
997
+ plan_expires_at: newPlan === 'free' ? null : expires.toISOString()
998
+ } : mu
999
  ));
1000
+ // 2. Save to backend
1001
  const res = await apiFetch(`/api/admin/monetization/users/${u.id}/vitals`, {
1002
  method: 'POST',
1003
  body: JSON.stringify({ plan: newPlan, is_approved: newPlan === 'free' ? 0 : 1 })
1004
  });
1005
  if (res.ok) {
1006
+ setSuccessMsg(`✅ Plan set to ${newPlan.toUpperCase()} for ${u.username}`);
1007
  setTimeout(() => setSuccessMsg(''), 4000);
1008
  } else {
1009
  setErrorMsg('Failed to update plan. Try again.');
1010
  setTimeout(() => setErrorMsg(''), 4000);
1011
  }
1012
+ // 3. Confirm with backend data
1013
  fetchMonetizationData();
1014
  }}
1015
  className={`bg-white/5 border border-white/10 rounded-lg px-3 py-1.5 text-[10px] font-black uppercase transition-all ${u.plan !== 'free' ? 'text-indigo-400 border-indigo-400/30' : 'text-foreground/20'}`}
 
1020
  <option value="premium">Premium</option>
1021
  <option value="unlimited">Unlimited (Admin Only)</option>
1022
  </select>
1023
+ {u.plan !== 'free' && u.plan_activated_at && (
1024
  <div className="text-[9px] font-bold text-foreground/40 mt-1 uppercase tracking-tighter">
1025
  Start: {new Date(u.plan_activated_at).toLocaleDateString()}
1026
  </div>
1027
  )}
1028
+ {u.plan !== 'free' && u.plan_expires_at && (
1029
  <div className="text-[9px] font-bold text-red-400 mt-0.5 uppercase tracking-tighter">
1030
  End: {new Date(u.plan_expires_at).toLocaleDateString()}
1031
  </div>
 
1069
  )}
1070
  </td>
1071
  <td className="px-6 py-4">
1072
+ {/* Approve button ONLY shows when user submitted proof AND is not yet approved */}
1073
+ {u.payment_proof && u.is_approved === 0 && (
1074
  <button
1075
  onClick={async () => {
1076
+ // Approve the plan the user paid for (stored in u.plan from their payment submission)
1077
+ const planToApprove = u.plan === 'free' ? 'basic' : u.plan;
1078
+ if (confirm(`Approve ${u.username}'s payment and activate "${planToApprove.toUpperCase()}" plan?`)) {
1079
+ // Optimistically update UI immediately
1080
+ const now = new Date();
1081
+ const expires = new Date(now);
1082
+ expires.setDate(expires.getDate() + 30);
1083
  setMonetizationUsers(prev => prev.map(mu =>
1084
+ mu.id === u.id ? {
1085
+ ...mu,
1086
+ is_approved: 1,
1087
+ plan: planToApprove,
1088
+ plan_activated_at: now.toISOString(),
1089
+ plan_expires_at: expires.toISOString()
1090
+ } : mu
1091
  ));
 
1092
  const res = await apiFetch(`/api/admin/monetization/users/${u.id}/vitals`, {
1093
  method: 'POST',
1094
  body: JSON.stringify({ plan: planToApprove, is_approved: 1 })
1095
  });
1096
  if (res.ok) {
1097
+ setSuccessMsg(`✅ ${u.username} approved on ${planToApprove} plan!`);
1098
+ setTimeout(() => setSuccessMsg(''), 5000);
1099
  } else {
1100
+ setErrorMsg('Approval failed. Check server.');
1101
  setTimeout(() => setErrorMsg(''), 4000);
1102
  }
 
1103
  fetchMonetizationData();
1104
  }
1105
  }}
 
1108
  <CheckCircle2 size={12} /> Approve
1109
  </button>
1110
  )}
1111
+ {/* Show approved badge when already approved */}
1112
+ {u.is_approved === 1 && (
1113
+ <span className="text-[10px] text-emerald-400 font-bold flex items-center gap-1">
1114
+ <CheckCircle2 size={12} /> Active
1115
+ </span>
1116
+ )}
1117
  </td>
1118
  </tr>
1119
  ))}