Skip to content

Commit c60b17d

Browse files
authored
fix: avatar image not updated (#4425)
1 parent adb8b61 commit c60b17d

File tree

8 files changed

+62
-25
lines changed

8 files changed

+62
-25
lines changed

examples-standalone/coffee-warehouse/src/app/app.module.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ import { DropDownsModule } from '@progress/kendo-angular-dropdowns';
3131
import { NotificationModule } from '@progress/kendo-angular-notification';
3232
import { IconsModule } from "@progress/kendo-angular-icons";
3333
import { MessageService } from '@progress/kendo-angular-l10n';
34+
import { ProfileImageService } from './services/profile-image.service';
3435

3536
const drawerRoutes = [
3637
{ path: '', component: TeamComponent },
@@ -84,7 +85,8 @@ import '@progress/kendo-angular-intl/locales/fr/all';
8485
],
8586
providers: [
8687
{ provide: MessageService, useClass: CustomMessagesService },
87-
{ provide: LOCALE_ID, useValue: 'en-US' }
88+
{ provide: LOCALE_ID, useValue: 'en-US' },
89+
ProfileImageService
8890
],
8991
bootstrap: [AppComponent]
9092
})

examples-standalone/coffee-warehouse/src/app/components/planning/cards/card.component.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,12 @@ export class CardComponent {
1313
@Output() public toggleEvents: EventEmitter<Employee> = new EventEmitter();
1414

1515
public cards: Employee[] = employees.slice(1, 6);
16-
1716
public images = images;
1817

18+
ngOnInit(){
19+
this.cards.map(card=>card.selected = false)
20+
}
21+
1922
public setCardColor(card: Employee): string | undefined {
2023
const currentTeam: Team | undefined = teams.find((team: Team) => team.teamID === card.teamId);
2124
return currentTeam?.teamColor;

examples-standalone/coffee-warehouse/src/app/components/profile/profile.component.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
<fieldset class="k-form-fieldset">
66
<kendo-formfield orientation="horizontal">
77
<label class="k-label">
8-
<kendo-avatar imageSrc="assets/user.jpg" width="80px" height="80px" shape="circle"> </kendo-avatar>
8+
<kendo-avatar [imageSrc]="profileImage" width="80px" height="80px" shape="circle"> </kendo-avatar>
99
</label>
1010
<kendo-fileselect
1111
(select)="selectAvatar($event)"

examples-standalone/coffee-warehouse/src/app/components/profile/profile.component.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,26 @@
11
import { AfterViewInit, Component } from '@angular/core';
22
import { Validators, FormGroup, FormControl } from '@angular/forms';
33

4-
54
import { SelectEvent, FileRestrictions } from '@progress/kendo-angular-upload';
65
import { MessageService } from '@progress/kendo-angular-l10n';
76
import { NotificationService } from '@progress/kendo-angular-notification';
87
import { countries } from '../../resources/countries';
98
import { FormModel } from '../../models/form.model';
109
import { CustomMessagesService } from '../../services/custom-messages.service';
10+
import { ProfileImageService } from '../../services/profile-image.service';
1111

1212
@Component({
1313
selector: 'app-profile-component',
1414
templateUrl: './profile.component.html'
1515
})
16-
export class ProfileComponent implements AfterViewInit {
16+
export class ProfileComponent {
1717
public formGroup: FormGroup = new FormGroup({});
1818
public countries = countries;
1919
public phoneNumberMask = '(+9) 0000-000-00-00';
20+
public profileImage: string = '';
2021
public fileRestrictions: FileRestrictions = {
2122
allowedExtensions: ['.png', '.jpeg', '.jpg']
2223
};
23-
public avatars?: NodeList;
2424

2525
public formValue: FormModel | null = {
2626
avatar: [''],
@@ -36,12 +36,13 @@ export class ProfileComponent implements AfterViewInit {
3636

3737
public customMsgService: CustomMessagesService;
3838

39-
constructor(public msgService: MessageService, private notificationService: NotificationService) {
39+
constructor(
40+
public msgService: MessageService,
41+
private notificationService: NotificationService,
42+
private profileService: ProfileImageService
43+
) {
4044
this.setFormValues();
4145
this.customMsgService = this.msgService as CustomMessagesService;
42-
}
43-
44-
ngAfterViewInit() {
4546
this.setAvatar();
4647
}
4748

@@ -64,12 +65,14 @@ export class ProfileComponent implements AfterViewInit {
6465
});
6566
}
6667

67-
public setAvatar() {
68-
this.avatars = document.querySelectorAll('.k-avatar .k-avatar-image');
68+
public setAvatar(): void {
6969
const avatarImg = localStorage.getItem('avatar');
7070
if (avatarImg) {
71-
this.avatars.forEach((avatar: any) => {
72-
avatar.style['background-image'] = `url("${avatarImg}")`;
71+
this.profileImage = avatarImg;
72+
this.profileService.updateProfileImage(avatarImg);
73+
} else {
74+
this.profileService.profileImage$.subscribe((image: string) => {
75+
this.profileImage = image;
7376
});
7477
}
7578
}
@@ -95,20 +98,17 @@ export class ProfileComponent implements AfterViewInit {
9598
}
9699

97100
public isFileAllowed(file: any): boolean {
98-
return <boolean>this.fileRestrictions.allowedExtensions?.includes(file.extension);
101+
return <boolean>this.fileRestrictions.allowedExtensions?.includes(file.extension.toLowerCase());
99102
}
100103

101104
public selectAvatar(ev: SelectEvent): void {
102-
103-
const avatars = this.avatars;
104105
const reader = new FileReader();
105106
const file = ev.files[0];
106107
if (file.rawFile && this.isFileAllowed(file)) {
107-
reader.onloadend = function () {
108-
avatars?.forEach((avatar: any) => {
109-
avatar.style['background-image'] = `url("${this.result}")`;
110-
localStorage.setItem('avatar', (<string>this.result).toString());
111-
});
108+
reader.onloadend = () => {
109+
this.profileImage = reader.result as string;
110+
this.profileService.updateProfileImage(this.profileImage);
111+
localStorage.setItem('avatar', this.profileImage);
112112
};
113113
reader.readAsDataURL(file.rawFile);
114114
}

examples-standalone/coffee-warehouse/src/app/header/header.commponent.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@ <h2 *ngIf="selectedPage">{{ this.customMsgService.translate(selectedPage.toLower
4040
</kendo-dropdownlist>
4141
</div>
4242

43-
<kendo-avatar imageSrc="assets/user.jpg" shape="circle"> </kendo-avatar>
43+
<kendo-avatar [imageSrc]="profileImage" shape="circle"> </kendo-avatar>
4444
</div>
4545
</div>

examples-standalone/coffee-warehouse/src/app/header/header.component.ts

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ import { MessageService } from '@progress/kendo-angular-l10n';
44
import { CustomMessagesService } from '../services/custom-messages.service';
55
import { SVGIcon, menuIcon, paletteIcon } from '@progress/kendo-svg-icons';
66
import { locales } from '../resources/locales';
7+
import { profileBase64 } from '../resources/profile-base64';
8+
import { ProfileImageService } from '../services/profile-image.service';
79

810
@Component({
911
selector: 'app-header-component',
@@ -35,16 +37,30 @@ export class HeaderComponent {
3537
}
3638
];
3739
public selectedTheme = this.themes[0];
40+
public profileImage: string = '';
41+
42+
constructor(
43+
public messages: MessageService,
44+
@Inject(LOCALE_ID) public localeId: string,
45+
public intlService: IntlService,
46+
private profileService: ProfileImageService
47+
) {
48+
this.setProfileImage();
3849

39-
constructor(public messages: MessageService, @Inject(LOCALE_ID) public localeId: string, public intlService: IntlService) {
4050
this.localeId = this.selectedLanguage.localeId;
4151
this.setLocale(this.localeId);
4252

4353
this.customMsgService = this.messages as CustomMessagesService;
4454
this.customMsgService.language = this.selectedLanguage.localeId;
4555
}
4656

47-
public changeTheme(theme: { href: string; text: string }) {
57+
public setProfileImage(): void {
58+
this.profileService.profileImage$.subscribe((image: string) => {
59+
this.profileImage = image;
60+
});
61+
}
62+
63+
public changeTheme(theme: { href: string; text: string }): void {
4864
this.selectedTheme = theme;
4965
const themeEl: any = document.getElementById('theme');
5066
themeEl.href = theme.href;

examples-standalone/coffee-warehouse/src/app/resources/profile-base64.ts

Lines changed: 1 addition & 0 deletions
Large diffs are not rendered by default.
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { Injectable } from '@angular/core';
2+
import { BehaviorSubject } from 'rxjs';
3+
import { profileBase64 } from '../resources/profile-base64';
4+
5+
@Injectable({
6+
providedIn: 'root',
7+
})
8+
export class ProfileImageService {
9+
private profileImageSubject = new BehaviorSubject<string>(profileBase64);
10+
public profileImage$ = this.profileImageSubject.asObservable();
11+
12+
public updateProfileImage(image: string): void {
13+
this.profileImageSubject.next(image);
14+
}
15+
}

0 commit comments

Comments
 (0)