Refactored the ticket attribute click handlers to be more modular for adding new attribute modifications

This commit is contained in:
Gregory Ballantine 2022-12-04 17:28:01 -05:00
parent ab2c87ce3a
commit 15ee9b78a3

View File

@ -1,61 +1,37 @@
$(document).ready -> $(document).ready ->
console.log('Hello, world!') console.log('Hello, world!')
$('.ticket-severity').on('click', handleSeverityClick) $('.ticket-severity').on('click', handleAttributeClick('severity'))
$('.ticket-status').on('click', handleStatusClick) $('.ticket-status').on('click', handleAttributeClick('status'))
handleSeverityClick = (e, msg = 'Set ticket severity:') -> validOptions =
validSeverity = ['low', 'medium', 'high'] 'severity' = ['low', 'medium', 'high'],
newSeverity = prompt(msg, $('.ticket-severity > span').text()) 'status' = ['open', 'closed', 'parked']
newSeverity = newSeverity.toLowerCase()
if (newSeverity != null) and (newSeverity != '') handleAttributeClick = (e, attr, fail = false) ->
if (newSeverity in validSeverity) newValue = prompt('Set ticket ' + attr + ':', $('.ticket-' + attr + ' > span').text())
console.log('Setting severity to ' + newSeverity) newValue = newValue.toLowerCase()
editLink = $('#ticketEditLink').attr('href') + '/severity'
if (newValue != null) and (newValue != '')
if (newValue in validOptions[attr])
console.log('Setting ' + attr + ' to ' + newValue)
editLink = $('#ticketEditLink').attr('href') + '/' + attr
console.log('Sending data to ' + editLink) console.log('Sending data to ' + editLink)
$.ajax({ $.ajax({
type: "POST", type: "POST",
url: editLink, url: editLink,
data: data:
severity: newSeverity, attr: newValue,
dataType: 'json', dataType: 'json',
success: (result) -> success: (result) ->
newSeverity = newSeverity.charAt(0).toUpperCase() + newSeverity.slice(1) newValue = newValue.charAt(0).toUpperCase() + newValue.slice(1)
$('.ticket-severity > span').text(newSeverity) $('.ticket-' + attr + ' > span').text(newValue)
updateTicketModified(result.updated_at) updateTicketModified(result.updated_at)
console.log('Ticket updated successfully.') console.log('Ticket updated successfully.')
}) })
else else
console.log('Invalid severity entered') console.log('Invalid ' + attr + ' entered')
handleSeverityClick(e, 'Invalid severity; valid values are low, medium, and high.') handleAttributeClick(e, attr, 'Invalid ' + attr + '; valid values are ' + validOptions.toString() + '.')
handleStatusClick = (e, msg = 'Set ticket status:') ->
validStatus = ['open', 'closed', 'parked']
newStatus = prompt(msg, $('.ticket-status > span').text())
newStatus = newStatus.toLowerCase()
if (newStatus != null) and (newStatus != '')
if (newStatus in validStatus)
console.log('Setting status to ' + newStatus)
editLink = $('#ticketEditLink').attr('href') + '/status'
console.log('Sending data to ' + editLink)
$.ajax(
type: "POST",
url: editLink,
data:
status: newStatus,
dataType: 'json',
success: (result) ->
newStatus = newStatus.charAt(0).toUpperCase() + newStatus.slice(1)
$('.ticket-status > span').text(newStatus)
updateTicketModified(result.updated_at)
console.log('Ticket updated successfully.')
)
else
console.log('Invalid status entered')
handleStatusClick(e, 'Invalid status; valid values are open, closed, and parked.')
updateTicketModified = (date) -> updateTicketModified = (date) ->
$('.ticket-updated > span').text(date) $('.ticket-updated > span').text(date)