<commit_before>import $ from 'jquery';
import 'foundation/js/foundation/foundation';
import 'foundation/js/foundation/foundation.reveal';
import nod from './common/nod';
import PageManager from '../page-manager';

export default class WishList extends PageManager {
    constructor() {
        super();

        this.options = {
            template: 'account/add-wishlist'
        };

        let $addWishListForm = $('.wishlist-form');

        this.wishlistDeleteConfirm();

        if ($addWishListForm.length) {
            this.registerAddWishListValidation($addWishListForm);
        }

        // Initialize wish list listener
        this.wishListHandler();
    }

    /**
     * Creates a confirm box before deleting all wish lists
     */
    wishlistDeleteConfirm() {
        $('body').on('click', '[data-wishlist-delete]', (event) => {
            let confirmed = confirm(this.context.wishlistDelete);
            if (confirmed) {
                return true;
            }

            event.preventDefault()
        });
    }

    registerAddWishListValidation($addWishlistForm) {
        this.addWishlistValidator = nod({
            submit: '.wishlist-form input[type="submit"]'
        });

        this.addWishlistValidator.add([
            {
                selector: '.wishlist-form input[name="wishlistname"]',
                validate: (cb, val) => {
                    let result = val.length > 0;
                    cb(result);
                },
                errorMessage: "You must enter a wishlist name"
            }
        ]);

        $addWishlistForm.submit((event) => {
            this.addWishlistValidator.performCheck();

            if (this.addWishlistValidator.areAll('valid')) {
                return;
            }

            event.preventDefault();
        });
    }

    wishListHandler() {
        $('body').on('click', '[data-wishlist]', (event) => {
            let $wishListUrl = event.currentTarget.href;

            event.preventDefault();

            this.getPageModal($wishListUrl, this.options);
        });
    }
}<commit_msg>BIG-20546 - Wishlist modal validation<commit_after>import $ from 'jquery';
import 'foundation/js/foundation/foundation';
import 'foundation/js/foundation/foundation.reveal';
import nod from './common/nod';
import PageManager from '../page-manager';

export default class WishList extends PageManager {
    constructor() {
        super();

        this.options = {
            template: 'account/add-wishlist'
        };
    }

    /**
     * Creates a confirm box before deleting all wish lists
     */
    wishlistDeleteConfirm() {
        $('body').on('click', '[data-wishlist-delete]', (event) => {
            let confirmed = confirm(this.context.wishlistDelete);
            if (confirmed) {
                return true;
            }

            event.preventDefault()
        });
    }

    registerAddWishListValidation($addWishlistForm) {
        this.addWishlistValidator = nod({
            submit: '.wishlist-form input[type="submit"]'
        });

        this.addWishlistValidator.add([
            {
                selector: '.wishlist-form input[name="wishlistname"]',
                validate: (cb, val) => {
                    let result = val.length > 0;
                    cb(result);
                },
                errorMessage: "You must enter a wishlist name"
            }
        ]);

        $addWishlistForm.submit((event) => {
            this.addWishlistValidator.performCheck();

            if (this.addWishlistValidator.areAll('valid')) {
                return;
            }

            event.preventDefault();
        });
    }

    wishListHandler() {
        $('body').on('click', '[data-wishlist]', (event) => {
            let $wishListUrl = event.currentTarget.href;

            event.preventDefault();

            this.getPageModal($wishListUrl, this.options, (err, content) => {
                if (err) {
                    throw new Error(err);
                }

                let $wishListForm = $('.wishlist-form');

                this.registerAddWishListValidation($wishListForm);
            });
        });
    }

    loaded(next) {

        let $addWishListForm = $('.wishlist-form');

        if ($addWishListForm.length) {
            this.registerAddWishListValidation($addWishListForm);
        }

        this.wishlistDeleteConfirm();
        this.wishListHandler();

        next();
    }
}
{'fraction_non_alphanumeric': 0.1286195286195286, 'fraction_numerical': 0.0015712682379349046, 'mean_word_length': 1.7625542467451953, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 14, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4596353', 'n_tokens_mistral': 1260, 'n_tokens_neox': 1197, 'n_words': 212}