Skip to content

Commit

Permalink
Quick Tags: Work correctly on non-NPF posts (#969)
Browse files Browse the repository at this point in the history
Co-authored-by: April Sylph <[email protected]>
  • Loading branch information
marcustyphoon and AprilSylph authored Aug 20, 2023
1 parent 69750ca commit fcf63cc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 12 deletions.
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

0 comments on commit fcf63cc

Please sign in to comment.