import { h, render } from 'preact';
import PropTypes from 'prop-types';
import { request } from '../utilities/http';
import { ButtonNew as Button } from '@crayons';
import RemoveIcon from '@images/x.svg';
async function confirmAdminUnpublishPost(id, username, slug) {
try {
const response = await request(`/articles/${id}/admin_unpublish`, {
method: 'PATCH',
body: JSON.stringify({ id, username, slug }),
credentials: 'same-origin',
});
const outcome = await response.json();
/* eslint-disable no-restricted-globals */
if (outcome.message == 'success') {
window.top.location.assign(`${window.location.origin}${outcome.path}`);
} else {
top.addSnackbarItem({
message: `Error: ${outcome.message}`,
addCloseButton: true,
});
}
} catch (error) {
top.addSnackbarItem({
message: `Error: ${error}`,
addCloseButton: true,
});
}
toggleUnpublishPostModal();
}
/**
* Shows or hides the flag user modal.
*/
export function toggleUnpublishPostModal() {
const modalContainer = top.document.getElementsByClassName(
'unpublish-post-modal-container',
)[0];
modalContainer.classList.toggle('hidden');
if (!modalContainer.classList.contains('hidden')) {
top.window.scrollTo(0, 0);
top.document.body.style.height = '100vh';
top.document.body.style.overflowY = 'hidden';
} else {
top.document.body.style.height = 'inherit';
top.document.body.style.overflowY = 'inherit';
}
}
/**
* Initializes the Unpublish Post modal for the given article ID, author username and article slug.
*
* @param {number} articleId
* @param {string} authorUsername
* @param {string} articleSlug
*/
export function initializeUnpublishPostModal(
articleId,
authorName,
authorUsername,
articleSlug,
) {
// Check whether context is ModCenter or Friday-Night-Mode
const modContainer = document.getElementById('mod-container');
if (!modContainer) {
return;
}
render(
Once unpublished, this post will become invisible to the public and only accessible to {authorName}.
They can still re-publish the post if they are not suspended.