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;
}
-
-
Save slkarsh/4f42bac75587625b10f4d5d71a3c3e60 to your computer and use it in GitHub Desktop.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment