|  | @@ -4,6 +4,8 @@ import { Row, Col, FormGroup, Button, Modal, ModalHeader, ModalBody, ModalFooter
 | 
	
		
			
				|  |  |  import { addBanding } from "@/actions/banding";
 | 
	
		
			
				|  |  |  import { connect } from "react-redux";
 | 
	
		
			
				|  |  |  import { toast } from "react-toastify";
 | 
	
		
			
				|  |  | +import { Formik, Form, Field, ErrorMessage } from "formik";
 | 
	
		
			
				|  |  | +import * as Yup from "yup";
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  let Dropzone = null;
 | 
	
		
			
				|  |  |  class DropzoneWrapper extends Component {
 | 
	
	
		
			
				|  | @@ -19,13 +21,39 @@ class DropzoneWrapper extends Component {
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  }
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | +const checkIfFilesAreTooBig = (files) => {
 | 
	
		
			
				|  |  | +	let valid = true;
 | 
	
		
			
				|  |  | +	if (files) {
 | 
	
		
			
				|  |  | +		files.map((file) => {
 | 
	
		
			
				|  |  | +			if (file.size > 15 * 1024 * 1024) {
 | 
	
		
			
				|  |  | +				valid = false;
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +		});
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	return valid;
 | 
	
		
			
				|  |  | +};
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const checkIfFilesAreCorrectType = (files) => {
 | 
	
		
			
				|  |  | +	let valid = true;
 | 
	
		
			
				|  |  | +	if (files) {
 | 
	
		
			
				|  |  | +		files.map((file) => {
 | 
	
		
			
				|  |  | +			if (!["image/jpeg", "image/png"].includes(file.type)) {
 | 
	
		
			
				|  |  | +				valid = false;
 | 
	
		
			
				|  |  | +			}
 | 
	
		
			
				|  |  | +		});
 | 
	
		
			
				|  |  | +	}
 | 
	
		
			
				|  |  | +	return valid;
 | 
	
		
			
				|  |  | +};
 | 
	
		
			
				|  |  | +
 | 
	
		
			
				|  |  | +const evaluasiSchema = Yup.object().shape({
 | 
	
		
			
				|  |  | +	dokumen: Yup.array().min(1, "Minimal terdapat 1 dokumen").required("Harus ada").test("filesize", "Maksimal ukuran dokumen 15mb", checkIfFilesAreTooBig),
 | 
	
		
			
				|  |  | +});
 | 
	
		
			
				|  |  |  export class ModalPermohonan extends Component {
 | 
	
		
			
				|  |  |  	constructor(props) {
 | 
	
		
			
				|  |  |  		super(props);
 | 
	
		
			
				|  |  |  		this.state = {
 | 
	
		
			
				|  |  |  			modal1: false,
 | 
	
		
			
				|  |  |  			files: [],
 | 
	
		
			
				|  |  | -			error: null,
 | 
	
		
			
				|  |  |  		};
 | 
	
		
			
				|  |  |  	}
 | 
	
		
			
				|  |  |  
 | 
	
	
		
			
				|  | @@ -66,42 +94,34 @@ export class ModalPermohonan extends Component {
 | 
	
		
			
				|  |  |  		});
 | 
	
		
			
				|  |  |  	};
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	onSubmit = async (e) => {
 | 
	
		
			
				|  |  | -		e.preventDefault();
 | 
	
		
			
				|  |  | +	onSubmit = async (data) => {
 | 
	
		
			
				|  |  |  		const { query, token } = this.props;
 | 
	
		
			
				|  |  |  		const { id } = query;
 | 
	
		
			
				|  |  |  		const formdata = new FormData();
 | 
	
		
			
				|  |  | -		if (this.state.files.length > 0) {
 | 
	
		
			
				|  |  | -			this.setState({
 | 
	
		
			
				|  |  | -				modal1: !this.state.modal1,
 | 
	
		
			
				|  |  | -			});
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  | -			this.state.files.forEach((e) => {
 | 
	
		
			
				|  |  | +		if (data.dokumen.length > 0) {
 | 
	
		
			
				|  |  | +			data.dokumen.forEach((e) => {
 | 
	
		
			
				|  |  |  				formdata.append("dokumen", e);
 | 
	
		
			
				|  |  |  			});
 | 
	
		
			
				|  |  | +		}
 | 
	
		
			
				|  |  | +		this.setState({
 | 
	
		
			
				|  |  | +			modal1: !this.state.modal1,
 | 
	
		
			
				|  |  | +		});
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -			const toastid = toast.loading("Please wait...");
 | 
	
		
			
				|  |  | -			const added = await addBanding(token, id, formdata);
 | 
	
		
			
				|  |  | +		const toastid = toast.loading("Please wait...");
 | 
	
		
			
				|  |  | +		const added = await addBanding(token, id, formdata);
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -			if (!added) {
 | 
	
		
			
				|  |  | -				toast.update(toastid, { render: "All is not good", type: "error", isLoading: false, autoClose: true, closeButton: true });
 | 
	
		
			
				|  |  | -			} else {
 | 
	
		
			
				|  |  | -				toast.update(toastid, { render: "All is good", type: "success", isLoading: false, autoClose: true, closeButton: true });
 | 
	
		
			
				|  |  | -				Router.push({
 | 
	
		
			
				|  |  | -					pathname: "/pt/jawaban-banding",
 | 
	
		
			
				|  |  | -				});
 | 
	
		
			
				|  |  | -			}
 | 
	
		
			
				|  |  | +		if (!added) {
 | 
	
		
			
				|  |  | +			toast.update(toastid, { render: "All is not good", type: "error", isLoading: false, autoClose: true, closeButton: true });
 | 
	
		
			
				|  |  |  		} else {
 | 
	
		
			
				|  |  | -			this.setState({ error: "Dokumen harus ada" });
 | 
	
		
			
				|  |  | +			toast.update(toastid, { render: "All is good", type: "success", isLoading: false, autoClose: true, closeButton: true });
 | 
	
		
			
				|  |  | +			Router.push({
 | 
	
		
			
				|  |  | +				pathname: "/pt/jawaban-banding",
 | 
	
		
			
				|  |  | +			});
 | 
	
		
			
				|  |  |  		}
 | 
	
		
			
				|  |  |  	};
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  | -	handleKirim = (e) => {
 | 
	
		
			
				|  |  | -		this.onSubmit(e);
 | 
	
		
			
				|  |  | -	};
 | 
	
		
			
				|  |  | -
 | 
	
		
			
				|  |  |  	render() {
 | 
	
		
			
				|  |  | -		const { files, error } = this.state;
 | 
	
		
			
				|  |  | +		const { files } = this.state;
 | 
	
		
			
				|  |  |  
 | 
	
		
			
				|  |  |  		const thumbs = files.map((file, index) => (
 | 
	
		
			
				|  |  |  			<Col md={3} key={index}>
 | 
	
	
		
			
				|  | @@ -123,38 +143,65 @@ export class ModalPermohonan extends Component {
 | 
	
		
			
				|  |  |  				</Modal>
 | 
	
		
			
				|  |  |  				<Modal isOpen={this.state.modal1} toggle={this.toggleModal1}>
 | 
	
		
			
				|  |  |  					<ModalHeader toggle={this.toggleModal1}>Upload Dokumen Banding</ModalHeader>
 | 
	
		
			
				|  |  | -					<ModalBody>
 | 
	
		
			
				|  |  | -						<form className="form-horizontal" method="get" action="/" onSubmit={this.onSubmit}>
 | 
	
		
			
				|  |  | -							<FormGroup>
 | 
	
		
			
				|  |  | -								<label>Dalam hal mengajukan permohonan banding maka wajib mengunggah surat permohonan banding & dokumen pendukungnya</label>
 | 
	
		
			
				|  |  | -								<div>
 | 
	
		
			
				|  |  | -									<DropzoneWrapper className="" onDrop={this.onDrop}>
 | 
	
		
			
				|  |  | -										{({ getRootProps, getInputProps, isDragActive }) => {
 | 
	
		
			
				|  |  | -											return (
 | 
	
		
			
				|  |  | -												<div {...getRootProps()} className={"dropzone card p-3 " + (isDragActive ? "dropzone-drag-active" : "")}>
 | 
	
		
			
				|  |  | -													<input {...getInputProps()} />
 | 
	
		
			
				|  |  | -													<div className="dropzone-previews flex">{this.state.files.length > 0 ? <Row>{thumbs}</Row> : <div className="text-center dz-default dz-message">Drop files here to upload</div>}</div>
 | 
	
		
			
				|  |  | -													<div className="d-flex align-items-center">
 | 
	
		
			
				|  |  | -														<small className="ml-auto">
 | 
	
		
			
				|  |  | -															<button type="button" className="btn btn-link" onClick={this.clearFiles}>
 | 
	
		
			
				|  |  | -																Clear files
 | 
	
		
			
				|  |  | -															</button>
 | 
	
		
			
				|  |  | -														</small>
 | 
	
		
			
				|  |  | -													</div>
 | 
	
		
			
				|  |  | -												</div>
 | 
	
		
			
				|  |  | -											);
 | 
	
		
			
				|  |  | -										}}
 | 
	
		
			
				|  |  | -									</DropzoneWrapper>
 | 
	
		
			
				|  |  | -									<span className={`form-text ${error ? "text-danger" : ""}`}>{error ? error : "Multiple files upload"}</span>
 | 
	
		
			
				|  |  | -								</div>
 | 
	
		
			
				|  |  | -							</FormGroup>
 | 
	
		
			
				|  |  | -						</form>
 | 
	
		
			
				|  |  | -					</ModalBody>
 | 
	
		
			
				|  |  | -					<ModalFooter>
 | 
	
		
			
				|  |  | -						<Button color="primary" onClick={this.handleKirim}>
 | 
	
		
			
				|  |  | -							Kirim
 | 
	
		
			
				|  |  | -						</Button>
 | 
	
		
			
				|  |  | -					</ModalFooter>
 | 
	
		
			
				|  |  | +					<Formik
 | 
	
		
			
				|  |  | +						initialValues={{
 | 
	
		
			
				|  |  | +							dokumen: [],
 | 
	
		
			
				|  |  | +						}}
 | 
	
		
			
				|  |  | +						validationSchema={evaluasiSchema}
 | 
	
		
			
				|  |  | +						onSubmit={this.onSubmit}
 | 
	
		
			
				|  |  | +					>
 | 
	
		
			
				|  |  | +						<Form className="form-horizontal">
 | 
	
		
			
				|  |  | +							<ModalBody>
 | 
	
		
			
				|  |  | +								<FormGroup>
 | 
	
		
			
				|  |  | +									<label>Dalam hal mengajukan permohonan banding maka wajib mengunggah surat permohonan banding & dokumen pendukungnya</label>
 | 
	
		
			
				|  |  | +									<div>
 | 
	
		
			
				|  |  | +										<Field name="dokumen">
 | 
	
		
			
				|  |  | +											{({ field, form, meta }) => (
 | 
	
		
			
				|  |  | +												<DropzoneWrapper
 | 
	
		
			
				|  |  | +													className=""
 | 
	
		
			
				|  |  | +													onDrop={(e) => {
 | 
	
		
			
				|  |  | +														this.onDrop(e);
 | 
	
		
			
				|  |  | +														form.setFieldValue(field.name, e);
 | 
	
		
			
				|  |  | +													}}
 | 
	
		
			
				|  |  | +												>
 | 
	
		
			
				|  |  | +													{({ getRootProps, getInputProps, isDragActive }) => {
 | 
	
		
			
				|  |  | +														return (
 | 
	
		
			
				|  |  | +															<div {...getRootProps()} className={"dropzone card p-3 " + (isDragActive ? "dropzone-drag-active" : "")}>
 | 
	
		
			
				|  |  | +																<input {...getInputProps()} />
 | 
	
		
			
				|  |  | +																<div className="dropzone-previews flex">
 | 
	
		
			
				|  |  | +																	{this.state.files.length > 0 ? <Row>{thumbs}</Row> : <div className="text-center dz-default dz-message">Drop files here to upload</div>}
 | 
	
		
			
				|  |  | +																</div>
 | 
	
		
			
				|  |  | +																<div className="d-flex align-items-center">
 | 
	
		
			
				|  |  | +																	<small className="ml-auto">
 | 
	
		
			
				|  |  | +																		<button
 | 
	
		
			
				|  |  | +																			type="button"
 | 
	
		
			
				|  |  | +																			className="btn btn-link"
 | 
	
		
			
				|  |  | +																			onClick={(e) => {
 | 
	
		
			
				|  |  | +																				this.clearFiles(e);
 | 
	
		
			
				|  |  | +																				form.setFieldValue(field.name, []);
 | 
	
		
			
				|  |  | +																			}}
 | 
	
		
			
				|  |  | +																		>
 | 
	
		
			
				|  |  | +																			Clear files
 | 
	
		
			
				|  |  | +																		</button>
 | 
	
		
			
				|  |  | +																	</small>
 | 
	
		
			
				|  |  | +																</div>
 | 
	
		
			
				|  |  | +															</div>
 | 
	
		
			
				|  |  | +														);
 | 
	
		
			
				|  |  | +													}}
 | 
	
		
			
				|  |  | +												</DropzoneWrapper>
 | 
	
		
			
				|  |  | +											)}
 | 
	
		
			
				|  |  | +										</Field>
 | 
	
		
			
				|  |  | +										<ErrorMessage name="dokumen" component="div" className="form-text text-danger" />
 | 
	
		
			
				|  |  | +									</div>
 | 
	
		
			
				|  |  | +								</FormGroup>
 | 
	
		
			
				|  |  | +							</ModalBody>
 | 
	
		
			
				|  |  | +							<ModalFooter>
 | 
	
		
			
				|  |  | +								<Button color="primary" type="submit">
 | 
	
		
			
				|  |  | +									Kirim
 | 
	
		
			
				|  |  | +								</Button>
 | 
	
		
			
				|  |  | +							</ModalFooter>
 | 
	
		
			
				|  |  | +						</Form>
 | 
	
		
			
				|  |  | +					</Formik>
 | 
	
		
			
				|  |  |  				</Modal>
 | 
	
		
			
				|  |  |  			</>
 | 
	
		
			
				|  |  |  		);
 |