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

Quick Tags: Work correctly on non-NPF posts #969

Merged
merged 6 commits into from
Aug 20, 2023
Merged
Show file tree
Hide file tree
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
31 changes: 19 additions & 12 deletions src/scripts/quick_tags.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,13 @@ import { cloneControlButton, createControlButtonTemplate } from '../util/control
import { keyToCss } from '../util/css_map.js';
import { dom } from '../util/dom.js';
import { postSelector } from '../util/interface.js';
import { megaEdit } from '../util/mega_editor.js';
import { pageModifications } from '../util/mutations.js';
import { notify } from '../util/notifications.js';
import { registerPostOption, unregisterPostOption } from '../util/post_actions.js';
import { getPreferences } from '../util/preferences.js';
import { timelineObject, editPostFormTags } from '../util/react_props.js';
import { apiFetch, createEditRequestBody } from '../util/tumblr_helpers.js';
import { apiFetch, createEditRequestBody, isNpfCompatible } from '../util/tumblr_helpers.js';

const symbolId = 'ri-price-tag-3-line';
const buttonClass = 'xkit-quick-tags-button';
Expand Down Expand Up @@ -148,7 +149,7 @@ const togglePostOptionPopupDisplay = async function ({ target, currentTarget })

const addTagsToPost = async function ({ postElement, inputTags = [] }) {
const postId = postElement.dataset.id;
const { blog: { uuid } } = await timelineObject(postElement);
const { blog: { uuid }, blogName } = await timelineObject(postElement);

const { response: postData } = await apiFetch(`/v2/blog/${uuid}/posts/${postId}`);
const { tags = [] } = postData;
Expand All @@ -159,15 +160,20 @@ const addTagsToPost = async function ({ postElement, inputTags = [] }) {
tags.push(...tagsToAdd);

try {
const { response: { displayText } } = await apiFetch(`/v2/blog/${uuid}/posts/${postId}`, {
method: 'PUT',
body: {
...createEditRequestBody(postData),
tags: tags.join(',')
}
});

notify(displayText);
if (isNpfCompatible(postData)) {
const { response: { displayText } } = await apiFetch(`/v2/blog/${uuid}/posts/${postId}`, {
method: 'PUT',
body: {
...createEditRequestBody(postData),
tags: tags.join(',')
}
});

notify(displayText);
} else {
await megaEdit([postId], { mode: 'add', tags: tagsToAdd });
notify(`Edited legacy post on ${blogName}`);
}

const tagsElement = dom('div', { class: tagsClass });

Expand All @@ -181,7 +187,8 @@ const addTagsToPost = async function ({ postElement, inputTags = [] }) {
}

postElement.querySelector('footer').parentNode.prepend(tagsElement);
} catch ({ body }) {
} catch (error) {
const body = error.body ?? error;
notify(body.errors[0].detail);
}
};
Expand Down
9 changes: 9 additions & 0 deletions src/util/tumblr_helpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,15 @@ export const createEditRequestBody = postData => {
};
};

/**
* @param {object} postData - /posts/{post-id} GET request response JSON
* @returns {boolean} isNpfCompatible - Whether the post can be edited as NPF
*/
export const isNpfCompatible = postData => {
const { isBlocksPostFormat, shouldOpenInLegacy } = postData;
return isBlocksPostFormat || shouldOpenInLegacy === false;
};

export const navigate = location =>
inject(location => window.tumblr.navigate(location), [location]);

Expand Down