Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.8 kB
2
Indexable
Never
uploadModalTrigger(e) {
        e.preventDefault();

        UploadImageModal.fire({
            title: 'Select image',
            input: 'file',
            showLoaderOnConfirm: true,
            showCancelButton: true,
            customClass: {
                container: 'file-upload-modal'
            },
            inputAttributes: {
                'accept': 'image/*',
                'multiple': 'multiple',
            },
            preConfirm: (inputValue) => {
                if (inputValue) {
                    return this.addDamageImage(inputValue)
                        .then(() => {
                            return this.refreshImages();
                        })
                        .catch(err => {
                        console.log(err);
                    });
                }
            }
        }).then(resp => {
            if (resp.value) {
                this.addDamageImage(resp.value)
                    .then(() => {
                        this.refreshImages();
                    })
                    .catch(err => {
                            console.log(err);
                    });
            }
        });

    }


addDamageImage(images) {

        LoadingModalSwal.fire({
            title: 'Please wait. Uploading images',
            showLoaderOnConfirm: false,
            showCancelButton: false,
            didOpen: () => {
                Swal.showLoading()
            }
        });
        return new Promise((resolve, reject) => {
            if (!images) throw new Error('Missing images');

            let formData = new FormData();

            for (let i = 0; i < images.length; i++) {
                formData.append('image[]', images[i]);
            }
            formData.append('damage_id', this.state.id);

            Axios.post(process.env.REACT_APP_API_BASE + '/images', formData, {
                headers: {
                    'Authorization': 'Bearer ' + this.state.token
                }
            } )
                .then(response => {


                    if (response.status === 200) {
                        this.props.refreshCargoData();
                        SuccessModal.fire({
                            title: 'Successfully added image',
                        }).then(() => {
                            resolve();
                        });
                    }
                })
                .catch(error => {
                    console.log(error);
                    this.setState({
                        'formErrors': error.response.data.message
                    }, () => {
                        reject(error.response.data.message);
                    });
                })
        });
    }
Leave a Comment