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

Select and unselect functionality #55

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions src/app/list/list.component.css
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,31 @@
h2 {
border-bottom: 1px solid lightgray;
}

.data-list-container {
display: flex;
}

.data-list {
width: 50%;
padding: 20px;
}

.card-list {
display: flex;
}

.card {
width: 200px;
border: 1px solid;
margin: 10px;
padding: 10px;
height: 100px;
position: relative;
}

.remove {
position: absolute;
top: -9px;
right: -10px;
}
36 changes: 36 additions & 0 deletions src/app/list/list.component.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,40 @@
<div class="list-container">
<h2>Providers</h2>

<div class="data-list-container">

<div class="data-list">
<h3>Provided List</h3>
<div class="card-list">
<ng-container *ngIf="unselectedProviders?.length; else noUnselectedProviders" >
<div *ngFor="let item of unselectedProviders; let i = index;" class="card">
{{item.name}}
<p>{{item.address}}</p>
<button (click)="select(item, i)">select</button>
</div>
</ng-container>
<ng-template #noUnselectedProviders>
<div class="card">no record
</div>
</ng-template>
</div>
</div>

<div class="data-list">
<h3>Selected Provided List</h3>
<div class="card-list">
<ng-container *ngIf="selectedProviders?.length; else noSelectedProviders" >
<div *ngFor="let item of selectedProviders; let i = index;" class="card">
{{item.name}}
<p>{{item.address}}</p>
<button (click)="deselect(item, i)" class="remove" title="click to remove item">X</button>
</div>
</ng-container>
<ng-template #noSelectedProviders>
<div class="card">no record
</div>
</ng-template>
</div>
</div>
</div>
</div>
13 changes: 13 additions & 0 deletions src/app/list/list.component.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,17 @@ describe('ListComponent', () => {
expect(component.selectedProviders.length).toEqual(0);
});
});

describe('provider selection and deselection', () => {
it('should select provider', () => {
component.select(component.unselectedProviders[0], 0);
expect(component.selectedProviders.length).toEqual(1);
});

it('should deselect provider', () => {
component.select(component.unselectedProviders[0], 0);
component.deselect(component.selectedProviders[0], 0);
expect(component.unselectedProviders.length).toEqual(3);
});
});
});
24 changes: 23 additions & 1 deletion src/app/list/list.component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,28 @@ export class ListComponent implements OnInit {

constructor() {}

ngOnInit() {}
ngOnInit() {
try {
let data = sessionStorage.getItem('selectedItems');;
data = data ? JSON.parse(data) : [];
this.selectedProviders = this.selectedProviders.concat(data);

this.unselectedProviders = this.unselectedProviders.filter(item => this.selectedProviders.findIndex(subitem => subitem.id === item.id) === -1);
} catch (e) {}
}

select(item, index) {
console.log('hi', index)
this.unselectedProviders.splice(index, 1);
this.selectedProviders.push(item);
sessionStorage.setItem('selectedItems', JSON.stringify(this.selectedProviders));
}

deselect(item, index) {
console.log('bye', index)

this.selectedProviders.splice(index, 1);
this.unselectedProviders.push(item);
sessionStorage.setItem('selectedItems', JSON.stringify(this.selectedProviders));
}
}