Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Feat] Favorite Posts from Preview Panel #3

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 45 additions & 3 deletions src/ui/preview_panel.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import Tag from "../tag.js";
import _ from "lodash";
import moment from "moment";

let _previewPanelPost = null

export default class PreviewPanel {
static initialize() {
// This is the main content panel that comes before the preview panel.
Expand Down Expand Up @@ -66,10 +68,50 @@ export default class PreviewPanel {

static async update($post) {
const postId = $post.data("id");
const post = await Post.get(postId);
const post = _previewPanelPost = await Post.get(postId);
const html = PreviewPanel.renderPost(post);

$("#ex-preview-panel > div").children().first().replaceWith(html);
const panelParent = $("#ex-preview-panel > div");

let $html = panelParent.children().first();

let favButton = $html.find(".fa-heart").parent();

if (favButton.length > 0) {
favButton[0].removeEventListener('click', PreviewPanel.favoritePost, false);
}

$html.replaceWith(html);
$html = panelParent.children().first();

favButton = $html.find('.fa-heart').parent();
if (favButton.length > 0) {
favButton[0].addEventListener('click', PreviewPanel.favoritePost, false);
}
}

static async favoritePost(e) {
const post = _previewPanelPost;

if (post == null) {
return;
}

e.preventDefault()

let isFav = post.is_favorited;

const type = isFav ? "DELETE" : "POST";
const params = isFav ? {} : { post_id: post.id };
const url = `/favorites${(isFav ? `/${post.id}` : "")}`;

await Post.request(type, url, params);
isFav = post.is_favorited = !isFav; // For future use, incase they unfavorite without changing the post

const remove = isFav ? "far" : "fas";
const add = isFav ? "fas" : "far";

$("#ex-preview-panel > div").children().first().find(".fa-heart").removeClass(remove).addClass(add);
}

static get $panel() {
Expand Down Expand Up @@ -129,7 +171,7 @@ export default class PreviewPanel {
${post.fav_count}

<a href="#">
<i class="far fa-heart" aria-hidden="true"></i>
<i class="fa${(post.is_favorited ? "s" : "r")} fa-heart" aria-hidden="true"></i>
</a>
</span>

Expand Down