|
|
@ -9,6 +9,7 @@ import {tap} from 'rxjs/operators';
|
|
|
|
import {BaseService} from '../base.service';
|
|
|
|
import {BaseService} from '../base.service';
|
|
|
|
import {formatDate} from '@angular/common';
|
|
|
|
import {formatDate} from '@angular/common';
|
|
|
|
import {IFileUploadResult} from '../../models/interfaces/IFileUploadResult';
|
|
|
|
import {IFileUploadResult} from '../../models/interfaces/IFileUploadResult';
|
|
|
|
|
|
|
|
import { IErrorResponse } from 'src/app/models/interfaces/IErrorResponse';
|
|
|
|
|
|
|
|
|
|
|
|
const createPostGqlQuery = `mutation($content: String!, $type: PostType) {
|
|
|
|
const createPostGqlQuery = `mutation($content: String!, $type: PostType) {
|
|
|
|
createPost(content: $content, type: $type) {
|
|
|
|
createPost(content: $content, type: $type) {
|
|
|
@ -98,6 +99,11 @@ export enum Sort {
|
|
|
|
NEW = 'NEW',
|
|
|
|
NEW = 'NEW',
|
|
|
|
TOP = 'TOP',
|
|
|
|
TOP = 'TOP',
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class PostingState {
|
|
|
|
|
|
|
|
posting = false;
|
|
|
|
|
|
|
|
errorOccured = false;
|
|
|
|
|
|
|
|
errorMessage = 'An error occured.';
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
providedIn: 'root'
|
|
|
@ -109,7 +115,7 @@ export class FeedService extends BaseService {
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public postsAvailable = new BehaviorSubject<boolean>(true);
|
|
|
|
public postsAvailable = new BehaviorSubject<boolean>(true);
|
|
|
|
public posting = new BehaviorSubject<boolean>(false);
|
|
|
|
public postingState = new BehaviorSubject<PostingState>(new PostingState());
|
|
|
|
public posts: BehaviorSubject<Post[]> = new BehaviorSubject([]);
|
|
|
|
public posts: BehaviorSubject<Post[]> = new BehaviorSubject([]);
|
|
|
|
private activePostList: Sort = Sort.NEW;
|
|
|
|
private activePostList: Sort = Sort.NEW;
|
|
|
|
private offset = 0;
|
|
|
|
private offset = 0;
|
|
|
@ -187,7 +193,7 @@ export class FeedService extends BaseService {
|
|
|
|
* @param file - a file that is being uploaded with the post
|
|
|
|
* @param file - a file that is being uploaded with the post
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
private createPostRequest(body: { variables: any; query: string }, file?: File) {
|
|
|
|
private createPostRequest(body: { variables: any; query: string }, file?: File) {
|
|
|
|
this.posting.next(true);
|
|
|
|
this.setPostingState(true);
|
|
|
|
if (file) {
|
|
|
|
if (file) {
|
|
|
|
return this.postGraphql(body, null, 0)
|
|
|
|
return this.postGraphql(body, null, 0)
|
|
|
|
.pipe(tap(response => {
|
|
|
|
.pipe(tap(response => {
|
|
|
@ -200,34 +206,60 @@ export class FeedService extends BaseService {
|
|
|
|
post.mediaType = result.fileName.endsWith('.png') ? 'IMAGE' : 'VIDEO';
|
|
|
|
post.mediaType = result.fileName.endsWith('.png') ? 'IMAGE' : 'VIDEO';
|
|
|
|
updatedPosts.unshift(post);
|
|
|
|
updatedPosts.unshift(post);
|
|
|
|
this.posts.next(updatedPosts);
|
|
|
|
this.posts.next(updatedPosts);
|
|
|
|
this.posting.next(false);
|
|
|
|
this.setPostingState(false);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
console.error(result.error);
|
|
|
|
console.error(result.error);
|
|
|
|
this.posting.next(false);
|
|
|
|
this.setPostingError(result.error);
|
|
|
|
this.deletePost(post.id).subscribe();
|
|
|
|
this.deletePost(post.id).subscribe();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, error => {
|
|
|
|
}, error => {
|
|
|
|
console.error(error);
|
|
|
|
console.error(error);
|
|
|
|
this.posting.next(false);
|
|
|
|
this.setPostingError(error);
|
|
|
|
this.deletePost(post.id).subscribe();
|
|
|
|
this.deletePost(post.id).subscribe();
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
}, (error: IErrorResponse) => {
|
|
|
|
|
|
|
|
this.setPostingError(error.error.errors[0].message);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
));
|
|
|
|
));
|
|
|
|
} else if (!file) {
|
|
|
|
} else if (!file) {
|
|
|
|
return this.postGraphql(body, null, 0)
|
|
|
|
return this.postGraphql(body, null, 0)
|
|
|
|
.pipe(tap(response => {
|
|
|
|
.pipe(tap(response => {
|
|
|
|
this.posting.next(false);
|
|
|
|
this.setPostingState(false);
|
|
|
|
const updatedPosts = this.posts.getValue();
|
|
|
|
const updatedPosts = this.posts.getValue();
|
|
|
|
if (this.activePostList === Sort.NEW) {
|
|
|
|
if (this.activePostList === Sort.NEW) {
|
|
|
|
const post = this.constructPost(response);
|
|
|
|
const post = this.constructPost(response);
|
|
|
|
updatedPosts.unshift(post);
|
|
|
|
updatedPosts.unshift(post);
|
|
|
|
this.posts.next(updatedPosts);
|
|
|
|
this.posts.next(updatedPosts);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}, (error: IErrorResponse) => {
|
|
|
|
|
|
|
|
console.log(error);
|
|
|
|
|
|
|
|
this.setPostingError(error.error.errors[0].message);
|
|
|
|
}));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setPostingState(b: boolean) {
|
|
|
|
|
|
|
|
if (b) {
|
|
|
|
|
|
|
|
this.postingState.getValue().posting = true;
|
|
|
|
|
|
|
|
this.postingState.getValue().errorOccured = false;
|
|
|
|
|
|
|
|
this.postingState.getValue().errorMessage = '';
|
|
|
|
|
|
|
|
this.postingState.next(this.postingState.getValue());
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
this.postingState.getValue().posting = false;
|
|
|
|
|
|
|
|
this.postingState.getValue().errorOccured = false;
|
|
|
|
|
|
|
|
this.postingState.getValue().errorMessage = '';
|
|
|
|
|
|
|
|
this.postingState.next(this.postingState.getValue());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
setPostingError(error: string) {
|
|
|
|
|
|
|
|
this.postingState.getValue().posting = false;
|
|
|
|
|
|
|
|
this.postingState.getValue().errorOccured = true;
|
|
|
|
|
|
|
|
this.postingState.getValue().errorMessage = error;
|
|
|
|
|
|
|
|
this.postingState.next(this.postingState.getValue());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* Uploads a file for a post
|
|
|
|
* Uploads a file for a post
|
|
|
|
* @param postId
|
|
|
|
* @param postId
|
|
|
|