New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
adds fallback to localstorage for usersettings if 403 for configmap #7301
adds fallback to localstorage for usersettings if 403 for configmap #7301
Conversation
d833c83
to
cbbd660
Compare
/retest |
2 similar comments
/retest |
/retest |
/test backend |
/ok-to-test |
/test frontend |
6faacb3
to
5ca6332
Compare
} | ||
}; | ||
|
||
export const deseralizeData = <T>(data: T) => { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should probably be:
export const deseralizeData = <T>(data: T) => { | |
export const deseralizeData = (data?: string | null) => { |
export const createConfigMap = async (configMapData: K8sResourceKind) => { | ||
try { | ||
const configMapDataResp = await k8sCreate(ConfigMapModel, configMapData); | ||
return Promise.resolve(configMapDataResp); | ||
} catch (err) { | ||
return Promise.reject(err); | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is an async
function. Therefore you only need to return the value or throw an error. There's no need to return a Promise
lsConfigMapData?.[keyRef.current] && | ||
seralizeData(lsConfigMapData[keyRef.current]) !== seralizeData(lsData) | ||
) { | ||
setLsData(lsConfigMapData[keyRef.current]); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be deserialized?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
in case of localStorage we are seralizing and deseralizing complete data in Configmap key so din't need to do it
const lsConfigMapData = deseralizeData(localStorage.getItem(storageConfigNameRef.current));
); | ||
const storageConfigNameRef = React.useRef(`${CONFIGMAP_LS_KEY}-${userUid}`); | ||
const [lsData, setLsData] = React.useState(() => { | ||
const valueInLocalStorage = deseralizeData(localStorage.getItem(storageConfigNameRef.current)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could use localStorage.hasItem
to check whether we fallback to the default or not
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hasItem
i din't see in API for localStorage so made a check against null
localStorage.getItem(storageConfigNameRef.current) !== null
? valueInLocalStorage[keyRef.current] | ||
: defaultValueRef.current; | ||
}); | ||
const lsDataRef = React.useRef<T>(lsData); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This never gets updated but is passed along as the previous value each time.
setLoaded(true); | ||
} | ||
}) | ||
.then(() => setFallbackLocalStorage(false)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fallback already starts as false
} else { | ||
setFallbackLocalStorage(false); | ||
setSettings(deseralizeData(defaultValueRef.current)); | ||
setLoaded(true); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
How will this work at all if we have any error creating the configmap or if the new backend returns an error?
I suppose that it should never error out if the service is working correctly.
Perhaps it shouldn't go to the loaded state if we have an error.
Thoughts?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yeah ideally it should never hit this block , have added it if in considering if it errors our and not with forbidden(403) then pass loaded with default value
setLoaded(true); | ||
} | ||
}) | ||
.then(() => setFallbackLocalStorage(false)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we should be using try / catch
if it's an async function
1b507df
to
35075a2
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested this functionality with PR #7051 . Works as expected. Looks good to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tested is with some local changes to 'fake the 403' case. Works fine. 👍
/lgtm
} catch (err) { | ||
if (err?.response?.status === 403) { | ||
setFallbackLocalStorage(true); | ||
} else { | ||
setSettings(defaultValueRef.current); | ||
setLoaded(true); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
All other cases of an error are just ignored a.t.m? Can you add here a console.warn / error as well?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we are logging for all in utils like updateConfigmap, createProject, get Project have added there for createConfigMap as well.
35075a2
to
6eb4b7a
Compare
/retest |
/lgtm |
/retest |
/approve |
[APPROVALNOTIFIER] This PR is APPROVED This pull-request has been approved by: christianvogt, invincibleJai, jerolimov The full list of commands accepted by this bot can be found here. The pull request process is described here
Needs approval from an approver in each of these files:
Approvers can indicate their approval by writing |
/retest |
Fixes:
https://issues.redhat.com/browse/ODC-4753
Analysis / Root cause:
useUserSettings doesn't support fallback to localStorage
Solution Description:
adds support fallback for support for
useUserSettings
Implementation can be tested with #7051
Screen shots / Gifs for design review:
NA
Browser conformance: