2021-03-22 02:07:12 +03:00
|
|
|
import { Button, Upload } from 'antd';
|
|
|
|
import { RcFile } from 'antd/lib/upload/interface';
|
2022-09-07 10:00:28 +03:00
|
|
|
import React, { useState, useContext, FC } from 'react';
|
2023-01-16 09:31:36 +03:00
|
|
|
import dynamic from 'next/dynamic';
|
2022-09-07 10:00:28 +03:00
|
|
|
import { FormStatusIndicator } from './FormStatusIndicator';
|
2021-03-22 02:07:12 +03:00
|
|
|
import { ServerStatusContext } from '../../utils/server-status-context';
|
|
|
|
import {
|
|
|
|
postConfigUpdateToAPI,
|
|
|
|
RESET_TIMEOUT,
|
|
|
|
TEXTFIELD_PROPS_LOGO,
|
|
|
|
} from '../../utils/config-constants';
|
|
|
|
import {
|
|
|
|
createInputStatus,
|
|
|
|
StatusState,
|
|
|
|
STATUS_ERROR,
|
|
|
|
STATUS_PROCESSING,
|
|
|
|
STATUS_SUCCESS,
|
|
|
|
} from '../../utils/input-statuses';
|
2021-03-26 06:06:29 +03:00
|
|
|
import { NEXT_PUBLIC_API_HOST } from '../../utils/apis';
|
2021-03-22 02:07:12 +03:00
|
|
|
|
2022-12-29 02:41:49 +03:00
|
|
|
import {
|
|
|
|
ACCEPTED_IMAGE_TYPES,
|
|
|
|
getBase64,
|
|
|
|
MAX_IMAGE_FILESIZE,
|
|
|
|
readableBytes,
|
|
|
|
} from '../../utils/images';
|
2021-03-22 02:07:12 +03:00
|
|
|
|
2023-01-16 09:31:36 +03:00
|
|
|
// Lazy loaded components
|
|
|
|
|
|
|
|
const LoadingOutlined = dynamic(() => import('@ant-design/icons/LoadingOutlined'), {
|
|
|
|
ssr: false,
|
|
|
|
});
|
|
|
|
|
|
|
|
const UploadOutlined = dynamic(() => import('@ant-design/icons/UploadOutlined'), {
|
|
|
|
ssr: false,
|
|
|
|
});
|
|
|
|
|
2023-01-10 07:57:29 +03:00
|
|
|
// eslint-disable-next-line import/prefer-default-export
|
2022-09-07 10:00:28 +03:00
|
|
|
export const EditLogo: FC = () => {
|
2021-03-22 02:07:12 +03:00
|
|
|
const [logoUrl, setlogoUrl] = useState(null);
|
|
|
|
const [loading, setLoading] = useState(false);
|
2021-04-08 08:48:39 +03:00
|
|
|
const [logoCachedbuster, setLogoCacheBuster] = useState(0);
|
2021-03-22 02:07:12 +03:00
|
|
|
|
|
|
|
const serverStatusData = useContext(ServerStatusContext);
|
|
|
|
const { setFieldInConfigState, serverConfig } = serverStatusData || {};
|
|
|
|
const currentLogo = serverConfig?.instanceDetails?.logo;
|
|
|
|
|
|
|
|
const [submitStatus, setSubmitStatus] = useState<StatusState>(null);
|
|
|
|
let resetTimer = null;
|
|
|
|
|
|
|
|
const { apiPath, tip } = TEXTFIELD_PROPS_LOGO;
|
|
|
|
|
|
|
|
// Clear out any validation states and messaging
|
|
|
|
const resetStates = () => {
|
|
|
|
setSubmitStatus(null);
|
|
|
|
clearTimeout(resetTimer);
|
|
|
|
resetTimer = null;
|
|
|
|
};
|
|
|
|
|
|
|
|
// validate file type and create base64 encoded img
|
|
|
|
const beforeUpload = (file: RcFile) => {
|
|
|
|
setLoading(true);
|
|
|
|
|
|
|
|
// eslint-disable-next-line consistent-return
|
|
|
|
return new Promise<void>((res, rej) => {
|
2022-12-29 02:41:49 +03:00
|
|
|
if (file.size > MAX_IMAGE_FILESIZE) {
|
|
|
|
const msg = `File size is too big: ${readableBytes(file.size)}`;
|
|
|
|
setSubmitStatus(createInputStatus(STATUS_ERROR, `There was an error: ${msg}`));
|
|
|
|
resetTimer = setTimeout(resetStates, RESET_TIMEOUT);
|
|
|
|
setLoading(false);
|
|
|
|
// eslint-disable-next-line no-promise-executor-return
|
|
|
|
return rej();
|
|
|
|
}
|
2022-12-12 19:40:43 +03:00
|
|
|
if (!ACCEPTED_IMAGE_TYPES.includes(file.type)) {
|
2021-03-22 02:07:12 +03:00
|
|
|
const msg = `File type is not supported: ${file.type}`;
|
|
|
|
setSubmitStatus(createInputStatus(STATUS_ERROR, `There was an error: ${msg}`));
|
|
|
|
resetTimer = setTimeout(resetStates, RESET_TIMEOUT);
|
|
|
|
setLoading(false);
|
2021-12-07 05:01:30 +03:00
|
|
|
// eslint-disable-next-line no-promise-executor-return
|
2021-03-22 02:07:12 +03:00
|
|
|
return rej();
|
|
|
|
}
|
|
|
|
|
|
|
|
getBase64(file, (url: string) => {
|
|
|
|
setlogoUrl(url);
|
2022-12-30 00:19:23 +03:00
|
|
|
setTimeout(() => res(), 100);
|
2021-03-22 02:07:12 +03:00
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
// Post new logo to api
|
|
|
|
const handleLogoUpdate = async () => {
|
|
|
|
if (logoUrl !== currentLogo) {
|
|
|
|
setSubmitStatus(createInputStatus(STATUS_PROCESSING));
|
|
|
|
await postConfigUpdateToAPI({
|
|
|
|
apiPath,
|
|
|
|
data: { value: logoUrl },
|
|
|
|
onSuccess: () => {
|
|
|
|
setFieldInConfigState({ fieldName: 'logo', value: logoUrl, path: '' });
|
|
|
|
setSubmitStatus(createInputStatus(STATUS_SUCCESS));
|
|
|
|
setLoading(false);
|
2021-04-08 08:48:39 +03:00
|
|
|
setLogoCacheBuster(Math.floor(Math.random() * 100)); // Force logo to re-load
|
2021-03-22 02:07:12 +03:00
|
|
|
},
|
|
|
|
onError: (msg: string) => {
|
|
|
|
setSubmitStatus(createInputStatus(STATUS_ERROR, `There was an error: ${msg}`));
|
|
|
|
setLoading(false);
|
|
|
|
},
|
|
|
|
});
|
|
|
|
resetTimer = setTimeout(resetStates, RESET_TIMEOUT);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-07-09 21:42:01 +03:00
|
|
|
const logoDisplayUrl = `${NEXT_PUBLIC_API_HOST}logo?random=${logoCachedbuster}`;
|
2021-03-26 06:06:29 +03:00
|
|
|
|
2021-03-22 02:07:12 +03:00
|
|
|
return (
|
|
|
|
<div className="formfield-container logo-upload-container">
|
|
|
|
<div className="label-side">
|
|
|
|
<span className="formfield-label">Logo</span>
|
|
|
|
</div>
|
|
|
|
|
|
|
|
<div className="input-side">
|
|
|
|
<div className="input-group">
|
2021-03-26 06:06:29 +03:00
|
|
|
<img src={logoDisplayUrl} alt="avatar" className="logo-preview" />
|
2021-03-22 02:07:12 +03:00
|
|
|
<Upload
|
|
|
|
name="logo"
|
|
|
|
listType="picture"
|
|
|
|
className="avatar-uploader"
|
|
|
|
showUploadList={false}
|
2022-12-12 19:40:43 +03:00
|
|
|
accept={ACCEPTED_IMAGE_TYPES.join(',')}
|
2021-03-22 02:07:12 +03:00
|
|
|
beforeUpload={beforeUpload}
|
|
|
|
customRequest={handleLogoUpdate}
|
|
|
|
disabled={loading}
|
|
|
|
>
|
|
|
|
{loading ? (
|
|
|
|
<LoadingOutlined style={{ color: 'white' }} />
|
|
|
|
) : (
|
|
|
|
<Button icon={<UploadOutlined />} />
|
|
|
|
)}
|
|
|
|
</Upload>
|
|
|
|
</div>
|
|
|
|
<FormStatusIndicator status={submitStatus} />
|
|
|
|
<p className="field-tip">{tip}</p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
);
|
2022-09-07 10:00:28 +03:00
|
|
|
};
|