Skip to content

Commit 6779a40

Browse files
committed
fix: ioet#141 load customers from api
1 parent a6f248b commit 6779a40

File tree

8 files changed

+88
-24
lines changed

8 files changed

+88
-24
lines changed

src/app/modules/customer-management/components/customer-info/components/create-customer/create-customer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { LoadCustomers } from './../../../../store/customer-management.actions';
12
import { Component, Input, Output, EventEmitter, OnDestroy, OnInit } from '@angular/core';
23
import { FormGroup, FormBuilder, Validators } from '@angular/forms';
34
import { Store, select } from '@ngrx/store';
@@ -40,6 +41,7 @@ export class CreateCustomerComponent implements OnInit, OnDestroy {
4041

4142
onSubmit(customerData) {
4243
this.store.dispatch(new CreateCustomer(customerData));
44+
this.store.dispatch(new LoadCustomers());
4345
this.showAlert = true;
4446
}
4547

Lines changed: 20 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,32 @@
1-
import { Component } from '@angular/core';
1+
import { allCustomers } from './../../../../store/customer-management.selectors';
2+
import { Component, OnInit } from '@angular/core';
3+
import { Store, select } from '@ngrx/store';
4+
5+
import { LoadCustomers } from './../../../../store/customer-management.actions';
6+
import { Customer } from './../../../../../shared/models/customer.model';
27
import { ITEMS_PER_PAGE } from 'src/environments/environment';
38

9+
410
@Component({
511
selector: 'app-customer-list',
612
templateUrl: './customer-list.component.html',
713
styleUrls: ['./customer-list.component.scss'],
814
})
9-
export class CustomerListComponent {
15+
export class CustomerListComponent implements OnInit {
16+
1017
initPage1 = 1;
1118
itemsPerPage = ITEMS_PER_PAGE;
1219

13-
customers = [
14-
{
15-
id: '1',
16-
name: 'GoSpace',
17-
},
18-
{
19-
id: '2',
20-
name: 'GruHub',
21-
},
22-
{
23-
id: '3',
24-
name: 'e&y',
25-
},
26-
{
27-
id: '4',
28-
name: 'Mido',
29-
},
30-
];
31-
32-
constructor() {}
20+
customers: Customer[] = [];
21+
22+
constructor(private store: Store<Customer>) {}
23+
24+
ngOnInit(): void {
25+
this.store.dispatch(new LoadCustomers());
26+
const customers$ = this.store.pipe(select(allCustomers));
27+
customers$.subscribe((response) => {
28+
this.customers = response;
29+
});
30+
}
3331

3432
}

src/app/modules/customer-management/services/customer.service.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,8 @@ export class CustomerService {
2020
};
2121
return this.http.post(this.baseUrl, body);
2222
}
23+
24+
getCustomers(): Observable<any> {
25+
return this.http.get(this.baseUrl);
26+
}
2327
}

src/app/modules/customer-management/store/customer-management.actions.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,24 @@ export enum CustomerManagementActionTypes {
55
CREATE_CUSTOMER = '[CustomerManagement] CREATE_CUSTOMER',
66
CREATE_CUSTOMER_SUCCESS = '[CustomerManagement] CREATE_CUSTOMER_SUCCESS',
77
CREATE_CUSTOMER_FAIL = '[CustomerManagement] CREATE_CUSTOMER_FAIL',
8+
LOAD_CUSTOMERS = '[CustomerManagement] LOAD_CUSTOMERS',
9+
LOAD_CUSTOMERS_SUCCESS = '[CustomerManagement] LOAD_CUSTOMERS_SUCCESS',
10+
LOAD_CUSTOMERS_FAIL = '[CustomerManagement] LOAD_CUSTOMERS_FAIL',
11+
}
12+
13+
export class LoadCustomers implements Action {
14+
public readonly type = CustomerManagementActionTypes.LOAD_CUSTOMERS;
15+
}
16+
17+
export class LoadCustomersSuccess implements Action {
18+
readonly type = CustomerManagementActionTypes.LOAD_CUSTOMERS_SUCCESS;
19+
constructor(readonly payload: Customer[]) {}
20+
}
21+
22+
export class LoadCustomersFail implements Action {
23+
public readonly type = CustomerManagementActionTypes.LOAD_CUSTOMERS_FAIL;
24+
25+
constructor(public error: string) {}
826
}
927

