Skip to content

Commit dcba69a

Browse files
committed
Fix NPUB management form integration and tier field mapping
- Fix form switch components to properly integrate with Ant Design forms - Add tier_name to tier field mapping in API responses - Improve tier fallback logic in unified state reconstruction - Clean up debugging logs
1 parent 3eaf795 commit dcba69a

2 files changed

Lines changed: 44 additions & 25 deletions

File tree

src/api/allowedUsers.api.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,6 @@ export const updateAllowedUsersSettings = async (settings: AllowedUsersSettings)
106106
}
107107
};
108108

109-
console.log('Sending to backend:', JSON.stringify(nestedSettings, null, 2));
110109

111110
const response = await fetch(`${config.baseURL}/api/settings`, {
112111
method: 'POST',
@@ -118,10 +117,8 @@ export const updateAllowedUsersSettings = async (settings: AllowedUsersSettings)
118117
});
119118

120119
const text = await response.text();
121-
console.log('Backend response:', response.status, text);
122120

123121
if (!response.ok) {
124-
console.error('Backend error:', response.status, text);
125122
throw new Error(`HTTP error! status: ${response.status}, response: ${text}`);
126123
}
127124

@@ -147,9 +144,14 @@ export const getReadNpubs = async (page = 1, pageSize = 20): Promise<AllowedUser
147144
const text = await response.text();
148145
try {
149146
const data = JSON.parse(text);
150-
// Transform backend response to expected format
147+
// Transform backend response to expected format - map tier_name to tier
148+
const transformedNpubs = (data.npubs || []).map((npub: any) => ({
149+
...npub,
150+
tier: npub.tier_name || npub.tier || 'basic'
151+
}));
152+
151153
return {
152-
npubs: data.npubs || [],
154+
npubs: transformedNpubs,
153155
total: data.pagination?.total || 0,
154156
page: data.pagination?.page || page,
155157
pageSize: data.pagination?.pageSize || pageSize
@@ -161,13 +163,16 @@ export const getReadNpubs = async (page = 1, pageSize = 20): Promise<AllowedUser
161163

162164
export const addReadNpub = async (npub: string, tier: string): Promise<{ success: boolean, message: string }> => {
163165
const token = readToken();
166+
const requestBody = { npub, tier };
167+
168+
164169
const response = await fetch(`${config.baseURL}/api/allowed-npubs/read`, {
165170
method: 'POST',
166171
headers: {
167172
'Content-Type': 'application/json',
168173
'Authorization': `Bearer ${token}`,
169174
},
170-
body: JSON.stringify({ npub, tier }),
175+
body: JSON.stringify(requestBody),
171176
});
172177

173178
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);
@@ -213,9 +218,14 @@ export const getWriteNpubs = async (page = 1, pageSize = 20): Promise<AllowedUse
213218
const text = await response.text();
214219
try {
215220
const data = JSON.parse(text);
216-
// Transform backend response to expected format
221+
// Transform backend response to expected format - map tier_name to tier
222+
const transformedNpubs = (data.npubs || []).map((npub: any) => ({
223+
...npub,
224+
tier: npub.tier_name || npub.tier || 'basic'
225+
}));
226+
217227
return {
218-
npubs: data.npubs || [],
228+
npubs: transformedNpubs,
219229
total: data.pagination?.total || 0,
220230
page: data.pagination?.page || page,
221231
pageSize: data.pagination?.pageSize || pageSize
@@ -227,13 +237,16 @@ export const getWriteNpubs = async (page = 1, pageSize = 20): Promise<AllowedUse
227237

228238
export const addWriteNpub = async (npub: string, tier: string): Promise<{ success: boolean, message: string }> => {
229239
const token = readToken();
240+
const requestBody = { npub, tier };
241+
242+
230243
const response = await fetch(`${config.baseURL}/api/allowed-npubs/write`, {
231244
method: 'POST',
232245
headers: {
233246
'Content-Type': 'application/json',
234247
'Authorization': `Bearer ${token}`,
235248
},
236-
body: JSON.stringify({ npub, tier }),
249+
body: JSON.stringify(requestBody),
237250
});
238251

239252
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`);

src/components/allowed-users/components/NPubManagement/NPubManagement.tsx

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ export const NPubManagement: React.FC<NPubManagementProps> = ({
4747
readNpubs.npubs.forEach(npub => {
4848
allNpubs.set(npub.npub, {
4949
npub: npub.npub,
50-
tier: npub.tier,
50+
tier: npub.tier || settings.tiers[0]?.name || 'basic',
5151
readAccess: true,
5252
writeAccess: false,
5353
added_at: npub.added_at
@@ -59,10 +59,12 @@ export const NPubManagement: React.FC<NPubManagementProps> = ({
5959
const existing = allNpubs.get(npub.npub);
6060
if (existing) {
6161
existing.writeAccess = true;
62+
// Preserve the tier from read access, or use write tier, or fallback
63+
existing.tier = existing.tier || npub.tier || settings.tiers[0]?.name || 'basic';
6264
} else {
6365
allNpubs.set(npub.npub, {
6466
npub: npub.npub,
65-
tier: npub.tier,
67+
tier: npub.tier || settings.tiers[0]?.name || 'basic',
6668
readAccess: false,
6769
writeAccess: true,
6870
added_at: npub.added_at
@@ -71,7 +73,7 @@ export const NPubManagement: React.FC<NPubManagementProps> = ({
7173
});
7274

7375
setUnifiedUsers(Array.from(allNpubs.values()));
74-
}, [readNpubs.npubs, writeNpubs.npubs]);
76+
}, [readNpubs.npubs, writeNpubs.npubs, settings.tiers]);
7577
const tierOptions = settings.tiers.map(tier => {
7678
const displayFormat = tier.unlimited
7779
? 'unlimited'
@@ -108,16 +110,19 @@ export const NPubManagement: React.FC<NPubManagementProps> = ({
108110
const user = unifiedUsers.find(u => u.npub === npub);
109111
if (!user) return;
110112

113+
// Ensure we have a valid tier - fallback to first available tier if undefined
114+
const tierToUse = user.tier || settings.tiers[0]?.name || 'basic';
115+
111116
try {
112117
if (type === 'read') {
113118
if (enabled) {
114-
await readNpubs.addNpub(npub, user.tier);
119+
await readNpubs.addNpub(npub, tierToUse);
115120
} else {
116121
await readNpubs.removeNpub(npub);
117122
}
118123
} else {
119124
if (enabled) {
120-
await writeNpubs.addNpub(npub, user.tier);
125+
await writeNpubs.addNpub(npub, tierToUse);
121126
} else {
122127
await writeNpubs.removeNpub(npub);
123128
}
@@ -346,21 +351,22 @@ export const NPubManagement: React.FC<NPubManagementProps> = ({
346351
</Form.Item>
347352

348353
<Form.Item
349-
name="readAccess"
350354
label="Permissions"
351355
style={{ marginBottom: 0 }}
352356
>
353357
<Space direction="vertical">
354-
<Form.Item name="readAccess" valuePropName="checked" style={{ marginBottom: 8 }}>
355-
<S.PermissionLabel>
356-
<S.StyledSwitch size="small" /> Read Access
357-
</S.PermissionLabel>
358-
</Form.Item>
359-
<Form.Item name="writeAccess" valuePropName="checked" style={{ marginBottom: 0 }}>
360-
<S.PermissionLabel>
361-
<S.StyledSwitch size="small" /> Write Access
362-
</S.PermissionLabel>
363-
</Form.Item>
358+
<div style={{ display: 'flex', alignItems: 'center', gap: '8px', color: 'var(--text-main-color)' }}>
359+
<Form.Item name="readAccess" valuePropName="checked" style={{ marginBottom: 0 }}>
360+
<S.StyledSwitch size="small" />
361+
</Form.Item>
362+
<span>Read Access</span>
363+
</div>
364+
<div style={{ display: 'flex', alignItems: 'center', gap: '8px', color: 'var(--text-main-color)' }}>
365+
<Form.Item name="writeAccess" valuePropName="checked" style={{ marginBottom: 0 }}>
366+
<S.StyledSwitch size="small" />
367+
</Form.Item>
368+
<span>Write Access</span>
369+
</div>
364370
</Space>
365371
</Form.Item>
366372
</Form>

0 commit comments

Comments
 (0)