All files / mobile.startup/amcOutreach amcOutreachDrawer.js

100% Statements 12/12
100% Branches 2/2
100% Functions 3/3
100% Lines 12/12

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97  1x 1x 1x 1x   1x 1x                                                                     3x                                                                   1x 1x                       1x 1x         1x  
const
	Drawer = require( '../Drawer' ),
	Anchor = require( '../Anchor' ),
	util = require( '../util' ),
	AmcEnableForm = require( './AmcEnableForm' ),
	// These constants should be kept in sync with SpecialMobileOptions.php
	AMC_ENABLE_FIELD_NAME = 'enableAMC',
	AMC_ENABLE_FIELD_VALUE = '1';
 
/**
 * Callback intended to allow the client run extra logic (e.g. show a modal)
 * after the drawer is dismissed.
 *
 * @callback onBeforeHide
 */
 
/**
 * @param {string} action Used by the drawer to notify promoCampaign when the
 * action has become 'ineligible' (e.g. after enabling or dismissing the drawer).
 * @param {PromoCampaign} promoCampaign
 * @param {mw.message} mwMessage Used for i18n
 * @param {mw.util} mwUtil Used to determine POST url for the enable form
 * @param {Toast} toast Displays success message after user submits enable form
 * @param {string} csrfToken
 * @param {onBeforeHide} onBeforeHide
 * @param {string} returnToTitle Title to redirect to after user enables
 * AMC
 * @param {string} [returnToQuery] Optional query params to add to redirected
 * URL after user enables AMC
 * @return {Drawer} Returns the drawer that is shown
 */
function amcOutreachDrawer(
	action,
	promoCampaign,
	mwMessage,
	mwUtil,
	toast,
	csrfToken,
	onBeforeHide,
	returnToTitle,
	returnToQuery
) {
	return new Drawer( {
		className: 'amc-outreach-drawer',
		children: [
			util.parseHTML( '<div>' ).addClass( 'amc-outreach-image' ),
			util.parseHTML( '<p>' ).append(
				util.parseHTML( '<strong>' ).text(
					mwMessage( 'mobile-frontend-amc-outreach-intro' ).text()
				)
			),
			util.parseHTML( '<p>' ).text(
				mwMessage( 'mobile-frontend-amc-outreach-description' ).text()
			),
			new AmcEnableForm( {
				postUrl: mwUtil.getUrl( 'Special:MobileOptions', {
					returnto: returnToTitle,
					returntoquery: returnToQuery || ''
				} ),
				fields: [
					{
						name: 'updateSingleOption',
						value: AMC_ENABLE_FIELD_NAME
					},
					{
						name: 'enableAMC',
						value: AMC_ENABLE_FIELD_VALUE
					},
					{
						name: 'token',
						value: csrfToken
					}
				],
				buttonLabel: mwMessage( 'mobile-frontend-amc-outreach-enable' ).text(),
				events: {
					submit: () => {
						promoCampaign.makeActionIneligible( action );
						toast.showOnPageReload( mwMessage( 'mobile-frontend-amc-outreach-enabled-message' ).text() );
					}
				}
			} ).$el,
			new Anchor( {
				href: '#',
				additionalClassNames: 'cancel',
				progressive: true,
				label: mwMessage( 'mobile-frontend-amc-outreach-no-thanks' ).text()
			} ).$el
		],
		onBeforeHide: () => {
			promoCampaign.makeActionIneligible( action );
			onBeforeHide();
		}
	} );
}
 
module.exports = amcOutreachDrawer;