Last active
January 9, 2019 14:51
-
-
Save AntonGorelov/d1a62bfab166b00e53bdcfe5dee59c09 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { Injectable } from '@angular/core'; | |
import { BehaviorSubject, Observable } from 'rxjs'; | |
import { BlogPostService } from '@app/core/services'; | |
import { MODE } from '@app/companies/components'; | |
import { IBlogPost, ICompany, ICountry, IOrganization } from '@lib/models'; | |
@Injectable() | |
export class BlogPostStateService { | |
private _getMode$ = new BehaviorSubject<string>(MODE.show); | |
/** | |
* Initial state of blog, needs in reset case, should be updated after save changes. | |
*/ | |
private _initBlog$ = new BehaviorSubject<IBlogPost>(null); | |
/** | |
* Current state of blog, needs for "edit" and "preview" modes. | |
*/ | |
private _draftBlog$ = new BehaviorSubject<IBlogPost>(null); | |
constructor(private _blogPostService: BlogPostService) {} | |
public get isEditable() { | |
return this._isEditMode() || this._isNewMode(); | |
} | |
public get initBlog$(): Observable<IBlogPost> { | |
return this._initBlog$.asObservable(); | |
} | |
/** | |
* Get value of current state of blog. | |
*/ | |
public get draftBlog$(): Observable<IBlogPost> { | |
return this._draftBlog$.asObservable(); | |
} | |
public get mode$(): Observable<string> { | |
return this._getMode$.asObservable(); | |
} | |
public setMode(mode: string) { | |
this._getMode$.next(mode); | |
} | |
public createBlog$(companyId, blog: IBlogPost): Observable<IBlogPost> { | |
return this._blogPostService.create(companyId, blog); | |
} | |
public updateBlog$(id, companyId, blogData: IBlogPost): Observable<IBlogPost> { | |
return this._blogPostService.update(id, companyId, blogData); | |
} | |
public setInitBlog(blog: IBlogPost) { | |
this._initBlog$.next(blog); | |
} | |
public getInitBlogValue(): IBlogPost { | |
return this._initBlog$.value; | |
} | |
public getDraftBlogValue(): IBlogPost { | |
return this._draftBlog$.value; | |
} | |
public setDraftBlog(blog: IBlogPost) { | |
this._draftBlog$.next(blog); | |
} | |
public createEmptyBlog(parentId?: string, parentType?): IBlogPost { | |
return { | |
id: null, | |
title: '', | |
coverImage: '', | |
preamble: '', | |
content: '', | |
parent: <ICompany>{ id: parentId } || <IOrganization>{} || <ICountry>{}, | |
parentType: parentType, | |
published: true, | |
featured: true, | |
}; | |
} | |
/** | |
* Check current mode. Set true if current edit mode. | |
*/ | |
private _isEditMode() { | |
return this._getMode$.value === MODE.edit; | |
} | |
/** | |
* Check current mode. Set true if current new mode. | |
*/ | |
private _isNewMode() { | |
return this._getMode$.value === MODE.new; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment