I have a change profile picture modal that pops up so you upload the image press save and what should happen is that the profile picture is updated across the site but that doesnt happen, only after you refresh the profile picture has updated
my save function on the profile picture change modal
save(): void {
const self = this;
this.saving = true;
self._profileService.updateProfilePicture(input)
.finally(() => { this.saving = false; })
.subscribe(() => {
const self = this;
self._$jcropApi.destroy();
self._$jcropApi = null;
abp.event.trigger('profilePictureChanged');
console.log('changed');
this._userService.updateProfilePicture();
self.close();
});
}
so when the user presses save it uploads the image then it calls the updateProfilePicture function on my user service...
my user service is set up like so..
import { Injectable } from '#angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
import { Subject } from 'rxjs/subject';
#Injectable()
export class UserService {
private profilePictureSource = new Subject<any>();
profilePicture = this.profilePictureSource.asObservable();
updateProfilePicture() {
this.profilePictureSource.next();
}
}
then in the component I want the profile picture to change
import { UserService } from '/userService';
import { ProfileService } from '/profileService';
export class ....
profilePicture: any;
constructor(
private _userService: UserService,
private _profileService: ProfileService
) { }
ngOnInit() {
// grab the profile picture on init
this.userPic();
// Listen for profile picture change
this._userService.profilePicture.subscribe(result => {
this.userPic();
}
}
userPic() {
this._profileService.getProfilePicture().subscribe(result => {
if (result && result.profilePicture) {
this.profilePicture = 'data:image/jpeg;base64,' + result.profilePicture;
}
});
}
then in my HTML
<img [src]="profilePicture" />
I tried to comment out self.close(); just incase that was causing an issue like it was closing before it got a change to call the service but that didnt change anything
EDIT
When I use the chrome debugger Ive put breakpoints on all the functions and the service call.. when I press save the userService function triggers a breakpoint.. but no other functions in the stack are called after that Im not sure why?
2nd EDIT
Ive followed Abylay Kurakbayev answer and changed
profilePicture = this.profilePictureSource.asObservable(); //from
profilePicture = this.profilePictureSource; //to
but that didn't fix the issue
EDIT 3
here is the getProfilePicture() function
getProfilePicture(): Observable<GetProfilePictureOutput> {
let url_ = this.baseUrl + "/api/services/app/Profile/GetProfilePicture";
url_ = url_.replace(/[?&]$/, "");
let options_ : any = {
method: "get",
headers: new Headers({
"Content-Type": "application/json",
"Accept": "application/json"
})
};
return this.http.request(url_, options_).flatMap((response_ : any) => {
return this.processGetProfilePicture(response_);
}).catch((response_: any) => {
if (response_ instanceof Response) {
try {
return this.processGetProfilePicture(response_);
} catch (e) {
return <Observable<GetProfilePictureOutput>><any>Observable.throw(e);
}
} else
return <Observable<GetProfilePictureOutput>><any>Observable.throw(response_);
});
}
EDIT 4
This is the processGetProfilePicture() method
protected processGetProfilePicture(response: Response): Observable<GetProfilePictureOutput> {
const status = response.status;
let _headers: any = response.headers ? response.headers.toJSON() : {};
if (status === 200) {
const _responseText = response.text();
let result200: any = null;
let resultData200 = _responseText === "" ? null : JSON.parse(_responseText, this.jsonParseReviver);
result200 = resultData200 ? GetProfilePictureOutput.fromJS(resultData200) : new GetProfilePictureOutput();
return Observable.of(result200);
} else if (status !== 200 && status !== 204) {
const _responseText = response.text();
return throwException("An unexpected server error occurred.", status, _responseText, _headers);
}
return Observable.of<GetProfilePictureOutput>(<any>null);
}
EDIT 5
Im wondering if there is a way to force refresh the component that the userPic() function is in?? Because the profile picture updates as soon as you refresh the page??
Thanks
From what I see UserService how is declared in the providers ?
In order to check this
a console.log() in the UserService constructor to see if the constructur is called multiple times
also check the providers from components you should have only one provider.
You must have the same istance in order for this flow to work as you
are describing it. If you have one instance across all components can you make a plunker or provide some access to the full implementation of the components.
This issue could be caused by Angular digest too, because I ran and checked your code, I'm able to listen to profilePicture Observable every time. So below block will execute every time, Which calls getProfilePicture always.
this._userService.profilePicture.subscribe(result => {
this.userPic();
}
I can see on ngOnInit() you are able to fetch profile pic using userPic() but not on observable level. So I doubt that you might be doing some Javascript level manipulations on your data, which Angular will not be aware of. I faced this issue multiple times, So I'm not sure if you are doing anything like that in this.processGetProfilePicture(response_) method or somewhere else. Please make sure.
Hope this helps.
Change this code:
userPic() {
this._profileService.getProfilePicture().subscribe(result => {
if (result && result.profilePicture) {
this.profilePicture = 'data:image/jpeg;base64,' + result.profilePicture;
}
});
}
to
userPic() {
this._profileService.getProfilePicture().subscribe(result => {
if (result && result.profilePicture) {
this.profilePicture = 'data:image/jpeg;base64,' + result.profilePicture;
}
}, (err) => console.log(error));
}
Edit
getProfilePicture(): Observable {
let url_ = this.baseUrl + "/api/services/app/Profile/GetProfilePicture";
url_ = url_.replace(/[?&]$/, "");
let options_ : any = {
method: "get",
headers: new Headers({
"Content-Type": "application/json",
"Accept": "application/json"
})
};
return this.http.request(url_, options_).flatMap((response_ : any) => {
console.log(respone); // <-----
return this.processGetProfilePicture(response_);
}).catch((response_: any) => {
console.log(response); // <-----
if (response_ instanceof Response) {
try {
return this.processGetProfilePicture(response_);
} catch (e) {
console.log(e);//<--------
return <Observable<GetProfilePictureOutput>><any>Observable.throw(e);
}
} else
return <Observable<GetProfilePictureOutput>><any>Observable.throw(response_);
});
}
And see if there is anything in console regarding this. I have marked my addition into code as //----
This is a hack, but should work. Call the function inside a timeout after you changed the picture. This will trick Angular to reload the view.
setTimeout(() => {
this.userPic();
}, 500);
Related
I've been learning the mern stack from this book
I'm now on Nested Routes under React Router chapter
The web application is supposed to render this on the page.
When clicking the Select link under the Action column, the description of
an issue is displayed on the bottom part of the page.
But in my case, this thing happens:
and at the same time this error is being thrown in the console:
The only time the web application runs properly is when I downgraded the
graphql version to 0.13.2 (this is the version the book uses).
The thing is I try to use up to date versions of the project dependencies
as much as possible. There has never been much trouble as I follow the book
until I got into this.
I don't understand, why is this error being thrown when I use a more up to
date version of the graphql over the old version?
(I use graphql version 15.8.0 and apollo-server-express version 2.25.4)
I tried to modify the .jsx file that renders the description data
on the page.
async loadData() {
const { match: { params: { id } } } = this.props;
//I tried to parse the id to make it an int type before getting it into
//the graphql query
id = parseInt(id); // this is the thing that I've added
const query = `query issue($id: Int!) {
issue (id: $id) {
id description
}
}`;
const data = await graphQLFetch(query, { id });
if (data) {
this.setState({ issue: data.issue });
} else {
this.setState({ issue: {} });
}
}
This is the codes graphQLFetch function
const dateRegex = new RegExp('^\\d\\d\\d\\d-\\d\\d-\\d\\d');
function jsonDateReviver(key, value) {
if (dateRegex.test(value)) return new Date(value);
return value;
}
async function graphQLFetch(query, variables = {}) {
try {
const response = await fetch(window.ENV.UI_API_ENDPOINT, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({ query, variables }),
});
const body = await response.text();
const result = JSON.parse(body, jsonDateReviver);
if (result.errors) {
const error = result.errors[0];
if (error.extensions.code === 'BAD_USER_INPUT') {
const details = error.extensions.exception.errors.join('\n');
alert(`${error.message}:\n ${details}`);
} else {
alert(`${error.extensions.code}: ${error.message}`);
}
}
return result.data;
} catch (e) {
alert(`Error in sending data to server: ${e.message}`);
return null;
}
}
When I did this, it doesn't throw any error anymore but it doesn't render
the description data on the page either.
Can someone please help me with this?? Thanks in advance...
I have a Vue.js application and on the /callback route I am trying to have it do a few things. So far I am not having any luck with it because I am seeing things run async. I understand that it is normally how Vue/Javascript works however I am trying to force it to not be async.
The main issue I am having is sometimes the this.$store... are running before the items are set. This is an issue because of how things run on other Vuex actions. Mainly the getCompany one requires the loadToken one to complete as it is pulling the values from the local storage which is being set above.
I don't want to change this and how it works because of how the Vue router is set up to pull the token from local storage on each page reload. This token is used to connect to the backend so it needs to be pulled from local storage each reload as I don't want a user to log in just because they reload the page.
Code:
created() {
setTimeout(() => {
localStorage.setItem('token', this.$auth.token)
localStorage.setItem('user_data', JSON.stringify(this.$auth.user))
// Load company data
this.$store.dispatch('loadToken')
this.$store.dispatch('getCompany')
if(this.$auth == null || this.$auth.id_token['https://hello.io/account_signup_type/is_new']) {
this.$router.push('/setup')
} else {
// Load user data from Auth0
// Go to chat page
this.$router.push('/chat')
}
}, 500)
}
edit main.js code
import { Auth0Plugin } from '#/auth/auth0-plugin';
// Install the authentication plugin
Vue.use(Auth0Plugin, {
domain,
clientId,
audience,
onRedirectCallback: (appState) => {
router.push(
appState && appState.targetUrl
? appState.targetUrl
: window.location.pathname,
);
},
});
auth0-plugin
/**
* External Modules
*/
import Vue from 'vue';
import createAuth0Client from '#auth0/auth0-spa-js';
/**
* Vue.js Instance Definition
*/
let instance;
export const getInstance = () => instance;
/**
* Vue.js Instance Initialization
*/
export const useAuth0 = ({
onRedirectCallback = () =>
window.history.replaceState({}, document.title, window.location.pathname),
redirectUri = `${window.location.origin}/callback`,
...pluginOptions
}) => {
if (instance) return instance;
instance = new Vue({
data() {
return {
auth0Client: null,
isLoading: true,
isAuthenticated: false,
user: {},
error: null,
token: null,
id_token: null
};
},
methods: {
async handleRedirectCallback() {
this.isLoading = true;
try {
await this.auth0Client.handleRedirectCallback();
this.user = await this.auth0Client.getUser();
this.isAuthenticated = true;
} catch (error) {
this.error = error;
} finally {
this.isLoading = false;
}
},
loginWithRedirect(options) {
return this.auth0Client.loginWithRedirect(options);
},
logout(options) {
return this.auth0Client.logout(options);
},
getTokenSilently(o) {
return this.auth0Client.getTokenSilently(o);
},
getIdTokenClaims(o) {
return this.auth0Client.getIdTokenClaims(o);
}
},
async created() {
this.auth0Client = await createAuth0Client({
...pluginOptions,
// responseType: 'id_token',
domain: pluginOptions.domain,
client_id: pluginOptions.clientId,
audience: pluginOptions.audience,
redirect_uri: redirectUri,
});
try {
if (
window.location.search.includes('code=') &&
window.location.search.includes('state=')
) {
const { appState } = await this.auth0Client.handleRedirectCallback();
onRedirectCallback(appState);
}
} catch (error) {
this.error = error;
} finally {
this.isAuthenticated = await this.auth0Client.isAuthenticated();
this.user = await this.auth0Client.getUser();
this.$auth.getTokenSilently().then(token => this.token = token)
this.$auth.getIdTokenClaims().then(id_token => this.id_token = id_token)
this.isLoading = false;
}
},
});
return instance;
};
/**
* Vue.js Plugin Definition
*/
export const Auth0Plugin = {
install(Vue, options) {
Vue.prototype.$auth = useAuth0(options);
},
};
edit - updated router.beforeEach
router.beforeEach(async (to, from, next) => {
const auth = getInstance()
if(to.path == '/callback' && auth != null) {
console.log('Callback')
console.log(`Token: ${auth.token}`)
console.log(`User: ${JSON.stringify(auth.user)}`)
localStorage.setItem('token', auth.token)
localStorage.setItem('user_data', JSON.stringify(auth.user))
await store.dispatch('loadToken')
await store.dispatch('getCompany')
return next()
}
if(to.path != '/login' && to.path != '/setup') {
await store.dispatch('loadToken')
await store.dispatch('getCompany')
.then(() => {
return next()
})
} else {
return next()
}
})
edit - adding guide that I followed from Auth0 to get the code I have now - mostly
https://auth0.com/blog/complete-guide-to-vue-user-authentication/
The problem is that there is race condition because dispatch calls return promises that weren't chained before accessing the result of their work.
A good practice is to chain every promise, unless proven other wise.
The code that created contains actually belongs to the router in general because authentication logic is application-wide.
It's unnecessary to access global dependencies on this component instance. This is done for historical reasons because Vue originally was used in non-modular environment. In order to use outside components, global dependencies such as store need to be explicitly imported. In case this cannot be done, this needs to be fixed.
In this case auth instance is available through getInstance. In case the authentication shouldn't be done on each navigation, this needs to be done on condition, e.g.:
import { getInstance } from '.../auth';
import store from '.../store';
...
router.beforeEach(async (to, from, next) => {
const auth = getInstance();
if (...) {
...
await store.dispatch('loadToken')
await store.dispatch('getCompany')
...
next('/setup')
...
} else {
next()
}
})
getInstance doesn't serve a good purpose because it just exposes a variable. Instead, instance could be exported and imported directly, the behaviour would be the same due to how ES modules work.
Also global store already holds application logic and commonly used to handle authentication, including local storage operations.
im builiding a notebloc, so each time I go to the Edit note screen then go back, this warnings appears, heres the code:
when the app runs, the first component that rendes is Notes:
class Notes extends Component {
state = {
onpress: false,
array_notes: [],
selected_notes: [],
update: false,
}
note = "";
note_index = "";
note_number = "";
item_selected = 0;
onpress = false;
componentDidMount() {
const {navigation} = this.props;
this._unsubscribe = navigation.addListener("didFocus", () => this.fetch_notes());
}
componentWillUnmount() {
this._unsubscribe.remove();
}
fetch_notes = async() => { //fetch all the data
const data = await fetch_notes();
if (typeof data != "function") {
this.setState({array_notes: data.array_notes});
}else {
data();
}
}
as you can see, in the ComponentDidmount() I run the function fetch_data to set the data, heres the fetch_data function:
import AsyncStorage from "#react-native-community/async-storage";
export const fetch_notes = async () => {
try {
const data = JSON.parse(await AsyncStorage.getItem("data"));
if (data != null) {
return data;
}else {
return () => alert("with no data");
}
}catch (error) {
alert(error);
}
}
here everything works, because its only fetch the data, now when I go to edit note screen and I edit one of the notes, I need to save it, so when I go back I need to fetch the data again so it will update:
save_changes = async() => {
try {
const data = JSON.parse(await AsyncStorage.getItem("data"));
const index_to_find = this.array_notes.findIndex(obj => obj.note_number === this.note[0].note_number);
const edited_note = this.note.map((note) => {
note.content = this.state.content;
return {...note}
});
this.array_notes.splice(index_to_find, 1, edited_note[0]);
data.array_notes = this.array_notes;
await AsyncStorage.setItem("data", JSON.stringify(data));
}catch(error) {
alert(error);
}
when I get back to Notes screen the function runs and works, the data are updated, but the warning still appears, once I saved the edit note and go back, how can I avoid this?
This warning is thrown when you try to set the state of a component after it has unmounted.
Now, some things to point out here
The navigation flow, from what have you mentioned is like Notes --> Edit Notes --> Notes. Assuming you are using the StackNavigator from react-navigation, the Notes screen will not unmount when you navigate to Edit Notes screen.
The only screen unmounting is Edit Notes when you go back. So you should check the code to verify that you don't have any asynchoronous setState calls in that screen.
P.S : The syntax to remove the focus event listener is to just call the returned function as mentioned here.
So in your Notes screen inside componentWillUnmount it should be
componentWillUnmount() {
this._unsubscribe();
}
you need to use componentWillUnmount() function inside the function which you are unmounting.
You can use conditional rendering for mounting or unmounting any componets.
I have an issue in my Angular web store when i refresh the window, i create a service that takes the user data from the server and then inject to the 'products' section with BehaviorSubject, my goal is to make just one request to the server:
import { Injectable } from '#angular/core';
import { BehaviorSubject } from 'rxjs/BehaviorSubject';
#Injectable({
providedIn: 'root'
})
export class DataService {
private userId = new BehaviorSubject<any>('');
currentUserId = this.userId.asObservable();
constructor() { }
sendUserId(message: string){
this.userId.next(message)
}
}
This works fine but the problem is when i refresh the window in products section, in console i can see that the service takes the user data but when i getProducts() it throws an error, it seems like getProducts() makes the request before the service had the response, i need the user Id to make the products request. My question: Is there a way to await the response of BehaviorSubject and then make the getProducts() request?. This is the code in the products section:
ngOnInit(): void {
this._dataService.currentUserId.subscribe(userId => this.userId = userId);
if(this.userId.length === 0){
this.userService.getUserProfile().subscribe(
res => {
this.userDetails = res['user'];
this.userId = this.userDetails._id;
this.getProducts();
},
err => {
console.log(err);
}
);
} else {
this.getProducts();
}
}
As you can see, i do a condition to check if userId exists, if not i have to make a new user request, this fix the bug but i think there's a better way to solve this. Thanks in advance.
How about placing all your logic within the observer's next function as below:
this._dataService.currentUserId.subscribe(userId => {
if (userId.length === 0)
{
this.userService.getUserProfile().subscribe(
res => {
this.userDetails = res['user'];
this.userId = this.userDetails._id;
this.getProducts();
},
err => {
console.log(err);
}
);
} else
{
this.getProducts();
}
});
In my angular 2 app, I am making a call from component to service and from service to the back end Web API. The response obtained from Web API is sent back from service to the component and I am subscribing to the response inside the component. For error handling, I am using a common error component which is used across the app. This error component is used as a modal popup inside other components. If an error occurs, this modal pops up with a 'Retry' button. Currently, clicking on the 'Retry' button reloads the entire page again. But when the user clicks the 'Retry' button, I want to make the failed Web API call again without reloading the entire page. If this call succeeds on retry, then normal execution flow should continue without any interruptions. I might have to use http request interceptor for angular 2 and promises but I couldn't figure out how to implement them. Could you please help me find the solution?
The call from my component.ts file:
this._accountdetailsService.getContacts(this.group.id)
.subscribe(
contacts => this.contacts = contacts,
error => this.callErrorPage(error);
);
_accountdetailsService is an instance of the service.
The call from my service.ts file to back end Web API:
getContacts(groupId: number): any {
return this._http.get(this._serverName + 'api/CustomerGroups/' + groupId + '/contacts')
.map(response => {
if(response.status < 200 || response.status >= 300) {
throw new Error('This request has failed' + response);
}
else {
return response.json();
}
});
}
Error handling inside the component.ts file:
callErrorPage(error: any) {
this.error = error;
this.showErrorModal();
}
onRetry() {
this.hideErrorModal();
window.location.reload();
}
showErrorModal(): void {
this.errorModal.show();
}
hideErrorModal(): void {
this.errorModal.hide();
}
The common error component which is used inside the modal on every other component is shown below:
error.component.ts
export class ErrorDetails implements OnInit {
#Input() error;
#Output() onRetry = new EventEmitter();
private sub: any;
errorStatustext: string;
errorMessage: string;
constructor(private _route: ActivatedRoute) { }
ngOnInit() {
if (this.error.status == 0 || this.error.status == 500) {
this.errorStatustext = "Sorry! Could not connect to server."
}
else {
this.errorStatustext = this.error.statusText;
var responseBody = JSON.parse(this.error._body);
if (responseBody) {
this.errorMessage = responseBody.resultText;
}
}
}
onRetryClick() {
this.onRetry.emit();
}
I am not sure if my idea is practical. I want to give it a try.
In your component.ts:
retryAction: any;
callErrorPage(error: any, retryAction: any) {
this.error = error;
this.retryAction = retryAction;
this.showErrorModal();
}
onRetry() {
this.hideErrorModal();
//window.location.reload();
this.retryAction();
}
onGetCountacts() {
let that = this;
this._accountdetailsService.getContacts(this.group.id)
.subscribe(
contacts => this.contacts = contacts,
error => this.callErrorPage(error, () => { that.onGetCountacts(); });
);
}