Skip to content

Instantly share code, notes, and snippets.

@KVeitch
Last active December 3, 2019 15:43
Show Gist options
  • Save KVeitch/997c69f490933e7cb969d4f67f6db453 to your computer and use it in GitHub Desktop.
Save KVeitch/997c69f490933e7cb969d4f67f6db453 to your computer and use it in GitHub Desktop.
export const fetchCoWorkers = async () => {
  const url = 'http://localhost:3001/api/v1/coworkers'
  const response = await fetch(url);
  if (!response.ok) {
    throw new Error('There was an error getting your co-workers.');
  }
  const coWorkers = await response.json();
  return coWorkers
}


export const postCoWorker = async newCoWorker => {
  const url = 'http://localhost:3001/api/v1/coworkers';
  const options = {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json'
    },
    body: JSON.stringify({ ...newCoWorker })
  };
  const response = await fetch(url, options);
  if (!response.ok) {
    throw new Error('Sorry.  Unable to create your new co-worker.');
  }
  const newCoWorkerId = await response.json();
  return newCoWorkerId;
}


export const deleteCoWorker = async id => {
  const url = `http://localhost:3001/api/v1/coworkers/${id}`;
  const options = {
    method: 'DELETE',
    headers: {
      'Content-Type': 'application/json'
    }
  }
  const response = await fetch(url, options);
  if (!response.ok) {
    throw new Error('There was a problem removing this co-worker.')
  }
  const remainingCoWorkers = await response.json();
  return remainingCoWorkers;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment