Commit a7f0a708b372dae33b10a31cba307774e7eaa451
1 parent
7921e436
Exists in
master
and in
26 other branches
Use block endpoint to get block content
Showing
4 changed files
with
53 additions
and
35 deletions
Show diff stats
src/app/layout/blocks/recent-documents/recent-documents-block.component.spec.ts
... | ... | @@ -11,9 +11,9 @@ describe("Components", () => { |
11 | 11 | describe("Recent Documents Block Component", () => { |
12 | 12 | |
13 | 13 | let settingsObj = {}; |
14 | - let mockedArticleService = { | |
15 | - getByProfile: (profile: noosfero.Profile, filters: any): any => { | |
16 | - return Promise.resolve({ data: [{ name: "article1" }], headers: (name: string) => { return name; } }); | |
14 | + let mockedBlockService = { | |
15 | + getApiContent: (block: noosfero.Block): any => { | |
16 | + return Promise.resolve({ articles: [{ name: "article1" }], headers: (name: string) => { return name; } }); | |
17 | 17 | } |
18 | 18 | }; |
19 | 19 | let profile = { name: 'profile-name' }; |
... | ... | @@ -25,8 +25,8 @@ describe("Components", () => { |
25 | 25 | function getProviders() { |
26 | 26 | return [ |
27 | 27 | new Provider('$state', { useValue: state }), |
28 | - new Provider('ArticleService', { | |
29 | - useValue: mockedArticleService | |
28 | + new Provider('BlockService', { | |
29 | + useValue: mockedBlockService | |
30 | 30 | }), |
31 | 31 | ].concat(provideFilters("truncateFilter", "stripTagsFilter")); |
32 | 32 | } |
... | ... | @@ -44,7 +44,7 @@ describe("Components", () => { |
44 | 44 | } |
45 | 45 | |
46 | 46 | |
47 | - it("get recent documents from the article service", done => { | |
47 | + it("get recent documents from the block service", done => { | |
48 | 48 | tcb.createAsync(getComponent()).then(fixture => { |
49 | 49 | let recentDocumentsBlock: RecentDocumentsBlockComponent = fixture.debugElement.componentViewChildren[0].componentInstance; |
50 | 50 | expect(recentDocumentsBlock.documents).toEqual([{ name: "article1" }]); |
... | ... | @@ -61,20 +61,5 @@ describe("Components", () => { |
61 | 61 | }); |
62 | 62 | }); |
63 | 63 | |
64 | - it("it uses default limit 5 if not defined on block", done => { | |
65 | - settingsObj = null; | |
66 | - mockedArticleService = jasmine.createSpyObj("mockedArticleService", ["getByProfile"]); | |
67 | - (<any>mockedArticleService).mocked = true; | |
68 | - let thenMocked = jasmine.createSpy("then"); | |
69 | - mockedArticleService.getByProfile = jasmine.createSpy("getByProfile").and.returnValue({then: thenMocked}); | |
70 | - let getByProfileFunct = mockedArticleService.getByProfile; | |
71 | - tcb.createAsync(getComponent()).then(fixture => { | |
72 | - let recentDocumentsBlock: RecentDocumentsBlockComponent = fixture.debugElement.componentViewChildren[0].componentInstance; | |
73 | - recentDocumentsBlock.openDocument({ path: "path", profile: { identifier: "identifier" } }); | |
74 | - expect(getByProfileFunct).toHaveBeenCalledWith(profile, { content_type: 'TinyMceArticle', per_page: 5 }); | |
75 | - done(); | |
76 | - }); | |
77 | - }); | |
78 | - | |
79 | 64 | }); |
80 | 65 | }); | ... | ... |
src/app/layout/blocks/recent-documents/recent-documents-block.component.ts
1 | 1 | import {Component, Inject, Input} from "ng-forward"; |
2 | -import {ArticleService} from "../../../../lib/ng-noosfero-api/http/article.service"; | |
2 | +import {BlockService} from "../../../../lib/ng-noosfero-api/http/block.service"; | |
3 | 3 | |
4 | 4 | @Component({ |
5 | 5 | selector: "noosfero-recent-documents-block", |
6 | 6 | templateUrl: 'app/layout/blocks/recent-documents/recent-documents-block.html' |
7 | 7 | }) |
8 | -@Inject(ArticleService, "$state") | |
8 | +@Inject(BlockService, "$state") | |
9 | 9 | export class RecentDocumentsBlockComponent { |
10 | 10 | |
11 | 11 | @Input() block: any; |
... | ... | @@ -13,23 +13,15 @@ export class RecentDocumentsBlockComponent { |
13 | 13 | |
14 | 14 | profile: any; |
15 | 15 | documents: any; |
16 | - | |
17 | 16 | documentsLoaded: boolean = false; |
18 | 17 | |
19 | - constructor(private articleService: ArticleService, private $state: any) { | |
20 | - } | |
18 | + constructor(private blockService: BlockService, private $state: any) { } | |
21 | 19 | |
22 | 20 | ngOnInit() { |
23 | 21 | this.profile = this.owner; |
24 | 22 | this.documents = []; |
25 | - | |
26 | - let limit = ((this.block && this.block.settings) ? this.block.settings.limit : null) || 5; | |
27 | - // FIXME get all text articles | |
28 | - // FIXME make the getByProfile a generic method where we tell the type passing a class TinyMceArticle | |
29 | - // and the promise should be of type TinyMceArticle[], per example | |
30 | - this.articleService.getByProfile(this.profile, { content_type: 'TinyMceArticle', per_page: limit }) | |
31 | - .then((result: noosfero.RestResult<noosfero.Article[]>) => { | |
32 | - this.documents = <noosfero.Article[]>result.data; | |
23 | + this.blockService.getApiContent(this.block).then((content: any) => { | |
24 | + this.documents = content.articles; | |
33 | 25 | this.documentsLoaded = true; |
34 | 26 | }); |
35 | 27 | } | ... | ... |
... | ... | @@ -0,0 +1,40 @@ |
1 | +import { Injectable, Inject } from "ng-forward"; | |
2 | +import {RestangularService} from "./restangular_service"; | |
3 | +import {ProfileService} from "./profile.service"; | |
4 | + | |
5 | +@Injectable() | |
6 | +@Inject("Restangular", "$q", "$log") | |
7 | +export class BlockService extends RestangularService<noosfero.Block> { | |
8 | + | |
9 | + constructor(Restangular: restangular.IService, $q: ng.IQService, $log: ng.ILogService) { | |
10 | + super(Restangular, $q, $log); | |
11 | + } | |
12 | + | |
13 | + getResourcePath() { | |
14 | + return "blocks"; | |
15 | + } | |
16 | + | |
17 | + getDataKeys() { | |
18 | + return { | |
19 | + singular: 'block', | |
20 | + plural: 'blocks' | |
21 | + }; | |
22 | + } | |
23 | + | |
24 | + getApiContent(block: noosfero.Block) { | |
25 | + let apiContentPromise = this.$q.defer(); | |
26 | + if (block) { | |
27 | + if (block.api_content) { | |
28 | + apiContentPromise.resolve(block.api_content); | |
29 | + } else { | |
30 | + this.get(block.id) | |
31 | + .then((result: noosfero.RestResult<noosfero.Block>) => { | |
32 | + block = result.data; | |
33 | + apiContentPromise.resolve(block.api_content); | |
34 | + }); | |
35 | + } | |
36 | + } | |
37 | + return apiContentPromise.promise; | |
38 | + } | |
39 | + | |
40 | +} | ... | ... |