All files / src/components/imageOperator SingleImage.tsx

27.88% Statements 29/104
27.65% Branches 13/47
15.78% Functions 3/19
28.43% Lines 29/102

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261                                          66x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x   4x 4x 2x   2x 2x 2x                                                           2x     4x 2x 2x                                                                         4x                                                   4x                                         4x                                                                                     4x                                                                                                       4x 4x 4x        
import React, { ReactElement, useEffect, useRef, useState } from 'react';
import {
  Avatar, Button, Image, Popconfirm, Space, Spin, Upload, message
} from 'antd';
import { EditableControllerProps } from "@kernel/ComponentsConfig";
import { fetchCurrentValue } from "@utils/FetchUtils";
import { getBase64 } from "../fileOperator/FileOperatorUtils";
import { ExtendUploadFile } from "@props/RecordProps";
import ImgCrop from "antd-img-crop";
import { emptyMethod } from "@utils/Constants";
import { useTranslation } from "react-i18next";
import ReconnectableSocket, { SocketInterface } from "@utils/WebsocketUtils";
import { WebSocketDummyMessage, WEBSOCKET_SERVER_URL } from "@config/base";
import { RcFile } from 'antd/es/upload';
import { deleteAttachment } from '@utils/FetchUtils';
import { BaseResponseProps } from '@props/RecordProps';
 
export interface SingleImageDisplayProps extends EditableControllerProps {
  avatarMode?: boolean;
}
 
