mirror of
https://github.com/chillpadclub/bedolaga-cabinet.git
synced 2026-07-28 09:33:46 +00:00
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:
@@ -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' },
|
||||
};
|
||||
|
||||
export const buttonStylesApi = {
|
||||
getStyles: async (): Promise<ButtonStylesConfig> => {
|
||||
try {
|
||||
const response = await apiClient.get<ButtonStylesConfig>('/cabinet/admin/button-styles');
|
||||
const data = response.data;
|
||||
function normalizeConfig(data: ButtonStylesConfig): ButtonStylesConfig {
|
||||
const result = {} as ButtonStylesConfig;
|
||||
for (const section of BUTTON_SECTIONS) {
|
||||
data[section] = {
|
||||
result[section] = {
|
||||
...DEFAULT_SECTION,
|
||||
...data[section],
|
||||
labels: { ...(data[section]?.labels || {}) },
|
||||
};
|
||||
}
|
||||
return data;
|
||||
return result;
|
||||
}
|
||||
|
||||
export const buttonStylesApi = {
|
||||
getStyles: async (): Promise<ButtonStylesConfig> => {
|
||||
try {
|
||||
const response = await apiClient.get<ButtonStylesConfig>('/cabinet/admin/button-styles');
|
||||
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);
|
||||
},
|
||||
};
|
||||
|
||||
@@ -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"
|
||||
>
|
||||
|
||||
@@ -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",
|
||||
|
||||
@@ -1001,6 +1001,7 @@
|
||||
"emojiId": "شناسه ایموجی سفارشی",
|
||||
"emojiPlaceholder": "شناسه ایموجی سفارشی (اختیاری)",
|
||||
"resetAll": "بازنشانی همه سبکها",
|
||||
"resetConfirm": "همه سبکهای دکمه به حالت پیشفرض بازنشانی شوند؟",
|
||||
"hidden": "پنهان",
|
||||
"customLabels": "نامهای دکمه",
|
||||
"labelPlaceholder": "متن سفارشی دکمه",
|
||||
|
||||
@@ -1807,6 +1807,7 @@
|
||||
"emojiId": "Custom Emoji ID",
|
||||
"emojiPlaceholder": "ID кастомного эмодзи (необязательно)",
|
||||
"resetAll": "Сбросить все стили",
|
||||
"resetConfirm": "Сбросить все стили кнопок к значениям по умолчанию?",
|
||||
"hidden": "Скрыта",
|
||||
"customLabels": "Названия кнопки",
|
||||
"labelPlaceholder": "Свой текст кнопки",
|
||||
|
||||
@@ -1039,6 +1039,7 @@
|
||||
"emojiId": "自定义表情 ID",
|
||||
"emojiPlaceholder": "自定义表情 ID(可选)",
|
||||
"resetAll": "重置所有样式",
|
||||
"resetConfirm": "将所有按钮样式重置为默认值?",
|
||||
"hidden": "已隐藏",
|
||||
"customLabels": "按钮名称",
|
||||
"labelPlaceholder": "自定义按钮文本",
|
||||
|
||||
Reference in New Issue
Block a user