Skip to content

Commit 64cc591

Browse files
committed
refactor: add cleanup function to animated modals
this fixes issues with drop downs causing performance / duplication issues
1 parent 3a06531 commit 64cc591

File tree

6 files changed

+48
-33
lines changed

6 files changed

+48
-33
lines changed

frontend/src/ts/modals/quote-report.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,11 +60,6 @@ export async function show(
6060
async function hide(clearChain = false): Promise<void> {
6161
void modal.hide({
6262
clearModalChain: clearChain,
63-
afterAnimation: async () => {
64-
CaptchaController.reset("quoteReportModal");
65-
state.reasonSelect?.destroy();
66-
state.reasonSelect = undefined;
67-
},
6863
});
6964
}
7065

@@ -131,7 +126,14 @@ async function setup(modalEl: HTMLElement): Promise<void> {
131126
});
132127
}
133128

129+
async function cleanup(): Promise<void> {
130+
CaptchaController.reset("quoteReportModal");
131+
state.reasonSelect?.destroy();
132+
state.reasonSelect = undefined;
133+
}
134+
134135
const modal = new AnimatedModal({
135136
dialogId: "quoteReportModal",
136137
setup,
138+
cleanup,
137139
});

frontend/src/ts/modals/quote-search.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,6 @@ export async function show(showOptions?: ShowOptions): Promise<void> {
312312
function hide(clearChain = false): void {
313313
void modal.hide({
314314
clearModalChain: clearChain,
315-
afterAnimation: async () => {
316-
lengthSelect?.destroy();
317-
lengthSelect = undefined;
318-
},
319315
});
320316
}
321317

@@ -456,7 +452,13 @@ async function setup(modalEl: HTMLElement): Promise<void> {
456452
});
457453
}
458454

455+
async function cleanup(): Promise<void> {
456+
lengthSelect?.destroy();
457+
lengthSelect = undefined;
458+
}
459+
459460
const modal = new AnimatedModal({
460461
dialogId: "quoteSearchModal",
461462
setup,
463+
cleanup,
462464
});

frontend/src/ts/modals/quote-submit.ts

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,6 @@ export async function show(showOptions: ShowOptions): Promise<void> {
8282
function hide(clearModalChain: boolean): void {
8383
void modal.hide({
8484
clearModalChain,
85-
afterAnimation: async () => {
86-
CaptchaController.reset("submitQuote");
87-
select?.destroy();
88-
select = undefined;
89-
},
9085
});
9186
}
9287

@@ -97,7 +92,14 @@ async function setup(modalEl: HTMLElement): Promise<void> {
9792
});
9893
}
9994

95+
async function cleanup(): Promise<void> {
96+
CaptchaController.reset("submitQuote");
97+
select?.destroy();
98+
select = undefined;
99+
}
100+
100101
const modal = new AnimatedModal({
101102
dialogId: "quoteSubmitModal",
102103
setup,
104+
cleanup,
103105
});

frontend/src/ts/modals/user-report.ts

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -64,13 +64,7 @@ export async function show(options: ShowOptions): Promise<void> {
6464
}
6565

6666
async function hide(): Promise<void> {
67-
void modal.hide({
68-
afterAnimation: async () => {
69-
select?.destroy();
70-
select = undefined;
71-
CaptchaController.reset("userReportModal");
72-
},
73-
});
67+
void modal.hide();
7468
}
7569

7670
async function submitReport(): Promise<void> {
@@ -140,4 +134,9 @@ const modal = new AnimatedModal({
140134
void submitReport();
141135
});
142136
},
137+
cleanup: async (): Promise<void> => {
138+
select?.destroy();
139+
select = undefined;
140+
CaptchaController.reset("userReportModal");
141+
},
143142
});

frontend/src/ts/modals/word-filter.ts

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +185,6 @@ export async function show(showOptions?: ShowOptions): Promise<void> {
185185
function hide(hideOptions?: HideOptions<OutgoingData>): void {
186186
void modal.hide({
187187
...hideOptions,
188-
afterAnimation: async () => {
189-
languageSelect?.destroy();
190-
layoutSelect?.destroy();
191-
presetSelect?.destroy();
192-
languageSelect = undefined;
193-
layoutSelect = undefined;
194-
presetSelect = undefined;
195-
},
196188
});
197189
}
198190

@@ -326,6 +318,15 @@ async function setup(): Promise<void> {
326318
});
327319
}
328320

