Barkmaster commited on
Commit
8307beb
·
verified ·
1 Parent(s): 2ef7be1

Update index.html

Browse files
Files changed (1) hide show
  1. index.html +17 -3
index.html CHANGED
@@ -1000,18 +1000,32 @@ async function updateUsername() {
1000
  try {
1001
  const res = await fetch(`${SB_URL}/rest/v1/player_stats?id=eq.${authUser.id}`, {
1002
  method:'PATCH',
1003
- headers:{'apikey':SB_KEY,'Authorization':`Bearer ${authUser.token}`,'Content-Type':'application/json','Prefer':'return=minimal'},
 
 
 
 
 
1004
  body:JSON.stringify({username:newName})
1005
  });
1006
- if (!res.ok) throw new Error('Update failed');
 
 
1007
  authUser.username = newName;
 
 
1008
  $('prof-display-name').textContent = newName;
1009
  $('prof-username-nav').textContent = newName;
1010
  $('prof-avatar').textContent = newName[0].toUpperCase();
1011
  if($('dash-username')) $('dash-username').textContent = newName;
 
1012
  const p=players.find(p=>p.id===authUser.id);
1013
  if(p) p.username=newName;
1014
- setAlert('prof-username-alert','success','✓ CALLSIGN UPDATED SUCCESSFULLY');
 
 
 
 
1015
  } catch(e) {
1016
  setAlert('prof-username-alert','error',`⚠ ${e.message.toUpperCase()}`);
1017
  }
 
1000
  try {
1001
  const res = await fetch(`${SB_URL}/rest/v1/player_stats?id=eq.${authUser.id}`, {
1002
  method:'PATCH',
1003
+ headers:{
1004
+ 'apikey':SB_KEY,
1005
+ 'Authorization':`Bearer ${authUser.token}`,
1006
+ 'Content-Type':'application/json',
1007
+ 'Prefer':'return=minimal'
1008
+ },
1009
  body:JSON.stringify({username:newName})
1010
  });
1011
+ if (!res.ok) throw new Error('Database rejected update. Check RLS Policies or try again later.');
1012
+
1013
+ // Update local state variables immediately
1014
  authUser.username = newName;
1015
+ localStorage.setItem('warzone_session', JSON.stringify(authUser));
1016
+
1017
  $('prof-display-name').textContent = newName;
1018
  $('prof-username-nav').textContent = newName;
1019
  $('prof-avatar').textContent = newName[0].toUpperCase();
1020
  if($('dash-username')) $('dash-username').textContent = newName;
1021
+
1022
  const p=players.find(p=>p.id===authUser.id);
1023
  if(p) p.username=newName;
1024
+
1025
+ setAlert('prof-username-alert','success','✓ CALLSIGN UPDATED SUCCESSFULLY IN DATABASE');
1026
+
1027
+ // Force a fresh silent database pull in the background
1028
+ await loadData();
1029
  } catch(e) {
1030
  setAlert('prof-username-alert','error',`⚠ ${e.message.toUpperCase()}`);
1031
  }