Skip to content

Commit

Permalink
navigation and reactive form
Browse files Browse the repository at this point in the history
  • Loading branch information
samurex committed May 31, 2019
1 parent 7015dfa commit cfd6c64
Show file tree
Hide file tree
Showing 10 changed files with 94 additions and 28 deletions.
5 changes: 4 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,5 +49,8 @@
"ts-node": "~7.0.0",
"tslint": "~5.11.0",
"typescript": "~3.2.2"
}
},
"browserslist": [
"last 1 chrome versions"
]
}
4 changes: 2 additions & 2 deletions src/app/app-routing.module.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { NgModule } from '@angular/core';
import { Routes, RouterModule } from '@angular/router';

import { AddAgentComponent } from './screens/add-agent/add-agent.component';
import { AddAgentComponent } from './views/add-agent/add-agent.component';

const routes: Routes = [
{ path: 'add-agent', component: AddAgentComponent }
{ path: 'add-agent', component: AddAgentComponent },
];

@NgModule({
Expand Down
27 changes: 26 additions & 1 deletion src/app/app.component.html
Original file line number Diff line number Diff line change
@@ -1 +1,26 @@
<router-outlet></router-outlet>
<mat-sidenav-container class="sidenav-container">
<mat-sidenav #drawer class="sidenav" fixedInViewport="true"
[attr.role]="(isHandset$ | async) ? 'dialog' : 'navigation'"
[mode]="(isHandset$ | async) ? 'over' : 'side'"
[opened]="!(isHandset$ | async)">
<mat-toolbar>Menu</mat-toolbar>
<mat-nav-list>
<a mat-list-item routerLink="/add-agent">Add Agent</a>
</mat-nav-list>
</mat-sidenav>
<mat-sidenav-content>
<mat-toolbar color="primary">
<button
type="button"
aria-label="Toggle sidenav"
mat-icon-button
(click)="drawer.toggle()"
*ngIf="isHandset$ | async">
<mat-icon aria-label="Side nav toggle icon">menu</mat-icon>
</button>
<span>reciprocity-tracker-angular</span>
</mat-toolbar>
<router-outlet></router-outlet>
</mat-sidenav-content>
</mat-sidenav-container>

10 changes: 10 additions & 0 deletions src/app/app.component.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
import { Component } from '@angular/core';
import { BreakpointObserver, Breakpoints } from '@angular/cdk/layout';
import { Observable } from 'rxjs';
import { map } from 'rxjs/operators';

@Component({
selector: 'app-root',
Expand All @@ -7,4 +10,11 @@ import { Component } from '@angular/core';
})
export class AppComponent {
title = 'reciprocity-tracker-angular';

isHandset$: Observable<boolean> = this.breakpointObserver.observe(Breakpoints.Handset)
.pipe(
map(result => result.matches)
);

constructor(private breakpointObserver: BreakpointObserver) {}
}
20 changes: 16 additions & 4 deletions src/app/app.module.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,29 @@
import { BrowserModule } from '@angular/platform-browser';
import { NgModule } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { FormsModule, ReactiveFormsModule } from '@angular/forms';
import { CommonModule } from '@angular/common';

import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component';

import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
import { AddAgentComponent } from './screens/add-agent/add-agent.component';
import { AddAgentComponent } from './views/add-agent/add-agent.component';

import {
MatButtonModule,
MatInputModule,
MatFormFieldModule,
MatListModule,
MatToolbarModule,
MatSidenavModule,
MatIconModule,
MatCardModule, MatSelectModule, MatRadioModule,
} from '@angular/material';

import { LayoutModule } from '@angular/cdk/layout';
@NgModule({
declarations: [
AppComponent,
AddAgentComponent
AddAgentComponent,
],
imports: [
BrowserModule,
Expand All @@ -31,6 +35,14 @@ import {
MatFormFieldModule,
FormsModule,
MatListModule,
LayoutModule,
MatToolbarModule,
MatSidenavModule,
MatIconModule,
MatCardModule,
MatSelectModule,
MatRadioModule,
ReactiveFormsModule,
],
bootstrap: [AppComponent]
})
Expand Down
10 changes: 0 additions & 10 deletions src/app/screens/add-agent/add-agent.component.html

This file was deleted.

23 changes: 23 additions & 0 deletions src/app/views/add-agent/add-agent.component.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<form [formGroup]="agent" novalidate (ngSubmit)="onSubmit(agent)">
<mat-card class="shipping-card">
<mat-card-header>
<mat-card-title>Add Agent</mat-card-title>
</mat-card-header>
<mat-card-content>
<div class="row">
<div class="col">
<mat-form-field class="full-width">
<input matInput placeholder="Name" formControlName="name">
</mat-form-field>
</div>
</div>
</mat-card-content>
<mat-card-actions>
<button mat-raised-button color="primary" type="submit">Submit</button>
</mat-card-actions>
</mat-card>
</form>

<mat-nav-list>
<mat-list-item role="listitem" *ngFor="let agent of agents$ | async"> {{ agent | json }} </mat-list-item>
</mat-nav-list>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, OnInit } from '@angular/core';
import { StateService } from '../../state.service';
import { NgForm } from '@angular/forms';
import { FormBuilder, Validators } from '@angular/forms';
import { Agent } from 'reciprocity-tracker-state-actor/lib/interfaces';

import cuid from 'cuid';
Expand All @@ -14,19 +14,22 @@ import { map } from 'rxjs/operators';
styleUrls: ['./add-agent.component.scss'],
})
export class AddAgentComponent implements OnInit {
agents$: Observable<Agent[]>;
agent = this.fb.group({
name: ['', [Validators.required]],
});
agents$ = this.stateService.state$.pipe(
map((state: any) => state.agents || []),
);

constructor(
private stateService: StateService,
) {
this.agents$ = this.stateService.state$.pipe(
map((state: any) => state.agents || []),
);
}
private fb: FormBuilder,
) {}

onSubmit(form: NgForm) {
if (form.valid) {
this.stateService.actions.addAgent({ id: cuid(), name: form.value.name });
onSubmit({ value, valid }: { value: object, valid: boolean }) {
console.log(value);
if (valid) {
this.stateService.actions.addAgent({ id: cuid(), ...value });
}
}

Expand Down

0 comments on commit cfd6c64

Please sign in to comment.