Sprankelprachtig aan/afmeldsysteem

buttonhandlers.jsx.js 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. // Provides handlers for the buttons!
  2. document.addEventListener("turbolinks:load", setup_handlers);
  3. // Add the presence handler to all buttons on the page
  4. function setup_handlers()
  5. {
  6. $('.btn-presence').on("click", change_presence);
  7. $('.editable').editable();
  8. }
  9. // Callback that is triggered when a presence button is clicked.
  10. // Creates an AJAX-request and registers the appropriate handlers once it is done.
  11. function change_presence(e)
  12. {
  13. // Gather data
  14. var group, person, activity, state;
  15. group = this.dataset["groupId"];
  16. person = this.dataset["personId"];
  17. activity = this.dataset["activityId"];
  18. rstate = this.dataset["newState"];
  19. var state;
  20. switch (rstate)
  21. {
  22. case "present":
  23. state = true;
  24. break;
  25. case "absent":
  26. state = false;
  27. break;
  28. case "unknown":
  29. state = null;
  30. break;
  31. }
  32. // Make request
  33. var req;
  34. req = $.ajax(`/groups/${group}/activities/${activity}/presence`,
  35. {
  36. method: 'PUT',
  37. data: {person_id: person, attending: state}
  38. }
  39. )
  40. .done( activity_changed )
  41. .fail( alert_failure );
  42. // Pack data for success
  43. req.aardbei_activity_data =
  44. {
  45. group: group,
  46. person: person,
  47. activity: activity,
  48. state: state
  49. };
  50. }
  51. // Update all references on the page to this activity:
  52. // 1. The activity's row-color in any tables
  53. // 2. The present/absent buttons
  54. function activity_changed(data, textStatus, xhr)
  55. {
  56. // Unpack activity-data
  57. var target;
  58. target = xhr.aardbei_activity_data;
  59. // Determine what color and icons we're going to use
  60. var new_rowclass;
  61. var new_confirm_icon, new_decline_icon;
  62. switch (target.state)
  63. {
  64. case true:
  65. new_rowclass = "success";
  66. new_confirm_icon = check_selected;
  67. new_decline_icon = times_unselected;
  68. break;
  69. case false:
  70. new_rowclass = "danger";
  71. new_confirm_icon = check_unselected;
  72. new_decline_icon = times_selected;
  73. break;
  74. case null:
  75. new_rowclass = "warning";
  76. new_confirm_icon = check_unselected;
  77. new_decline_icon = times_unselected;
  78. break;
  79. }
  80. // Update all tr's containing this person's presence
  81. $(`tr[data-person-id=${target.person}][data-activity-id=${target.activity}]`)
  82. .removeClass('success danger warning')
  83. .addClass(new_rowclass);
  84. // Update all buttons for this person's presence
  85. $(`.btn-present[data-person-id=${target.person}][data-activity-id=${target.activity}]`)
  86. .html(new_confirm_icon);
  87. $(`.btn-absent[data-person-id=${target.person}][data-activity-id=${target.activity}]`)
  88. .html(new_decline_icon);
  89. // Add text to all 'wide' buttons
  90. $(`.btn-present[data-person-id=${target.person}][data-activity-id=${target.activity}][data-wide=1]`)
  91. .append(" Present");
  92. $(`.btn-absent[data-person-id=${target.person}][data-activity-id=${target.activity}][data-wide=1]`)
  93. .append(" Absent");
  94. }
  95. function alert_failure(data, textStatus, xhr)
  96. {
  97. alert(`Something broke! We got a ${textStatus}, (${data}).`);
  98. }
  99. var check_unselected = '<i class="fa fa-check"></i>';
  100. var check_selected = '<i class="fa fa-check-circle"></i>';
  101. var times_unselected = '<i class="fa fa-times"></i>';
  102. var times_selected = '<i class="fa fa-times-circle"></i>';