-
Notifications
You must be signed in to change notification settings - Fork 1
89 edit and delete customres #157
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
Changes from 1 commit
4e1bc8f
dd5446f
b0b90d7
cba828e
6a8cc0c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,8 +3,14 @@ import { FormGroup, FormBuilder, Validators } from '@angular/forms'; | |
| import { Store, select } from '@ngrx/store'; | ||
|
|
||
| import { Subscription } from 'rxjs'; | ||
| import { CustomerState, CreateCustomer, LoadCustomers } from 'src/app/modules/customer-management/store'; | ||
| import { getStatusMessage } from './../../../../store/customer-management.selectors'; | ||
| import { getStatusMessage, getCustomerById } from './../../../../store/customer-management.selectors'; | ||
| import { Customer } from 'src/app/modules/shared/models/index'; | ||
|
||
| import { | ||
| CustomerState, | ||
| CreateCustomer, | ||
| LoadCustomers, | ||
| UpdateCustomer, | ||
| } from 'src/app/modules/customer-management/store'; | ||
|
|
||
| @Component({ | ||
| selector: 'app-create-customer', | ||
|
|
@@ -17,7 +23,9 @@ export class CreateCustomerComponent implements OnInit, OnDestroy { | |
| @Output() changeValueAreTabsActives = new EventEmitter<boolean>(); | ||
| showAlert = false; | ||
| messageToShow = ''; | ||
| customerToEdit: Customer; | ||
| saveSubscription: Subscription; | ||
| editSubscription: Subscription; | ||
|
|
||
| constructor(private formBuilder: FormBuilder, private store: Store<CustomerState>) { | ||
| this.customerForm = this.formBuilder.group({ | ||
|
|
@@ -31,17 +39,35 @@ export class CreateCustomerComponent implements OnInit, OnDestroy { | |
| this.saveSubscription = messages$.subscribe((valueMessage) => { | ||
| this.setStatusOnScreen(valueMessage); | ||
| }); | ||
|
|
||
| const customers$ = this.store.pipe(select(getCustomerById)); | ||
| this.editSubscription = customers$.subscribe((customer) => { | ||
| this.customerToEdit = customer; | ||
| this.setDataToUpdate(this.customerToEdit); | ||
| }); | ||
| } | ||
|
|
||
| ngOnDestroy() { | ||
| this.areTabsActive = false; | ||
| this.saveSubscription.unsubscribe(); | ||
| this.editSubscription.unsubscribe(); | ||
| } | ||
|
|
||
| onSubmit(customerData) { | ||
| this.store.dispatch(new CreateCustomer(customerData)); | ||
| this.store.dispatch(new LoadCustomers()); | ||
| const key = 'id'; | ||
| if (this.customerToEdit) { | ||
| const customer = { | ||
| ...customerData, | ||
| id: this.customerToEdit[key], | ||
|
||
| }; | ||
| this.store.dispatch(new UpdateCustomer(customer)); | ||
| this.customerForm.reset(); | ||
| } else { | ||
| this.store.dispatch(new CreateCustomer(customerData)); | ||
| this.store.dispatch(new LoadCustomers()); | ||
| } | ||
| this.showAlert = true; | ||
| setTimeout(() => (this.showAlert = false), 3000); | ||
| } | ||
|
|
||
| setStatusOnScreen(message: string) { | ||
|
|
@@ -54,6 +80,16 @@ export class CreateCustomerComponent implements OnInit, OnDestroy { | |
| this.areTabsActive = false; | ||
| this.changeValueAreTabsActives.emit(this.areTabsActive); | ||
| } | ||
| this.messageToShow = message; | ||
| } | ||
|
|
||
| setDataToUpdate(customerData: Customer) { | ||
| if (customerData) { | ||
| this.customerForm.setValue({ | ||
| name: customerData.name, | ||
| description: customerData.description, | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| resetCustomerForm() { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,32 +1,62 @@ | ||
| import { Component, OnInit } from '@angular/core'; | ||
| import { Component, OnInit, Input, Output, EventEmitter, OnDestroy } from '@angular/core'; | ||
| import { Store, select } from '@ngrx/store'; | ||
|
|
||
| import { allCustomers } from './../../../../store/customer-management.selectors'; | ||
| import { LoadCustomers } from './../../../../store/customer-management.actions'; | ||
| import { Subscription } from 'rxjs'; | ||
| import { allCustomers, getStatusMessage } from './../../../../store/customer-management.selectors'; | ||
| import { LoadCustomers, DeleteCustomer, SetCustomerToEdit } from './../../../../store/customer-management.actions'; | ||
| import { Customer } from './../../../../../shared/models/customer.model'; | ||
| import { ITEMS_PER_PAGE } from 'src/environments/environment'; | ||
|
|
||
|
|
||
| @Component({ | ||
| selector: 'app-customer-list', | ||
| templateUrl: './customer-list.component.html', | ||
| styleUrls: ['./customer-list.component.scss'], | ||
| }) | ||
| export class CustomerListComponent implements OnInit { | ||
|
|
||
| export class CustomerListComponent implements OnInit, OnDestroy { | ||
| initPage1 = 1; | ||
| itemsPerPage = ITEMS_PER_PAGE; | ||
| @Input() showCustomerForm: boolean; | ||
| @Output() changeValueShowCustomerForm = new EventEmitter<boolean>(); | ||
|
|
||
| customers: Customer[] = []; | ||
| messageToShow = ''; | ||
| showAlert = false; | ||
| customerSubscription: Subscription; | ||
| customerMessageSubscription: Subscription; | ||
|
|
||
| constructor(private store: Store<Customer>) {} | ||
|
|
||
| ngOnInit(): void { | ||
| this.store.dispatch(new LoadCustomers()); | ||
| const customers$ = this.store.pipe(select(allCustomers)); | ||
| customers$.subscribe((response) => { | ||
| this.customerSubscription = customers$.subscribe((response) => { | ||
| this.customers = response; | ||
| }); | ||
|
|
||
| const messages$ = this.store.pipe(select(getStatusMessage)); | ||
| this.customerMessageSubscription = messages$.subscribe((valueMessage) => { | ||
| this.setStatusOnScreen(valueMessage); | ||
| }); | ||
| } | ||
|
|
||
| ngOnDestroy() { | ||
| this.customerSubscription.unsubscribe(); | ||
| this.customerMessageSubscription.unsubscribe(); | ||
| } | ||
|
|
||
| setStatusOnScreen(message: string) { | ||
| this.messageToShow = message; | ||
| } | ||
|
|
||
| editCustomer(customerId: string) { | ||
| this.showCustomerForm = true; | ||
| this.changeValueShowCustomerForm.emit(this.showCustomerForm); | ||
| this.store.dispatch(new SetCustomerToEdit(customerId)); | ||
| } | ||
|
|
||
| deleteCustomer(customerId: string) { | ||
| this.showAlert = true; | ||
| setTimeout(() => (this.showAlert = false), 3000); | ||
| this.store.dispatch(new DeleteCustomer(customerId)); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,35 +1,42 @@ | ||
|
|
||
| <form style="width: 600px;" [formGroup]="projectTypeForm" (ngSubmit)="onSubmit(projectTypeForm.value)"> | ||
| <div class="form-group"> | ||
| <input | ||
| type="text" | ||
| class="form-control form-control-sm" | ||
| id="name" | ||
| formControlName="name" | ||
| aria-describedby="" | ||
| [class.is-invalid]="name.invalid && name.touched" | ||
| required | ||
| placeholder="Name" | ||
| /> | ||
| <div class="text-danger" *ngIf="(name.dirty || name.touched) && name.invalid && name.errors.required"> | ||
| Name is required. | ||
| <form style="width: 600px;" [formGroup]="projectTypeForm" (ngSubmit)="onSubmit(projectTypeForm.value)"> | ||
| <div class="form-group"> | ||
| <input | ||
| type="text" | ||
| class="form-control form-control-sm" | ||
| id="name" | ||
| formControlName="name" | ||
| aria-describedby="" | ||
| [class.is-invalid]="name.invalid && name.touched" | ||
| required | ||
| placeholder="Name" | ||
| /> | ||
| <div class="text-danger" *ngIf="(name.dirty || name.touched) && name.invalid && name.errors.required"> | ||
| Name is required. | ||
| </div> | ||
| <textarea | ||
| class="form-control form-control-sm mt-2" | ||
| id="descriptionTextArea" | ||
| rows="3" | ||
| placeholder="Description" | ||
| formControlName="description" | ||
| ></textarea> | ||
| <div class="btn-toolbar" role="toolbar"> | ||
| <div class="btn-group mr-2" role="group"> | ||
| <button type="submit" class="btn btn-sm btn-primary mb-2 ml-2 mt-2" [disabled]="!projectTypeForm.valid"> | ||
| Save | ||
| </button> | ||
| </div> | ||
| <textarea | ||
| class="form-control form-control-sm mt-2" | ||
| id="descriptionTextArea" | ||
| rows="3" | ||
| placeholder="Description" | ||
| formControlName="description" | ||
| ></textarea> | ||
| <div class="btn-toolbar" role="toolbar"> | ||
| <div class="btn-group mr-2" role="group"> | ||
| <button type="submit" class="btn btn-sm btn-primary mb-2 ml-2 mt-2" [disabled]="!projectTypeForm.valid">Save</button> | ||
| </div> | ||
| <div class="btn-group mr-2" role="group"> | ||
| <button class="btn btn-sm btn-secondary mb-2 ml-2 mt-2" type="reset" [hidden]="!projectTypeToEdit" (click)="cancelButton()">Cancel</button> | ||
| </div> | ||
| <div class="btn-group mr-2" role="group"> | ||
| <button | ||
| class="btn btn-sm btn-secondary mb-2 ml-2 mt-2" | ||
| type="reset" | ||
| [hidden]="!projectTypeToEdit" | ||
| (click)="cancelButton()" | ||
| > | ||
| Cancel | ||
| </button> | ||
| </div> | ||
| </div> | ||
| </form> | ||
| <hr /> | ||
|
|
||
| </div> | ||
| </form> | ||
| <hr /> |
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.
import should be
import { Customer } from 'src/app/modules/shared/models';
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.
done