feat: better notifications for api key page

This commit is contained in:
Rohit
2025-02-10 15:06:21 +05:30
parent 85132c3d50
commit ed5e96b476

View File

@@ -83,11 +83,36 @@ const ApiKeyManager = () => {
setLoading(true); setLoading(true);
try { try {
const { data } = await axios.post(`${apiUrl}/auth/generate-api-key`); const { data } = await axios.post(`${apiUrl}/auth/generate-api-key`);
setApiKey(data.api_key); if (data.ok && data.api_key) {
setApiKey(data.api_key);
notify('success', t('apikey.notifications.generate_success')); notify('success', t('apikey.notifications.success.generate'));
}
} catch (error: any) { } catch (error: any) {
notify('error', t('apikey.notifications.generate_error', { error: error.message })); const status = error.response?.status;
let errorKey = 'unknown';
switch (status) {
case 401:
errorKey = 'unauthorized';
break;
case 403:
errorKey = 'limit_reached';
break;
case 500:
errorKey = 'server';
break;
default:
if (error.message?.includes('Network Error')) {
errorKey = 'network';
}
}
notify(
'error',
t(`apikey.notifications.errors.generate.${errorKey}`, {
error: error.response?.data?.message || error.message
})
);
} finally { } finally {
setLoading(false); setLoading(false);
} }
@@ -96,22 +121,54 @@ const ApiKeyManager = () => {
const deleteApiKey = async () => { const deleteApiKey = async () => {
setLoading(true); setLoading(true);
try { try {
await axios.delete(`${apiUrl}/auth/delete-api-key`); const response = await axios.delete(`${apiUrl}/auth/delete-api-key`);
setApiKey(null); if (response.data.ok) {
notify('success', t('apikey.notifications.delete_success')); setApiKey(null);
notify('success', t('apikey.notifications.success.delete'));
}
} catch (error: any) { } catch (error: any) {
notify('error', t('apikey.notifications.delete_error', { error: error.message })); const status = error.response?.status;
let errorKey = 'unknown';
switch (status) {
case 401:
errorKey = 'unauthorized';
break;
case 404:
errorKey = 'not_found';
break;
case 500:
errorKey = 'server';
break;
default:
if (error.message?.includes('Network Error')) {
errorKey = 'network';
}
}
notify(
'error',
t(`apikey.notifications.errors.delete.${errorKey}`, {
error: error.response?.data?.message || error.message
})
);
} finally { } finally {
setLoading(false); setLoading(false);
} }
}; };
const copyToClipboard = () => { const copyToClipboard = async () => {
if (apiKey) { if (!apiKey) return;
navigator.clipboard.writeText(apiKey);
try {
await navigator.clipboard.writeText(apiKey);
setCopySuccess(true); setCopySuccess(true);
notify('success', t('apikey.notifications.success.copy'));
// Reset copy success state after 2 seconds
setTimeout(() => setCopySuccess(false), 2000); setTimeout(() => setCopySuccess(false), 2000);
notify('info', t('apikey.notifications.copy_success')); } catch (error) {
notify('error', t('apikey.notifications.errors.copy.failed'));
} }
}; };