Skip to content

Commit 75e88cb

Browse files
committed
Fix flag handling
1 parent 2960f68 commit 75e88cb

File tree

4 files changed

+47
-30
lines changed

4 files changed

+47
-30
lines changed

‎app/assets/javascripts/application.js

-30
Original file line numberDiff line numberDiff line change
@@ -37,36 +37,6 @@ document.addEventListener('DOMContentLoaded', async () => {
3737
dialog.classList.toggle('is-active');
3838
});
3939

40-
QPixel.DOM.addSelectorListener('click', '.flag-resolve', async (ev) => {
41-
ev.preventDefault();
42-
const tgt = /** @type {HTMLElement} */(ev.target);
43-
const id = tgt.dataset.flagId;
44-
const data = {
45-
result: tgt.dataset.result,
46-
message: tgt.parentNode.parentNode.querySelector('.flag-resolve-comment').value
47-
};
48-
49-
const req = await fetch(`/mod/flags/${id}/resolve`, {
50-
method: 'POST',
51-
body: JSON.stringify(data),
52-
credentials: 'include',
53-
headers: { 'X-CSRF-Token': QPixel.csrfToken() }
54-
});
55-
if (req.status === 200) {
56-
const res = await req.json();
57-
if (res.status === 'success') {
58-
const flagContainer = /** @type {HTMLElement} */(tgt.parentNode.parentNode.parentNode);
59-
QPixel.DOM.fadeOut(flagContainer, 200);
60-
}
61-
else {
62-
QPixel.createNotification('danger', `<strong>Failed:</strong> ${res.message}`);
63-
}
64-
}
65-
else {
66-
QPixel.createNotification('danger', `<strong>Failed:</strong> Unexpected status (${req.status})`);
67-
}
68-
});
69-
7040
if (document.cookie.indexOf('dismiss_fvn') === -1) {
7141
QPixel.DOM.addSelectorListener('click', '#fvn-dismiss', (_ev) => {
7242
document.cookie = 'dismiss_fvn=true; path=/; expires=Fri, 31 Dec 9999 23:59:59 GMT';

‎app/assets/javascripts/moderator.js

+25
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,29 @@ $(() => {
99
$(this).remove();
1010
});
1111
});
12+
13+
QPixel.DOM.addSelectorListener('click', '.flag-resolve', async (ev) => {
14+
ev.preventDefault();
15+
const tgt = /** @type {HTMLElement} */(ev.target);
16+
const id = tgt.dataset.flagId;
17+
const data = {
18+
result: tgt.dataset.result,
19+
message: tgt.parentNode.parentNode.querySelector('.flag-resolve-comment').value
20+
};
21+
22+
const req = await QPixel.jsonPost(`/mod/flags/${id}/resolve`, data);
23+
if (req.status === 200) {
24+
const res = await req.json();
25+
if (res.status === 'success') {
26+
const flagContainer = /** @type {HTMLElement} */(tgt.parentNode.parentNode.parentNode);
27+
QPixel.DOM.fadeOut(flagContainer, 200);
28+
}
29+
else {
30+
QPixel.createNotification('danger', `<strong>Failed:</strong> ${res.message}`);
31+
}
32+
}
33+
else {
34+
QPixel.createNotification('danger', `<strong>Failed:</strong> Unexpected status (${req.status})`);
35+
}
36+
});
1237
});

‎app/assets/javascripts/qpixel_api.js

+21
Original file line numberDiff line numberDiff line change
@@ -493,5 +493,26 @@ window.QPixel = {
493493
splatIdx += 1;
494494
} while (searchIdx < posIdx);
495495
return [currentSequence, posInSeq];
496+
},
497+
498+
/**
499+
* Send a POST request with JSON data, pre-authorized with QPixel credentials for the signed in user.
500+
* @param {string} uri The URI to which to send the request.
501+
* @param {any} data An object containing data to send as the request body. Must be acceptable by JSON.stringify.
502+
* @param {RequestInit?} options An optional RequestInit object. Options specified here will override the defaults
503+
* provided by this method.
504+
* @returns {Promise<Response>} The Response promise returned from fetch().
505+
*/
506+
jsonPost: async (uri, data, options) => {
507+
const defaultOptions = {
508+
method: 'POST',
509+
headers: {
510+
'X-CSRF-Token': QPixel.csrfToken(),
511+
'Content-Type': 'application/json'
512+
},
513+
credentials: 'include',
514+
body: JSON.stringify(data)
515+
};
516+
return fetch(uri, Object.assign(defaultOptions, options));
496517
}
497518
};

‎global.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ interface QPixel {
121121
setPreference?: (name: string, value: unknown, community?: boolean) => Promise<void>;
122122
user?: () => Promise<User>;
123123
validatePost?: (postText: string) => [boolean, PostValidatorMessage[]];
124+
jsonPost?: (uri: string, data: any, options?: RequestInit) => Promise<Response>;
124125

125126
// qpixel_dom
126127
DOM?: QPixelDOM;

0 commit comments

Comments
 (0)