I have an application that needs to be authenticated with an API service before I can make any subsequent calls.
So, when the <Login/>
page loads, I call the authApp()
action from a useEffect
hook. Once the call succeeds, I get a token in the response body and save it in my store.
Here is my authApp
store:
class authApp {
applicationToken = ''
constructor() {
makeObservable(this, {
applicationToken: observable,
authApp: action,
setAppToken: action
})
}
setAppToken = applicationToken => {
this.applicationToken = applicationToken
}
authApp = async () => {
const apiRes = await apiAuth()
if (apiRes.ok){
const apiData = await apiRes.text()
this.setAppToken(JSON.parse(apiData)[0].token)
}
else{
//log error
}
}
}
The next step is to get the applications settings from the API. This call only takes the token mentioned above as an argument.
Additionally, the action to get the settings is located in a different store and, the call is made from a <Login/>
's page child component.
Here is the code for the second store:
class AppSettings {
appSetting = {}
applicationToken = authApp.applicationToken
constructor() {
makeObservable(this, {
appSetting : observable,
applicationToken: observable,
getTermsAndConditions: action,
setIsAppAuthFail: action
})
}
getTermsAndConditions = async () => {
const appSettingsRes = await getAppSettings(this.applicationToken)
if (appSettingsRes.ok){
const appSettingData = await appSettingsRes.text()
console.log(appSettingData)
}else{
//log error
}
}
}
In the front-end, the <Login/>
and its childs are wrapped in a observer
HoC.
Here is the <Login/>
child component code:
const TermAndCoditions = observer(() => {
useEffect(() => {
const termsAndConditions = async () => {
const response = await AppSettings.getTermsAndConditions()
}
termsAndConditions()
}, [])
return (
<div>{AppSettings.appSetting.text}</div>
)
})
The problem
When the AppSettings.getTermsAndConditions()
is called from the <TermAndCoditions/>
component, the applicationToken
value in the authApp
store it empty.
What am I doing wrong? and, Is this the correct approach?