321+
async function cleanup(): Promise<void> {
322+
languageSelect?.destroy();
323+
layoutSelect?.destroy();
324+
presetSelect?.destroy();
325+
languageSelect = undefined;
326+
layoutSelect = undefined;
327+
presetSelect = undefined;
328+
}
329+
329330
type OutgoingData = {
330331
text: string;
331332
set: boolean;
@@ -334,4 +335,5 @@ type OutgoingData = {
334335
const modal = new AnimatedModal<unknown, OutgoingData>({
335336
dialogId: "wordFilterModal",
336337
setup,
338+
cleanup,
337339
});

frontend/src/ts/utils/animated-modal.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ type ConstructorParams<T> = {
4848
customEscapeHandler?: (e: KeyboardEvent) => void;
4949
customWrapperClickHandler?: (e: MouseEvent) => void;
5050
setup?: (modal: HTMLElement) => Promise<void>;
51+
cleanup?: () => Promise<void>;
5152
};
5253

5354
const DEFAULT_ANIMATION_DURATION = 125;
@@ -73,6 +74,7 @@ export default class AnimatedModal<
7374
private customEscapeHandler: ((e: KeyboardEvent) => void) | undefined;
7475
private customWrapperClickHandler: ((e: MouseEvent) => void) | undefined;
7576
private setup: ((modal: HTMLElement) => Promise<void>) | undefined;
77+
private cleanup: (() => Promise<void>) | undefined;
7678

7779
constructor(constructorParams: ConstructorParams<IncomingModalChainData>) {
7880
if (constructorParams.dialogId.startsWith("#")) {
@@ -123,6 +125,7 @@ export default class AnimatedModal<
123125
this.customWrapperClickHandler =
124126
constructorParams?.customWrapperClickHandler;
125127
this.setup = constructorParams?.setup;
128+
this.cleanup = constructorParams?.cleanup;
126129

127130
Skeleton.save(this.dialogId);
128131
}
@@ -132,24 +135,26 @@ export default class AnimatedModal<
132135
}
133136

134137
async runSetup(): Promise<void> {
135-
this.wrapperEl.addEventListener("keydown", (e) => {
138+
this.wrapperEl.addEventListener("keydown", async (e) => {
136139
if (e.key === "Escape" && isPopupVisible(this.dialogId)) {
137140
e.preventDefault();
138141
e.stopPropagation();
139142
if (this.customEscapeHandler !== undefined) {
140143
this.customEscapeHandler(e);
144+
void this.cleanup?.();
141145
} else {
142-
void this.hide();
146+
await this.hide();
143147
}
144148
}
145149
});
146150

147-
this.wrapperEl.addEventListener("mousedown", (e) => {
151+
this.wrapperEl.addEventListener("mousedown", async (e) => {
148152
if (e.target === this.wrapperEl) {
149153
if (this.customWrapperClickHandler !== undefined) {
150154
this.customWrapperClickHandler(e);
155+
void this.cleanup?.();
151156
} else {
152-
void this.hide();
157+
await this.hide();
153158
}
154159
}
155160
});
@@ -392,6 +397,7 @@ export default class AnimatedModal<
392397
Skeleton.remove(this.dialogId);
393398
this.open = false;
394399
await options?.afterAnimation?.(this.modalEl);
400+
void this.cleanup?.();
395401

396402
if (
397403
this.previousModalInChain !== undefined &&
@@ -428,6 +434,7 @@ export default class AnimatedModal<
428434
Skeleton.remove(this.dialogId);
429435
this.open = false;
430436
await options?.afterAnimation?.(this.modalEl);
437+
void this.cleanup?.();
431438

432439
if (
433440
this.previousModalInChain !== undefined &&
@@ -453,6 +460,7 @@ export default class AnimatedModal<
453460
destroy(): void {
454461
this.wrapperEl.close();
455462
this.wrapperEl.classList.add("hidden");
463+
void this.cleanup?.();
456464
Skeleton.remove(this.dialogId);
457465
this.open = false;
458466
}

0 commit comments

Comments
 (0)