Skip to content

Commit

Permalink
fix(frontend): fix filter saving
Browse files Browse the repository at this point in the history
  • Loading branch information
pYassine committed Nov 27, 2024
1 parent d9b10fb commit b4d3de9
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 354 deletions.
3 changes: 2 additions & 1 deletion packages/backend/src/usagers/dto/create-note.dto.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { ApiProperty } from "@nestjs/swagger";
import { IsNotEmpty, IsString, MaxLength, MinLength } from "class-validator";
import { StripTagsTransform } from "../../_common/decorators";
import { StripTagsTransform, Trim } from "../../_common/decorators";

export class CreateNoteDto {
@ApiProperty({
Expand All @@ -13,5 +13,6 @@ export class CreateNoteDto {
@MaxLength(1000)
@IsString()
@StripTagsTransform()
@Trim()
public message!: string;
}
8 changes: 4 additions & 4 deletions packages/backend/src/util/search/normalize-string.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ describe("normalizeString", () => {
expect(normalizeString("test test")).toBe("test test");
expect(normalizeString("test\t\ttest")).toBe("test test");
expect(normalizeString("test\n\ntest")).toBe("test test");
expect(normalizeString(" test ")).toBe("test");
expect(normalizeString(" test ")).toBe(" test ");
});

test("phrases complètes", () => {
expect(normalizeString("L'été sera TRÈS chaud!")).toBe(
"l ete sera tres chaud"
"l ete sera tres chaud "
);
expect(normalizeString("Une chaîne avec\tdes\ttabulations")).toBe(
"une chaine avec des tabulations"
Expand All @@ -44,12 +44,12 @@ describe("normalizeString", () => {
test("caractères spéciaux", () => {
expect(normalizeString("100% sûr")).toBe("100 sur");
expect(normalizeString("test&test")).toBe("test test");
expect(normalizeString("prix: 15€")).toBe("prix 15");
expect(normalizeString("prix: 15€")).toBe("prix 15 ");
});

test("cas limites", () => {
expect(normalizeString("")).toBe("");
expect(normalizeString(" ")).toBe("");
expect(normalizeString(" ")).toBe(" ");
expect(normalizeString(null)).toBe("");
expect(normalizeString(undefined)).toBe("");
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { Component, EventEmitter, Input, Output } from "@angular/core";
import { UsagersFilterCriteria } from "../usager-filter/UsagersFilterCriteria";
import { Component, EventEmitter, Input, OnInit, Output } from "@angular/core";
import {
UsagersFilterCriteria,
UsagersFilterCriteriaSortKey,
} from "../usager-filter/UsagersFilterCriteria";
import {
extractDeadlines,
UsagersFilterCriteriaDernierPassage,
Expand All @@ -11,7 +14,7 @@ import {
templateUrl: "./manage-filters.component.html",
styleUrls: ["../manage-usagers-page/manage-usagers-page.component.scss"],
})
export class ManageFiltersComponent {
export class ManageFiltersComponent implements OnInit {
@Input() public filters: UsagersFilterCriteria;
@Input() public usagersRadiesLoadedCount: number;
@Input() public usagersRadiesTotalCount: number;
Expand Down Expand Up @@ -46,10 +49,22 @@ export class ManageFiltersComponent {
};

public sortLabel = "échéance";
public sortMenuItems = this.getSortKeys();
public sortMenuItems: Array<{
id: UsagersFilterCriteriaSortKey;
label: string;
}> = [];

public getSortKeys() {
const sortElements = [
ngOnInit(): void {
this.sortMenuItems = this.getSortKeys();
}
public getSortKeys(): Array<{
id: UsagersFilterCriteriaSortKey;
label: string;
}> {
const sortElements: Array<{
id: UsagersFilterCriteriaSortKey;
label: string;
}> = [
{ id: "ID", label: "ID" },
{ id: "NAME", label: "nom" },
{ id: "ECHEANCE", label: this.getEcheanceLabel() },
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -496,9 +496,9 @@ export class ManageUsagersPageComponent
filters: UsagersFilterCriteria;
allUsagers: UsagerLight[];
}): void {
console.log("applyFilters");
this.searching = true;
this.resetCheckboxes();
localStorage.setItem("MANAGE_USAGERS", JSON.stringify(filters));

this.filteredUsagers = usagersFilter.filter(allUsagers, {
criteria: filters,
Expand Down Expand Up @@ -542,12 +542,16 @@ export class ManageUsagersPageComponent
this.usagers = [];
return;
}

console.log({
sortKey: this.filters.sortKey,
sortValue: this.filters.sortValue,
});
this.filteredUsagers = usagersSorter.sortBy(this.filteredUsagers, {
sortKey: this.filters.sortKey,
sortValue: this.filters.sortValue,
}) as UsagerFormModel[];

this.setFilters();
this.applyPaginationFromStore();
}

Expand Down Expand Up @@ -593,6 +597,9 @@ export class ManageUsagersPageComponent
this.filters.sortValue = sortValue;
}

private setFilters() {
localStorage.setItem("MANAGE_USAGERS", JSON.stringify(this.filters));
}
private getFilters(): null | Partial<UsagersFilterCriteria> {
const filters = localStorage.getItem("MANAGE_USAGERS");
return filters === null ? {} : JSON.parse(filters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ export class UsagersFilterCriteria {
public page: number;

constructor(search?: Partial<UsagersFilterCriteria> | null) {
console.log({ search });
this.interactionType = search?.interactionType || null;
this.lastInteractionDate = search?.lastInteractionDate || null;
this.entretien = search?.entretien || null;
Expand All @@ -51,12 +52,5 @@ export class UsagersFilterCriteria {

this.sortKey = search?.sortKey || "NAME";
this.sortValue = search?.sortValue || "asc";

// Ne pas trier par autre que les nom & ID si on est sur TOUS
if (this.statut === "TOUS") {
if (this.sortKey !== "ID" && this.sortKey !== "NAME") {
this.sortKey = "NAME";
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ function check({
const interactionTime = new Date(
usager.lastInteraction.dateInteraction
).getTime();
console.log({ deadlineTime, interactionTime });
return deadlineTime > interactionTime;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ function sortBy(
asc,
}
);

console.log({ sortAttributes });
return sortAttributes;
},
});
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -6,107 +6,8 @@ import { SortableAttributeType } from "./SortableAttributeType.type";
export const dataObjectCompare = {
buildCompareAttributes,
compareComparableObjects,
compareObjects,
objectsEquals,
};

function objectsEquals<T>(
a: T,
b: T,
{
attributes,
}: {
attributes: (item: T) => any[];
}
): boolean {
return compareObjectsEquals(a, b, {
attributes: (item) => {
const values = item ? attributes(item) : undefined;
if (values) {
return values.map((value) => ({
value,
}));
}
return undefined;
},
});
}

function compareObjectsEquals<T>(
a: T,
b: T,
{
attributes,
}: {
attributes: (item: T) => SortableAttribute[];
}
): boolean {
return compareComparableObjectsEquals(
{
item: a,
attributes: buildCompareAttributes(a, attributes),
},
{
item: b,
attributes: buildCompareAttributes(b, attributes),
}
);
}

function compareComparableObjectsEquals<T>(
a: DataComparableObject<T>,
b: DataComparableObject<T>
): boolean {
const nullComparison = dataCompare.compareNullValues<T>(a.item, b.item, {});
if (nullComparison !== undefined) {
return false;
}
const attrs1 = a.attributes;
const attrs2 = b.attributes;

return attrs1.reduce((comparison, attr1, i) => {
if (comparison) {
return attr1.value === attrs2[i].value;
}
return comparison;
}, true as boolean);
}

function compareObjects<T>(
a: T,
b: T,
{
asc,
nullFirst,
attributes,
}: {
attributes: (item: T) => SortableAttribute[];
asc?: boolean;
nullFirst?: boolean;
}
): DataComparisonResult {
const globalAsc = asc !== false;

if (nullFirst === undefined) {
nullFirst = false;
}

return compareComparableObjects(
{
item: a,
attributes: buildCompareAttributes(a, attributes),
},
{
item: b,
attributes: buildCompareAttributes(b, attributes),
},
{
nullFirst,
globalAsc,
}
);
}

export type DataComparableObjectAttribute = {
value: any;
asc: boolean;
Expand Down
Loading

0 comments on commit b4d3de9

Please sign in to comment.