forked from rubyforgood/casa
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcasa_case.js
More file actions
180 lines (154 loc) · 4.31 KB
/
casa_case.js
File metadata and controls
180 lines (154 loc) · 4.31 KB
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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
/* eslint-env jquery */
/* global FormData */
/* global DOMParser */
/* global spinner */
import Swal from 'sweetalert2'
function copyOrdersFromCaseWithConfirmation () {
const id = $(this).next().val()
const caseNumber = $('select.siblings-casa-cases').find(':selected').text()
const text = `Are you sure you want to copy all orders from case #${caseNumber}?`
Swal.fire({
icon: 'warning',
title: `Copy all orders from case #${caseNumber}?`,
text,
showCloseButton: true,
showCancelButton: true,
focusConfirm: false,
confirmButtonColor: '#d33',
cancelButtonColor: '#39c',
confirmButtonText: 'Copy',
cancelButtonText: 'Cancel'
}).then((result) => {
if (result.isConfirmed) {
copyOrdersFromCaseAction(id, caseNumber)
}
})
}
function copyOrdersFromCaseAction (id, caseNumber) {
$.ajax({
url: `/casa_cases/${id}/copy_court_orders`,
method: 'patch',
data: {
case_number_cp: caseNumber
},
success: () => {
Swal.fire({
icon: 'success',
text: 'Court orders have been copied.',
showCloseButton: true,
timer: 2000
}).then(() => window.location.reload(true))
},
error: () => {
Swal.fire({
icon: 'error',
text: 'Something went wrong when attempting to copy court orders.',
showCloseButton: true
})
}
})
}
function showBtn (el) {
if (!el) return
el.classList.remove('d-none')
}
function hideBtn (el) {
if (!el) return
el.classList.add('d-none')
}
function disableBtn (el) {
if (!el) return
el.disabled = true
el.classList.add('disabled')
el.setAttribute('aria-disabled', true)
}
function enableBtn (el) {
if (!el) return
el.disabled = false
el.classList.remove('disabled')
el.removeAttribute('aria-disabled')
}
function showAlert (html) {
const alertEl = new DOMParser().parseFromString(html, 'text/html').body.firstElementChild
const flashContainer = document.querySelector('.header-flash')
flashContainer && flashContainer.replaceWith(alertEl)
}
function validateForm (formEl, errorEl) {
if (!formEl) {
return
}
// check html validations, checkValidity returns false if doesn't pass validation
if (errorEl && !formEl.checkValidity()) {
errorEl.classList.remove('d-none')
}
}
function handleGenerateReport (e) {
e.preventDefault()
const form = e.currentTarget.form
const formData = Object.fromEntries(new FormData(form))
const errorEl = document.querySelector('.select-required-error')
validateForm(form, errorEl ?? null)
if (formData.case_number.length === 0) return
const generateBtn = e.currentTarget
disableBtn(generateBtn)
const url = e.currentTarget.form.action
const options = {
method: 'POST',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
},
body: JSON.stringify(formData)
}
showBtn(spinner)
hideBtn($('#btnGenerateReport .lni-download')[0])
window.fetch(url, options)
.then(response => {
return response.json()
})
.then(data => {
if (data.status !== 'ok') {
showAlert(data.error_messages)
enableBtn(generateBtn)
hideBtn(spinner)
return
}
hideBtn(spinner)
showBtn($('#btnGenerateReport .lni-download')[0])
enableBtn(generateBtn)
window.open(data.link, '_blank')
})
.catch((error) => {
console.error('Debugging info, error:', error)
})
}
function clearSelectErrors () {
const errorEl = document.querySelector('.select-required-error')
if (!errorEl) return
errorEl.classList.add('d-none')
}
$(() => { // JQuery's callback for the DOM loading
$('button.copy-court-button').on('click', copyOrdersFromCaseWithConfirmation)
if ($('button.copy-court-button').length) {
disableBtn($('button.copy-court-button')[0])
}
$('#case-selection').on('change', clearSelectErrors)
$('select.siblings-casa-cases').on('change', () => {
if ($('select.siblings-casa-cases').find(':selected').text()) {
enableBtn($('button.copy-court-button')[0])
} else {
disableBtn($('button.copy-court-button')[0])
}
})
$('#btnGenerateReport').on('click', handleGenerateReport)
if (/\/casa_cases\/.*\?.*success=true/.test(window.location.href)) {
$('#thank_you').modal()
}
})
export {
showBtn,
hideBtn,
disableBtn,
enableBtn,
showAlert
}