All files / app/assets/javascripts/access_tokens index.js

68.97% Statements 20/29
62.5% Branches 5/8
66.67% Functions 6/9
71.43% Lines 20/28

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 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114                      1x 2x   2x 1x     1x   1x     1x                 1x 2x   2x 1x     1x   1x 1x 1x                     1x       1x   1x         1x                                             1x                                                  
import Vue from 'vue';
 
import createFlash from '~/flash';
import { convertObjectPropsToCamelCase } from '~/lib/utils/common_utils';
import { parseRailsFormFields } from '~/lib/utils/forms';
import { __ } from '~/locale';
 
import ExpiresAtField from './components/expires_at_field.vue';
import TokensApp from './components/tokens_app.vue';
import { FEED_TOKEN, INCOMING_EMAIL_TOKEN, STATIC_OBJECT_TOKEN } from './constants';
 
export const initExpiresAtField = () => {
  const el = document.querySelector('.js-access-tokens-expires-at');
 
  if (!el) {
    return null;
  }
 
  const { expiresAt: inputAttrs } = parseRailsFormFields(el);
 
  return new Vue({
    el,
    render(h) {
      return h(ExpiresAtField, {
        props: {
          inputAttrs,
        },
      });
    },
  });
};
 
export const initProjectsField = () => {
  const el = document.querySelector('.js-access-tokens-projects');
 
  if (!el) {
    return null;
  }
 
  const { projects: inputAttrs } = parseRailsFormFields(el);
 
  Eif (window.gon.features.personalAccessTokensScopedToProjects) {
    return new Promise((resolve) => {
      Promise.all([
        import('./components/projects_field.vue'),
        import('vue-apollo'),
        import('~/lib/graphql'),
      ])
        .then(
          ([
            { default: ProjectsField },
            { default: VueApollo },
            { default: createDefaultClient },
          ]) => {
            const apolloProvider = new VueApollo({
              defaultClient: createDefaultClient(),
            });
 
            Vue.use(VueApollo);
 
            resolve(
              new Vue({
                el,
                apolloProvider,
                render(h) {
                  return h(ProjectsField, {
                    props: {
                      inputAttrs,
                    },
                  });
                },
              }),
            );
          },
        )
        .catch(() => {
          createFlash({
            message: __(
              'An error occurred while loading the access tokens form, please try again.',
            ),
          });
        });
    });
  }
 
  return null;
};
 
export const initTokensApp = () => {
  const el = document.getElementById('js-tokens-app');
 
  if (!el) return false;
 
  const tokensData = convertObjectPropsToCamelCase(JSON.parse(el.dataset.tokensData), {
    deep: true,
  });
 
  const tokenTypes = {
    [FEED_TOKEN]: tokensData[FEED_TOKEN],
    [INCOMING_EMAIL_TOKEN]: tokensData[INCOMING_EMAIL_TOKEN],
    [STATIC_OBJECT_TOKEN]: tokensData[STATIC_OBJECT_TOKEN],
  };
 
  return new Vue({
    el,
    provide: {
      tokenTypes,
    },
    render(createElement) {
      return createElement(TokensApp);
    },
  });
};