const SingleImage = (props: SingleImageDisplayProps): ReactElement => {
  const { column, form, avatarMode, domainName, record, updatable } = props;
  const { id } = record || {};
  const [previewBlob, setPreviewBlob] = useState<string>();
  const [loading, setLoading] = useState<boolean>(false);
  const fileList = useRef<Array<ExtendUploadFile>>([]);
  const [isDirty, setIsDirty] = useState<boolean>(false);
  const [saving, setSaving] = useState<boolean>(false);
  const [deleting, setDeleting] = useState<boolean>(false);
  const [websocket, setWebsocket] = useState<SocketInterface>();
  const { t } = useTranslation();
  const isAvatar = avatarMode || (column.key === 'avatar');
 
  useEffect(() => {
    if (websocket != null) {
      return;
    }
    const socketInterface = ReconnectableSocket(`${WEBSOCKET_SERVER_URL}/websocket/fileUpload`);
    setWebsocket(socketInterface);
    socketInterface.on((wsMessage: string) => {
      if (wsMessage === WebSocketDummyMessage) {
        return;
      }
      const messageProps: ExtendUploadFile = JSON.parse(wsMessage);
      const { data } = messageProps;
      // 上传
      //1. 将保存中标记设置为 false,
      //2. 将 fileList 中的元素的 id 设置为后台返回的 data.id
      //3. 将 form 中, 对应字段的值设置为后台返回的 id
      //4. 如果保存没有发生错误,则设置 isDirty 为 false
      setSaving(false);
      if (data != null && data.id != null) {
        setIsDirty(false);
        message.success(t('Operation success'));
        const dataId = data.id;
        form?.setFieldsValue({
          [column.key]: dataId,
        });
        const newFileList: Array<ExtendUploadFile> = [];
        newFileList[0] = fileList.current[0];
        newFileList[0].id = dataId;
        fileList.current = newFileList;
      } else {
        message.error(t('Operation failed'));
        console.error(`Backend returned ${wsMessage}, no data.id attribute, will not be able to save the image`);
        fileList.current = [];
      }
    });
    //组件 unload 的时候关闭 websocket 连接
    return;
  }, [t, websocket, column.key, form, fileList]);
 
  useEffect(() => {
    Eif (id == null) {
      return;
    }
    setLoading(true);
    fetchCurrentValue(domainName, id).then((json) => {
      if (column.key in json) {
        const value = json[column.key];
        if (value != null && ('id' in value) && value.id != null) {
          const storageFieldValueId = value.id;
          getBase64({
            id: storageFieldValueId,
            name: 'avatar.png'
          } as ExtendUploadFile).then(content => {
            setPreviewBlob(content as string);
            fileList.current = [
              {
                id: storageFieldValueId,
                name: 'avatar.png',
                status: 'done',
                uid: storageFieldValueId,
              }
            ];
          }).catch((err) => {
            console.error(`Failed to get StorageFieldValue(${storageFieldValueId}): ${JSON.stringify(err)}`);
          }).finally(() => {
            setLoading(false);
          });
        } else {
          setLoading(false);
        }
      } else {
        console.error(`${column.key} not found in ${domainName}`);
      }
    }).catch((err) => {
      console.error(`Failed to get ${domainName} current value ${id}(${JSON.stringify(record)}): ${JSON.stringify(err)}`);
    });
  }, [domainName, id, column.key, record]);
 
  const onBeforeUpload = (file: RcFile): boolean => {
    const newFile = file as ExtendUploadFile;
    if (fileList.current.length === 0) {
      newFile.status = undefined;
    } else {
      const first = fileList.current[0];
      if (first.id != null) {
        newFile.id = first.id;
      }
      newFile.status = undefined;
    }
    fileList.current = [newFile];
    setIsDirty(true);
    //TODO 代码重构,将这里的和 FileOperatorUtils 中的 Promise 相关代码去重
    new Promise((resolve, reject) => {
      const reader = new FileReader();
      reader.readAsDataURL(file as RcFile);
      reader.onload = () => resolve(reader.result);
      reader.onerror = error => reject(error);
    }).then(content => {
      setPreviewBlob(content as string);
    });
    return false;
  };
 
  //TODO 上传和删除附件的逻辑和 fileOperator 中的合并,删除重复代码
  const saveAvatar = (): void => {
    // 如果没有设定头像,或者删除头像操作,
    // 则点击保存的时候无需有动作
    if (fileList.current.length === 0) {
      return;
    }
    const file = fileList.current[0];
    // 如果是修改或者新增头像或图片,
    // 则需要请求后台,保存新的图片
    const attachmentId = file.id;
    setSaving(true);
    websocket?.sendFile({
      id: attachmentId ?? undefined,
      uid: file.uid,
      ownerId: id,
      ownerClass: domainName,
      columnNameInOwnerClass: column.key,
      multiple: false
    }, (file as unknown) as File);
  };
 
  const onConfirm = (): void => {
    const backendId = fileList.current[0].id;
    setDeleting(true);
    if (backendId != null) {
      deleteAttachment({
        id: backendId,
        uid: backendId.toString(),
        ownerId: id,
        ownerClass: domainName,
        columnNameInOwnerClass: column.key,
        multiple: false
      }).then((json: BaseResponseProps) => {
        const { success } = json;
 
        //删除,
        //1. 将删除中标记设置为 false,
        //2. 将 fileList 设置为空数组, previewBlob 设置为空
        //3. 将 form 中, 对应字段的值设置为 null
        //4. 如果删除没有发生错误,则设置 isDirty 为 false
        setDeleting(false);
        if (success === true) {
          message.success(t('Operation success'));
          fileList.current = [];
          setPreviewBlob(undefined);
          form?.setFieldsValue({
            [column.key]: null
          });
          setIsDirty(false);
        } else {
          console.error("Failed to update avator", json);
          message.error(t('Operation failed'));
        }
      }).catch(e => {
        message.error(t('Operation failed'), e);
      });
    } else {
      setIsDirty(false);
      setDeleting(false);
      fileList.current = [];
      setPreviewBlob(undefined);
    }
  };
  const avatarContent = (
    <Space
      direction="horizontal"
      size={6}
      style={{ textAlign: "center" }}
    >
      {previewBlob && <Avatar
        src={<Image src={previewBlob} preview={false} />}
        size={120}
      />}
      {updatable && fileList.current.length > 0 && <Popconfirm
        title={t('Are you sure to remove this avatar')}
        onConfirm={onConfirm}
        okText={t("Confirm")}
        cancelText={t("Cancel")}
      >
        <Button
          size="small"
          loading={deleting}
        >
          {t('Remove avatar')}
        </Button>
      </Popconfirm>
      }
      {updatable &&
        <span>
          <ImgCrop
            rotationSlider={true}
            cropShape={"round"}
          >
            <Upload
              beforeUpload={onBeforeUpload}
              fileList={fileList.current}
              onPreview={emptyMethod}
              multiple={false}
              showUploadList={false}
              accept=".jpg,.jpeg,.gif,.png,.bmp"
            >
              <Button size="small">{t('Change avatar')}</Button>
            </Upload>
          </ImgCrop>
        </span>
      }
      {updatable && isDirty &&
        <Button
          size="small"
          onClick={() => saveAvatar()}
          loading={saving}
        >
          {t('Save avatar')}
        </Button>
      }
    </Space>);
  const imageContent = <Image src={previewBlob} />;
  const content = isAvatar ? avatarContent : imageContent;
  return loading ? (<Spin />) : content;
};
 
export default SingleImage;