diff --git a/src/Squidex.Core/Contents/ContentData.cs b/src/Squidex.Core/Contents/ContentData.cs index 8d1cd11cd..2e959e77a 100644 --- a/src/Squidex.Core/Contents/ContentData.cs +++ b/src/Squidex.Core/Contents/ContentData.cs @@ -48,7 +48,7 @@ namespace Squidex.Core.Contents return new ContentData(request.ToImmutableDictionary(x => x.Key, x => new ContentFieldData(x.Value.ToImmutableDictionary(StringComparer.OrdinalIgnoreCase)), StringComparer.OrdinalIgnoreCase)); } - public Dictionary> ToApiResponse(Schema schema, IReadOnlyCollection languages, Language masterLanguage) + public Dictionary> ToApiResponse(Schema schema, IReadOnlyCollection languages, Language masterLanguage, bool excludeHidden = true) { Guard.NotNull(schema, nameof(schema)); Guard.NotNull(languages, nameof(languages)); @@ -62,7 +62,7 @@ namespace Squidex.Core.Contents { Field field; - if (!schema.FieldsByName.TryGetValue(fieldValue.Key, out field) || field.IsHidden) + if (!schema.FieldsByName.TryGetValue(fieldValue.Key, out field) || (!excludeHidden && field.IsHidden)) { continue; } diff --git a/src/Squidex/Controllers/ContentApi/ContentsController.cs b/src/Squidex/Controllers/ContentApi/ContentsController.cs index bd141fb3b..47f0c35ce 100644 --- a/src/Squidex/Controllers/ContentApi/ContentsController.cs +++ b/src/Squidex/Controllers/ContentApi/ContentsController.cs @@ -42,7 +42,7 @@ namespace Squidex.Controllers.ContentApi [HttpGet] [Route("content/{app}/{name}")] - public async Task GetContents(string name, [FromQuery] string query = null, [FromQuery] int? take = null, [FromQuery] int? skip = null, [FromQuery] bool nonPublished = false) + public async Task GetContents(string name, [FromQuery] string query = null, [FromQuery] int? take = null, [FromQuery] int? skip = null, [FromQuery] bool nonPublished = false, [FromQuery] bool hidden = false) { var schemaEntity = await schemaProvider.FindSchemaByNameAsync(AppId, name); @@ -77,7 +77,7 @@ namespace Squidex.Controllers.ContentApi [HttpGet] [Route("content/{app}/{name}/{id}")] - public async Task GetContent(string name, Guid id) + public async Task GetContent(string name, Guid id, bool hidden = false) { var schemaEntity = await schemaProvider.FindSchemaByNameAsync(AppId, name); @@ -97,7 +97,7 @@ namespace Squidex.Controllers.ContentApi if (content.Data != null) { - model.Data = content.Data.ToApiResponse(schemaEntity.Schema, App.Languages, App.MasterLanguage); + model.Data = content.Data.ToApiResponse(schemaEntity.Schema, App.Languages, App.MasterLanguage, hidden); } return Ok(model); diff --git a/src/Squidex/app/features/content/module.ts b/src/Squidex/app/features/content/module.ts index a50227464..06700cc4e 100644 --- a/src/Squidex/app/features/content/module.ts +++ b/src/Squidex/app/features/content/module.ts @@ -11,6 +11,7 @@ import { RouterModule, Routes } from '@angular/router'; import { HistoryComponent, ResolveAppLanguagesGuard, + ResolveContentGuard, ResolvePublishedSchemaGuard, SqxFrameworkModule, SqxSharedModule @@ -40,13 +41,7 @@ const routes: Routes = [ children: [ { path: 'new', - component: ContentPageComponent, - resolve: { - schema: ResolvePublishedSchemaGuard, appLanguages: ResolveAppLanguagesGuard - }, - data: { - disableHistory: true - } + component: ContentPageComponent }, { path: 'history', component: HistoryComponent, @@ -57,7 +52,7 @@ const routes: Routes = [ path: ':contentId', component: ContentPageComponent, resolve: { - schema: ResolvePublishedSchemaGuard, appLanguages: ResolveAppLanguagesGuard + content: ResolveContentGuard }, children: [ { diff --git a/src/Squidex/app/features/content/pages/content/content-page.component.ts b/src/Squidex/app/features/content/pages/content/content-page.component.ts index c9f37098b..ba2d5704d 100644 --- a/src/Squidex/app/features/content/pages/content/content-page.component.ts +++ b/src/Squidex/app/features/content/pages/content/content-page.component.ts @@ -7,14 +7,15 @@ import { Component } from '@angular/core'; import { AbstractControl, FormControl, FormGroup, ValidatorFn, Validators } from '@angular/forms'; -import { ActivatedRoute } from '@angular/router'; +import { ActivatedRoute, Router } from '@angular/router'; -import { ContentAdded } from './../messages'; +import { ContentChanged } from './../messages'; import { AppComponentBase, AppLanguageDto, AppsStoreService, + ContentDto, ContentsService, MessageBus, NotificationService, @@ -35,32 +36,34 @@ export class ContentPageComponent extends AppComponentBase { public contentFormSubmitted = false; public contentForm: FormGroup; + public content: ContentDto = null; public languages: AppLanguageDto[] = []; - public isNewMode = false; + public get isNewMode() { + return this.content !== null; + } constructor(apps: AppsStoreService, notifications: NotificationService, users: UsersProviderService, private readonly contentsService: ContentsService, private readonly route: ActivatedRoute, + private readonly router: Router, private readonly messageBus: MessageBus ) { super(apps, notifications, users); } public ngOnInit() { - this.route.params.map(p => p['contentId']).subscribe(contentId => { - this.isNewMode = !contentId; - }); - - this.route.data.map(p => p['appLanguages']).subscribe((languages: AppLanguageDto[]) => { + this.route.parent.data.map(p => p['appLanguages']).subscribe((languages: AppLanguageDto[]) => { this.languages = languages; }); - this.route.data.map(p => p['schema']).subscribe((schema: SchemaDetailsDto) => { - this.schema = schema; + this.route.parent.data.map(p => p['schema']).subscribe((schema: SchemaDetailsDto) => { + this.setupForm(schema, this.route.snapshot.data['content']); + }); - this.setupForm(schema); + this.route.data.map(p => p['content']).subscribe((content: ContentDto) => { + this.populateForm(content); }); } @@ -73,14 +76,21 @@ export class ContentPageComponent extends AppComponentBase { const data = this.contentForm.value; this.appName() - .switchMap(app => this.contentsService.postContent(app, this.schema.name, data)) - .subscribe(() => { - this.reset(); - this.messageBus.publish(new ContentAdded()); - }, error => { - this.notifyError(error); - this.enable(); - }); + .switchMap(app => { + if (this.isNewMode) { + return this.contentsService.postContent(app, this.schema.name, data); + } else { + return this.contentsService.putContent(app, this.schema.name, data, this.content.id); + } + }) + .subscribe(() => { + this.router.navigate(['../'], { relativeTo: this.route }); + + this.messageBus.publish(new ContentChanged()); + }, error => { + this.notifyError(error); + this.enable(); + }); } } @@ -107,7 +117,9 @@ export class ContentPageComponent extends AppComponentBase { } } - private setupForm(schema: SchemaDetailsDto) { + private setupForm(schema: SchemaDetailsDto, content?: ContentDto) { + this.schema = schema; + const controls: { [key: string]: AbstractControl } = {}; for (const field of schema.fields) { @@ -146,5 +158,22 @@ export class ContentPageComponent extends AppComponentBase { this.contentForm = new FormGroup(controls); } + + private populateForm(content: ContentDto) { + this.content = content; + + for (const field of this.schema.fields) { + const fieldValue = content.data[field.name] || {}; + const fieldForm = this.contentForm.controls[field.name]; + + if (field.properties.isLocalizable) { + for (let language of this.languages) { + fieldForm.controls[language.iso2Code].setValue(fieldValue[language.iso2Code]); + } + } else { + fieldForm.controls['iv'].setValue(fieldValue['iv']); + } + } + } } diff --git a/src/Squidex/app/features/content/pages/contents/contents-page.component.html b/src/Squidex/app/features/content/pages/contents/contents-page.component.html index 701f4f215..6785d8f0f 100644 --- a/src/Squidex/app/features/content/pages/contents/contents-page.component.html +++ b/src/Squidex/app/features/content/pages/contents/contents-page.component.html @@ -52,7 +52,7 @@