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: upload component adjustment

shuai5660017beaaf
diff --git a/ui/src/common/interface.ts b/ui/src/common/interface.ts
index 23602e3c..6f7fc25d 100644
--- a/ui/src/common/interface.ts
+++ b/ui/src/common/interface.ts
@@ -126,7 +126,8 @@ export interface UserInfoRes extends UserInfoBase {
[prop: string]: any;
}
-export interface AvatarUploadReq {
+export type UploadType = 'post' | 'avatar' | 'branding';
+export interface UploadReq {
file: FormData;
}
@@ -359,3 +360,10 @@ export interface AdminDashboard {
};
};
}
+
+export interface BrandReqParams {
+ logo: string;
+ square_icon: string;
+ mobile_logo?: string;
+ favicon?: string;
+}
diff --git a/ui/src/components/BrandUpload/index.tsx b/ui/src/components/BrandUpload/index.tsx
index d1be627a..99f8dcf6 100644
--- a/ui/src/components/BrandUpload/index.tsx
+++ b/ui/src/components/BrandUpload/index.tsx
@@ -2,22 +2,17 @@ import { FC } from 'react';
import { ButtonGroup, Button } from 'react-bootstrap';
import { Icon, UploadImg } from '@/components';
-import { uploadAvatar } from '@/services';
+import { UploadType } from '@/common/interface';
interface Props {
- type: 'logo' | 'avatar' | 'mobile_logo' | 'square_icon' | 'favicon';
+ type: UploadType;
value: string;
onChange: (value: string) => void;
}
-const Index: FC<Props> = ({ type = 'logo', value, onChange }) => {
- const onUpload = (file: any) => {
- return new Promise((resolve) => {
- uploadAvatar(file).then((res) => {
- onChange(res);
- resolve(true);
- });
- });
+const Index: FC<Props> = ({ type = 'post', value, onChange }) => {
+ const onUpload = (imgPath: string) => {
+ onChange(imgPath);
};
const onRemove = () => {
@@ -29,7 +24,7 @@ const Index: FC<Props> = ({ type = 'logo', value, onChange }) => {
<img src={value} alt="" height={100} />
</div>
<ButtonGroup vertical className="fit-content">
- <UploadImg type={type} upload={onUpload} className="mb-0">
+ <UploadImg type={type} uploadCallback={onUpload} className="mb-0">
<Icon name="cloud-upload" />
</UploadImg>
diff --git a/ui/src/components/Editor/ToolBars/image.tsx b/ui/src/components/Editor/ToolBars/image.tsx
index bfa4c530..2eb1c5e8 100644
--- a/ui/src/components/Editor/ToolBars/image.tsx
+++ b/ui/src/components/Editor/ToolBars/image.tsx
@@ -61,7 +61,7 @@ const Image: FC<IEditorContext> = ({ editor }) => {
files: FileList,
): Promise<{ url: string; name: string }[]> => {
const promises = Array.from(files).map(async (file) => {
- const url = await uploadImage(file);
+ const url = await uploadImage({ file, type: 'post' });
return {
name: file.name,
@@ -209,7 +209,7 @@ const Image: FC<IEditorContext> = ({ editor }) => {
return;
}
- uploadImage(e.target.files[0]).then((url) => {
+ uploadImage({ file: e.target.files[0], type: 'post' }).then((url) => {
setLink({ ...link, value: url });
});
};
diff --git a/ui/src/components/SchemaForm/index.tsx b/ui/src/components/SchemaForm/index.tsx
index b3d30e84..82b234ae 100644
--- a/ui/src/components/SchemaForm/index.tsx
+++ b/ui/src/components/SchemaForm/index.tsx
@@ -54,7 +54,7 @@ export interface UISchema {
invalid?: string;
validator?: (value) => boolean;
textRender?: () => React.ReactElement;
- imageType?: 'avatar' | 'logo' | 'mobile_logo' | 'square_icon' | 'favicon';
+ imageType?: Type.UploadType;
};
};
}
diff --git a/ui/src/components/UploadImg/index.tsx b/ui/src/components/UploadImg/index.tsx
index 90cca3e4..3ac56b2a 100644
--- a/ui/src/components/UploadImg/index.tsx
+++ b/ui/src/components/UploadImg/index.tsx
@@ -1,14 +1,22 @@
import React, { useState } from 'react';
import { useTranslation } from 'react-i18next';
+import { uploadImage } from '@/services';
+import * as Type from '@/common/interface';
+
interface IProps {
- type: string;
+ type: Type.UploadType;
className?: string;
children?: React.ReactNode;
- upload: (data: FormData) => Promise<any>;
+ uploadCallback: (img: string) => void;
}
-const Index: React.FC<IProps> = ({ type, upload, children, className }) => {
+const Index: React.FC<IProps> = ({
+ type,
+ uploadCallback,
+ children,
+ className,
+}) => {
const { t } = useTranslation();
const [status, setStatus] = useState(false);
@@ -26,13 +34,13 @@ const Index: React.FC<IProps> = ({ type, upload, children, className }) => {
// return;
// }
setStatus(true);
- const data = new FormData();
-
- data.append('file', e.target.files[0]);
- // do
- upload(data).finally(() => {
- setStatus(false);
- });
+ uploadImage({ file: e.target.files[0], type })
+ .then((res) => {
+ uploadCallback(res);
+ })
+ .finally(() => {
+ setStatus(false);
+ });
}
};
@@ -44,7 +52,6 @@ const Index: React.FC<IProps> = ({ type, upload, children, className }) => {
className="d-none"
accept="image/jpeg,image/jpg,image/png,image/webp"
onChange={onChange}
- id={type}
/>
</label>
);
diff --git a/ui/src/pages/Admin/Branding/index.tsx b/ui/src/pages/Admin/Branding/index.tsx
index 490122bb..acf900a6 100644
--- a/ui/src/pages/Admin/Branding/index.tsx
+++ b/ui/src/pages/Admin/Branding/index.tsx
@@ -3,7 +3,9 @@ import { useTranslation } from 'react-i18next';
import { JSONSchema, SchemaForm, UISchema } from '@/components';
import { FormDataType } from '@/common/interface';
+import { brandSetting } from '@/services';
+const uploadType = 'branding';
const Index: FC = () => {
const { t } = useTranslation('translation', {
keyPrefix: 'admin.branding',
@@ -80,25 +82,25 @@ const Index: FC = () => {
logo: {
'ui:widget': 'upload',
'ui:options': {
- imageType: 'logo',
+ imageType: uploadType,
},
},
mobile_logo: {
'ui:widget': 'upload',
'ui:options': {
- imageType: 'mobile_logo',
+ imageType: uploadType,
},
},
square_icon: {
'ui:widget': 'upload',
'ui:options': {
- imageType: 'square_icon',
+ imageType: uploadType,
},
},
favicon: {
'ui:widget': 'upload',
'ui:options': {
- imageType: 'favicon',
+ imageType: uploadType,
},
},
};
@@ -108,7 +110,15 @@ const Index: FC = () => {
};
const onSubmit = () => {
- // undo
+ const params = {
+ logo: formData.logo.value,
+ mobile_logo: formData.mobile_logo.value,
+ square_icon: formData.square_icon.value,
+ favicon: formData.favicon.value,
+ };
+ brandSetting(params).then((res) => {
+ console.log(res);
+ });
};
return (
diff --git a/ui/src/pages/Users/Settings/Profile/index.tsx b/ui/src/pages/Users/Settings/Profile/index.tsx
index f62a747d..0b874997 100644
--- a/ui/src/pages/Users/Settings/Profile/index.tsx
+++ b/ui/src/pages/Users/Settings/Profile/index.tsx
@@ -9,7 +9,7 @@ import type { FormDataType } from '@/common/interface';
import { UploadImg, Avatar } from '@/components';
import { loggedUserInfoStore } from '@/stores';
import { useToast } from '@/hooks';
-import { modifyUserInfo, uploadAvatar, getLoggedUserInfo } from '@/services';
+import { modifyUserInfo, getLoggedUserInfo } from '@/services';
const Index: React.FC = () => {
const { t } = useTranslation('translation', {
@@ -60,21 +60,16 @@ const Index: React.FC = () => {
setFormData({ ...formData, ...params });
};
- const avatarUpload = (file: any) => {
- return new Promise((resolve) => {
- uploadAvatar(file).then((res) => {
- setFormData({
- ...formData,
- avatar: {
- ...formData.avatar,
- type: 'custom',
- custom: res,
- isInvalid: false,
- errorMsg: '',
- },
- });
- resolve(true);
- });
+ const avatarUpload = (path: string) => {
+ setFormData({
+ ...formData,
+ avatar: {
+ ...formData.avatar,
+ type: 'custom',
+ custom: path,
+ isInvalid: false,
+ errorMsg: '',
+ },
});
};
@@ -366,7 +361,7 @@ const Index: React.FC = () => {
<div>
<UploadImg
type="avatar"
- upload={avatarUpload}
+ uploadCallback={avatarUpload}
className="mb-2"
/>
<div>
diff --git a/ui/src/services/admin/settings.ts b/ui/src/services/admin/settings.ts
index 6bae3576..1c128795 100644
--- a/ui/src/services/admin/settings.ts
+++ b/ui/src/services/admin/settings.ts
@@ -88,3 +88,7 @@ export const getAdminLanguageOptions = () => {
const apiUrl = `/answer/admin/api/language/options`;
return request.get<Type.LangsType[]>(apiUrl);
};
+
+export const brandSetting = (params: Type.BrandReqParams) => {
+ return request.put('/answer/admin/api/siteinfo/branding', params);
+};
diff --git a/ui/src/services/common.ts b/ui/src/services/common.ts
index 1ddc7f18..8a541f03 100644
--- a/ui/src/services/common.ts
+++ b/ui/src/services/common.ts
@@ -4,12 +4,13 @@ import useSWR from 'swr';
import request from '@/utils/request';
import type * as Type from '@/common/interface';
-export const uploadImage = (file) => {
+export const uploadImage = (params: { file: File; type: Type.UploadType }) => {
const form = new FormData();
-
- form.append('file', file);
- return request.post('/answer/api/v1/user/post/file', form);
+ form.append('source', String(params.type));
+ form.append('file', params.file);
+ return request.post('/answer/api/v1/file', form);
};
+
export const useQueryQuestionByTitle = (title) => {
return useSWR<Record<string, any>>(
title ? `/answer/api/v1/question/similar?title=${title}` : '',
@@ -127,10 +128,6 @@ export const modifyUserInfo = (params: Type.ModifyUserReq) => {
return request.put('/answer/api/v1/user/info', params);
};
-export const uploadAvatar = (params: Type.AvatarUploadReq) => {
- return request.post('/answer/api/v1/user/avatar/upload', params);
-};
-
export const resetPassword = (params: Type.PasswordResetReq) => {
return request.post('/answer/api/v1/user/password/reset', params);
};