Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(edit-content): Issues with n-1 relationship #31103

Merged
merged 6 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -140,13 +140,15 @@
<p-button
[label]="'Cancel' | dm"
[outlined]="true"
(onClick)="closeDialog()"
(onClick)="cancel()"
[text]="true"
severity="primary" />
severity="primary"
data-testId="cancel-button" />
<p-button
[disabled]="totalItems === 0"
(onClick)="closeDialog()"
[label]="$applyLabel()" />
(onClick)="applyChanges()"
[label]="$applyLabel()"
data-testId="apply-button" />
</div>
</div>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -105,18 +105,24 @@ describe('DotSelectExistingContentComponent', () => {
];
spectator.component.$selectedItems.set(mockItems);

spectator.component.closeDialog();
spectator.component.applyChanges();

expect(dialogRef.close).toHaveBeenCalledWith(mockItems);
});

it('should close dialog with empty array when no items selected', () => {
spectator.component.$selectedItems.set([]);

spectator.component.closeDialog();
spectator.component.applyChanges();

expect(dialogRef.close).toHaveBeenCalledWith([]);
});

it('should close dialog when cancel button is clicked', () => {
spectator.component.cancel();

expect(dialogRef.close).toHaveBeenCalledWith();
});
});

describe('Selected Items State', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,18 @@ export class DotSelectExistingContentComponent implements OnInit {
* A method that closes the existing content dialog.
* It sets the visibility signal to false, hiding the dialog.
*/
closeDialog() {
applyChanges() {
this.#dialogRef.close(this.$items());
}

/**
* A method that closes the existing content dialog.
* It sets the visibility signal to false, hiding the dialog.
*/
cancel() {
this.#dialogRef.close();
}

/**
* Checks if an item is selected.
* @param item - The item to check.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,12 +105,6 @@ export class DotEditContentRelationshipFieldComponent implements ControlValueAcc
command: () => {
this.showExistingContentDialog();
}
},
{
label: this.#dotMessageService.get('dot.file.relationship.field.table.new.content'),
command: () => {
// TODO: Implement new content
}
}
];
});
Expand Down Expand Up @@ -260,7 +254,7 @@ export class DotEditContentRelationshipFieldComponent implements ControlValueAcc

this.#dialogRef.onClose
.pipe(
filter((file) => !!file),
filter((items) => !!items),
takeUntilDestroyed(this.#destroyRef)
)
.subscribe((items: DotCMSContentlet[]) => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { RelationshipTypes } from './models/relationship.models';

/**
* Maps cardinality numbers to RelationshipTypes enum values.
* Used to determine the type of relationship between content types.
*
* @constant
* @type {Record<number, RelationshipTypes>}
*
* @property {RelationshipTypes} 0 - One-to-Many relationship type
* @property {RelationshipTypes} 1 - Many-to-Many relationship type
* @property {RelationshipTypes} 2 - One-to-One relationship type
* @property {RelationshipTypes} 3 - Many-to-One relationship type
*/

export const RELATIONSHIP_OPTIONS = {
0: RelationshipTypes.ONE_TO_MANY,
1: RelationshipTypes.MANY_TO_MANY,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import { RelationshipTypes } from '../models/relationship.models';
describe('Relationship Field Utils', () => {
describe('getSelectionModeByCardinality', () => {
it('should return "single" for ONE_TO_ONE relationship', () => {
// Find the cardinality value for ONE_TO_ONE
const oneToOneCardinality = Object.entries(RELATIONSHIP_OPTIONS).find(
([_, value]) => value === RelationshipTypes.ONE_TO_ONE
)?.[0];
Expand All @@ -17,8 +16,16 @@ describe('Relationship Field Utils', () => {
expect(result).toBe('single');
});

it('should return "single" for MANY_TO_ONE relationship', () => {
const manyToOneCardinality = Object.entries(RELATIONSHIP_OPTIONS).find(
([_, value]) => value === RelationshipTypes.MANY_TO_ONE
)?.[0];

const result = getSelectionModeByCardinality(Number(manyToOneCardinality));
expect(result).toBe('single');
});

it('should return "multiple" for ONE_TO_MANY relationship', () => {
// Find the cardinality value for ONE_TO_MANY
const oneToManyCardinality = Object.entries(RELATIONSHIP_OPTIONS).find(
([_, value]) => value === RelationshipTypes.ONE_TO_MANY
)?.[0];
Expand All @@ -28,7 +35,10 @@ describe('Relationship Field Utils', () => {
});

it('should throw error for invalid cardinality', () => {
expect(() => getSelectionModeByCardinality(999)).toThrow('Invalid relationship type');
const invalidCardinality = 999;
expect(() => getSelectionModeByCardinality(invalidCardinality)).toThrow(
`Invalid relationship type for cardinality: ${invalidCardinality}`
);
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ export function getSelectionModeByCardinality(cardinality: number) {
throw new Error(`Invalid relationship type for cardinality: ${cardinality}`);
}

return relationshipType === RelationshipTypes.ONE_TO_ONE ? 'single' : 'multiple';
const isSingleMode =
relationshipType === RelationshipTypes.ONE_TO_ONE ||
relationshipType === RelationshipTypes.MANY_TO_ONE;

return isSingleMode ? 'single' : 'multiple';
}

/**
Expand Down