import React from 'react';
import ReactDOM from 'react-dom';
import Upload from '..';
import { Button, Dialog } from '../..';

const afterSelect = file => {
  return new Promise((resolve, reject) => {
    const reader = new FileReader();
    reader.onload = () => {
      const img = new Image();
      img.onload = () => {
        if (img.width === 1200) {
          resolve(undefined);
        } else {
          Dialog.alert({
            content: `Image width must be 1200px now ${img.width}px！`,
            // @ts-ignore
            closable: false,
            title: 'Warning',
          });
          reject(new Error('error'));
        }
      };
      img.src = reader.result as string;
    };
    reader.readAsDataURL(file.originFileObj);
  });
};

ReactDOM.render(
  <Upload
    action="https://www.easy-mock.com/mock/5b713974309d0d7d107a74a3/alifd/upload"
    autoUpload={false}
    listType="text"
    // @ts-ignore
    afterSelect={afterSelect}
    onError={err => console.log('Error', err)}
  >
    <Button>Upload</Button>
  </Upload>,
  document.getElementById('upload-demo-16'),
);
