Skip to content
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

Merged

Conversation

invincibleJai
Copy link
Member

@invincibleJai invincibleJai commented Nov 23, 2020

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:

  • Chrome
  • Firefox
  • Safari
  • Edge

@openshift-ci-robot openshift-ci-robot added the component/shared Related to console-shared label Nov 23, 2020
@invincibleJai invincibleJai force-pushed the fallback-localstorage branch 3 times, most recently from d833c83 to cbbd660 Compare November 24, 2020 15:22
@invincibleJai
Copy link
Member Author

/retest

2 similar comments
@invincibleJai
Copy link
Member Author

/retest

@invincibleJai
Copy link
Member Author

/retest

@invincibleJai
Copy link
Member Author

/test backend

@invincibleJai
Copy link
Member Author

/ok-to-test

@openshift-ci-robot openshift-ci-robot added the ok-to-test Indicates a non-member PR verified by an org member that is safe to test. label Nov 24, 2020
@invincibleJai
Copy link
Member Author

/test frontend

@invincibleJai invincibleJai reopened this Nov 24, 2020
@invincibleJai invincibleJai force-pushed the fallback-localstorage branch 2 times, most recently from 6faacb3 to 5ca6332 Compare November 24, 2020 16:31
}
};

export const deseralizeData = <T>(data: T) => {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be:

Suggested change
export const deseralizeData = <T>(data: T) => {
export const deseralizeData = (data?: string | null) => {

Comment on lines 37 to 48
export const createConfigMap = async (configMapData: K8sResourceKind) => {
try {
const configMapDataResp = await k8sCreate(ConfigMapModel, configMapData);
return Promise.resolve(configMapDataResp);
} catch (err) {
return Promise.reject(err);
}
};
Copy link
Contributor

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]);
Copy link
Contributor

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?

Copy link
Member Author

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));
Copy link
Contributor

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

Copy link
Member Author

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);
Copy link
Contributor

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))
Copy link
Contributor

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

Comment on lines 73 to 77
} else {
setFallbackLocalStorage(false);
setSettings(deseralizeData(defaultValueRef.current));
setLoaded(true);
}
Copy link
Contributor

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?

Copy link
Member Author

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))
Copy link
Contributor

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

@invincibleJai invincibleJai force-pushed the fallback-localstorage branch 3 times, most recently from 1b507df to 35075a2 Compare November 25, 2020 07:40
Copy link
Contributor

@sahil143 sahil143 left a 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.

Copy link
Member

@jerolimov jerolimov left a 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

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Nov 25, 2020
Comment on lines +70 to +76
} catch (err) {
if (err?.response?.status === 403) {
setFallbackLocalStorage(true);
} else {
setSettings(defaultValueRef.current);
setLoaded(true);
}
Copy link
Member

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?

Copy link
Member Author

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.

@openshift-ci-robot openshift-ci-robot removed the lgtm Indicates that a PR is ready to be merged. label Nov 25, 2020
@invincibleJai
Copy link
Member Author

/retest

@jerolimov
Copy link
Member

/lgtm
/retest

@openshift-ci-robot openshift-ci-robot added the lgtm Indicates that a PR is ready to be merged. label Nov 25, 2020
@jerolimov
Copy link
Member

/retest

@christianvogt
Copy link
Contributor

/approve
/retest

@openshift-ci-robot
Copy link
Contributor

[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 /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@openshift-ci-robot openshift-ci-robot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Nov 25, 2020
@jerolimov
Copy link
Member

/retest

@openshift-merge-robot openshift-merge-robot merged commit 867b5d8 into openshift:master Nov 26, 2020
@spadgett spadgett added this to the v4.7 milestone Nov 30, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
approved Indicates a PR has been approved by an approver from all required OWNERS files. component/shared Related to console-shared lgtm Indicates that a PR is ready to be merged. ok-to-test Indicates a non-member PR verified by an org member that is safe to test.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants