diff --git a/src/app/feed/feed.component.ts b/src/app/feed/feed.component.ts index 4e4507c..52deabe 100644 --- a/src/app/feed/feed.component.ts +++ b/src/app/feed/feed.component.ts @@ -1,56 +1,20 @@ -import { Component } from '@angular/core'; +import { Component, OnInit } from '@angular/core'; import { Post } from '../../model/Post'; +import { PostService } from '../services/data/post.service'; @Component({ selector: 'app-feed', templateUrl: './feed.component.html', styleUrl: './feed.component.scss' }) -export class FeedComponent { - posts: Array = new Array( - { - user: { - username: "Zámbó Jimmy", - picture: "assets/placeholder-profile-picture.png", - followed: true - }, - content: "Bukott diák vagyok én, nem bírom el a szenvedést.", - image: "assets/placeholder-profile-picture.png", - likes: 1, - liked: true - }, - { - user: { - username: "Zámbó Jimmy", - picture: "assets/placeholder-profile-picture.png", - followed: true - }, - content: "Bukott diák vagyok én, nem bírom el a szenvedést.", - image: "assets/placeholder-profile-picture.png", - likes: 1, - liked: true - }, - { - user: { - username: "Zámbó Jimmy", - picture: "assets/placeholder-profile-picture.png", - followed: true - }, - content: "Bukott diák vagyok én, nem bírom el a szenvedést.", - image: "assets/placeholder-profile-picture.png", - likes: 1, - liked: true - }, - { - user: { - username: "Zámbó Jimmy", - picture: "assets/placeholder-profile-picture.png", - followed: true - }, - content: "Bukott diák vagyok én, nem bírom el a szenvedést.", - image: "assets/placeholder-profile-picture.png", - likes: 1, - liked: true - }, - ); +export class FeedComponent implements OnInit { + posts: Array | undefined; + + constructor(private postService: PostService) { } + + ngOnInit(): void { + this.postService.getFeed(10, 0) + .then(posts => { this.posts = posts; }) + .catch(()=> { alert('failed to fetch feed'); }); + } } diff --git a/src/app/services/data/post.service.spec.ts b/src/app/services/data/post.service.spec.ts new file mode 100644 index 0000000..913642b --- /dev/null +++ b/src/app/services/data/post.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { PostService } from './post.service'; + +describe('PostService', () => { + let service: PostService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(PostService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/data/post.service.ts b/src/app/services/data/post.service.ts new file mode 100644 index 0000000..8857433 --- /dev/null +++ b/src/app/services/data/post.service.ts @@ -0,0 +1,80 @@ +import { Injectable } from '@angular/core'; +import { Post } from '../../../model/Post'; +import { AuthService } from '../auth/auth.service'; + +@Injectable({ + providedIn: 'root' +}) +export class PostService { + + constructor(private auth: AuthService) { } + + examplePosts: Array = new Array( + { + user: { + username: "Zámbó Jimmy", + picture: "assets/placeholder-profile-picture.png", + followed: true + }, + content: "Bukott diák vagyok én, nem bírom el a szenvedést.", + image: "assets/placeholder-profile-picture.png", + likes: 1, + liked: true + }, + { + user: { + username: "Zámbó Jimmy", + picture: "assets/placeholder-profile-picture.png", + followed: true + }, + content: "Bukott diák vagyok én, nem bírom el a szenvedést.", + image: "assets/placeholder-profile-picture.png", + likes: 1, + liked: true + }, + { + user: { + username: "Zámbó Jimmy", + picture: "assets/placeholder-profile-picture.png", + followed: true + }, + content: "Bukott diák vagyok én, nem bírom el a szenvedést.", + image: "assets/placeholder-profile-picture.png", + likes: 1, + liked: true + }, + { + user: { + username: "Zámbó Jimmy", + picture: "assets/placeholder-profile-picture.png", + followed: true + }, + content: "Bukott diák vagyok én, nem bírom el a szenvedést.", + image: "assets/placeholder-profile-picture.png", + likes: 1, + liked: true + }, + ); + + getFeed(length: Number, start: Number): Promise> { + return new Promise>((resolve, reject) => { + // TODO: fetch feed + this.auth.getToken() + .then( (token)=> { + resolve(this.examplePosts); + }) + .catch( ()=> { reject() }); + }); + } + + getPosts(username: String, lenght: Number, start: Number) { + return new Promise>((resolve, reject) => { + // TODO: fetch feed + this.auth.getToken() + .then( (token)=> { + resolve(this.examplePosts); + }) + .catch( ()=> { reject() }); + }); + } +} diff --git a/src/app/services/data/user.service.spec.ts b/src/app/services/data/user.service.spec.ts new file mode 100644 index 0000000..3f804c9 --- /dev/null +++ b/src/app/services/data/user.service.spec.ts @@ -0,0 +1,16 @@ +import { TestBed } from '@angular/core/testing'; + +import { UserService } from './user.service'; + +describe('UserService', () => { + let service: UserService; + + beforeEach(() => { + TestBed.configureTestingModule({}); + service = TestBed.inject(UserService); + }); + + it('should be created', () => { + expect(service).toBeTruthy(); + }); +}); diff --git a/src/app/services/data/user.service.ts b/src/app/services/data/user.service.ts new file mode 100644 index 0000000..852daed --- /dev/null +++ b/src/app/services/data/user.service.ts @@ -0,0 +1,28 @@ +import { Injectable } from '@angular/core'; +import { User } from '../../../model/User'; +import { AuthService } from '../auth/auth.service'; + +@Injectable({ + providedIn: 'root' +}) +export class UserService { + + exampleUser: User = { + username: "Zámbó Jimmy", + picture: "assets/placeholder-profile-picture.png", + followed: true + } + + constructor(private auth: AuthService) { } + + getUser(username: String): Promise { + return new Promise((resolve, reject) => { + // TODO: get user + this.auth.getToken() + .then((token) => { + resolve(this.exampleUser); + }) + .catch(() => { reject() }); + }); + } +} diff --git a/src/app/user/user.component.html b/src/app/user/user.component.html index 52ec478..9038fa5 100644 --- a/src/app/user/user.component.html +++ b/src/app/user/user.component.html @@ -1,6 +1,6 @@
- +
diff --git a/src/app/user/user.component.ts b/src/app/user/user.component.ts index f503afb..f542a63 100644 --- a/src/app/user/user.component.ts +++ b/src/app/user/user.component.ts @@ -1,47 +1,28 @@ -import { Component } from '@angular/core'; +import { Component, OnInit } from '@angular/core'; import { Post } from '../../model/Post'; import { User } from '../../model/User'; +import { PostService } from '../services/data/post.service'; +import { ActivatedRoute } from '@angular/router'; +import { UserService } from '../services/data/user.service'; @Component({ selector: 'app-user', templateUrl: './user.component.html', styleUrl: './user.component.scss' }) -export class UserComponent { - exampleUser: User = { - username: "Zámbó Jimmy", - picture: "assets/placeholder-profile-picture.png", - followed: true - } +export class UserComponent implements OnInit{ + user: User | undefined; + posts: Array | undefined; - posts: Array = new Array( - { - user: this.exampleUser, - content: "Bukott diák vagyok én, nem bírom el a szenvedést.", - image: "assets/placeholder-profile-picture.png", - likes: 1, - liked: true - }, - { - user: this.exampleUser, - content: "Bukott diák vagyok én, nem bírom el a szenvedést.", - image: "assets/placeholder-profile-picture.png", - likes: 1, - liked: true - }, - { - user: this.exampleUser, - content: "Bukott diák vagyok én, nem bírom el a szenvedést.", - image: "assets/placeholder-profile-picture.png", - likes: 1, - liked: true - }, - { - user: this.exampleUser, - content: "Bukott diák vagyok én, nem bírom el a szenvedést.", - image: "assets/placeholder-profile-picture.png", - likes: 1, - liked: true - }, - ); + constructor(private postService: PostService, private userService: UserService, private route: ActivatedRoute) { } + + ngOnInit(): void { + this.userService.getUser(this.route.snapshot.params['username']) + .then(user => { this.user = user }) + .catch(() => alert('failed to get user')); + + this.postService.getPosts(this.route.snapshot.params['username'], 10, 0) + .then(posts => { this.posts = posts; }) + .catch(()=> { alert('failed to fetch feed'); }); + } }