fix: normalize all API responses, add error handling and reset confirmation

- Extract normalizeConfig() for consistent response normalization
- Add onError handlers to mutations with useNotify
- Add window.confirm before destructive reset action
- Add resetConfirm translation key to all locales
This commit is contained in:
Fringg
2026-02-13 00:41:00 +03:00
parent 1a0a5ff453
commit 150a1b2dba
6 changed files with 33 additions and 12 deletions

View File

@@ -33,6 +33,7 @@ export const BUTTON_SECTIONS = [
export type ButtonSection = (typeof BUTTON_SECTIONS)[number];
// Bot-side locales (includes 'ua' for Ukrainian, mapped from ISO 'uk' internally).
export const BOT_LOCALES = ['ru', 'en', 'ua', 'zh', 'fa'] as const;
export type BotLocale = (typeof BOT_LOCALES)[number];
@@ -54,19 +55,23 @@ export const DEFAULT_BUTTON_STYLES: ButtonStylesConfig = {
admin: { ...DEFAULT_SECTION, style: 'danger' },
};
function normalizeConfig(data: ButtonStylesConfig): ButtonStylesConfig {
const result = {} as ButtonStylesConfig;
for (const section of BUTTON_SECTIONS) {
result[section] = {
...DEFAULT_SECTION,
...data[section],
labels: { ...(data[section]?.labels || {}) },
};
}
return result;
}
export const buttonStylesApi = {
getStyles: async (): Promise<ButtonStylesConfig> => {
try {
const response = await apiClient.get<ButtonStylesConfig>('/cabinet/admin/button-styles');
const data = response.data;
for (const section of BUTTON_SECTIONS) {
data[section] = {
...DEFAULT_SECTION,
...data[section],
labels: { ...(data[section]?.labels || {}) },
};
}
return data;
return normalizeConfig(response.data);
} catch {
return DEFAULT_BUTTON_STYLES;
}
@@ -77,11 +82,11 @@ export const buttonStylesApi = {
'/cabinet/admin/button-styles',
update,
);
return response.data;
return normalizeConfig(response.data);
},
resetStyles: async (): Promise<ButtonStylesConfig> => {
const response = await apiClient.post<ButtonStylesConfig>('/cabinet/admin/button-styles/reset');
return response.data;
return normalizeConfig(response.data);
},
};

View File

@@ -10,6 +10,7 @@ import {
BOT_LOCALES,
} from '../../api/buttonStyles';
import { Toggle } from './Toggle';
import { useNotify } from '../../platform/hooks/useNotify';
type StyleValue = 'primary' | 'success' | 'danger' | 'default';
@@ -40,6 +41,7 @@ function stylesEqual(a: ButtonStylesConfig, b: ButtonStylesConfig): boolean {
export function ButtonsTab() {
const { t } = useTranslation();
const queryClient = useQueryClient();
const notify = useNotify();
const { data: serverStyles } = useQuery({
queryKey: ['button-styles'],
@@ -73,6 +75,9 @@ export function ButtonsTab() {
setDraftStyles(data);
queryClient.setQueryData(['button-styles'], data);
},
onError: () => {
notify.error(t('common.error'));
},
});
const resetMutation = useMutation({
@@ -82,6 +87,9 @@ export function ButtonsTab() {
setDraftStyles(data);
queryClient.setQueryData(['button-styles'], data);
},
onError: () => {
notify.error(t('common.error'));
},
});
const updateSection = useCallback(
@@ -333,7 +341,11 @@ export function ButtonsTab() {
{/* Reset */}
<div className="flex justify-end">
<button
onClick={() => resetMutation.mutate()}
onClick={() => {
if (window.confirm(t('admin.buttons.resetConfirm'))) {
resetMutation.mutate();
}
}}
disabled={resetMutation.isPending}
className="rounded-xl bg-dark-700 px-4 py-2 text-sm text-dark-300 transition-colors hover:bg-dark-600 disabled:opacity-50"
>

View File

@@ -1292,6 +1292,7 @@
"emojiId": "Custom Emoji ID",
"emojiPlaceholder": "Custom emoji ID (optional)",
"resetAll": "Reset all styles",
"resetConfirm": "Reset all button styles to defaults?",
"hidden": "Hidden",
"customLabels": "Button labels",
"labelPlaceholder": "Custom button text",

View File

@@ -1001,6 +1001,7 @@
"emojiId": "شناسه ایموجی سفارشی",
"emojiPlaceholder": "شناسه ایموجی سفارشی (اختیاری)",
"resetAll": "بازنشانی همه سبک‌ها",
"resetConfirm": "همه سبک‌های دکمه به حالت پیش‌فرض بازنشانی شوند؟",
"hidden": "پنهان",
"customLabels": "نام‌های دکمه",
"labelPlaceholder": "متن سفارشی دکمه",

View File

@@ -1807,6 +1807,7 @@
"emojiId": "Custom Emoji ID",
"emojiPlaceholder": "ID кастомного эмодзи (необязательно)",
"resetAll": "Сбросить все стили",
"resetConfirm": "Сбросить все стили кнопок к значениям по умолчанию?",
"hidden": "Скрыта",
"customLabels": "Названия кнопки",
"labelPlaceholder": "Свой текст кнопки",

View File

@@ -1039,6 +1039,7 @@
"emojiId": "自定义表情 ID",
"emojiPlaceholder": "自定义表情 ID可选",
"resetAll": "重置所有样式",
"resetConfirm": "将所有按钮样式重置为默认值?",
"hidden": "已隐藏",
"customLabels": "按钮名称",
"labelPlaceholder": "自定义按钮文本",