Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { IPosts } from "../models/IPosts";
- export const getDataAPIJson = async (url: string): Promise<any> => {
- const result = await fetch(url);
- if (result === undefined) {
- alert("API url is not correct");
- }
- const resultJson = await result.json();
- return resultJson;
- };
- export const getIdFirstHundredPosts = (
- JSONData: Array<Number>
- ): Array<Number> => {
- return JSONData.slice(0, 99);
- };
- export const getDataFromIdPosts = (arrIdPosts: any) => {
- const Posts: IPosts = {
- id: [],
- title: [],
- score: [],
- nickname: [],
- date: [],
- };
- arrIdPosts.map(async (id) => {
- const url = `https://hacker-news.firebaseio.com/v0/item/${id}.json?print=pretty`;
- const result = getDataAPIJson(url);
- await result.then((posts) => {
- Posts.title.push(posts.title);
- Posts.score.push(posts.score);
- Posts.nickname.push(posts.by);
- const time = posts.time * 1000;
- const date = new Date(time);
- const hours = date.getHours();
- const minutes = date.getMinutes();
- Posts.date.push(`${hours}:${minutes}`);
- });
- });
- console.log(Posts);
- return Posts;
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement