Skip to content

Commit

Permalink
fix: Run "Reorder models" on copy of array to prevent side effects
Browse files Browse the repository at this point in the history
The models array on the parent component was implicitly modified, which
could lead to an incorrect model order if the dialog was closed without saving.
  • Loading branch information
MoritzWeber0 committed Dec 11, 2023
1 parent de564a6 commit 7eaf4a5
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ <h2>Reorder models</h2>
(cdkDropListDropped)="drop($event)"
class="my-[15px] rounded border border-solid"
>
@for (model of data.models; track model.id) {
@for (model of models; track model.id) {
<div
cdkDrag
class="flex cursor-move items-center border-b border-solid bg-white"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,20 @@ export class ReorderModelsDialogComponent {
private toastService: ToastService,
@Inject(MAT_DIALOG_DATA)
public data: { projectSlug: string; models: Model[] },
) {}
) {
this.models = [...data.models];
}

models?: Model[] = undefined;

drop(event: CdkDragDrop<Model[]>): void {
moveItemInArray(this.data.models, event.previousIndex, event.currentIndex);
if (this.models === undefined) return;
moveItemInArray(this.models, event.previousIndex, event.currentIndex);
}

async reorderModels() {
const modelsToPatch = this.data.models.map((model, index) => {
if (this.models === undefined) return;
const modelsToPatch = this.models.map((model, index) => {
return {
modelSlug: model.slug,
patchModel: { display_order: index + 1 },
Expand Down

0 comments on commit 7eaf4a5

Please sign in to comment.