summaryrefslogtreecommitdiff
path: root/app/assets/javascripts/admin/users/index.js
blob: 0c485d2a2399f18679c5a3706f769f8a38498cec (plain)
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
import Vue from 'vue';
import VueApollo from 'vue-apollo';
import createDefaultClient from '~/lib/graphql';
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
import csrf from '~/lib/utils/csrf';
import AdminUsersApp from './components/app.vue';
import ModalManager from './components/modals/user_modal_manager.vue';
import UserActions from './components/user_actions.vue';
import {
  CONFIRM_DELETE_BUTTON_SELECTOR,
  MODAL_TEXTS_CONTAINER_SELECTOR,
  MODAL_MANAGER_SELECTOR,
} from './constants';

Vue.use(VueApollo);

const apolloProvider = new VueApollo({
  defaultClient: createDefaultClient(),
});

const initApp = (el, component, userPropKey, props = {}) => {
  if (!el) {
    return false;
  }

  const { [userPropKey]: user, paths } = el.dataset;

  return new Vue({
    el,
    apolloProvider,
    render: (createElement) =>
      createElement(component, {
        props: {
          [userPropKey]: convertObjectPropsToCamelCase(JSON.parse(user), { deep: true }),
          paths: convertObjectPropsToCamelCase(JSON.parse(paths)),
          ...props,
        },
      }),
  });
};

export const initAdminUsersApp = (el = document.querySelector('#js-admin-users-app')) =>
  initApp(el, AdminUsersApp, 'users');

export const initAdminUserActions = (el = document.querySelector('#js-admin-user-actions')) =>
  initApp(el, UserActions, 'user', { showButtonLabels: true });

export const initDeleteUserModals = () => {
  const modalsMountElement = document.querySelector(MODAL_TEXTS_CONTAINER_SELECTOR);

  if (!modalsMountElement) {
    return;
  }

  const modalConfiguration = Array.from(modalsMountElement.children).reduce((accumulator, node) => {
    const { modal, ...config } = node.dataset;

    return {
      ...accumulator,
      [modal]: {
        title: node.dataset.title,
        ...config,
        content: node.innerHTML,
      },
    };
  }, {});

  // eslint-disable-next-line no-new
  new Vue({
    el: MODAL_MANAGER_SELECTOR,
    functional: true,
    methods: {
      show(...args) {
        this.$refs.manager.show(...args);
      },
    },
    render(h) {
      return h(ModalManager, {
        ref: 'manager',
        props: {
          selector: CONFIRM_DELETE_BUTTON_SELECTOR,
          modalConfiguration,
          csrfToken: csrf.token,
        },
      });
    },
  });
};