Skip to content

Commit

Permalink
fix: Refactor data model for reordering view
Browse files Browse the repository at this point in the history
  • Loading branch information
romeonicholas committed Nov 29, 2023
1 parent 1ed8ada commit a926fa8
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 34 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ <h2>Reorder models</h2>
>
<div
cdkDrag
*ngFor="let model of models"
*ngFor="let model of data.models"
class="flex items-center border-b border-solid"
>
<span class="ml-[10px]"
Expand All @@ -34,12 +34,7 @@ <h2>Reorder models</h2>
</div>

<div class="flex justify-end">
<button
mat-flat-button
color="primary"
(click)="reorderModels(models, project)"
class=""
>
<button mat-flat-button color="primary" (click)="reorderModels()" class="">
Save order
</button>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,58 +11,38 @@ import {
Model,
ModelService,
} from 'src/app/projects/models/service/model.service';
import {
Project,
ProjectService,
} from 'src/app/projects/service/project.service';
import {} from 'src/app/projects/service/project.service';
@Component({
selector: 'app-reorder-models',
templateUrl: './reorder-models.component.html',
})
export class ReorderModelsComponent {
models?: Model[];
project?: Project;

constructor(
public modelService: ModelService,
private dialogRef: MatDialogRef<ReorderModelsComponent>,
private toastService: ToastService,
public projectService: ProjectService,
@Inject(MAT_DIALOG_DATA)
public data: { projectSlug: string; models: Model[] },
) {
this.projectService.loadProjectsForRole('manager');
this.models = data.models;
this.projectService.project$.subscribe((project) => {
this.project = project;
});
}
) {}

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

async reorderModels(models?: Model[], project?: Project) {
if (models === undefined || project === undefined) {
return;
}

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

this.modelService
.updateModels(project.slug, modelsToPatch)
.updateModels(this.data.projectSlug, modelsToPatch)
.subscribe(() => {
this.toastService.showSuccess(
`Model order updated`,
`Successfully reordered models in project ${project.slug}`,
`Successfully reordered models in project ${this.data.projectSlug}`,
);
this.dialogRef.close();
});
Expand Down

0 comments on commit a926fa8

Please sign in to comment.