Skip to content

Commit

Permalink
refactored util service turned into Util helper
Browse files Browse the repository at this point in the history
  • Loading branch information
brianParcel committed Feb 21, 2018
1 parent f4575e0 commit c83ad85
Show file tree
Hide file tree
Showing 16 changed files with 315 additions and 278 deletions.
8 changes: 6 additions & 2 deletions .angular-cli.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,8 @@
"environments": {
"dev": "environments/environment.ts",
"prod": "environments/environment.prod.ts"
}
},
"showCircularDependencies":false
}
],
"e2e": {
Expand Down Expand Up @@ -55,6 +56,9 @@
},
"defaults": {
"styleExt": "css",
"component": {}
"component": {},
"build": {
"showCircularDependencies": false
}
}
}
2 changes: 1 addition & 1 deletion src/app/app.routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ const APP_ROUTES: Routes = [

{ path: 'login', component: LoginComponent,pathMatch: 'full' },

{ path: 'user/profile', component: ProfileComponent,canActivate:[ApiGuard], pathMatch: 'full' },
{ path: 'user/update', component: ProfileComponent,canActivate:[ApiGuard], pathMatch: 'full' },

{ path: 'company/create', component: CompanyCreateComponent, canActivate:[ApiGuard], pathMatch: 'full' },
{ path: 'company/list', component: CompanyListComponent, canActivate:[ApiGuard], pathMatch: 'full' },
Expand Down
14 changes: 7 additions & 7 deletions src/app/components/auth/login/login.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { UtilService } from "../../../services/util.service";
import { Util } from "../../../helpers/util.helper";
import { User } from './../../../models/user.model';

export interface UserLoginInfo {
Expand Down Expand Up @@ -46,7 +46,7 @@ export class LoginComponent implements OnInit {
this.loginForm.get(input_name).setErrors({custom: message});
}

constructor(private util:UtilService) { }
constructor() { }

ngOnInit() {

Expand All @@ -71,7 +71,7 @@ export class LoginComponent implements OnInit {

async login(data: Object){
var err;
[err, this.user] = await this.util.to(User.LoginReg(data));
[err, this.user] = await Util.to(User.LoginReg(data));
if(err){
if(err.message.includes('password') || err.message.includes('Password')){
this.throwInputError('password', err.message);
Expand All @@ -87,7 +87,7 @@ export class LoginComponent implements OnInit {
return;
}

this.util.route('user/profile');
return this.user.to('update');
}

async create(data: Object){
Expand All @@ -98,10 +98,10 @@ export class LoginComponent implements OnInit {
}

let err;
[err, this.user] = await this.util.to(User.CreateAccount(data))
[err, this.user] = await Util.to(User.CreateAccount(data))

if(err) this.util.TE(err);
if(err) Util.TE(err);

this.util.route('user/profile');
return this.user.to('update');
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { Component, OnInit } from '@angular/core';
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { Company } from './../../../models/company.model';
import { UtilService } from "../../../services/util.service";
import { MatSnackBar } from '@angular/material';
import { Company } from './../../../models/company.model';
import { MatSnackBar } from '@angular/material';
import { Util } from './../../../helpers/util.helper';

export interface CompanyInfo {
name:string,
Expand All @@ -15,7 +15,7 @@ export interface CompanyInfo {
})
export class CompanyCreateComponent implements OnInit {
company_info:CompanyInfo = {name:''};
constructor(private util:UtilService, public snackBar: MatSnackBar) { }
constructor(public snackBar: MatSnackBar) { }

companyForm = new FormGroup({
name: new FormControl('', [Validators.required]),
Expand All @@ -36,7 +36,7 @@ export class CompanyCreateComponent implements OnInit {

async onSubmit(){
let err, company;
[err, company] = await this.util.to(Company.CreateAPI(this.company_info));
[err, company] = await Util.to(Company.CreateAPI(this.company_info));
company.to('update');
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { Component, OnInit } from '@angular/core';
import {ActivatedRoute} from "@angular/router";
import { UtilService } from "../../../services/util.service";
import { ActivatedRoute } from "@angular/router";
import { Util } from "../../../helpers/util.helper";
import { Company } from "./../../../models/company.model";
import { User } from "./../../../models/user.model";
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { MatSnackBar } from '@angular/material';

@Component({
selector: 'app-company-update',
Expand All @@ -30,18 +31,25 @@ export class CompanyUpdateComponent implements OnInit {
return err_message;
}

constructor(private aRoute: ActivatedRoute, private util:UtilService) {
constructor(private aRoute: ActivatedRoute, public snackBar: MatSnackBar) {
}

async ngOnInit() {
this.url_params = await this.util.getUrlParams(this.aRoute);
this.url_params = await Util.getUrlParams(this.aRoute);
this.company = await Company.getById(this.url_params.id);
this.users = this.company.Users();

console.log('users', this.users);
}

async onSubmit(){

let err, res;
[err, res] = await Util.to(this.company.saveAPI());
if(err){
if(err.message == 'Nothing Updated') this.snackBar.open('Company', 'Nothing to Update', {duration: 2000});
return;
}

this.snackBar.open('Company', 'Successfully updated', {duration: 2000});
}
}
3 changes: 2 additions & 1 deletion src/app/components/partials/footer/footer.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@

</mat-grid-list>
<div style="padding-bottom:10px;" fxLayout="row" fxLayoutAlign="center center">
© {{date}} <a href="#"> {{ util.env.app_first}} <span class="orange-text"> {{util.env.app_second}} </span></a>
test
<!--© {{date}} <a href="#"> {{ util.env.app_first}} <span class="orange-text"> {{util.env.app_second}} </span></a>-->
</div>
</footer>
4 changes: 2 additions & 2 deletions src/app/components/partials/footer/footer.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, OnInit } from '@angular/core';
import { UtilService } from "../../../services/util.service";
import { Util } from "../../../helpers/util.helper";

@Component({
selector: 'app-footer',
Expand All @@ -8,7 +8,7 @@ import { UtilService } from "../../../services/util.service";
})
export class FooterComponent implements OnInit {
date:Number = new Date().getFullYear();
constructor(public util:UtilService) { }
constructor() { }

ngOnInit() {
}
Expand Down
7 changes: 4 additions & 3 deletions src/app/components/partials/navbar/navbar.component.html
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

<button mat-button routerLink="/">
<mat-icon>home</mat-icon>
{{util.env.app_name}}
<!--{{util.env.app_name}}-->
test
</button>

<!-- This fills the remaining space of the current row -->
Expand All @@ -11,7 +12,7 @@

<div fxLayout="row" fxShow="false" fxShow.gt-sm>
<div *ngIf="user">
<button class="nav-button" mat-button routerLink="/user/profile">Profile</button>
<button class="nav-button" mat-button routerLink="/user/update">Profile</button>
<button class="nav-button" mat-button routerLink="/company/create">Create Company</button>
<button class="nav-button" mat-button (click)="onLogout()">Logout</button>
</div>
Expand All @@ -28,7 +29,7 @@
</mat-toolbar>
<mat-menu yPosition="below" #menu="matMenu" [overlapTrigger]="false">
<div *ngIf="user">
<button class="nav-menu-item" mat-menu-item routerLink="/user/profile">Profile</button>
<button class="nav-menu-item" mat-menu-item routerLink="/user/update">Profile</button>
<button class="nav-menu-item" mat-menu-item routerLink="/company/create">Create Company</button>
<button class="nav-menu-item" mat-menu-item routerLink="/company/list">My Companies</button>
<mat-divider></mat-divider>
Expand Down
4 changes: 2 additions & 2 deletions src/app/components/partials/navbar/navbar.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Component, OnInit, ChangeDetectionStrategy, ChangeDetectorRef } from '@angular/core';
import { UtilService } from './../../../services/util.service';
import { Util } from './../../../helpers/util.helper';
import { User } from './../../../models/user.model';

@Component({
Expand All @@ -10,7 +10,7 @@ import { User } from './../../../models/user.model';
})
export class NavbarComponent implements OnInit {
user:User ;
constructor(public util:UtilService, private cd:ChangeDetectorRef) {
constructor(private cd:ChangeDetectorRef) {
}

ngOnInit() {
Expand Down
7 changes: 4 additions & 3 deletions src/app/components/user/profile/profile.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup } from '@angular/forms';
import { UtilService } from "../../../services/util.service";
import { Util } from "../../../helpers/util.helper";

import { User } from './../../../models/user.model';
import { Company } from './../../../models/company.model';
import { MatSnackBar } from '@angular/material';
Expand Down Expand Up @@ -36,7 +37,7 @@ export class ProfileComponent implements OnInit {
this.profileForm.get(input_name).setErrors({custom: message});
}

constructor(private util:UtilService, public snackBar: MatSnackBar) { }
constructor(public snackBar: MatSnackBar) { }

ngOnInit() {
this.user = User.Auth();
Expand All @@ -56,7 +57,7 @@ export class ProfileComponent implements OnInit {
async onSubmit(){
// console.log('user', this.user);
let err, res;
[err, res] = await this.util.to(this.user.saveAPI());
[err, res] = await Util.to(this.user.saveAPI());

if(err){
if(err.message.includes('phone') || err.message.includes('Phone')){
Expand Down
6 changes: 3 additions & 3 deletions src/app/guards/api.guard.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { Injectable } from '@angular/core';
import { CanActivate} from '@angular/router';
import { User } from './../models/user.model';
import { UtilService } from './../services/util.service';
import { Util } from './../helpers/util.helper';

@Injectable()
export class ApiGuard implements CanActivate {
constructor(private utilService: UtilService){}
constructor(){}
canActivate(): boolean {
if(User.Auth()){
return true;
}else{
this.utilService.route('/home');
Util.route('/home');
return false;
}
}
Expand Down
42 changes: 42 additions & 0 deletions src/app/helpers/api.helper.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { UtilService } from './../services/util.service';
import { AppInjector } from './../app.module';
import * as _ from 'underscore';
import { LoginOptions } from 'ngx-facebook';
import {Util} from './util.helper';

export class API {

constructor(){
}


//************************************
//********* STATIC METHODS ***********
//************************************

static get util(){
return AppInjector.get(UtilService);
}

static async save(model, uri:string, update_data?:any){
let err, res:any;

if(!update_data){
let differences = model.instanceDifference();
if(_.isEmpty(differences)) Util.TE('Nothing Updated');
update_data = _.pick(differences, (value, key, object) => model.apiUpdateValues.includes(key) );
if(_.isEmpty(update_data)) Util.TE('Nothing Updated');
}

[err, res] = await Util.to(Util.put(uri, update_data ));

if(err) Util.TE(err, true);
if(!res.success) Util.TE(res.error, true);

model.emit('saveApi', update_data, true);
model.save();
return true;
}


}
Loading

0 comments on commit c83ad85

Please sign in to comment.