User:JJPMaster/warn.js

// <nowiki>
$(() => {
	var porty;
	$("#bodyContent").append(`
	<div id="warnDialog">
		What warning would you like to add? <br />
		<select name="warningName" id="warningName">
			<option>Select the warning</option>
			<option id="Test1">Test edits (level 1)</option>
			<option id="Test2">Test edits (level 2)</option>
			<option id="Blatantvandal">Blatant vandalism + block warning</option>
			<option id="Edit summary">Failing to use edit summaries</option>
			<option id="Image warning">Uploading files with inaccurate license information</option>
			<option id="NPOV note">Violations of neutral point of view</option>
			<option id="Please agf">Assuming bad faith</option>
			<option id="Please facts">Posting inaccurate or poorly sourced information</option>
			<option id="Spam">External link spamming</option>
			<option id="Use categories">Not using categories</option>
			<option id="Warn">Generic final warning</option>
	    </select><br />
	    <textarea id="warnMessage" name="warnMessage"></textarea><br />
	    <button type="button" id="warnUser">Warn user</button></div>
	`);
	(async () => {
		if (await waitForPreferences()) {
			for (var [i, j] of Object.entries(Luna.myPreferences.warn)) {
			    $("#warningName").append(`<option id="${i}">${j}</option>`);
			}
		}
		else {
		    console.error("Luna: Preferences never fully loaded.");
		}
	})();
	$("#warnDialog").hide();
	if (mw.config.get("wgRelevantUserName") !== null) {
		porty = mw.util.addPortletLink('luna-actions', '#', 'Warn user', 'luna-warn', 'Warn this user');
	}
	else return;
	$(porty).click(() => $("#warnDialog").dialog({ title: "Warn this user", width: "auto", height: "auto" } ));
	$("#warnUser").click(function() {
		var mySelect = document.getElementById("warningName");
		var critValue = mySelect.options[warningName.selectedIndex].id;
		
		if (critValue == "Select the warning") alert("You must select a warning template.");
		else {
			var warnMsg = document.getElementById("warnUser").value;
			var params = {
				action: 'edit',
				appendtext: `\n== {{subst:#time:F Y}} ==\n{{subst:${critValue}|1=${warnMsg}}}\n~~~~`,
				title: `User_talk:${mw.config.get("wgRelevantUserName")}`,
				summary: `Warning user with {{${critValue}}}`,
				tags: "Luna",
				format: 'json'
			},
			api = new mw.Api();
		
			api.postWithToken( 'csrf', params ).done( function ( data ) {
				console.log( data );
			} );
			mw.notify("Luna:\nThe warning has been added. Redirecting to the user's talk page...");
			setTimeout(() => {
				if (mw.config.get("wgPageName") == `User_talk:${mw.config.get("wgRelevantUserName")}`) window.location.reload();
				else window.location.href = window.location.href.replace(mw.config.get("wgPageName"), `User_talk:${mw.config.get("wgRelevantUserName")}`);
			}, 2000);
		}
	});
});
// </nowiki>