Skip to content

Commit

Permalink
Merge pull request #45 from code4romania/chq-client-alex-albu-jwt
Browse files Browse the repository at this point in the history
edit app before approve
  • Loading branch information
razvanpavel authored May 25, 2017
2 parents 03af6b7 + 4846a75 commit c736de0
Show file tree
Hide file tree
Showing 11 changed files with 316 additions and 74 deletions.
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"typescript.tsdk": "./node_modules/typescript/lib"
}
159 changes: 139 additions & 20 deletions src/app/pages/AddAppPage/components/add-app/add-app.component.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { CivicFile } from './../../../../shared/models/civic-file.model';
import { UploadService } from './../../../../services/upload.service';
import { Component, OnInit, Input } from '@angular/core';
import { Component, OnInit, Input, OnChanges, SimpleChanges } from '@angular/core';
import { AppProfile } from './../../../../shared/models/app-profile.model';
import { AppDetails } from './../../../../shared/models/app-details.model';
import { NgoDetails } from './../../../../shared/models/ngo-details.model';
Expand All @@ -20,16 +20,18 @@ import './add-app.component.scss';
templateUrl: './add-app.template.html',
})

export class AddAppComponent implements OnInit {
export class AddAppComponent implements OnInit, OnChanges {


constructor(private appService: AppService, private tagsService: TagsService, private categoriesService: CategoriesService, private uploadService: UploadService) {

this.submitted = false;
this.isAddingNewApp = true;
}


@Input() app: AddAppModel;
@Input() isAddingNewApp: boolean
public categories: Array<Object> = [{ id: null, text: 'Alege o categorie' }];
public tags: Array<string> = [];
private value: any = {};
Expand All @@ -44,23 +46,45 @@ export class AddAppComponent implements OnInit {
private isAppLogoValid: boolean;
private isNgoLogoUploaded: boolean;
private isNgoLogoValid: boolean;
private needsToUpdateAppLogo: boolean;
private needsToUpdateNgoLogo: boolean;
public phoneRegex = '\\+4\\d{10}';

ngOnChanges(changes: SimpleChanges) {
if (changes['app']) {
this.submitted = false;
this.newTag = '';
this.value = this.app ? this.app.apphashtags : '';

this.selectedAppTags = (this.app && this.app.apphashtags) ? this.app.apphashtags.split("#"): [];


}
}

ngOnInit() {
this.app = new AddAppModel();
this.setDefaultsForLogoRelated();
this.app.appcategoryid = null;

if (this.isAddingNewApp) {
//console.log('is adding new app');
this.app = new AddAppModel();
this.setDefaultsForLogoRelated();
this.app.appcategoryid = null;
}
else{
this.setDefaultsForLogoWhenEditingApp();
}


this.categoriesService.getCategories()
.subscribe(cats => {
this.categories = this.categories.concat(cats.map((category) => {
return { id: category.id, text: category.catname }
}))

})

}

public selectTags(value: any): void {
//console.log('raised selectTags');
this.newTag = '';
this.selectedAppTags.push(value.text)
}
Expand All @@ -72,10 +96,12 @@ export class AddAppComponent implements OnInit {


public refreshValue(value: any): void {
//console.log('raised refreshValue');
this.value = value;
}

public removeTags(value: any): void {
//console.log('raised removeTags');
this.selectedAppTags.splice(this.selectedAppTags.indexOf(value.text), 1);
}

Expand All @@ -98,6 +124,7 @@ export class AddAppComponent implements OnInit {

appLogoChangeEvent(fileInput: any) {
this.isAppLogoValid = true;
this.needsToUpdateAppLogo = !this.isAddingNewApp;
var theLogo = fileInput.target.files[0];
this.app.applogoname = this.uploadService.GetNewLogoName(theLogo.name);
this.appLogo = new CivicFile(theLogo, this.app.applogoname);
Expand All @@ -122,6 +149,7 @@ export class AddAppComponent implements OnInit {

ngoLogoChangeEvent(fileInput: any) {
this.isNgoLogoValid = true;
this.needsToUpdateNgoLogo = !this.isAddingNewApp;
var theLogo = fileInput.target.files[0];
this.app.ngologoname = this.uploadService.GetNewLogoName(theLogo.name);
this.ngoLogo = new CivicFile(theLogo, this.app.ngologoname);
Expand Down Expand Up @@ -152,27 +180,111 @@ export class AddAppComponent implements OnInit {
} else {
this.isNgoLogoUploaded = (r == '200');
}
if (r != '200') {
var msg = 'Eroare la upload-ul logo-urilor! '
if (!this.isAddingNewApp) {
msg = msg + r.message;
}
this.handleLogoUploadError(isUploadingAppLogo, msg);
}

}).catch((err) => {
if (isUploadingAppLogo) {
this.isAppLogoUploaded = false;
} else {
this.isNgoLogoUploaded = false;
}
this.error = err.toString();
this.handleLogoUploadError(isUploadingAppLogo, err);
});
}

private handleLogoUploadError(isUploadingAppLogo: boolean, err: any) {
if (isUploadingAppLogo) {
this.isAppLogoUploaded = false;
} else {
this.isNgoLogoUploaded = false;
}
this.error = err.toString();
}

addApp(form) {
console.log('form', form)
if (this.isAddingNewApp) {
this.addNewApp(form)
} else {
this.editExistingApp(form)
}

}

private editExistingApp(form) {
//console.log('submit for edit')
this.submitted = true;

var l1 = new Promise((resolve, reject) => {
this.isAppLogoUploaded = true;
resolve('')
});

var l2 = new Promise((resolve, reject) => {
this.isNgoLogoUploaded = true;
resolve('')
});

if (this.isAppLogoValid && this.isNgoLogoValid) {
//console.log('editing app logo');
if(this.needsToUpdateAppLogo){
l1 = this.uploadLogo(this.appLogo, true);
}



if (this.isNgoLogoValid && this.ngoLogo && this.needsToUpdateNgoLogo) {
//console.log('editing ngo logo');
l2 = this.uploadLogo(this.ngoLogo, false);
}

Promise.all([l1, l2]).then(() => {
if (this.isAppLogoUploaded && this.isNgoLogoUploaded) {
//console.log('editing started');

if (form.valid) {
//console.log('app hashtags before '+ this.app.apphashtags)
this.app.apphashtags.length ? this.app.apphashtags = this.app.apphashtags.split(" #").join("#")
: this.app.apphashtags = "";

//console.log('app hashtags after '+ this.app.apphashtags)

this.appService.editApp(this.app)
.subscribe(response => {
this.message = response.data
//console.log('response este')
//console.log(response)
if (this.message === 'success') {
this.error = null;
this.setDefaultsForLogoWhenEditingApp();
console.log('succes')

}

else {
let errorRegex = /\(ERROR\)\:(.*)/;
this.error = errorRegex.exec(response.data);
console.log('ERROR')

}
})
}
}
});
}
}

private addNewApp(form) {
//console.log('form', form)
this.submitted = true;

if (this.isAppLogoValid && this.isNgoLogoValid) {
var l1 = this.uploadLogo(this.appLogo, true);

var l2 = new Promise((resolve, reject) =>{
var l2 = new Promise((resolve, reject) => {
this.isNgoLogoUploaded = true;
resolve('')});
resolve('')
});
if (this.isNgoLogoValid && this.ngoLogo) {
l2 = this.uploadLogo(this.ngoLogo, false);
}
Expand Down Expand Up @@ -202,14 +314,13 @@ export class AddAppComponent implements OnInit {

});
}
else{
else {
this.error = 'Logo-ul aplicației sau al organizației este invalid, poate sa fie doar .png sau .jpg, maxim 500px x 500px.';
}


}

private setDefaultsForLogoRelated(){
private setDefaultsForLogoRelated() {
this.ngoLogo = null;
this.appLogo = null;
this.isAppLogoUploaded = false;
Expand All @@ -218,7 +329,15 @@ export class AddAppComponent implements OnInit {
this.isNgoLogoValid = true; //da, true
}


private setDefaultsForLogoWhenEditingApp(){
this.isAppLogoUploaded = false;
this.isAppLogoValid = true;
this.isNgoLogoUploaded = false;
this.isNgoLogoValid = true; //da, true
this.needsToUpdateAppLogo = false;
this.needsToUpdateNgoLogo = false;

}


}
Loading

0 comments on commit c736de0

Please sign in to comment.