docbrown/app/javascript/admin/controllers/reaction_controller.js
rhymes e5226b9951
Upgrade Stimulus to 3.0 (#14869)
* Upgrade Stimulus to 3.0

* Remove unused variable

* Bump postcss from 8.3.8 to 8.3.9 (#14956)

Bumps [postcss](https://github.com/postcss/postcss) from 8.3.8 to 8.3.9.
- [Release notes](https://github.com/postcss/postcss/releases)
- [Changelog](https://github.com/postcss/postcss/blob/main/CHANGELOG.md)
- [Commits](https://github.com/postcss/postcss/compare/8.3.8...8.3.9)

---
updated-dependencies:
- dependency-name: postcss
  dependency-type: direct:production
  update-type: version-update:semver-patch
...

Signed-off-by: dependabot[bot] <support@github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
2021-10-05 20:00:37 +02:00

76 lines
1.7 KiB
JavaScript

import { Controller } from '@hotwired/stimulus';
export default class ReactionController extends Controller {
static targets = ['invalid', 'confirmed'];
static values = {
id: Number,
url: String,
};
updateReaction(status) {
const id = this.idValue;
fetch(this.urlValue, {
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 {
window.alert(json.error);
}
})
.catch((error) => {
window.alert(error);
}),
);
}
updateReactionInvalid() {
this.updateReaction(this.invalidStatus);
}
updateReactionConfirmed() {
this.updateReaction(this.confirmedStatus);
}
reactableUserCheck() {
if (this.reactableType === 'user') {
if (
window.confirm(
'You are confirming a User vomit reaction; are you sure?',
)
) {
this.updateReaction(this.confirmedStatus);
}
} else {
this.updateReaction(this.confirmedStatus);
}
}
get reactableType() {
return this.confirmedTarget.dataset.reactable;
}
get confirmedStatus() {
return this.confirmedTarget.dataset.status;
}
get invalidStatus() {
return this.invalidTarget.dataset.altstatus;
}
}