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

Add ability to manually discard post drafts #1460

Draft
wants to merge 3 commits into
base: develop
Choose a base branch
from
Draft
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
60 changes: 39 additions & 21 deletions app/assets/javascripts/posts.js
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,33 @@ $(() => {
}
};

/**
* @typedef {{ removeNotice?: boolean }} DeleteDraftOptions
*
* Attempts to remove a post draft
* @param {DeleteDraftOptions} [options]
* @returns {Promise<boolean>}
*/
const deleteDraft = async (options = {}) => {
const res = await fetch('/posts/delete-draft', {
method: 'POST',
credentials: 'include',
headers: {
'X-CSRF-Token': QPixel.csrfToken(),
'Content-Type': 'application/json'
},
body: JSON.stringify({ path: location.pathname })
});

const success = res.status === 200;

if (success && options.removeNotice) {
$('.js-draft-notice').remove()
}

return success;
}

/**
* Extracts draft info from a given target
* @param {EventTarget} target post input field or "save draft" button
Expand Down Expand Up @@ -179,6 +206,12 @@ $(() => {
return { draft, field: $bodyField };
};

$('.js-delete-draft').on('click', async () => {
await deleteDraft({
removeNotice: true
});
});

$('.js-save-draft').on('click', async (ev) => {
const { draft, field } = parseDraft(ev.target);
await saveDraft(draft, field, true);
Expand Down Expand Up @@ -295,16 +328,9 @@ $(() => {

// Draft handling
if (!draftDeleted) {
const resp = await fetch('/posts/delete-draft', {
method: 'POST',
credentials: 'include',
headers: {
'X-CSRF-Token': QPixel.csrfToken(),
'Content-Type': 'application/json'
},
body: JSON.stringify({ path: location.pathname })
});
if (resp.status === 200) {
const status = await deleteDraft();

if (status) {
$tgt.attr('data-draft-deleted', 'true');

if (isValidated) {
Expand Down Expand Up @@ -360,8 +386,8 @@ $(() => {
}
});

$('.js-draft-loaded').each((i, e) => {
$(e).parents('.widget').after(`<div class="notice is-info has-font-size-caption">
$('.js-draft-loaded').each((_i, e) => {
$(e).parents('.widget').after(`<div class="notice is-info has-font-size-caption js-draft-notice">
<i class="fas fa-exclamation-circle"></i> <strong>Draft loaded.</strong>
You had edited this before but haven't saved it. We loaded the edits for you.
</div>`);
Expand Down Expand Up @@ -451,15 +477,7 @@ $(() => {
return;
}

await fetch('/posts/delete-draft', {
method: 'POST',
credentials: 'include',
headers: {
'X-CSRF-Token': QPixel.csrfToken(),
'Content-Type': 'application/json'
},
body: JSON.stringify({ path: location.pathname })
});
await deleteDraft();

location.href = $btn.attr('href');
});
Expand Down
Loading