Skip to content
index.js 5.43 KiB
Newer Older
const BASE_API_URL = "http://localhost:8081/"
function apiRequest(url, data) {
  return new Promise((resolve, reject) => {
    fetch(url, data)
      .then(response => {
        if([200, 201, 204, 400].includes(response.status)) { // valid status codes
          resolve(response.json());
        } else {
          response.json().then(jsonValue => dispatchApiErrorEvent(jsonValue));
        }
      })
      .catch(error => {
        dispatchApiErrorEvent(error);
      });
  });
}

function dispatchApiErrorEvent(error) {
  let message;
  if(error.message) {
    message = error.message;
  } else {
    message = 'Generic error';
  }

  let event = new CustomEvent('apiError');
  event.message = message;
  document.dispatchEvent(event);
}

export default {
  fetchGroupsModel (input) {
    let url = BASE_API_URL
            + 'groups?groupId=' + input.selectedGroupId
            + '&tab=' + input.selectedTab
            + '&paginatorPageSize=' + input.paginatorPageSize
            + '&paginatorPage=' + input.paginatorPage
            + '&page=' + input.page;
    return apiRequest(url, {
      method: 'GET',
      cache: 'no-cache',
      credentials: 'include',
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
    });
  },
  addGroup (newGroupName, input) {
    let url = BASE_API_URL + 'group';
    return apiRequest(url, {
      method: 'POST',
      cache: 'no-cache',
      credentials: 'include',
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
      },
      body: JSON.stringify({
        newGroupName: newGroupName,
        parentGroupId: input.selectedGroupId,
        paginatorPageSize: input.paginatorPageSize,
        paginatorPage: input.paginatorPage
      })
    });
  },
  renameGroup (groupId, newGroupName, input) {
    let url = BASE_API_URL + 'group/' + groupId;
    return apiRequest(url, {
      method: 'PUT',
      cache: 'no-cache',
      credentials: 'include',
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
      },
      body: JSON.stringify({
        newGroupName: newGroupName,
        paginatorPageSize: input.paginatorPageSize,
        paginatorPage: input.paginatorPage
      })
    });
  },
  removeGroup (groupId, input) {
    let url = BASE_API_URL + 'group/' + groupId
            + '?paginatorPageSize=' + input.paginatorPageSize
            + '&paginatorPage=' + input.paginatorPage;
    return apiRequest(url, {
      method: 'DELETE',
      cache: 'no-cache',
      credentials: 'include',
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
      }
    });
Sonia Zorba's avatar
Sonia Zorba committed
  },
  searchUser (searchInput) {
    let url = BASE_API_URL + 'users?search=' + searchInput;

    return apiRequest(url, {
      method: 'GET',
      cache: 'no-cache',
      credentials: 'include',
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
      }
    });
  },
  addPermission (userId, permission, input) {
    let url = BASE_API_URL + 'permission';

    return apiRequest(url, {
      method: 'POST',
      cache: 'no-cache',
      credentials: 'include',
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
      },
      body: JSON.stringify({
        groupId: input.selectedGroupId,
        userId: userId,
        permission: permission,
        paginatorPageSize: input.paginatorPageSize,
        paginatorPage: input.paginatorPage
      })
    });
  },
  getPermission (groupId, userId) {
    let url = BASE_API_URL + 'permission?groupId=' + groupId + '&userId=' + userId;

    return apiRequest(url, {
      method: 'GET',
      cache: 'no-cache',
      credentials: 'include',
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
      }
    });
  },
  addMember (userId, permission, input) {
    let url = BASE_API_URL + 'member';

    return apiRequest(url, {
      method: 'POST',
      cache: 'no-cache',
      credentials: 'include',
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
      },
      body: JSON.stringify({
        groupId: input.selectedGroupId,
        userId: userId,
        permission: permission,
        paginatorPageSize: input.paginatorPageSize,
        paginatorPage: input.paginatorPage
      })
    });
  },
  removeMember (userId, removeAlsoPermission, input) {
    let url = BASE_API_URL + 'member'
            + '?groupId=' + input.selectedGroupId
            + '&userId=' + userId
            + '&removeAlsoPermission=' + removeAlsoPermission
            + '&paginatorPageSize=' + input.paginatorPageSize
            + '&paginatorPage=' + input.paginatorPage;
    return apiRequest(url, {
      method: 'DELETE',
      cache: 'no-cache',
      credentials: 'include',
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
      }
    });
  },
  removePermission (userId, input) {
    let url = BASE_API_URL + 'permission'
            + '?groupId=' + input.selectedGroupId
            + '&userId=' + userId
            + '&paginatorPageSize=' + input.paginatorPageSize
            + '&paginatorPage=' + input.paginatorPage;
    return apiRequest(url, {
      method: 'DELETE',
      cache: 'no-cache',
      credentials: 'include',
      headers: {
        'Content-Type': 'application/json',
        'Accept': 'application/json',
      }
    });