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

get categories childrens and add breadcrumb #23260

Merged
merged 5 commits into from
Nov 4, 2022
Merged
Show file tree
Hide file tree
Changes from all 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
@@ -0,0 +1,106 @@
import { TestBed } from '@angular/core/testing';
import { HttpClientTestingModule, HttpTestingController } from '@angular/common/http/testing';

import { CoreWebService } from '@dotcms/dotcms-js';

import { DotHttpErrorManagerService } from '@services/dot-http-error-manager/dot-http-error-manager.service';
import {
DotCategoriesService,
CATEGORY_API_URL,
CATEGORY_CHILDREN_API_URL
} from './dot-categories.service';
import { CoreWebServiceMock } from '@tests/core-web.service.mock';
import { of } from 'rxjs';
import { CATEGORY_SOURCE, DotCategory } from '@models/categories/dot-categories.model';

const mockCategory: DotCategory = {
categoryId: '1222',
categoryName: 'Test',
key: 'adsdsd',
sortOrder: 1,
deleted: false,
categoryVelocityVarName: 'sdsdsds',
friendlyName: 'asas',
identifier: '1222',
inode: '2121',
name: 'Test',
type: 'ggg',
source: CATEGORY_SOURCE.DB
};

describe('DotCategorysService', () => {
let service: DotCategoriesService;
let httpMock: HttpTestingController;

beforeEach(() => {
TestBed.configureTestingModule({
providers: [
DotCategoriesService,
{
provide: DotHttpErrorManagerService,
useValue: {
handle() {
return of({});
}
}
},
{
provide: CoreWebService,
useClass: CoreWebServiceMock
}
],
imports: [HttpClientTestingModule]
});
service = TestBed.inject(DotCategoriesService);

httpMock = TestBed.inject(HttpTestingController);
});

it('should get a categories list', () => {
service
.getCategories({
first: 0,
rows: 40,
sortOrder: 1,
filters: {},
globalFilter: null
})
.subscribe((categories: DotCategory[]) => {
expect(categories).toEqual([mockCategory]);
});

const req = httpMock.expectOne(`${CATEGORY_API_URL}?direction=ASC&per_page=40`);

expect(req.request.method).toBe('GET');

req.flush({
entity: [mockCategory]
});
});

it('should get a children categories list', () => {
service
.getChildrenCategories({
first: 0,
rows: 40,
sortOrder: 1,
filters: {
inode: { value: '123' }
},
globalFilter: null
})
.subscribe((categories: DotCategory[]) => {
expect(categories).toEqual([mockCategory]);
});

const req = httpMock.expectOne(
`${CATEGORY_CHILDREN_API_URL}?direction=ASC&per_page=40&inode=123`
);

expect(req.request.method).toBe('GET');

req.flush({
entity: [mockCategory]
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,49 @@ import { LazyLoadEvent } from 'primeng/api';
import { Observable } from 'rxjs';
import { OrderDirection, PaginatorService } from '../paginator';

export const CATEGORY_API_URL = 'v1/categories';

export const CATEGORY_CHILDREN_API_URL = 'v1/categories/children';

@Injectable()
export class DotCategoriesService extends PaginatorService {
constructor(coreWebService: CoreWebService) {
super(coreWebService);
this.url = 'v1/categories';
this.url = CATEGORY_API_URL;
}

updatePaginationService(event?: LazyLoadEvent) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why event here is optional?

const { sortField, sortOrder, filters } = event;
this.setExtraParams('inode', filters?.inode?.value || null);
this.filter = event?.filters?.global?.value || '';
this.sortField = sortField;
this.sortOrder = sortOrder === 1 ? OrderDirection.ASC : OrderDirection.DESC;
}

/**
* Get categories according to pagination and search
* @param {LazyLoadEvent} [filters]
* @param {LazyLoadEvent} [event]
* @return {*} {Observable<DotCategory[]>}
* @memberof DotCategoriesService
*/
getCategories(filters?: LazyLoadEvent): Observable<DotCategory[]> {
const { sortField, sortOrder } = filters;
const page = parseInt(String(filters.first / this.paginationPerPage), 10) + 1;
this.filter = filters?.filters?.global?.value || '';
this.sortField = sortField;
this.sortOrder = sortOrder === 1 ? OrderDirection.ASC : OrderDirection.DESC;
getCategories(event?: LazyLoadEvent): Observable<DotCategory[]> {
this.url = CATEGORY_API_URL;
this.updatePaginationService(event);
const page = parseInt(String(event.first / this.paginationPerPage), 10) + 1;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the page do not come in the event to avoid this calculation ?


return this.getPage(page);
}

/**
* Get children categories according to pagination and search
* @param {LazyLoadEvent} [event]
* @return {*} {Observable<DotCategory[]>}
* @memberof DotCategoriesService
*/
getChildrenCategories(event?: LazyLoadEvent): Observable<DotCategory[]> {
this.url = CATEGORY_CHILDREN_API_URL;
this.updatePaginationService(event);
const page = parseInt(String(event.first / this.paginationPerPage), 10) + 1;

return this.getPage(page);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ export class PaginatorService {
}

private setLinks(linksString: string): void {
const linkSplit = linksString.split(',');
const linkSplit = linksString?.split(',') || [];

linkSplit.forEach((linkRel) => {
const linkrealSplit = linkRel.split(';');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ComponentFixture, TestBed } from '@angular/core/testing';
import { DotCategoriesCreateEditComponent } from './dot-categories-create-edit.component';

import { DotPortletBaseModule } from '@components/dot-portlet-base/dot-portlet-base.module';
import { DotCategoriesListModule } from '../dot-categories-list/dot-categories-list.module';
import { DotCategoriesListingModule } from '../dot-categories-list/dot-categories-list.module';
import { TabViewModule } from 'primeng/tabview';

@Pipe({ name: 'dm' })
Expand All @@ -19,7 +19,7 @@ describe('CategoriesCreateEditComponent', () => {
beforeEach(async () => {
await TestBed.configureTestingModule({
declarations: [DotCategoriesCreateEditComponent, MockPipe],
imports: [DotPortletBaseModule, DotCategoriesListModule, TabViewModule]
imports: [DotPortletBaseModule, DotCategoriesListingModule, TabViewModule]
}).compileComponents();

fixture = TestBed.createComponent(DotCategoriesCreateEditComponent);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,12 @@
<dot-portlet-base *ngIf="vm$ | async as vm">
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need to update the testing on this.

<div class="px-3 py-2">
<p-breadcrumb
[model]="vm.categoryBreadCrumbs"
[home]="breadCrumbHome"
(onItemClick)="updateBreadCrumb($event)"
>
</p-breadcrumb>
</div>
<div class="category_listing">
<p-table
#dataTable
Expand All @@ -8,13 +16,13 @@
[rows]="vm.paginationPerPage"
[totalRecords]="vm.totalRecords"
[paginator]="true"
[metaKeySelection]="true"
[reorderableColumns]="true"
[columns]="vm.tableColumns"
(onLazyLoad)="loadCategories($event)"
(onRowSelect)="handleRowCheck()"
(onRowUnselect)="handleRowCheck()"
(onHeaderCheckboxToggle)="handleRowCheck()"
(onRowSelect)="handleRowCheck($event)"
(onRowUnselect)="handleRowCheck($event)"
(onHeaderCheckboxToggle)="handleRowCheck($event)"
(onFilter)="handleFilter()"
selectionMode="checkbox"
loadingIcon="fa fa-circle-o-notch fa-spin"
dataKey="categoryId"
Expand All @@ -30,7 +38,7 @@
type="button"
pButton
icon="pi pi-ellipsis-v"
attr.data-testid="actions"
attr.data-testId="actions"
></button>
<p-menu
#actionsMenu
Expand All @@ -40,31 +48,35 @@
></p-menu>
</div>
<div class="w-2">
<div class="p-inputgroup searchBox">
<span class="p-inputgroup-addon"><i class="pi pi-search"></i></span>
<div class="p-inputgroup">
<span class="border-right-none p-inputgroup-addon"
><i class="pi pi-search"></i
></span>
<input
class="border-left-none"
#gf
[placeholder]="'message.category.search' | dm"
(input)="dataTable.filterGlobal($event.target.value, 'contains')"
type="text"
pInputText
placeholder="Type to filter"
/>
</div>
</div>
<div>
<button
class="p-button-outlined"
[label]="'message.category.import' | dm"
pButton
pRipple
type="button"
label="IMPORT"
icon="pi pi-upload"
></button>
<button
class="p-button-outlined mx-4"
[label]="'message.category.export' | dm"
pButton
pRipple
type="button"
label="EXPORT"
icon="pi pi-download"
></button>
<button
Expand All @@ -78,14 +90,15 @@
</div>
</ng-template>
<ng-template pTemplate="header" let-columns>
<tr>
<tr data-testId="testHeadTableRow">
<th class="tableHeader" style="width: 3rem"></th>
<th class="tableHeader" style="width: 3rem">
<p-tableHeaderCheckbox></p-tableHeaderCheckbox>
</th>
<th
class="tableHeader"
*ngFor="let col of columns"
[ngStyle]="{ width: col.width }"
[pSortableColumn]="col.fieldName"
>
{{ col.header }}
Expand All @@ -94,21 +107,31 @@
</tr>
</ng-template>
<ng-template pTemplate="body" let-category let-index="rowIndex" let-columns="columns">
<tr [pReorderableRow]="index" [pSelectableRow]="category">
<tr
[pReorderableRow]="index"
[pSelectableRow]="category"
(click)="addBreadCrumb(category)"
data-testId="testTableRow"
>
<td>
<span class="pi pi-bars" pReorderableRowHandle></span>
</td>
<td style="width: 3rem">
<td (click)="$event.stopPropagation()" style="width: 3rem">
<p-tableCheckbox [value]="category"></p-tableCheckbox>
</td>
<td *ngFor="let col of columns">
<td *ngFor="let col of columns" [ngStyle]="{ width: col.width }">
<ng-container *ngIf="col.fieldName === 'sortOrder'; else Actions">
<p-inplace #editText styleClass="listing__categories__sortOrder">
<p-inplace
#editText
(click)="$event.stopPropagation()"
styleClass="category_listing__sortOrder"
>
<ng-template pTemplate="content">
<span class="p-input-icon-right">
<i class="pi pi-times" (click)="editText.deactivate()"></i>
<p-inputNumber
[ngModel]="category.sortOrder"
inputStyleClass="category_listing__sortOrder__field"
></p-inputNumber>
</span>
</ng-template>
Expand All @@ -119,22 +142,46 @@
</p-inplace>
</ng-container>
<ng-template #Actions>
<ng-container *ngIf="col.fieldName === 'Actions'; else third">
<ng-container *ngIf="col.fieldName === 'Actions'; else last">
<dot-action-menu-button
class="listing-categories__action-button"
[attr.data-testid]="category.categoryId"
[attr.data-testId]="category.categoryId"
[actions]="vm.categoriesActions"
[item]="category"
>
</dot-action-menu-button>
</ng-container>
</ng-template>
<ng-template #third>
<ng-template #last>
{{ category[col.fieldName] }}
</ng-template>
</td>
</tr>
</ng-template>
<ng-template pTemplate="emptymessage" let-columns>
<tr>
<td class="p-0" [attr.colspan]="columns.length + 3">
<div
class="category_listing-datatable__empty"
*ngIf="isContentFiltered; else emptyState"
data-testId="category_listing-datatable__empty"
>
{{ 'No-Results-Found' | dm }}
</div>
<ng-template #emptyState>
<dot-empty-state
[rows]="10"
[colsTextWidth]="[60, 50, 60, 80]"
[title]="'message.category.empty.title' | dm"
[content]="'message.category.empty.content' | dm"
[buttonLabel]="'message.category.empty.button.label' | dm"
icon="web"
>
</dot-empty-state>
</ng-template>
</td>
</tr>
</ng-template>
</p-table>
</div>
</dot-portlet-base>
Loading