-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
94 lines (88 loc) · 2.93 KB
/
index.ts
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
88
89
90
91
92
93
import Swal from 'sweetalert2';
import { doPost } from 's-apis'
export const swalSuccess = (message: string,title: string): void => {
let title_: string = title ?? 'success!';
Swal.fire(title_, message, 'success');
};
export const swalError = (message: string, title: string): void => {
let title_: string = title ?? 'Error!';
Swal.fire(title_, message, 'error');
};
export const showToast = (message: string, toastType?: string,toastPosition?:string, toastTime?:number): void => {
const toastPosition_:string = toastPosition ?? 'top-end'
const toastTime_:number = toastTime ?? 3000
// @ts-ignore
const Toast = Swal.mixin({
toast: true,
position: toastPosition_,
timer: toastTime_,
showConfirmButton: false,
timerProgressBar: true,
didOpen: (toast) => {
toast.addEventListener('mouseenter', Swal.stopTimer)
toast.addEventListener('mouseleave', Swal.resumeTimer)
}
})
let type_: string = toastType ?? 'success';
// @ts-ignore
Toast.fire({
icon: toastType,
title: message
})
}
// @ts-ignore
export const confirmRequest = async (url: string, message: string, title?: string, data?: any): Promise<Swal.SweetAlertResult> => {
const result = await Swal.fire({
title: title ?? 'Are you sure?',
html: message,
showCancelButton: true,
confirmButtonColor: '#32c787',
cancelButtonText: 'No, cancel',
confirmButtonText: 'Yes, Proceed!',
reverseButtons: true,
showLoaderOnConfirm: true,
preConfirm: async () => {
try {
const response = await doPost(url, data);
return {
response: response.data,
success: true,
};
} catch (error) {
return {
success: false,
error: error,
};
}
},
allowOutsideClick: () => !Swal.isLoading(),
});
return result;
}
export const showAutoclose = (title?: string, content?: string, timer?: number ): void => {
const title_: string = title ?? 'Auto close Alert'
const timer_ = timer ?? 3000
const content_: string = content ?? 'I will close in <b></b> milliseconds.'
let timerInterval
Swal.fire({
title: title_,
html: content_,
timer:timer_,
timerProgressBar: true,
didOpen: () => {
Swal.showLoading()
const b = Swal.getHtmlContainer().querySelector('b')
timerInterval = setInterval(() => {
// @ts-ignore
b.textContent = b ? Swal.getTimerLeft() : ''
}, 100)
},
willClose: () => {
clearInterval(timerInterval)
}
}).then((result) => {
if (result.dismiss === Swal.DismissReason.timer) {
console.log('I was closed by the timer')
}
})
}