docbrown/app/javascript/admin/controllers/reaction_controller.js
Julianna Tetreault 09c3c25bf8
Add Options to "Invalidate" and "Mark as Valid" Vomit Reactions (#9769) [deploy]
* Adds valid status to the feedback_message model and a spec to validate the status

* Adds an unresolve button to resolved vomits in _abuse_reports.html.erb

* Adds functions to update valid status in reaction_controller.js

* Use invalidate in place of unresolve for unconfirming vomit button

* Add option to reopen vomit reactions marked as invalid and invalidate resolved vomits
  - Adds a Reopen button under the invalid tab
  - Adds confirmedStatus JS to reaction_controller.js
  - Adds conditional to _abuse_reports.html.erb
2020-08-18 11:48:23 -06:00

74 lines
1.9 KiB
JavaScript

/* eslint-disable no-alert */
import { Controller } from 'stimulus';
export default class ReactionController extends Controller {
static targets = ['invalid', 'confirmed'];
// eslint-disable-next-line class-methods-use-this
/* eslint no-alert: "error" */
updateReaction(id, status) {
fetch(`/admin/reactions/${id}`, {
method: 'PATCH',
headers: {
Accept: 'application/json',
'X-CSRF-Token': document.querySelector("meta[name='csrf-token']")
.content,
'Content-Type': 'application/json',
},
body: JSON.stringify({
id,
status,
}),
credentials: 'same-origin',
}).then((response) =>
response
.json()
.then((json) => {
if (json.outcome === 'Success') {
this.element.remove();
document.getElementById(`js__reaction__div__hr__${id}`).remove();
} else {
alert(json.error);
}
})
.catch((error) => {
alert(error);
}),
);
}
updateReactionInvalid() {
this.updateReaction(this.reactionId, this.invalidStatus);
}
updateReactionConfirmed() {
this.updateReaction(this.reactionId, this.confirmedStatus);
}
reactableUserCheck() {
if (this.reactableType === 'user') {
// eslint-disable-next-line no-restricted-globals
if (confirm('You are confirming a User vomit reaction; are you sure?')) {
this.updateReaction(this.reactionId, this.confirmedStatus);
}
} else {
this.updateReaction(this.reactionId, this.confirmedStatus);
}
}
get reactionId() {
return parseInt(this.data.get('id'), 10);
}
get confirmedStatus() {
return this.confirmedTarget.dataset.status;
}
get reactableType() {
return this.confirmedTarget.dataset.reactable;
}
get invalidStatus() {
return this.invalidTarget.dataset.altstatus;
}
}