Advertisement
Guest User

Untitled

a guest
Oct 27th, 2022
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import { IPosts } from "../models/IPosts";
  2.  
  3. export const getDataAPIJson = async (url: string): Promise<any> => {
  4.   const result = await fetch(url);
  5.   if (result === undefined) {
  6.     alert("API url is not correct");
  7.   }
  8.   const resultJson = await result.json();
  9.   return resultJson;
  10. };
  11. export const getIdFirstHundredPosts = (
  12.   JSONData: Array<Number>
  13. ): Array<Number> => {
  14.   return JSONData.slice(0, 99);
  15. };
  16.  
  17. export const getDataFromIdPosts = (arrIdPosts: any) => {
  18.   const Posts: IPosts = {
  19.     id: [],
  20.     title: [],
  21.     score: [],
  22.     nickname: [],
  23.     date: [],
  24.   };
  25.   arrIdPosts.map(async (id) => {
  26.     const url = `https://hacker-news.firebaseio.com/v0/item/${id}.json?print=pretty`;
  27.     const result = getDataAPIJson(url);
  28.     await result.then((posts) => {
  29.       Posts.title.push(posts.title);
  30.       Posts.score.push(posts.score);
  31.       Posts.nickname.push(posts.by);
  32.       const time = posts.time * 1000;
  33.       const date = new Date(time);
  34.       const hours = date.getHours();
  35.       const minutes = date.getMinutes();
  36.       Posts.date.push(`${hours}:${minutes}`);
  37.     });
  38.   });
  39.   console.log(Posts);
  40.   return Posts;
  41. };
  42.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement