Skip to content

Commit

Permalink
Implemented fix to get user profile dob to work with the datepicker, …
Browse files Browse the repository at this point in the history
…added checks for a changed region to update the set region, various style tweaks
  • Loading branch information
zakhenry committed Sep 22, 2015
1 parent 5ad90eb commit 62dae88
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 32 deletions.
3 changes: 2 additions & 1 deletion app/src/app/user/profile/profile.spec.ts
Expand Up @@ -2,6 +2,7 @@ namespace app.user.profile {

describe('Profile', () => {

let seededChance = new Chance(Math.random());
let ProfileController:ProfileController,
$scope:ng.IScope,
$rootScope:ng.IRootScopeService,
Expand All @@ -17,7 +18,7 @@ namespace app.user.profile {
password:'Password'
},
userProfile:common.models.UserProfile = <common.models.UserProfile>{
dob:'1921-01-01',
dob: seededChance.date(),
mobile:'04123123',
phone:'',
gender:'M',
Expand Down
25 changes: 12 additions & 13 deletions app/src/app/user/profile/profile.tpl.html
Expand Up @@ -29,20 +29,19 @@ <h3>About You</h3>
</md-select>
</md-input-container>

<md-input-container>
<label>Birth Date</label>
<md-datepicker ng-model="ProfileController.fullUserInfo._userProfile.dob" md-placeholder="Enter date"></md-datepicker>
</md-input-container>
<label>Birth Date</label>
<md-datepicker ng-model="ProfileController.fullUserInfo._userProfile.dob" md-placeholder="Enter date"></md-datepicker>

<label>Gender</label>
<md-radio-group ng-model="ProfileController.fullUserInfo._userProfile.gender" class="md-primary">
<md-radio-button
ng-repeat="option in ProfileController.genderOptions"
ng-value="option.value"
aria-label="{{option.label}}">
{{option.label}}
</md-radio-button>
</md-radio-group>

<md-input-container>
<label>Gender</label>
<md-radio-group ng-model="ProfileController.fullUserInfo._userProfile.gender">
<md-radio-button ng-repeat="option in ProfileController.genderOptions"
ng-value="option.value">
{{ option.label }}
</md-radio-button>
</md-radio-group>
</md-input-container>
</md-whiteframe>
</md-content>

Expand Down
3 changes: 2 additions & 1 deletion app/src/app/user/profile/profile.ts
Expand Up @@ -111,12 +111,13 @@ namespace app.user.profile {
public countries:common.services.countries.ICountryDefinition,
public timezones:common.services.timezones.ITimezoneDefinition,
public fullUserInfo:common.models.User,
private regions:global.ISupportedRegion[],
public genderOptions:common.models.IGenderOption[],
private regions:global.ISupportedRegion[],
private authService:common.services.auth.AuthService,
public providerTypes:string[],
private $location:ng.ILocationService
) {

if (this.emailConfirmed) {
let updatedUser = userService.getAuthUser();

Expand Down
17 changes: 14 additions & 3 deletions app/src/common/models/abstractModel.ts
Expand Up @@ -54,13 +54,15 @@ namespace common.models {
*/
protected hydrate(data:any, exists:boolean) {
if (_.isObject(data)) {

_.transform(data, (model, value, key) => {

if(_.has(this.__attributeCastMap, key)) {
model[key] = this.__attributeCastMap[key](value);
}
else {
} else {
model[key] = value;
}

}, this);

if (_.size(this.__nestedEntityMap) > 1) {
Expand All @@ -73,9 +75,18 @@ namespace common.models {
/**
* Converts a date time
* @param value
* @returns {Date}
*/
protected castDate(value:string):Date {
return moment(value).toDate();
}

/**
* Converts a moment object
* @param value
* @returns {Moment}
*/
protected dateTime(value:string):moment.Moment {
protected castMoment(value:string):moment.Moment {
return moment(value);
}

Expand Down
2 changes: 1 addition & 1 deletion app/src/common/models/article/articleCommentModel.ts
Expand Up @@ -4,7 +4,7 @@ namespace common.models {
export class ArticleComment extends AbstractModel {

protected __attributeCastMap:IAttributeCastMap = {
createdAt: this.dateTime,
createdAt: this.castMoment,
};

public articleCommentId:string = undefined;
Expand Down
2 changes: 1 addition & 1 deletion app/src/common/models/user/userProfileModel.mock.ts
Expand Up @@ -11,7 +11,7 @@ namespace common.models {
let seededChance = new Chance(Math.random());

return {
dob: moment(seededChance.birthday()).format('YYYY-MM-DD'),
dob: moment(seededChance.birthday()).toDate(),
mobile: seededChance.phone({ mobile: true }),
phone: seededChance.phone(),
gender: seededChance.pick(_.pluck(UserProfile.genderOptions, 'value')),
Expand Down
10 changes: 5 additions & 5 deletions app/src/common/models/user/userProfileModel.ts
Expand Up @@ -7,7 +7,11 @@ module common.models {
@common.decorators.changeAware
export class UserProfile extends AbstractModel {

public dob:string = undefined;
protected __attributeCastMap:IAttributeCastMap = {
dob: this.castDate,
};

public dob:Date = undefined;
public mobile:string = undefined;
public phone:string = undefined;
public gender:string = undefined;
Expand All @@ -28,10 +32,6 @@ module common.models {

super(data, exists);

if (_.has(data, 'dob')){
data.dob = moment(data.dob).toDate();
}

this.hydrate(data, exists);
}

Expand Down
29 changes: 25 additions & 4 deletions app/src/common/services/user/userService.spec.ts
Expand Up @@ -261,7 +261,7 @@ namespace common.services.user {

user.firstName = 'Joe';
user._userProfile = common.models.UserProfileMock.entity();
user._userProfile.dob = '1995-01-01';
user._userProfile.dob = moment('1995-01-01').toDate();
user._userProfile.about = 'Ipsum';

$httpBackend.expectPATCH('/api/users/' + user.userId, (jsonData:string) => {
Expand All @@ -270,8 +270,8 @@ namespace common.services.user {
}).respond(204);

$httpBackend.expectPATCH('/api/users/' + user.userId + '/profile', (jsonData:string) => {
let data:common.models.UserProfile = JSON.parse(jsonData);
return data.dob == user._userProfile.dob && data.about == user._userProfile.about;
let data:any = JSON.parse(jsonData);
return data.dob == user._userProfile.dob.toISOString() && data.about == user._userProfile.about;
}).respond(204);

let profileUpdatePromise = userService.saveUserWithRelated(user);
Expand All @@ -284,7 +284,6 @@ namespace common.services.user {
});



it('should not make an api call if nothing has changed', () => {

let user = common.models.UserMock.entity();
Expand All @@ -296,6 +295,28 @@ namespace common.services.user {

});


it('should update the region setting when the user updates their profile', () => {

let user = common.models.UserMock.entity({
regionCode: 'us',
});
user.setExists(true);

$httpBackend.expectPATCH('/api/users/' + user.userId, {
regionCode: 'au',
}).respond(204);

user.regionCode = 'au';

let savePromise = userService.saveUserWithRelated(user);

$httpBackend.flush();

expect(savePromise).eventually.to.equal(user);

});

});

});
Expand Down
11 changes: 8 additions & 3 deletions app/src/common/services/user/userService.ts
Expand Up @@ -4,13 +4,14 @@ namespace common.services.user {

export class UserService extends AbstractApiService {

static $inject:string[] = ['ngRestAdapter', 'paginationService', '$q', 'ngJwtAuthService', '$mdDialog', ];
static $inject:string[] = ['ngRestAdapter', 'paginationService', '$q', 'ngJwtAuthService', '$mdDialog', 'regionService'];

constructor(ngRestAdapter:NgRestAdapter.INgRestAdapterService,
paginationService:common.services.pagination.PaginationService,
$q:ng.IQService,
private ngJwtAuthService:NgJwtAuth.NgJwtAuthService,
private $mdDialog:ng.material.IDialogService) {
private $mdDialog:ng.material.IDialogService,
private regionService:common.services.region.RegionService) {
super(ngRestAdapter, paginationService, $q);
}

Expand Down Expand Up @@ -150,12 +151,16 @@ namespace common.services.user {
*/
public saveUser(user:common.models.User):ng.IPromise<common.models.User|boolean> {

let changes = (<common.decorators.IChangeAwareDecorator>user).getChanged();
let changes:any = (<common.decorators.IChangeAwareDecorator>user).getChanged();

if (_.isEmpty(changes)){
return this.$q.when(false);
}

if (_.has(changes, 'regionCode')){
this.regionService.setRegion(this.regionService.getRegionByCode(changes.regionCode));
}

return this.ngRestAdapter
.patch(this.apiEndpoint()+'/' + user.userId, changes)
.then(() => user);
Expand Down

0 comments on commit 62dae88

Please sign in to comment.