I based my project on friendlypix-web and I want to allow users to edit the text of a post, just as they can already edit a comment. I created a method in FirebaseHelper.js to do this.
The code below, using window.prompt() works (see commented section). But I want to use SweetAlert2 which allows textarea to help with longer captions and comments.
Here's my code:
_setupEditButton(postId, author = {}, imageText) {
const post = this.postElement;
if (this.auth.currentUser && this.auth.currentUser.uid === author.uid) {
post.addClass('fp-owned-post');
} else {
post.removeClass('fp-owned-post');
}
$('.fp-edit-post', post).click(() => {
// const newPost = window.prompt('Edit the post?', imageText);
// if (newPost !== null && newPost !== '') {
// this.firebaseHelper.editPost(postId, newPost).then(() => {
// $('.fp-first-comment .fp-text', post).text(newPost);
// });
// }
swal({
title: 'Edit post',
showCancelButton: true,
input: 'textarea', // sweetalert2 allows textarea
inputValue: imageText,
reverseButtons: true,
}).then(function(result) {
const newPost = result;
if (newPost !== null && newPost !== '') {
try {
this.firebaseHelper.editPost(postId, newPost).then(() => {
$('.fp-first-comment .fp-text', post).text(newPost);
});
} catch (e) {
console.log(e);
}
}
});
});
}
Sadly, my feeble understanding of JavaScript results in
TypeError: Cannot read property 'firebaseHelper' of undefined
Why is "this" undefined in the swal() version and not in the window.prompt()?