Skip to content

Commit

Permalink
refactor(quote submit modal): use new modal system
Browse files Browse the repository at this point in the history
  • Loading branch information
Miodec committed Mar 17, 2024
1 parent 5e69c5d commit 9ca32d3
Show file tree
Hide file tree
Showing 5 changed files with 144 additions and 184 deletions.
27 changes: 11 additions & 16 deletions frontend/src/html/popups.html
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,9 @@
</div>
</div>
</div>
<div id="quoteSubmitPopupWrapper" class="popupWrapper hidden">
<div id="quoteSubmitPopup" mode="">
<div class="title">Submit a Quote</div>
<dialog id="quoteSubmitModal" class="modalWrapper hidden">
<div class="modal">
<div class="title">Submit a quote</div>
<ul>
<li>
Do not include content that contains any libelous or otherwise unlawful,
Expand All @@ -677,24 +677,19 @@
</b>
</li>
</ul>
<label>Quote</label>
<label>quote</label>
<div style="position: relative">
<textarea
id="submitQuoteText"
class=""
type="text"
autocomplete="off"
></textarea>
<textarea class="newQuoteText" type="text" autocomplete="off"></textarea>
<div class="characterCount">-</div>
</div>
<label>Source</label>
<input id="submitQuoteSource" class="" type="text" autocomplete="off" />
<label>Language</label>
<select name="language" id="submitQuoteLanguage"></select>
<label>source</label>
<input class="newQuoteSource" type="text" autocomplete="off" />
<label>language</label>
<select name="language" class="newQuoteLanguage"></select>
<div class="g-recaptcha"></div>
<div id="submitQuoteButton" class="button">Submit</div>
<button>submit</button>
</div>
</div>
</dialog>
<div id="apeKeysPopupWrapper" class="hidden popupWrapper">
<div id="apeKeysPopup" mode="">
<div class="top">
Expand Down
59 changes: 24 additions & 35 deletions frontend/src/styles/popups.scss
Original file line number Diff line number Diff line change
Expand Up @@ -910,43 +910,32 @@
}
}

#quoteSubmitPopup {
background: var(--bg-color);
border-radius: var(--roundness);
padding: 2rem;
display: grid;
gap: 1rem;
width: 1000px;
grid-template-rows: auto auto auto auto auto auto auto auto auto;
height: 100%;
max-height: 40rem;
overflow-y: scroll;
#quoteSubmitModal {
.modal {
max-width: 1000px;

label {
color: var(--sub-color);
margin-bottom: -1rem;
}
label {
color: var(--sub-color);
margin-bottom: -0.5em;
}

.title {
font-size: 1.5rem;
color: var(--sub-color);
}
textarea {
resize: vertical;
width: 100%;
padding: 10px;
line-height: 1.2rem;
min-height: 5rem;
}
.characterCount {
position: absolute;
top: -1.25rem;
right: 0.25rem;
color: var(--sub-color);
-webkit-user-select: none;
user-select: none;
&.red {
color: var(--error-color);
textarea {
resize: vertical;
width: 100%;
padding: 10px;
line-height: 1.2rem;
min-height: 5rem;
}
.characterCount {
position: absolute;
top: -1.75rem;
right: 0.25rem;
color: var(--sub-color);
-webkit-user-select: none;
user-select: none;
&.red {
color: var(--error-color);
}
}
}
}
Expand Down
105 changes: 105 additions & 0 deletions frontend/src/ts/modals/quote-submit.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import Ape from "../ape";
import * as Loader from "../elements/loader";
import * as Notifications from "../elements/notifications";
import * as CaptchaController from "../controllers/captcha-controller";
import * as Misc from "../utils/misc";
import Config from "../config";
import SlimSelect from "slim-select";
import AnimatedModal, { ShowOptions } from "../utils/animated-modal";

let dropdownReady = false;
async function initDropdown(): Promise<void> {
if (dropdownReady) return;
const languageGroups = await Misc.getLanguageGroups();
for (const group of languageGroups) {
if (group.name === "swiss_german") continue;
$("#quoteSubmitModal .newQuoteLanguage").append(
`<option value="${group.name}">${group.name.replace(/_/g, " ")}</option>`
);
}
dropdownReady = true;
}

let select: SlimSelect | undefined = undefined;

async function submitQuote(): Promise<void> {
const text = $("#quoteSubmitModal .newQuoteText").val() as string;
const source = $("#quoteSubmitModal .newQuoteSource").val() as string;
const language = $("#quoteSubmitModal .newQuoteLanguage").val() as string;
const captcha = CaptchaController.getResponse("submitQuote");

if (!text || !source || !language) {
return Notifications.add("Please fill in all fields", 0);
}

Loader.show();
const response = await Ape.quotes.submit(text, source, language, captcha);
Loader.hide();

if (response.status !== 200) {
return Notifications.add("Failed to submit quote: " + response.message, -1);
}

Notifications.add("Quote submitted.", 1);
$("#quoteSubmitModal .newQuoteText").val("");
$("#quoteSubmitModal .newQuoteSource").val("");
$("#quoteSubmitModal .characterCount").removeClass("red");
$("#quoteSubmitModal .characterCount").text("-");
CaptchaController.reset("submitQuote");
}

export async function show(showOptions: ShowOptions): Promise<void> {
void modal.show({
...showOptions,
focusFirstInput: true,
beforeAnimation: async () => {
CaptchaController.render(
document.querySelector("#quoteSubmitModal .g-recaptcha") as HTMLElement,
"submitQuote"
);
await initDropdown();

select = new SlimSelect({
select: "#quoteSubmitModal .newQuoteLanguage",
});

$("#quoteSubmitModal .newQuoteLanguage").val(
Misc.removeLanguageSize(Config.language)
);
$("#quoteSubmitModal .newQuoteLanguage").trigger("change");
$("#quoteSubmitModal input").val("");
},
});
}

function hide(clearModalChain: boolean): void {
void modal.hide({
clearModalChain,
afterAnimation: async () => {
CaptchaController.reset("submitQuote");
select?.destroy();
select = undefined;
},
});
}

function setup(modalEl: HTMLElement): void {
modalEl.querySelector("textarea")?.addEventListener("input", (e) => {
const len = (e.target as HTMLTextAreaElement).value.length;
$("#quoteSubmitModal .characterCount").text(len);
if (len < 60) {
$("#quoteSubmitModal .characterCount").addClass("red");
} else {
$("#quoteSubmitModal .characterCount").removeClass("red");
}
});
modalEl.querySelector("button")?.addEventListener("click", () => {
void submitQuote();
hide(true);
});
}

const modal = new AnimatedModal({
dialogId: "quoteSubmitModal",
setup,
});
6 changes: 4 additions & 2 deletions frontend/src/ts/popups/quote-search-popup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import Config, * as UpdateConfig from "../config";
import * as DB from "../db";
import * as ManualRestart from "../test/manual-restart-tracker";
import * as Notifications from "../elements/notifications";
import * as QuoteSubmitPopup from "./quote-submit-popup";
import * as QuoteSubmitPopup from "../modals/quote-submit";
import * as QuoteApprovePopup from "./quote-approve-popup";
import * as QuoteReportPopup from "./quote-report-popup";
import {
Expand Down Expand Up @@ -374,7 +374,9 @@ $("#popups").on(
return;
}
hide();
void QuoteSubmitPopup.show(true);
void QuoteSubmitPopup.show({
// modalChain: modal
});
}
);

Expand Down
131 changes: 0 additions & 131 deletions frontend/src/ts/popups/quote-submit-popup.ts

This file was deleted.

0 comments on commit 9ca32d3

Please sign in to comment.