Skip to content

Commit 27a9c91

Browse files
authored
Reduce the dart:html API use in pkg/web_app. (dart-lang#9335)
1 parent 4c9f660 commit 27a9c91

3 files changed

Lines changed: 74 additions & 89 deletions

File tree

pkg/web_app/lib/src/_dom_helper.dart

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,12 @@ import '_dart_html_focusability.dart';
1313
import 'deferred/markdown.dart' deferred as md;
1414

1515
/// Displays a message via the modal window.
16-
Future<void> modalMessage(String title, Element content) async {
17-
await modalWindow(titleText: title, content: content, isQuestion: false);
16+
Future<void> modalMessage(String title, String message) async {
17+
await modalWindow(
18+
titleText: title,
19+
content: await markdown(message),
20+
isQuestion: false,
21+
);
1822
}
1923

2024
/// Display [content] to user (assumed it has a question format), and return
@@ -152,9 +156,6 @@ Element _buildDialog({
152156
DivElement()..classes.add('mdc-dialog__scrim'),
153157
];
154158

155-
/// Creates an [Element] with unformatted [text] content.
156-
Element text(String text) => DivElement()..text = text;
157-
158159
/// Creates an [Element] with Markdown-formatted content.
159160
Future<Element> markdown(String text) async {
160161
await md.loadLibrary();

pkg/web_app/lib/src/admin_pages.dart

Lines changed: 50 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void _initGenericForm() {
6767
final message =
6868
result['message']?.toString() ??
6969
'Success. The page will reload.';
70-
await modalMessage('Success', text(message));
70+
await modalMessage('Success', message);
7171

7272
window.location.reload();
7373
},
@@ -189,9 +189,8 @@ class _PkgAdminWidget {
189189
}
190190
updateButton.onClick.listen((event) async {
191191
await api_client.rpc<void>(
192-
confirmQuestion: await markdown(
193-
'Are you sure you want to update the publishing config?',
194-
),
192+
confirmQuestion:
193+
'Are you sure you want to update the publishing config?',
195194
fn: () async {
196195
await api_client.client.setAutomatedPublishing(
197196
pageData.pkgData!.package,
@@ -218,7 +217,7 @@ class _PkgAdminWidget {
218217
),
219218
);
220219
},
221-
successMessage: text('Config updated. The page will reload.'),
220+
successMessage: 'Config updated. The page will reload.',
222221
onSuccess: (_) => window.location.reload(),
223222
);
224223
});
@@ -237,10 +236,7 @@ class _PkgAdminWidget {
237236
Future<bool> _doInviteUploader() async {
238237
final email = _inviteUploaderInput!.value!.trim();
239238
if (email.isEmpty || !email.contains('@') || !email.contains('.')) {
240-
await modalMessage(
241-
'Input validation',
242-
text('Please specify a valid e-mail.'),
243-
);
239+
await modalMessage('Input validation', 'Please specify a valid e-mail.');
244240
return false;
245241
}
246242

@@ -251,7 +247,7 @@ class _PkgAdminWidget {
251247
InviteUploaderRequest(email: email),
252248
);
253249
},
254-
successMessage: await markdown('`$email` was invited.'),
250+
successMessage: '`$email` was invited.',
255251
onSuccess: (_) {
256252
_inviteUploaderInput!.value = '';
257253
},
@@ -261,38 +257,33 @@ class _PkgAdminWidget {
261257

262258
Future<void> _removeUploader(String email) async {
263259
await api_client.rpc<void>(
264-
confirmQuestion: await markdown(
265-
'Are you sure you want to remove uploader `$email` from this package?',
266-
),
260+
confirmQuestion:
261+
'Are you sure you want to remove uploader `$email` from this package?',
267262
fn: () async {
268263
await api_client.client.removeUploaderFromUI(
269264
pageData.pkgData!.package,
270265
RemoveUploaderRequest(email: email),
271266
);
272267
},
273-
successMessage: await markdown(
274-
'Uploader `$email` removed from this package. The page will reload.',
275-
),
268+
successMessage:
269+
'Uploader `$email` removed from this package. The page will reload.',
276270
onSuccess: (_) => window.location.reload(),
277271
);
278272
}
279273

280274
Future<void> _toggleDiscontinued() async {
281275
final oldValue = _discontinuedCheckbox!.defaultChecked ?? false;
282276
final newValue = await api_client.rpc<bool>(
283-
confirmQuestion: text(
284-
'Are you sure you want change the "discontinued" status of the package?',
285-
),
277+
confirmQuestion:
278+
'Are you sure you want change the "discontinued" status of the package?',
286279
fn: () async {
287280
final rs = await api_client.client.setPackageOptions(
288281
pageData.pkgData!.package,
289282
PkgOptions(isDiscontinued: !oldValue),
290283
);
291284
return rs.isDiscontinued;
292285
},
293-
successMessage: text(
294-
'"discontinued" status changed. The page will reload.',
295-
),
286+
successMessage: '"discontinued" status changed. The page will reload.',
296287
onSuccess: (_) => window.location.reload(),
297288
onError: (err) => null,
298289
);
@@ -303,37 +294,34 @@ class _PkgAdminWidget {
303294

304295
Future<void> _updateReplacedBy() async {
305296
await api_client.rpc<bool?>(
306-
confirmQuestion: text(
307-
'Are you sure you want change the "suggested replacement" field of the package?',
308-
),
297+
confirmQuestion:
298+
'Are you sure you want change the "suggested replacement" field of the package?',
309299
fn: () async {
310300
final rs = await api_client.client.setPackageOptions(
311301
pageData.pkgData!.package,
312302
PkgOptions(isDiscontinued: true, replacedBy: _replacedByInput?.value),
313303
);
314304
return rs.isDiscontinued;
315305
},
316-
successMessage: text(
317-
'"suggested replacement" field changed. The page will reload.',
318-
),
306+
successMessage:
307+
'"suggested replacement" field changed. The page will reload.',
319308
onSuccess: (_) => window.location.reload(),
320309
);
321310
}
322311

323312
Future<void> _toggleUnlisted() async {
324313
final oldValue = _unlistedCheckbox!.defaultChecked ?? false;
325314
final newValue = await api_client.rpc(
326-
confirmQuestion: text(
327-
'Are you sure you want change the "unlisted" status of the package?',
328-
),
315+
confirmQuestion:
316+
'Are you sure you want change the "unlisted" status of the package?',
329317
fn: () async {
330318
final rs = await api_client.client.setPackageOptions(
331319
pageData.pkgData!.package,
332320
PkgOptions(isUnlisted: !oldValue),
333321
);
334322
return rs.isUnlisted;
335323
},
336-
successMessage: text('"unlisted" status changed.'),
324+
successMessage: '"unlisted" status changed.',
337325
onError: (err) => null,
338326
);
339327
if (newValue == null) {
@@ -353,17 +341,16 @@ class _PkgAdminWidget {
353341
}
354342

355343
await api_client.rpc<void>(
356-
confirmQuestion: await markdown(
357-
'Are you sure you want to retract the package version `$version`?',
358-
),
344+
confirmQuestion:
345+
'Are you sure you want to retract the package version `$version`?',
359346
fn: () async {
360347
await api_client.client.setVersionOptions(
361348
pageData.pkgData!.package,
362349
version,
363350
VersionOptions(isRetracted: true),
364351
);
365352
},
366-
successMessage: text('Retraction completed. The page will reload.'),
353+
successMessage: 'Retraction completed. The page will reload.',
367354
onSuccess: (_) => window.location.reload(),
368355
);
369356
}
@@ -378,9 +365,8 @@ class _PkgAdminWidget {
378365
}
379366

380367
await api_client.rpc<void>(
381-
confirmQuestion: await markdown(
382-
'Are you sure you want to restore package version `$version`?',
383-
),
368+
confirmQuestion:
369+
'Are you sure you want to restore package version `$version`?',
384370
fn: () async {
385371
print('before setVersionOption');
386372
await api_client.client.setVersionOptions(
@@ -389,40 +375,35 @@ class _PkgAdminWidget {
389375
VersionOptions(isRetracted: false),
390376
);
391377
},
392-
successMessage: text('Restoring complete. The page will reload.'),
378+
successMessage: 'Restoring complete. The page will reload.',
393379
onSuccess: (_) => window.location.reload(),
394380
);
395381
}
396382

397383
Future<void> _validateVersionSelection() async {
398-
await modalMessage('Input validation', text('Please select a version.'));
384+
await modalMessage('Input validation', 'Please select a version.');
399385
}
400386

401387
Future<void> _setPublisher() async {
402388
final publisherId =
403389
materialDropdownSelected(_setPublisherInput)?.trim() ?? '';
404390
if (publisherId.isEmpty) {
405-
await modalMessage(
406-
'Input validation',
407-
text('Please specify a publisher.'),
408-
);
391+
await modalMessage('Input validation', 'Please specify a publisher.');
409392
return;
410393
}
411394

412395
await api_client.rpc<void>(
413-
confirmQuestion: await markdown(
414-
'Are you sure you want to transfer the package to publisher `$publisherId`?',
415-
),
396+
confirmQuestion:
397+
'Are you sure you want to transfer the package to publisher `$publisherId`?',
416398
fn: () async {
417399
final payload = PackagePublisherInfo(publisherId: publisherId);
418400
await api_client.client.setPackagePublisher(
419401
pageData.pkgData!.package,
420402
payload,
421403
);
422404
},
423-
successMessage: text(
424-
'Transfer completed. Caches and search index will update in the next 15-20 minutes. The page will reload.',
425-
),
405+
successMessage:
406+
'Transfer completed. Caches and search index will update in the next 15-20 minutes. The page will reload.',
426407
onSuccess: (_) => window.location.reload(),
427408
);
428409
}
@@ -449,22 +430,20 @@ class _CreatePublisherWidget {
449430
if (publisherId.isEmpty || !publisherIdPattern.hasMatch(publisherId)) {
450431
await modalMessage(
451432
'Input validation',
452-
text('Please use a domain name as publisher identifier.'),
433+
'Please use a domain name as publisher identifier.',
453434
);
454435
return;
455436
}
456437

457438
await api_client.rpc<void>(
458-
confirmQuestion: await markdown(
459-
'Are you sure you want to create publisher for `$publisherId`?',
460-
),
439+
confirmQuestion:
440+
'Are you sure you want to create publisher for `$publisherId`?',
461441
fn: () async {
462442
await api_client.client.createPublisher(publisherId);
463443
},
464-
successMessage: text(
465-
'Publisher created. You can now transfer packages to this publisher. '
466-
'The page will reload.',
467-
),
444+
successMessage:
445+
'Publisher created. You can now transfer packages to this publisher. '
446+
'The page will reload.',
468447
onSuccess: (_) {
469448
window.location.pathname = '/publishers/$publisherId/admin';
470449
},
@@ -519,7 +498,7 @@ class _PublisherAdminWidget {
519498
'addresses we will send a confirmation request.';
520499
}
521500
await api_client.rpc<void>(
522-
confirmQuestion: confirmQuestion == null ? null : text(confirmQuestion),
501+
confirmQuestion: confirmQuestion,
523502
fn: () async {
524503
final payload = UpdatePublisherRequest(
525504
description: _descriptionTextArea!.value,
@@ -531,7 +510,7 @@ class _PublisherAdminWidget {
531510
payload,
532511
);
533512
},
534-
successMessage: text('Publisher was updated. The page will reload.'),
513+
successMessage: 'Publisher was updated. The page will reload.',
535514
onSuccess: (_) => window.location.reload(),
536515
);
537516
}
@@ -549,10 +528,7 @@ class _PublisherAdminWidget {
549528
Future<bool> _inviteMember() async {
550529
final email = _inviteMemberInput!.value!.trim();
551530
if (email.isEmpty || !email.contains('@') || !email.contains('.')) {
552-
await modalMessage(
553-
'Input validation',
554-
text('Please specify a valid e-mail.'),
555-
);
531+
await modalMessage('Input validation', 'Please specify a valid e-mail.');
556532
return false;
557533
}
558534

@@ -563,7 +539,7 @@ class _PublisherAdminWidget {
563539
InviteMemberRequest(email: email),
564540
);
565541
},
566-
successMessage: await markdown('`$email` was invited.'),
542+
successMessage: '`$email` was invited.',
567543
onSuccess: (_) {
568544
_inviteMemberInput!.value = '';
569545
},
@@ -573,18 +549,16 @@ class _PublisherAdminWidget {
573549

574550
Future<void> _removeMember(String userId, String email) async {
575551
await api_client.rpc<void>(
576-
confirmQuestion: await markdown(
577-
'Are you sure you want to remove `$email` from this publisher?',
578-
),
552+
confirmQuestion:
553+
'Are you sure you want to remove `$email` from this publisher?',
579554
fn: () async {
580555
await api_client.client.removePublisherMember(
581556
pageData.publisher!.publisherId,
582557
userId,
583558
);
584559
},
585-
successMessage: await markdown(
586-
'`$email` removed from this publisher. The page will reload.',
587-
),
560+
successMessage:
561+
'`$email` removed from this publisher. The page will reload.',
588562
onSuccess: (_) => window.location.reload(),
589563
);
590564
}
@@ -613,30 +587,30 @@ class _ConsentWidget {
613587

614588
Future<void> _accept() async {
615589
await api_client.rpc(
616-
confirmQuestion: text('Are you sure you want to accept?'),
590+
confirmQuestion: 'Are you sure you want to accept?',
617591
fn: () async {
618592
final rs = await api_client.client.resolveConsent(
619593
pageData.consentId!,
620594
ConsentResult(granted: true),
621595
);
622596
return rs.granted;
623597
},
624-
successMessage: text('Consent accepted.'),
598+
successMessage: 'Consent accepted.',
625599
onSuccess: _updateButtons,
626600
);
627601
}
628602

629603
Future<void> _reject() async {
630604
await api_client.rpc(
631-
confirmQuestion: text('Are you sure you want to reject?'),
605+
confirmQuestion: 'Are you sure you want to reject?',
632606
fn: () async {
633607
final rs = await api_client.client.resolveConsent(
634608
pageData.consentId!,
635609
ConsentResult(granted: false),
636610
);
637611
return rs.granted;
638612
},
639-
successMessage: text('Consent rejected.'),
613+
successMessage: 'Consent rejected.',
640614
onSuccess: _updateButtons,
641615
);
642616
}

0 commit comments

Comments
 (0)