Real estate input clean up.

This commit is contained in:
Naida Vatric
2020-03-09 23:30:37 +01:00
parent 96bc66ef7b
commit 5f674230e1
10 changed files with 182 additions and 186 deletions

View File

@@ -36,6 +36,8 @@
$(document).ready(function(){
$('.tabs').tabs();
//VALIDATION
//Helper validation functions
const isValidEmail = $email => {
const simpleEmailRegex = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
@@ -151,9 +153,86 @@
}
}
//FILE UPLOADS
// Manual dropzone init
const dropzoneOptions = {
url: "/photos-upload", //not used
autoProcessQueue:false, //not to upload files automaticly
method: "put",
parallelUploads: 1,
addRemoveLinks: true,
maxFilesize: 2, //MB,
resizeWidth: 600,
maxFiles: 10,
init: function() {
this.on("addedfile", function(event) {
while (this.files.length > this.options.maxFiles) { //removes to many files
this.removeFile(this.files[0]);
}
});
},
acceptedFiles: "image/*",
dictDefaultMessage: `<span class="text-center">
<h3 class="custom-h3">Prevuci fotografije ili klikni za dodavanje!</h3>
(Maksimalno 10 fotografija.)
</span>`,
dictResponseError: 'Greska pri uploadu fajla!',
dictRemoveFile: 'Ukloni',
dictFileTooBig: `Fajl mora biti manji od 2 MB!`,
dictInvalidFileType: 'Izabrani fajl mora biti fotografija!'
};
const photosUploader = new Dropzone('#photos-upload', dropzoneOptions);
function uuidv4() {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
const r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
}
function getFileName(fileName) {
const encodedFileName = (uuidv4() + fileName).replace(/\s+/g, '');
return encodedFileName;
}
async function generateSignedURL(file) {
const fileName = getFileName(file.name);
const response = await fetch('/generateSignedURL?filename=' + fileName);
if (!response.ok) {
throw new Error('Network response for fetch was not ok.');
}
let signedUrl = await response.text();
signedUrl = signedUrl.replace(/\"/g, "")
uploadFile(file, fileName, signedUrl);
return true;
}
function uploadFile(file, fileName, url) {
fetch(url, {
method: 'PUT',
headers: new Headers({'content-type': 'image/*'}),
mode: 'cors',
body: file
})
.then(response => response.text())
.then (response => {
return response;
}
)
.catch(error => console.error(error)
)
.then(response => {
$("#imageUrls").val($("#imageUrls").val()+ fileName+"|");
});
}
$("#submit").click( function () {
//SET LOCATION DATA
if (marker) {
const currentLocation = marker.getPosition();
@@ -170,16 +249,20 @@
$("#lng").val(0);
}
//UPLOAD IMAGE FILES
const addedFiles = photosUploader.files.filter(file => file.status!=="error");
addedFiles.forEach( file => {
generateSignedURL(file);
})
//SHOW ERRORS
//Tag for checking of error presence
let hasErrors = false;
//Check if email is valid
const validEmail = isValidEmail($("#email").val());
//Show messeges for invalid email is present
if (!validEmail) {
const errorMsgs = ["Unesite validan email."];
const email = document.querySelector("#email");
showErrorsForInput( email, errorMsgs)
$("#error-label-email").text("Greška ! Unešeni email nije validan");
hasErrors = true;
};
//Check if other input fields are valid - vratiti se na ovo!!