-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.js
87 lines (79 loc) · 1.86 KB
/
app.js
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
$(document).ready(function(){
let form = $('#form'); let del = $('a.delete');
$(del).each(function(){
$(this).on('click', function(e){
e.preventDefault();
let link = $(this);
let target = $(this).attr('href');
// if(!confirm('Confirmez vous la suppression?')){
// return false;
// }
Swal.fire({
title: 'Confirmez vous la suppression?',
text: 'Cette action est irréversible',
type: 'warning',
showCancelButton: true,
confirmButtonText: 'Oui. Supprime!',
cancelButtonText: 'Nooon!',
}).then((result) => {
if(result.value){
fetch(target, {method: 'get'}).then(response => response.json()).then(message => {
console.log(message);
Swal.fire({
title: 'Yeah !',
html: '<p>'+message.success+'</p>',
type: 'success',
});
});
$(link).closest('tr').fadeOut();
}
}).catch(err => {
console.log(err);
Swal.fire({
title: 'Oups!',
text: 'Un erreur est survenue.',
type: 'error',
});
});
});
});
$(form).on('submit', function(e){
e.preventDefault();
$.ajax({
url: $(this).attr('action'),
type: $(this).attr('method'),
data: $(this).serialize(),
dataType: 'json',
success: function(response){
console.log(response);
if(response.errors){
let errorString = '';
$.each(response.errors, function(key, value){
errorString += '<p>'+value+'</p>';
});
Swal.fire({
title: 'Erreur!',
html: errorString,
type: 'error',
confirmButtonText: 'Ok',
});
}
if(response.success){
Swal.fire({
title: 'Succès!',
text: response.success,
type: 'success',
confirmButtonText: 'Ok',
}).then((result) => {
if(result.value){
document.location.reload(true);
}
});
}
},
error: function(err){
console.log(err);
}
});
});
});