All files / web/bundles/pimui/js/controller user.js

79.41% Statements 27/34
76.92% Branches 10/13
100% Functions 7/7
79.41% Lines 27/34

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    16x                         16x         19x     19x 1x     18x   18x   17x 1x     17x 17x 17x 17x 17x 17x 5x 5x                                 17x 6x 6x 6x 6x 6x     17x 17x 17x   17x              
'use strict';
 
define([
        'underscore',
        'pim/controller/front',
        'pim/fetcher-registry',
        'pim/page-title',
        'pim/form-builder',
    ], function (
        _,
        BaseController,
        FetcherRegistry,
        PageTitle,
        FormBuilder,
    ) {
        return BaseController.extend({
            /**
             * {@inheritdoc}
             */
            renderForm: function (route) {
                return FetcherRegistry.getFetcher('user').fetch(
                    route.params.identifier,
                ).then((user) => {
                    if (!this.active) {
                        return;
                    }
 
                    PageTitle.set({ 'username': _.escape(user.username) });
 
                    return FormBuilder.build(user.meta.form)
                        .then((form) => {
                            this.on('pim:controller:can-leave', function (event) {
                                form.trigger('pim_enrich:form:can-leave', event);
                            });
 
                            let previousCatalogScope = user.catalog_default_scope;
                            let previousDefaultCategoryTree = user.default_category_tree;
                            let previousUserLocale = user.user_default_locale;
                            let previousCatalogLocale = user.catalog_default_locale;
                            let previousAvatarFilePath = user.avatar ? user.avatar.filePath : null;
                            form.on('pim_enrich:form:entity:post_save', (data) => {
                                const dataAvatarFilePath = data.avatar ? data.avatar.filePath : null;
                                Iif (data.user_default_locale !== previousUserLocale ||
                                    data.catalog_default_locale !== previousCatalogLocale ||
                                    data.catalog_default_scope !== previousCatalogScope ||
                                    data.default_category_tree !== previousDefaultCategoryTree ||
                                    dataAvatarFilePath !== previousAvatarFilePath
                                ) {
                                    previousUserLocale = data.user_default_locale;
                                    previousCatalogLocale = data.catalog_default_locale;
                                    previousCatalogScope = data.catalog_default_scope;
                                    previousDefaultCategoryTree = data.default_category_tree;
                                    previousAvatarFilePath = dataAvatarFilePath;
                                    // Prevent warning message (Firefox only)
                                    form.getExtension('state').collectAndRender();
                                    // Reload the page to reload new user interface variables
                                    location.reload();
                                }
                            });
                            form.on('pim_enrich:form:entity:pre_save', () => {
                                const data = form.getFormData();
                                data.current_password = null;
                                data.new_password = null;
                                data.new_password_repeat = null;
                                form.setData(data);
                            });
 
                            form.setData(user);
                            form.trigger('pim_enrich:form:entity:post_fetch', user);
                            form.setElement(this.$el).render();
 
                            return form;
                        });
                });
            }
        });
    }
)