Implements item edit

This commit is contained in:
Rene Vergara 2021-10-26 10:09:34 -05:00
parent 172ebb0171
commit a7062e7b11
10 changed files with 122 additions and 23 deletions

View file

@ -208,11 +208,25 @@ app.get('/api/getitems', (req, res, next) => {
app.post('/api/item', (req, res, next) => { app.post('/api/item', (req, res, next) => {
console.log('Post: /api/item'); console.log('Post: /api/item');
const item = new itemmodel(req.body.item); if ( req.body.item._id == '' ) {
item.save(); const item = new itemmodel(req.body.item);
res.status(201).json({ item.save();
message: 'Item added' res.status(201).json({
}); message: 'Item added'
});
} else {
console.log('Editing', req.body.item._id);
itemmodel.findByIdAndUpdate(req.body.item._id, {'name': req.body.item.name, 'description': req.body.item.description, 'cost': req.body.item.cost},
function(err, docs) {
if (err) {
console.log(err);
} else {
res.status(201).json({
message: 'Item updated'
});
}
});
}
}); });
app.get('/api/price', (req, res, next) => { app.get('/api/price', (req, res, next) => {

11
package-lock.json generated
View file

@ -22,6 +22,7 @@
"angular-local-storage": "^0.7.1", "angular-local-storage": "^0.7.1",
"coingecko-api": "^1.0.10", "coingecko-api": "^1.0.10",
"easyqrcodejs": "^4.4.6", "easyqrcodejs": "^4.4.6",
"material-design-icons": "^3.0.1",
"rxjs": "~6.6.0", "rxjs": "~6.6.0",
"tslib": "^2.3.0", "tslib": "^2.3.0",
"urlsafe-base64": "^1.0.0", "urlsafe-base64": "^1.0.0",
@ -8816,6 +8817,11 @@
"node": ">=0.10.0" "node": ">=0.10.0"
} }
}, },
"node_modules/material-design-icons": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/material-design-icons/-/material-design-icons-3.0.1.tgz",
"integrity": "sha1-mnHEh0chjrylHlGmbaaCA4zct78="
},
"node_modules/mdn-data": { "node_modules/mdn-data": {
"version": "2.0.14", "version": "2.0.14",
"resolved": "https://registry.npmjs.org/mdn-data/-/mdn-data-2.0.14.tgz", "resolved": "https://registry.npmjs.org/mdn-data/-/mdn-data-2.0.14.tgz",
@ -22325,6 +22331,11 @@
"object-visit": "^1.0.0" "object-visit": "^1.0.0"
} }
}, },
"material-design-icons": {
"version": "3.0.1",
"resolved": "https://registry.npmjs.org/material-design-icons/-/material-design-icons-3.0.1.tgz",
"integrity": "sha1-mnHEh0chjrylHlGmbaaCA4zct78="
},
"mdn-data": { "mdn-data": {
"version": "2.0.14", "version": "2.0.14",
"resolved": "https://registry.npmjs.org/mdn-data/-/mdn-data-2.0.14.tgz", "resolved": "https://registry.npmjs.org/mdn-data/-/mdn-data-2.0.14.tgz",

View file

@ -24,6 +24,7 @@
"angular-local-storage": "^0.7.1", "angular-local-storage": "^0.7.1",
"coingecko-api": "^1.0.10", "coingecko-api": "^1.0.10",
"easyqrcodejs": "^4.4.6", "easyqrcodejs": "^4.4.6",
"material-design-icons": "^3.0.1",
"rxjs": "~6.6.0", "rxjs": "~6.6.0",
"tslib": "^2.3.0", "tslib": "^2.3.0",
"urlsafe-base64": "^1.0.0", "urlsafe-base64": "^1.0.0",

View file

@ -8,6 +8,7 @@ import { MatToolbarModule } from '@angular/material/toolbar';
import { MatExpansionModule } from '@angular/material/expansion'; import { MatExpansionModule } from '@angular/material/expansion';
import { HttpClientModule } from '@angular/common/http'; import { HttpClientModule } from '@angular/common/http';
import { MatDialogModule } from '@angular/material/dialog'; import { MatDialogModule } from '@angular/material/dialog';
import { MatIconModule } from '@angular/material/icon';
import { AppRoutingModule } from './app-routing.module'; import { AppRoutingModule } from './app-routing.module';
import { AppComponent } from './app.component'; import { AppComponent } from './app.component';
@ -36,6 +37,7 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
AppRoutingModule, AppRoutingModule,
FormsModule, FormsModule,
ReactiveFormsModule, ReactiveFormsModule,
MatIconModule,
HttpClientModule, HttpClientModule,
MatInputModule, MatInputModule,
MatCardModule, MatCardModule,
@ -45,6 +47,9 @@ import { BrowserAnimationsModule } from '@angular/platform-browser/animations';
MatDialogModule, MatDialogModule,
BrowserAnimationsModule BrowserAnimationsModule
], ],
exports: [
MatIconModule
],
providers: [], providers: [],
bootstrap: [AppComponent], bootstrap: [AppComponent],
entryComponents: [ItemCreateComponent] entryComponents: [ItemCreateComponent]

View file

@ -2,6 +2,7 @@
<mat-dialog-content [formGroup]="form"> <mat-dialog-content [formGroup]="form">
<mat-form-field> <mat-form-field>
<input type="hidden" formControlName="id">
<input matInput placeholder="Item" formControlName="name"> <input matInput placeholder="Item" formControlName="name">
</mat-form-field> </mat-form-field>
<mat-form-field> <mat-form-field>

View file

