Skip to content

[USH-1632] Use only parameters for category in web client #1492

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

Merged
merged 4 commits into from
Dec 2, 2024
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
@@ -1,6 +1,6 @@
import { Component, QueryList, ViewChildren } from '@angular/core';
import { CategoryItemComponent } from './category-item/category-item.component';
import { CategoriesService, CategoryInterface } from '@mzima-client/sdk';
import { apiHelpers, CategoriesService, CategoryInterface } from '@mzima-client/sdk';
import { forkJoin } from 'rxjs';
import { ConfirmModalService, NotificationService } from '@services';
import { TranslateService } from '@ngx-translate/core';
Expand All @@ -27,11 +27,13 @@ export class CategoriesComponent {
}

public getCategories(): void {
this.categoriesService.get().subscribe({
next: (data) => {
this.categories = data.results;
},
});
this.categoriesService
.getCategories({ only: apiHelpers.ONLY.TAG_ID_PARENTID_PARENT_SLUG_CHILDREN })
.subscribe({
next: (data) => {
this.categories = data.results;
},
});
}

public displayChildren(id: number) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angu
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import { MatDialog } from '@angular/material/dialog';
import { Router } from '@angular/router';
import { TranslationInterface, LanguageInterface } from '@mzima-client/sdk';
import { TranslationInterface, LanguageInterface, apiHelpers } from '@mzima-client/sdk';
import { UntilDestroy, untilDestroyed } from '@ngneat/until-destroy';
import { TranslateService } from '@ngx-translate/core';
import { formHelper } from '@helpers';
Expand Down Expand Up @@ -165,13 +165,15 @@ export class CreateCategoryFormComponent extends BaseComponent implements OnInit
}

private getCategories() {
this.categoriesService.get().subscribe({
next: (data) => {
this.categories = data.results
.filter((cat: CategoryInterface) => !cat.parent_id)
.filter((cat: CategoryInterface) => cat.id !== this.category?.id);
},
});
this.categoriesService
.getCategories({ only: apiHelpers.ONLY.TAG_ID_PARENTID_PARENT_SLUG })
.subscribe({
next: (data) => {
this.categories = data.results
.filter((cat: CategoryInterface) => !cat.parent_id)
.filter((cat: CategoryInterface) => cat.id !== this.category?.id);
},
});
}

private getRoles() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
CategoryInterface,
FormAttributeInterface,
SurveyItem,
apiHelpers,
} from '@mzima-client/sdk';
import { NotificationService } from '@services';
import _ from 'lodash';
Expand Down Expand Up @@ -118,7 +119,7 @@ export class CreateFieldModalComponent implements OnInit {
private getCategories() {
const array: MultilevelSelectOption[] = [];
this.categoriesService
.get()
.getCategories({ only: apiHelpers.ONLY.TAG_ID_PARENTID_CHILDREN })
.pipe(
map((res) => {
for (const category of res?.results) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -330,33 +330,35 @@ export class SearchFormComponent extends BaseComponent implements OnInit {
}

private getCategories() {
this.categoriesService.get().subscribe({
next: (response) => {
const mainResults = response?.results.filter((c: CategoryInterface) => !c.parent_id);
this.categoriesData = mainResults?.map((category: CategoryInterface) => {
return {
id: category.id,
name: category.tag,
children: category?.children?.map((cat: CategoryInterface) => {
return {
id: cat.id,
name: cat.tag,
};
}),
};
});
if (!this.categoriesData.length) {
document.body.classList.add('filters-panel-no-categories');
} else {
document.body.classList.remove('filters-panel-no-categories');
}
},
error: (err) => {
if (err.message.match(/Http failure response for/)) {
setTimeout(() => this.getCategories(), 2000);
}
},
});
this.categoriesService
.getCategories({ only: apiHelpers.ONLY.TAG_ID_PARENTID_CHILDREN })
.subscribe({
next: (response) => {
const mainResults = response?.results.filter((c: CategoryInterface) => !c.parent_id);
this.categoriesData = mainResults?.map((category: CategoryInterface) => {
return {
id: category.id,
name: category.tag,
children: category?.children?.map((cat: CategoryInterface) => {
return {
id: cat.id,
name: cat.tag,
};
}),
};
});
if (!this.categoriesData.length) {
document.body.classList.add('filters-panel-no-categories');
} else {
document.body.classList.remove('filters-panel-no-categories');
}
},
error: (err) => {
if (err.message.match(/Http failure response for/)) {
setTimeout(() => this.getCategories(), 2000);
}
},
});
}

private getActiveFilters(values: any): void {
Expand Down
3 changes: 3 additions & 0 deletions libs/sdk/src/lib/helpers/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export const ONLY = {
NAME_ID_DESCRIPTION: 'name,id,description',
NEEDED_POSTS_LIST_PROPERTIES:
'id,title,status,color,contact,locks,post_media,data_source_message_id,post_date',
TAG_ID_PARENTID_PARENT_SLUG: 'tag,id,parent_id,parent,slug',
TAG_ID_PARENTID_PARENT_SLUG_CHILDREN: 'tag,id,parent_id,parent,slug,children',
TAG_ID_PARENTID_CHILDREN: 'tag,id,parent_id,children',
};

export const API_V_3 = `api/v3/`;
Expand Down
4 changes: 4 additions & 0 deletions libs/sdk/src/lib/services/categories.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,8 @@ export class CategoriesService extends ResourceService<any> {
getResourceUrl(): string {
return 'categories';
}

public getCategories(params: any) {
return super.get('', params);
}
}
Loading