This repository has been archived by the owner on Oct 24, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 21
Category Manager - list and view working; add+edit skeleton #190
Open
sdecianu
wants to merge
1
commit into
code4romania:master
Choose a base branch
from
sdecianu:feature/create_categories_manager
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { CommonModule } from '@angular/common'; | ||
import { CategoriesComponent } from './categories/categories.component'; | ||
import { CategoriesDashboardComponent } from './categories/components/categories-dashboard/categories-dashboard.component'; | ||
import { CategoryAddComponent } from './categories/components/category-add/category-add.component'; | ||
import { CategoryEditComponent } from './categories/components/category-edit/category-edit.component'; | ||
import { CategoryDetailsComponent } from './categories/components/category-details/category-details.component'; | ||
import { CategoriesRoutingModule } from '@app/pages/categories/categories.routing'; | ||
import { NgxMultiselectModule } from '@ngx-lib/multiselect'; | ||
import { SelectDropDownModule } from 'custom-select-dropdown'; | ||
import { NgbModule } from '@ng-bootstrap/ng-bootstrap'; | ||
import { FormsModule, ReactiveFormsModule } from '@angular/forms'; | ||
import { SharedModule } from '@app/shared'; | ||
|
||
@NgModule({ | ||
declarations: [ | ||
CategoriesComponent, | ||
CategoryAddComponent, | ||
CategoryEditComponent, | ||
CategoryDetailsComponent, | ||
CategoriesDashboardComponent | ||
], | ||
imports: [ | ||
NgxMultiselectModule, | ||
SelectDropDownModule, | ||
NgbModule, | ||
CommonModule, | ||
FormsModule, | ||
ReactiveFormsModule, | ||
SharedModule, | ||
CategoriesRoutingModule | ||
] | ||
}) | ||
export class CategoriesModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import { NgModule } from '@angular/core'; | ||
import { RoleGuard } from '@app/core'; | ||
import { RouterModule, Routes } from '@angular/router'; | ||
import { CategoriesComponent } from './categories/categories.component'; | ||
import { CategoriesDashboardComponent } from './categories/components/categories-dashboard/categories-dashboard.component'; | ||
import { CategoryAddComponent } from './categories/components/category-add/category-add.component'; | ||
import { CategoryEditComponent } from './categories/components/category-edit/category-edit.component'; | ||
import { CategoryDetailsComponent } from './categories/components/category-details/category-details.component'; | ||
|
||
const routes: Routes = [ | ||
{ | ||
path: '', | ||
component: CategoriesComponent, | ||
children: [ | ||
{ | ||
path: '', | ||
component: CategoriesDashboardComponent, | ||
canActivate: [RoleGuard], | ||
data: { roles: ['DSU'] } | ||
}, | ||
{ | ||
path: 'add', | ||
component: CategoryAddComponent, | ||
canActivate: [RoleGuard], | ||
data: { roles: ['DSU'] } | ||
}, | ||
{ | ||
path: 'edit/:id', | ||
component: CategoryEditComponent, | ||
canActivate: [RoleGuard], | ||
data: { roles: ['DSU'] } | ||
}, | ||
{ | ||
path: 'id/:id', | ||
component: CategoryDetailsComponent, | ||
canActivate: [RoleGuard], | ||
data: { roles: ['DSU'] } | ||
} | ||
] | ||
} | ||
]; | ||
|
||
@NgModule({ | ||
exports: [RouterModule], | ||
imports: [RouterModule.forChild(routes)] | ||
}) | ||
export class CategoriesRoutingModule {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { TestBed } from '@angular/core/testing'; | ||
|
||
import { CategoriesService } from './categories.service'; | ||
|
||
describe('CategoriesService', () => { | ||
beforeEach(() => TestBed.configureTestingModule({})); | ||
|
||
it('should be created', () => { | ||
const service: CategoriesService = TestBed.get(CategoriesService); | ||
expect(service).toBeTruthy(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,192 @@ | ||
import { Injectable } from '@angular/core'; | ||
import { Observable } from 'rxjs/internal/Observable'; | ||
import { HttpClient } from '@angular/common/http'; | ||
import { map, take } from 'rxjs/operators'; | ||
import * as lodash from 'lodash'; | ||
|
||
export interface Category { | ||
id: string; | ||
slug: string; | ||
name: string; | ||
subCategories: SubCategory[]; | ||
} | ||
|
||
export interface SubCategory { | ||
id: string; | ||
slug: string; | ||
name: string; | ||
parentCategory: Category; | ||
} | ||
|
||
interface ApiCategory { | ||
_id: string; | ||
parent_id: string | undefined; | ||
slug: string; | ||
name: string; | ||
} | ||
|
||
@Injectable({ | ||
providedIn: 'root' | ||
}) | ||
export class CategoriesService { | ||
constructor(private httpClient: HttpClient) {} | ||
|
||
/** | ||
* pager for categories table | ||
*/ | ||
pager: any = { | ||
sort: 1, | ||
method: 'ASC', | ||
page: 1, | ||
size: 15, | ||
total: 0, | ||
filters: {} | ||
}; | ||
|
||
/** | ||
* get the category pager | ||
* @returns pager | ||
*/ | ||
getPager() { | ||
return { ...this.pager }; | ||
} | ||
|
||
/** | ||
* init pager with default values | ||
*/ | ||
setPager() { | ||
this.pager = { | ||
sort: 1, | ||
method: 'ASC', | ||
page: 1, | ||
size: 15, | ||
total: 0, | ||
filters: {} | ||
}; | ||
} | ||
|
||
getCategoriesBySlug(slug?: string): Observable<Category[]> { | ||
const allCategories$: Observable<Category[]> = this.getAllCategories(); | ||
if (slug && slug !== '') { | ||
return allCategories$.pipe( | ||
map((categories: Category[]) => | ||
categories.filter((category: Category) => | ||
category.slug.toLowerCase().includes(slug.toLowerCase()) | ||
) | ||
) | ||
); | ||
} else { | ||
return allCategories$; | ||
} | ||
} | ||
|
||
getCategoryById(id: string): Observable<Category | undefined> { | ||
return this.getAllCategories().pipe( | ||
map((categories: Category[]) => { | ||
return categories.find(category => category.id === id); | ||
}) | ||
); | ||
} | ||
|
||
getAllCategories(): Observable<Category[]> { | ||
return this.httpClient.get<ApiCategory[]>('/resources/categories').pipe( | ||
take(1), | ||
map((apiCategories: ApiCategory[]) => | ||
this.groupSubCategoriesOnCategories(apiCategories) | ||
) | ||
); | ||
} | ||
|
||
groupSubCategoriesOnCategories(apiCategories: ApiCategory[]): Category[] { | ||
const partition: ApiCategory[][] = lodash.partition( | ||
apiCategories, | ||
category => | ||
category.parent_id === undefined || category.parent_id === '0' | ||
); | ||
const parentCategories = partition[0]; | ||
const subCategories = partition[1]; | ||
|
||
return parentCategories.map(apiCategory => | ||
this.convertToCategory( | ||
apiCategory, | ||
subCategories.filter( | ||
(subCategory: ApiCategory) => | ||
subCategory.parent_id === apiCategory._id | ||
) | ||
) | ||
); | ||
} | ||
|
||
convertToCategory( | ||
apiCategory: ApiCategory, | ||
apiSubCategories: ApiCategory[] | ||
) { | ||
const category: Category = { | ||
id: apiCategory._id, | ||
name: apiCategory.name, | ||
slug: apiCategory.slug, | ||
subCategories: apiSubCategories.map((apiSubCategory: ApiCategory) => | ||
this.convertToSubCategory(apiSubCategory, category) | ||
) | ||
}; | ||
return category; | ||
} | ||
|
||
convertToSubCategory( | ||
apiSubCategory: ApiCategory, | ||
parentCategory: Category | ||
): SubCategory { | ||
return { | ||
id: apiSubCategory._id, | ||
name: apiSubCategory.name, | ||
slug: apiSubCategory.slug, | ||
parentCategory: parentCategory | ||
}; | ||
} | ||
|
||
getCategory(id: string, paginationObj?: any): Observable<any> { | ||
let params: any = {}; | ||
|
||
params = { ...params, ...paginationObj }; | ||
if (params.filters) { | ||
Object.keys(params.filters).forEach(key => { | ||
if (params.filters[key]) { | ||
params['filters[' + key + ']'] = params.filters[key]; | ||
} | ||
}); | ||
delete params.filters; | ||
} | ||
|
||
return this.httpClient.get(`/resources/categories/${id}`, { | ||
params: params | ||
}); | ||
} | ||
|
||
/** | ||
* post a new category to website, auto add Header | ||
* @param {any} payload the org data to be added | ||
* @returns observable with response | ||
*/ | ||
addCategory(payload: any) { | ||
return this.httpClient.post('/resources/categories', payload); | ||
} | ||
|
||
/** | ||
* delete category by id | ||
* @param {string} id of the category to be deleted | ||
* @returns observable with response | ||
*/ | ||
deleteCategory(id: any) { | ||
return this.httpClient.delete(`/resources/categories/${id}`); | ||
} | ||
|
||
/** | ||
* edit a category | ||
* @param {any} payload the category data to be modified | ||
* @param {string} id of the category to be modified | ||
* @returns observable with response | ||
*/ | ||
editCategory(id: string, payload: any) { | ||
return this.httpClient.put(`/resources/categories/${id}`, payload); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<router-outlet></router-outlet> |
Empty file.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a bunch of code related to pagination copy-pasted from the Resources components, but they're not used since the RVM categories API doesn't currently support pagination.
I'm still considering whether I should just completely remove them, if we don't intend to add pagination, or keep them if we do :)