Change feedservice

master
Max 5 years ago
parent cf8b53bb38
commit 2250492ccc

@ -40,7 +40,7 @@
text-align: right text-align: right
#inputPreview #inputPreview
max-width: 100% max-width: 100%
max-height: 40vh max-height: 45vh
width: auto width: auto
border-radius: 4px border-radius: 4px
mask-mode: luminance mask-mode: luminance

@ -91,7 +91,8 @@ export class FeedComponent implements OnInit {
}); });
} else if (postElement) { } else if (postElement) {
this.posting = true; this.posting = true;
this.feedService.createPost(postElement.value, this.file).subscribe(() => { this.feedService.createPost(postElement.value, this.file).subscribe((result) => {
console.log('response in component');
this.posting = false; this.posting = false;
postElement.value = ''; postElement.value = '';
this.textInputValue = ''; this.textInputValue = '';
@ -103,6 +104,8 @@ export class FeedComponent implements OnInit {
this.showNew(); this.showNew();
} }
}, (error: IErrorResponse) => { }, (error: IErrorResponse) => {
console.log('an error occured in component');
console.log(error);
this.posting = false; this.posting = false;
this.errorOccurred = true; this.errorOccurred = true;
this.errorMessage = error.error.errors[0].message; this.errorMessage = error.error.errors[0].message;

@ -10,8 +10,8 @@ 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';
const createPostGqlQuery = `mutation($content: String!) { const createPostGqlQuery = `mutation($content: String!, $type: PostType) {
createPost(content: $content) { createPost(content: $content, type: $type) {
id, id,
content, content,
htmlContent, htmlContent,
@ -34,8 +34,8 @@ const createPostGqlQuery = `mutation($content: String!) {
createdAt} createdAt}
}`; }`;
const createPostActivityGqlQuery = `mutation($content: String!, $id: ID) { const createPostActivityGqlQuery = `mutation($content: String!, $id: ID, $type: PostType) {
createPost(content: $content activityId: $id) { createPost(content: $content activityId: $id, type: $type) {
id, id,
content, content,
htmlContent, htmlContent,
@ -70,7 +70,7 @@ const downvotePostGqlQuery = `mutation($postId: ID!) {
} }
}`; }`;
const getPostGqlQuery = `query($first: Int, $offset: Int, $sort: SortType){ const getPostsGqlQuery = `query($first: Int, $offset: Int, $sort: SortType){
getPosts (first: $first, offset: $offset, sort: $sort) { getPosts (first: $first, offset: $offset, sort: $sort) {
id, id,
content, content,
@ -122,7 +122,7 @@ export class FeedService extends BaseService {
*/ */
private static buildGetPostBody(sort: string, offset: number, first: number = 10) { private static buildGetPostBody(sort: string, offset: number, first: number = 10) {
return { return {
query: getPostGqlQuery, variables: { query: getPostsGqlQuery, variables: {
first, first,
offset, offset,
sort sort
@ -145,10 +145,15 @@ export class FeedService extends BaseService {
* @param file * @param file
*/ */
public createPost(pContent: String, file?: File) { public createPost(pContent: String, file?: File) {
let type: string;
if (file) { type = 'MEDIA'; } else {
type = 'TEXT';
}
const body = { const body = {
query: createPostGqlQuery, query: createPostGqlQuery,
variables: { variables: {
content: pContent content: pContent,
type
} }
}; };
return this.createPostRequest(body, file); return this.createPostRequest(body, file);
@ -161,10 +166,15 @@ export class FeedService extends BaseService {
* @param file * @param file
*/ */
public createPostActivity(pContent: String, activityId: String, file?: File) { public createPostActivity(pContent: String, activityId: String, file?: File) {
let type: string;
if (file) { type = 'MEDIA'; } else {
type = 'TEXT';
}
const body = { const body = {
query: createPostActivityGqlQuery, variables: { query: createPostActivityGqlQuery, variables: {
content: pContent, content: pContent,
id: activityId id: activityId,
type
} }
}; };
return this.createPostRequest(body, file); return this.createPostRequest(body, file);
@ -176,26 +186,34 @@ 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) {
return this.postGraphql(body, null, 0) if (file) {
return this.postGraphql(body, null, 0)
.pipe(tap(response => { .pipe(tap(response => {
const updatedPosts = this.posts.getValue();
if (this.activePostList === Sort.NEW) { if (this.activePostList === Sort.NEW) {
const updatedPosts = this.posts.getValue();
const post = this.constructPost(response); const post = this.constructPost(response);
updatedPosts.unshift(post); this.uploadPostImage(post.id, file).subscribe((result) => {
if (file) { post.mediaUrl = result.fileName;
this.uploadPostImage(post.id, file).subscribe((result) => { post.mediaType = result.fileName.endsWith('.png') ? 'IMAGE' : 'VIDEO';
post.mediaUrl = result.fileName; updatedPosts.unshift(post);
post.mediaType = result.fileName.endsWith('.png') ? 'IMAGE' : 'VIDEO';
this.posts.next(updatedPosts);
}, error => {
console.error(error);
this.deletePost(post.id);
});
} else {
this.posts.next(updatedPosts); this.posts.next(updatedPosts);
} }, error => {
console.error(error);
this.deletePost(post.id);
});
}
}));
} else if (!file) {
return this.postGraphql(body, null, 0)
.pipe(tap(response => {
const updatedPosts = this.posts.getValue();
if (this.activePostList === Sort.NEW) {
const post = this.constructPost(response);
updatedPosts.unshift(post);
this.posts.next(updatedPosts);
} }
})); }));
}
} }
/** /**

Loading…
Cancel
Save