Record Events
React Native ==> Session module
Record Upload To Inform
Listeners can be added to listen for success or failure of records submitted to the Validic API. Registration of listeners is typically done during component initialization to catch events for record uploads which may occur upon app restart.
ValidicSessionEvents.addListener(
"validic:session:onsuccess",
(record) => {
// the record was successfully uploaded to validic servers
console.log("Successfully got record" + JSON.stringify(record));
}
);
ValidicSessionEvents.addListener(
"validic:session:onerror",
(error, record) => {
// an error was returned while submitting the record
console.error("Got error: " + error + " for record: " + record.activity_id);
}
);
Listeners can also be set for when a Validic Session is ended. An event will be emitted with the existing organziation identifier, the user identifier, and any Records that were not submitted to the API before the session was ended. A Validic session will end and an event will be emitted in 3 scenarios.
- ValidicSession.endSession is called after ValidicSession.startSession has previously been called top persist a user.
- ValidicSession.startSession is called with a different user than the one that was previously persisted
- ValidicSession.startSession is persisted, but generates a 401 when making a call to the api.
ValidicSessionEvents.addListener("validic:session:onend", (endEvent) => {
// A session was ended
console.info(
"Session ended for " +
endEvent.user_id +
" with " +
endEvent.records.length +
" unsubmitted records"
);
});
Example
The following example starts a session if a session is not already active and registers session listeners.
componentDidMount(){
ValidicSessionEvents.addListener('validic:session:onsuccess', (record)=>{
// the record was successfully uploaded to validic servers
console.log("Successfully got record" + JSON.stringify(record));
});
ValidicSessionEvents.addListener('validic:session:onerror', (error, record)=>{
// an error was returned while submitting the record
console.error("Got error: "+error + " for record: " + record.activity_id);
});
var user = {
user_id: "replace with userid",
user_token : "replace with access token",
org_id : "replace with organization id"
};
// check if a persistent user already exists
ValidicSession.getUser().then(async user=>{
//if not, start a new session
if(user == null){
await ValidicSession.startSession(user);
}
});
}
//remove event listeners when the component is going to be unmounted
componentWillUnmount(){
ValidicSessionEvents.removeAllListeners('validic:session:onsuccess');
ValidicSessionEvents.removeAllListeners('validic:session:onerror');
ValidicSessionEvents.removeAllListeners('validic:session:onend');
}
Updated 3 days ago