keanacobarde / INDIVIDUAL-ASSIGNMENT-Team-Roster

0 stars 0 forks source link

API CALL IMPLEMENTATION + TESTING #6

Closed keanacobarde closed 1 year ago

keanacobarde commented 1 year ago

User Story

As highlighted in #5, API calls are needed to facilitate the functionality of the components. While the user will never SEE these calls occur, everything they do regarding the database is dependent on these calls working properly.

Acceptance Criteria

The following interactions are noted within the instructions: CREATE - 'POST' READ - 'GET' UPDATE - 'PATCH' DELETE - 'DELETE' As such, API calls performing these functions will be needed.

Dependecies

4 will need to be completed before any calls can be tested.

Dev Notes

EXAMPLES FROM SIMPLY BOOKS

'POST' REQUEST - Rather than creating a new book, you'll be creating a new teammate. When this request is sent, FireBase sends back an object with the contents { name: firebaseKey } that will later need to be patched.

const createBook = (payload) => new Promise((resolve, reject) => {
  fetch(`${endpoint}/books.json`, {
    method: 'POST',
    headers: {
      'Content-Type': 'application/json',
    }, 
    body: JSON.stringify(payload)
  })
    .then((response) => response.json())
    .then((data) => resolve(data))
    .catch(reject);
});

'GET' REQUEST - Rather than getting books, you'll be getting team members.

const getBooks = (uid) => new Promise((resolve, reject) => {
  fetch(`${endpoint}/books.json?orderBy="uid"&equalTo="${uid}"`, {
    method: 'GET',
    headers: {
      'Content-Type': 'application/json',
    },
  })
    .then((response) => response.json())
    .then((data) => resolve(Object.values(data)))
    .catch(reject);
});

'PATCH' REQUEST - Rather than updating a book. You'll be updating a singular teammate.

const updateBook = (payload) => new Promise((resolve, reject) => {
  fetch(`${endpoint}/books/${payload.firebaseKey}.json`, {
    method: 'PATCH',
    headers: {
      'Content-Type': 'application/json',
    },
    body: JSON.stringify(payload)
  })
    .then((response) => response.json())
    .then((data) => resolve(data))
    .catch(reject);
});

'DELETE' REQUEST - Rather than deleting a book, you'll be deleting a team member off a team.

const deleteBook = (firebaseKey) => new Promise((resolve, reject) => {
  fetch(`${endpoint}/books/${firebaseKey}.json`, {
    method: 'DELETE',
    headers: {
      'Content-Type': 'application/json',
    }, 
  })
    .then((response) => response.json())
    .then((data) => resolve((data)))
    .catch(reject);
});