Files
marketplaces/src/app/features/project-editor/sections/homepage-section.component.ts
sdarbinyan ff53265fc0
Some checks failed
Architecture Governance / architecture (push) Has been cancelled
fix(widgets): stop silently discarding invalid JSON edits in the props fallback textarea
updateJson() caught JSON.parse failures and did nothing, but the textarea
was bound to propsJson(committed props) - so on the next change-detection
pass, any in-progress invalid edit snapped back to the last-saved value
with zero feedback. Verified live via window.ng.getComponent(): typing
invalid JSON now keeps the user's draft on screen with an inline error;
fixing it commits and clears the draft/error.

Also: homepage-section drop() used CdkDragDrop<any[]> - switched to
unknown[] per the no-any rule, no behavior change.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-07-17 18:14:58 +04:00

88 lines
3.8 KiB
TypeScript

import { CdkDragDrop, DragDropModule, moveItemInArray } from '@angular/cdk/drag-drop';
import { ChangeDetectionStrategy, Component, computed, inject } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { ProjectEditorFacade } from '../facade/project-editor.facade';
import { TranslatePipe } from '../../../i18n/translate.pipe';
import { TranslateService } from '../../../i18n/translate.service';
import { InputComponent } from '../../../shared/ui/input/input.component';
import { SectionCardComponent } from '../../../shared/ui/section-card/section-card.component';
import { ToggleComponent } from '../../../shared/ui/toggle/toggle.component';
export interface LayoutStrategyOption {
value: 'stack' | 'grid' | 'hero' | 'carousel' | 'split';
labelKey: string;
descKey: string;
}
@Component({
selector: 'app-project-editor-homepage-section',
standalone: true,
imports: [DragDropModule, FormsModule, TranslatePipe, InputComponent, SectionCardComponent, ToggleComponent],
templateUrl: './homepage-section.component.html',
styleUrls: ['./section.shared.scss'],
changeDetection: ChangeDetectionStrategy.OnPush
})
export class ProjectEditorHomepageSectionComponent {
private readonly facade = inject(ProjectEditorFacade);
private readonly translate = inject(TranslateService);
readonly homePage = this.facade.homepagePage;
readonly fieldError = (key: string): string | null => {
const messageKey = this.facade.fieldError(key);
return messageKey ? this.translate.t(messageKey) : null;
};
readonly sections = computed(() => [...(this.homePage()?.sections ?? [])].sort((a, b) => a.order - b.order));
readonly layoutStrategyOptions: LayoutStrategyOption[] = [
{ value: 'stack', labelKey: 'builder.layoutStrategyStack', descKey: 'builder.layoutStrategyStackDesc' },
{ value: 'grid', labelKey: 'builder.layoutStrategyGrid', descKey: 'builder.layoutStrategyGridDesc' },
{ value: 'hero', labelKey: 'builder.layoutStrategyHero', descKey: 'builder.layoutStrategyHeroDesc' },
{ value: 'carousel', labelKey: 'builder.layoutStrategyCarousel', descKey: 'builder.layoutStrategyCarouselDesc' },
{ value: 'split', labelKey: 'builder.layoutStrategySplit', descKey: 'builder.layoutStrategySplitDesc' },
];
layoutStrategyDescKey(strategy: string | undefined): string {
return this.layoutStrategyOptions.find(option => option.value === strategy)?.descKey ?? '';
}
drop(event: CdkDragDrop<unknown[]>): void {
const sections = [...this.sections()];
moveItemInArray(sections, event.previousIndex, event.currentIndex);
this.facade.updateBootstrap(current => ({
...current,
pages: current.pages.map(page => page.id !== this.homePage()?.id ? page : ({
...page,
sections: sections.map((section, index) => ({ ...section, order: index + 1 }))
}))
}));
}
updateSection(sectionId: string, field: 'visible' | 'type', value: unknown): void {
this.facade.updateBootstrap(current => ({
...current,
pages: current.pages.map(page => page.id !== this.homePage()?.id ? page : ({
...page,
sections: page.sections.map(section => section.id !== sectionId ? section : ({
...section,
...(field === 'type' ? { type: String(value) } : { visible: Boolean(value) })
}))
}))
}));
}
updateLayout(sectionId: string, key: 'strategy' | 'columns', value: string): void {
this.facade.updateBootstrap(current => ({
...current,
pages: current.pages.map(page => page.id !== this.homePage()?.id ? page : ({
...page,
sections: page.sections.map(section => section.id !== sectionId ? section : ({
...section,
layout: {
...section.layout,
[key]: key === 'columns' ? Number(value) || 1 : value,
}
}))
}))
}));
}
}