nrg51098 / pinterest

This is pinterest mockup for the NSS exercise week 12
0 stars 0 forks source link

smashRead #19

Open nrg51098 opened 4 years ago

nrg51098 commented 4 years ago

User Story

As a developer, I should get all the pin data for a given user from the firebase database.

Dev Notes

Given A developer navigated to your repo There should see a file called smashRead.js at following location src/javascripts/components/data/smashRead.js this file should contain code to import dependensies as shown below

import pinData from './pinData';
import userData from './userData';
import boardData from './boardData';

In this function we are trying to get the object made in following format which gives all the details about the user, their board and their pins; in sql this is exactly like joining two table data together and fetching it.

example

{
   user_id: 10000,
   name: 'Luke',
   board_id: 'board1',
   pins: [
   { pin_id: 'pin1', pinTitle: 'xyz', category: 'Furniture', ....  },
   { pin_id: 'pin2', pinTitle: 'abc', category: 'Furniture',  .....  },
  ],
 }

Then to create this formate we are consuming chaining the .then() to finally resolve the formated data.

const getSingleUserWIthPins = (user_id) => new Promise((resolve, reject) => {
  // 1. get the user who's id is user_id
  userData.getUserById(user_Id)
    .then((response) => {
      const user = response.data;
      user.id = user_id;
      user.pins = [];
      // 2. get all of their boards using the user_id
      boardData.getUserPinsByUserID(user_id).then((boards) => {
        // 3. get ALL of the pins
        pinData.getPins().then((allPins) => {
          // 4. add the user owned pins to user.pin[]
          boards.forEach((board) => {
            const pin = allPins.find((m) => m.id === board.pin_Id);
            user.pins.push(pin);
          });         
          resolve(user);
        });
      });
    })
    .catch((err) => reject(err));
});