Skip to content

A Q&A platform software for teams at any scales. Whether it's a community forum, help center, or knowledge management platform, you can always count on Apache Answer.

refactor: branding save

shuaibfd1c6362dfe
diff --git a/ui/src/pages/Admin/Branding/index.tsx b/ui/src/pages/Admin/Branding/index.tsx
index acf900a6..bb3e857f 100644
--- a/ui/src/pages/Admin/Branding/index.tsx
+++ b/ui/src/pages/Admin/Branding/index.tsx
@@ -1,19 +1,23 @@
-import { FC, memo, useState } from 'react';
+import { FC, memo, useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';
import { JSONSchema, SchemaForm, UISchema } from '@/components';
import { FormDataType } from '@/common/interface';
-import { brandSetting } from '@/services';
+import { brandSetting, getBrandSetting } from '@/services';
+import { interfaceStore } from '@/stores';
+import { useToast } from '@/hooks';
const uploadType = 'branding';
const Index: FC = () => {
const { t } = useTranslation('translation', {
keyPrefix: 'admin.branding',
});
+ const { interface: storeInterface, updateLogo } = interfaceStore();
+ const Toast = useToast();
const [formData, setFormData] = useState<FormDataType>({
logo: {
- value: '',
+ value: storeInterface.logo,
isInvalid: false,
errorMsg: '',
},
@@ -34,24 +38,6 @@ const Index: FC = () => {
},
});
- // const onChange = (fieldName, fieldValue) => {
- // if (!formData[fieldName]) {
- // return;
- // }
- // const fieldData: FormDataType = {
- // [fieldName]: {
- // value: fieldValue,
- // isInvalid: false,
- // errorMsg: '',
- // },
- // };
- // setFormData({ ...formData, ...fieldData });
- // };
-
- // const [img, setImg] = useState(
- // 'https://image-static.segmentfault.com/405/057/4050570037-636c7b0609a49',
- // );
-
const schema: JSONSchema = {
title: t('page_title'),
properties: {
@@ -116,11 +102,40 @@ const Index: FC = () => {
square_icon: formData.square_icon.value,
favicon: formData.favicon.value,
};
- brandSetting(params).then((res) => {
- console.log(res);
- });
+ brandSetting(params)
+ .then((res) => {
+ console.log(res);
+ updateLogo(formData.logo.value);
+ Toast.onShow({
+ msg: t('update', { keyPrefix: 'toast' }),
+ variant: 'success',
+ });
+ })
+ .catch((err) => {
+ console.log(err);
+ if (err.key) {
+ formData[err.key].isInvalid = true;
+ formData[err.key].errorMsg = err.value;
+ setFormData({ ...formData });
+ }
+ });
};
+ const getBrandData = async () => {
+ const res = await getBrandSetting();
+ if (res) {
+ formData.logo.value = res.logo;
+ formData.mobile_logo.value = res.mobile_logo;
+ formData.square_icon.value = res.square_icon;
+ formData.favicon.value = res.favicon;
+ setFormData({ ...formData });
+ }
+ };
+
+ useEffect(() => {
+ getBrandData();
+ }, []);
+
return (
<div>
<h3 className="mb-4">{t('page_title')}</h3>
diff --git a/ui/src/services/admin/settings.ts b/ui/src/services/admin/settings.ts
index 1c128795..3e627284 100644
--- a/ui/src/services/admin/settings.ts
+++ b/ui/src/services/admin/settings.ts
@@ -89,6 +89,10 @@ export const getAdminLanguageOptions = () => {
return request.get<Type.LangsType[]>(apiUrl);
};
+export const getBrandSetting = () => {
+ return request.get('/answer/admin/api/siteinfo/branding');
+};
+
export const brandSetting = (params: Type.BrandReqParams) => {
return request.put('/answer/admin/api/siteinfo/branding', params);
};
diff --git a/ui/src/stores/interface.ts b/ui/src/stores/interface.ts
index e92e0f8a..a5933509 100644
--- a/ui/src/stores/interface.ts
+++ b/ui/src/stores/interface.ts
@@ -6,6 +6,7 @@ import { DEFAULT_LANG } from '@/common/constants';
interface InterfaceType {
interface: AdminSettingsInterface;
update: (params: AdminSettingsInterface) => void;
+ updateLogo: (logo: string) => void;
}
const interfaceSetting = create<InterfaceType>((set) => ({
@@ -21,6 +22,15 @@ const interfaceSetting = create<InterfaceType>((set) => ({
interface: params,
};
}),
+ updateLogo: (logo) =>
+ set((state) => {
+ return {
+ interface: {
+ ...state.interface,
+ logo,
+ },
+ };
+ }),
}));
export default interfaceSetting;
diff --git a/ui/src/utils/request.ts b/ui/src/utils/request.ts
index 072578b4..e553b803 100644
--- a/ui/src/utils/request.ts
+++ b/ui/src/utils/request.ts
@@ -48,10 +48,7 @@ class Request {
return data;
},
(error) => {
- if (error.isAxiosError) {
- return Promise.reject(false);
- }
- const { status, data: respData, msg: respMsg } = error.response;
+ const { status, data: respData, msg: respMsg } = error.response || {};
const { data, msg = '' } = respData;
if (status === 400) {
// show error message