1028
export class CreateCustomer implements Action {
@@ -25,4 +43,6 @@ export class CreateCustomerFail implements Action {
2543
constructor(public error: string) {}
2644
}
2745

28-
export type CustomerManagementActions = CreateCustomer | CreateCustomerSuccess | CreateCustomerFail;
46+
export type CustomerManagementActions =
47+
CreateCustomer | CreateCustomerSuccess | CreateCustomerFail |
48+
LoadCustomers | LoadCustomersFail | LoadCustomersSuccess;

src/app/modules/customer-management/store/customer-management.effects.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,19 @@ import { map, catchError, mergeMap } from 'rxjs/operators';
1111
export class CustomerEffects {
1212
constructor(private actions$: Actions, private customerService: CustomerService) {}
1313

14+
@Effect()
15+
loadCustomers$: Observable<Action> = this.actions$.pipe(
16+
ofType(actions.CustomerManagementActionTypes.LOAD_CUSTOMERS),
17+
mergeMap(() =>
18+
this.customerService.getCustomers().pipe(
19+
map((customers) => {
20+
return new actions.LoadCustomersSuccess(customers);
21+
}),
22+
catchError((error) => of(new actions.LoadCustomersFail(error)))
23+
)
24+
)
25+
);
26+
1427
@Effect()
1528
createCustomer$: Observable<Action> = this.actions$.pipe(
1629
ofType(actions.CustomerManagementActionTypes.CREATE_CUSTOMER),

src/app/modules/customer-management/store/customer-management.reducers.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,19 +5,42 @@ export interface CustomerState {
55
data: Customer[];
66
isLoading: boolean;
77
message: string;
8+
customers: Customer[];
89
}
910

1011
export const initialState: CustomerState = {
1112
data: [],
1213
isLoading: false,
1314
message: '',
15+
customers: [],
1416
};
1517

1618
export function customerManagementReducer(
1719
state: CustomerState = initialState,
1820
action: CustomerManagementActions
1921
): CustomerState {
2022
switch (action.type) {
23+
case CustomerManagementActionTypes.LOAD_CUSTOMERS: {
24+
return {
25+
...state,
26+
isLoading: true,
27+
};
28+
}
29+
case CustomerManagementActionTypes.LOAD_CUSTOMERS_SUCCESS: {
30+
return {
31+
...state,
32+
customers: action.payload,
33+
isLoading: false,
34+
};
35+
}
36+
case CustomerManagementActionTypes.LOAD_CUSTOMERS_FAIL: {
37+
return {
38+
...state,
39+
customers: [],
40+
isLoading: false,
41+
};
42+
}
43+
2144
case CustomerManagementActionTypes.CREATE_CUSTOMER: {
2245
return {
2346
...state,

src/app/modules/customer-management/store/customer-management.selectors.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,7 @@ export const getStatusMessage = createSelector(getCustomerState, (messageState)
88
return messageState.message;
99
}
1010
});
11+
12+
export const allCustomers = createSelector(getCustomerState, (state: CustomerState) => {
13+
return state.customers;
14+
});

src/environments/environment.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import * as keys from './keys';
55

66
export const environment = {
77
production: false,
8-
timeTrackerApiUrl: 'https://tsheets-apim.azure-api.net',
8+
timeTrackerApiUrl: 'https://timetracker-api.azurewebsites.net',
99
stackexchangeApiUrl: 'https://api.stackexchange.com',
1010
};
1111

0 commit comments

Comments
 (0)