Skip to content

Commit c828ca7

Browse files
committed
misc refactors
1 parent 74f7426 commit c828ca7

File tree

1 file changed

+23
-24
lines changed

1 file changed

+23
-24
lines changed

src/features/mass_privater/index.js

Lines changed: 23 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ const createNowString = () => {
2525
return `${YYYY}-${MM}-${DD}T${hh}:${mm}`;
2626
};
2727

28-
const showInitialPrompt = async (makePrivate) => {
29-
const initialForm = dom('form', { id: getPostsFormId }, { submit: event => confirmInitialPrompt(event, makePrivate).catch(showErrorModal) }, [
28+
const showInitialPrompt = async (mode) => {
29+
const initialForm = dom('form', { id: getPostsFormId }, { submit: event => confirmInitialPrompt(event, mode).catch(showErrorModal) }, [
3030
dom('label', null, null, [
3131
'Posts on blog:',
3232
dom('select', { name: 'blog', required: true }, null, userBlogs.map(createBlogOption)),
@@ -48,7 +48,7 @@ const showInitialPrompt = async (makePrivate) => {
4848
}
4949

5050
showModal({
51-
title: `Select posts to make ${makePrivate ? 'private' : 'public'}`,
51+
title: `Select posts to make ${mode}`,
5252
message: [initialForm],
5353
buttons: [
5454
modalCancelButton,
@@ -57,7 +57,7 @@ const showInitialPrompt = async (makePrivate) => {
5757
});
5858
};
5959

60-
const confirmInitialPrompt = async (event, makePrivate) => {
60+
const confirmInitialPrompt = async (event, mode) => {
6161
event.preventDefault();
6262

6363
const { submitter } = event;
@@ -76,7 +76,7 @@ const confirmInitialPrompt = async (event, makePrivate) => {
7676
.map(tag => tag.trim().toLowerCase())
7777
.filter(Boolean);
7878

79-
if (makePrivate && tags.length) {
79+
if (mode === 'private' && tags.length) {
8080
const getTagCount = async tag => {
8181
const { response: { totalPosts } } = await apiFetch(`/v2/blog/${uuid}/posts`, { method: 'GET', queryParams: { tag } });
8282
return totalPosts ?? 0;
@@ -99,20 +99,20 @@ const confirmInitialPrompt = async (event, makePrivate) => {
9999

100100
const message = tags.length
101101
? [
102-
`Every ${makePrivate ? 'published' : 'private'} post on `,
102+
`Every ${mode === 'private' ? 'published' : 'private'} post on `,
103103
createBlogSpan(name),
104104
' from before ',
105105
beforeElement,
106106
' tagged ',
107107
...elementsAsList(tags.map(createTagSpan), 'or'),
108-
` will be set to ${makePrivate ? 'private' : 'public'}.`,
108+
` will be set to ${mode}.`,
109109
]
110110
: [
111-
`Every ${makePrivate ? 'published' : 'private'} post on `,
111+
`Every ${mode === 'private' ? 'published' : 'private'} post on `,
112112
createBlogSpan(name),
113113
' from before ',
114114
beforeElement,
115-
` will be set to ${makePrivate ? 'private' : 'public'}.`,
115+
` will be set to ${mode}.`,
116116
];
117117

118118
showModal({
@@ -123,8 +123,8 @@ const confirmInitialPrompt = async (event, makePrivate) => {
123123
dom(
124124
'button',
125125
{ class: 'red' },
126-
{ click: () => editPosts({ makePrivate, uuid, name, tags, before }).catch(showErrorModal) },
127-
[makePrivate ? 'Private them!' : 'Unprivate them!'],
126+
{ click: () => editPosts({ mode, uuid, name, tags, before }).catch(showErrorModal) },
127+
[mode === 'private' ? 'Private them!' : 'Unprivate them!'],
128128
),
129129
],
130130
});
@@ -157,7 +157,7 @@ const showPostsNotFound = ({ name }) =>
157157
const dateFormat = new Intl.DateTimeFormat(document.documentElement.lang, { dateStyle: 'medium' });
158158
const timeFormat = new Intl.DateTimeFormat(document.documentElement.lang, { timeStyle: 'short' });
159159

160-
const editPosts = async ({ makePrivate, uuid, name, tags, before }) => {
160+
const editPosts = async ({ mode, uuid, name, tags, before }) => {
161161
const gatherStatus = dom('span', null, null, ['Gathering posts...']);
162162
const editStatus = dom('span');
163163

@@ -181,7 +181,7 @@ const editPosts = async ({ makePrivate, uuid, name, tags, before }) => {
181181
]);
182182

183183
showModal({
184-
title: `Making posts ${makePrivate ? 'private' : 'public'}...`,
184+
title: `Making posts ${mode}...`,
185185
message: [
186186
dom('small', null, null, ['Do not navigate away from this page.']),
187187
'\n\n',
@@ -200,7 +200,7 @@ const editPosts = async ({ makePrivate, uuid, name, tags, before }) => {
200200
apiFetch(resource).then(({ response }) => {
201201
response.posts
202202
.filter(({ canEdit }) => canEdit === true)
203-
.filter(({ state }) => state === (makePrivate ? 'published' : 'private'))
203+
.filter(({ state }) => state === (mode === 'private' ? 'published' : 'private'))
204204
.filter(({ timestamp }) => timestamp < before)
205205
.filter(postData =>
206206
tags.length
@@ -220,7 +220,7 @@ const editPosts = async ({ makePrivate, uuid, name, tags, before }) => {
220220
}
221221
};
222222

223-
if (makePrivate && tags.length) {
223+
if (mode === 'private' && tags.length) {
224224
for (const tag of tags) {
225225
await collect(`/v2/blog/${uuid}/posts?${$.param({ tag, before, limit: 50 })}`);
226226
}
@@ -233,18 +233,16 @@ const editPosts = async ({ makePrivate, uuid, name, tags, before }) => {
233233
return;
234234
}
235235

236-
const filteredPostIds = [...filteredPostsMap.keys()];
237-
const filteredPosts = [...filteredPostsMap.values()];
238-
239236
let successCount = 0;
240237
let failCount = 0;
241238

242-
if (makePrivate) {
239+
if (mode === 'private') {
240+
editStatus.textContent = '\nPrivating posts...';
241+
242+
const filteredPostIds = [...filteredPostsMap.keys()];
243243
while (filteredPostIds.length !== 0) {
244244
const postIds = filteredPostIds.splice(0, 100);
245245

246-
if (editStatus.textContent === '') editStatus.textContent = '\nPrivating posts...';
247-
248246
await Promise.all([
249247
megaEdit(postIds, { mode: 'private' }).then(() => {
250248
successCount += postIds.length;
@@ -259,6 +257,7 @@ const editPosts = async ({ makePrivate, uuid, name, tags, before }) => {
259257
} else {
260258
editStatus.textContent = '\nUnprivating posts...';
261259

260+
const filteredPosts = [...filteredPostsMap.values()];
262261
const editablePosts = [];
263262
filteredPosts.forEach(postData => isNpfCompatible(postData)
264263
? editablePosts.push(postData)
@@ -292,7 +291,7 @@ const editPosts = async ({ makePrivate, uuid, name, tags, before }) => {
292291
showModal({
293292
title: 'All done!',
294293
message: [
295-
`${makePrivate ? 'Privated' : 'Unprivated'} ${successCount} posts${failCount ? ` (failed: ${failCount})` : ''}.\n`,
294+
`${mode === 'private' ? 'Privated' : 'Unprivated'} ${successCount} posts${failCount ? ` (failed: ${failCount})` : ''}.\n`,
296295
'Refresh the page to see the result.',
297296
failedStatus,
298297
],
@@ -311,12 +310,12 @@ const sidebarOptions = {
311310
rows: [
312311
{
313312
label: 'Private posts',
314-
onclick: () => showInitialPrompt(true),
313+
onclick: () => showInitialPrompt('private'),
315314
carrot: true,
316315
},
317316
{
318317
label: 'Unprivate posts',
319-
onclick: () => showInitialPrompt(false),
318+
onclick: () => showInitialPrompt('public'),
320319
carrot: true,
321320
},
322321
],

0 commit comments

Comments
 (0)