@ -12,21 +12,36 @@ import { Item } from '../item.model';
export class ItemCreateComponent implements OnInit { export class ItemCreateComponent implements OnInit {
form: FormGroup; form: FormGroup;
id: string | undefined = '';
numberRegEx = /\d*\.?\d{1,2}/; numberRegEx = /\d*\.?\d{1,2}/;
constructor( constructor(
private fb: FormBuilder, private fb: FormBuilder,
private dialogRef: MatDialogRef<ItemCreateComponent>, private dialogRef: MatDialogRef<ItemCreateComponent>,
@Inject(MAT_DIALOG_DATA) {name, description, cost, user}:Item @Inject(MAT_DIALOG_DATA) public data: Item
){ ){
this.form = fb.group({ if (data._id === '') {
name: [null, Validators.required], this.form = fb.group({
description: [null, Validators.required], id: [null],
cost: new FormControl('', { name: [null, Validators.required],
validators: [Validators.required, Validators.pattern(this.numberRegEx)], description: [null, Validators.required],
updateOn: "blur" cost: new FormControl('', {
}) validators: [Validators.required, Validators.pattern(this.numberRegEx)],
}); updateOn: "blur"
})
});
} else {
this.id = data._id;
this.form = fb.group({
id: [data._id],
name: [data.name, Validators.required],
description: [data.description, Validators.required],
cost: new FormControl(data.cost, {
validators: [Validators.required, Validators.pattern(this.numberRegEx)],
updateOn: "blur"
})
});
}
} }
ngOnInit () { ngOnInit () {

View file

@ -1,7 +1,11 @@
* { .card {
font-family: 'Roboto-Mono', monospace; font-family: 'Roboto-Mono', monospace;
} }
.icons {
font-family: 'Material Icons';
}
.spacer{ .spacer{
flex: 1 1 auto; flex: 1 1 auto;
} }
@ -15,5 +19,6 @@ div.card{
} }
p.price{ p.price{
font-family: 'Roboto-Mono', monospace;
margin: 0px; margin: 0px;
} }

View file

@ -2,7 +2,7 @@
<div *ngIf="items.length > 0"> <div *ngIf="items.length > 0">
<div class="card" *ngFor="let item of itemsUpdate | async"> <div class="card" *ngFor="let item of itemsUpdate | async">
<mat-card> <mat-card>
<mat-card-title> <mat-card-title class="card">
<table cellspacing="0" width="100%"> <table cellspacing="0" width="100%">
<tr> <tr>
<td>{{item.name}}</td> <td>{{item.name}}</td>
@ -13,12 +13,29 @@
</tr> </tr>
</table> </table>
</mat-card-title> </mat-card-title>
<mat-card-subtitle> <mat-card-subtitle class="card">
<p>{{item.description}}</p> <p>{{item.description}}</p>
</mat-card-subtitle> </mat-card-subtitle>
<mat-card-actions>
<table cellspacing="0" width="100%">
<tr>
<td>
<button mat-icon-button class="icons" (click)="edit(item._id!)">
<mat-icon inline=true>edit</mat-icon>
</button>
</td>
<td align="right">
<button mat-raised-button color="primary" class="icons">
<mat-icon>shopping_cart</mat-icon>
</button>
</td>
</tr>
</table>
</mat-card-actions>
</mat-card> </mat-card>
</div> </div>
</div> </div>
<p *ngIf = "items.length <= 0">No items yet!</p> <p *ngIf = "items.length <= 0">No items yet!</p>
<br> <br>
<button mat-raised-button (click)="openDialog()">Add item</button> <button mat-raised-button (click)="openDialog()">
<mat-icon>add</mat-icon>Add item</button>

View file

@ -53,14 +53,44 @@ export class ItemListComponent implements OnInit{
dialogConfig.disableClose = true; dialogConfig.disableClose = true;
dialogConfig.autoFocus = true; dialogConfig.autoFocus = true;
dialogConfig.data = {name: '' , user: '', description: '', cost: 0} dialogConfig.data = {_id: '', name: '' , user: '', description: '', cost: 0}
const dialogRef = this.dialog.open(ItemCreateComponent, dialogConfig); const dialogRef = this.dialog.open(ItemCreateComponent, dialogConfig);
dialogRef.afterClosed().subscribe((val) => { dialogRef.afterClosed().subscribe((val) => {
//TODO connect to Item service if(val != null) {
var item:Item = {name: val.name, description: val.description, cost: val.cost, user: this.owner.address}; var item:Item = {name: val.name, description: val.description, cost: val.cost, user: this.owner.address};
this.itemService.addItem(item); this.itemService.addItem(item);
}
this.itemService.getItems(this.owner.address);
});
}
edit(id: string) {
//console.log('Edit:', id);
const item = this.items.find(element => element._id == id);
//console.log(item);
const dialogConfig = new MatDialogConfig();
dialogConfig.disableClose = true;
dialogConfig.autoFocus = true;
dialogConfig.data = item;
const dialogRef = this.dialog.open(ItemCreateComponent, dialogConfig);
dialogRef.afterClosed().subscribe((val) => {
if (val != null) {
var editItem: Item = {
_id: val.id,
name: val.name,
description: val.description,
cost: val.cost,
user: this.owner.address
};
//console.log('Edit:', editItem);
this.itemService.addItem(editItem).subscribe((response) => {
this.itemService.getItems(this.owner.address);
});;
}
this.itemService.getItems(this.owner.address); this.itemService.getItems(this.owner.address);
}); });
} }

View file

@ -2,7 +2,7 @@
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<title>Sell4zec</title> <title>Z-Go!</title>
<base href="/"> <base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1"> <meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="icon" type="image/x-icon" href="favicon.ico"> <link rel="icon" type="image/x-icon" href="favicon.ico">