Skip to content
Snippets Groups Projects
editGroup.js 3.77 KiB
Newer Older
Hermann's avatar
Hermann committed
import m from 'mithril';
Hermann's avatar
Hermann committed
import { TextField } from 'polythene-mithril';
import { ListSelect, DatalistController, Select } from 'amiv-web-ui-components';
Hermann's avatar
Hermann committed
// eslint-disable-next-line import/extensions
import { apiUrl } from 'networkConfig';
import { ResourceHandler } from '../auth';
import EditView from '../views/editView';
Hermann's avatar
Hermann committed

Hermann's avatar
Hermann committed

/**
 * Table of all possible permissions to edit
 *
 * @class      PermissionEditor (name)
 */
class PermissionEditor {
  oninit() {
    // load all possible API endpoints, as permissions are defined at endpoint/resource level
    m.request(apiUrl).then((response) => {
      this.apiEndpoints = response._links.child;
    });
  }

  /**
   *
   * @attr       {object}  permissions the permissions as defined so far for the group
   * @attr       {function}  onChange  is called with the changed permissions any timne the
   *                                   permissions are changed in this editor.
   */
  view({ attrs: { permissions, onChange } }) {
    // make a local copy of permissions to edit
    const internalPerm = Object.assign({}, permissions);

    if (!this.apiEndpoints) return '';
    return m('div', [
      m('span', {
        style: {
          color: 'rgba(0, 0, 0, 0.54)',
          'font-size': '10pt',
        },
      }, 'Permissions granted by membership in this group'),
      m('div', {
        style: {
          padding: '10px',
          border: '1px solid rgba(0, 0, 0, 0.54)',
          'border-radius': '10px',
        },
      }, m('div', {
        style: { display: 'flex', width: '100%', 'flex-flow': 'row wrap' },
      }, this.apiEndpoints.map(apiEndpoint => m('div', {
        style: { display: 'flex', width: '220px', 'padding-right': '20px' },
Hermann's avatar
Hermann committed
      }, [
        m(Select, {
Hermann's avatar
Hermann committed
          label: apiEndpoint.title,
          options: ['no permission', 'read', 'readwrite'],
          default: 'no permission',
          style: { width: '200px' },
          onChange({ value }) {
            if (value === 'no permission') {
              // the api equivalent to no permission is to delete the key out of the dict
Hermann's avatar
Hermann committed
              if (internalPerm[apiEndpoint.href]) delete internalPerm[apiEndpoint.href];
            } else internalPerm[apiEndpoint.href] = value;
Hermann's avatar
Hermann committed
            onChange(internalPerm);
          },
          value: internalPerm[apiEndpoint.href],
Hermann's avatar
Hermann committed
      ])))),
    ]);
  }
}
Hermann's avatar
Hermann committed


export default class NewGroup extends EditView {
  constructor(vnode) {
    this.userHandler = new ResourceHandler('users', ['firstname', 'lastname', 'email', 'nethz']);
boian's avatar
boian committed
    this.userController = new DatalistController((query, search) => this.userHandler.get(
      { search, ...query },
    ));
Hermann's avatar
Hermann committed
  }

Hermann's avatar
Hermann committed
    const data = Object.assign({}, this.form.data);
    // exchange moderator object with string of id, will return null if moderator is null
    data.moderator = data.moderator ? data.moderator._id : null;
    this.submit(data).then(() => this.controller.changeModus('view'));
      ...this.form.renderSchema(['name', 'allow_self_enrollment', 'requires_storage']),
      m('div', { style: { display: 'flex' } }, [
        m(TextField, { label: 'Group Moderator: ', disabled: true, style: { width: '160px' } }),
        m('div', { style: { 'flex-grow': 1 } }, m(ListSelect, {
          controller: this.userController,
          selection: this.form.data.moderator,
          listTileAttrs: user => Object.assign({}, { title: `${user.firstname} ${user.lastname}` }),
          selectedText: user => `${user.firstname} ${user.lastname}`,
          onSelect: (data) => { this.form.data.moderator = data; },
Hermann's avatar
Hermann committed
      m(PermissionEditor, {
        permissions: this.form.data.permissions,
        onChange: (newPermissions) => { this.form.data.permissions = newPermissions; },
Hermann's avatar
Hermann committed
      }),
Hermann's avatar
Hermann committed
  }
}