Skip to content

Commit

Permalink
fix: Hide read-only banner when app is no longer in read-only mode
Browse files Browse the repository at this point in the history
  • Loading branch information
leSamo authored and kpatticha committed Oct 25, 2021
1 parent eeb05a6 commit 5329dca
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 9 deletions.
8 changes: 3 additions & 5 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ const App = () => {
const { push } = useHistory();
const { pathname } = useLocation();
const [access, setAccess] = useState(true);
const [showReadOnlyBanner, setShowReadOnlyBanner] = useState(false);
const [isReadOnlyBannerVisible, setReadOnlyBannerVisible] = useState(false);

const dispatch = useDispatch();

Expand Down Expand Up @@ -72,15 +72,13 @@ const App = () => {
return () => unregister();
}, [appNavClick, pathname, dispatch]);

window.showReadOnlyBanner = () => {
setShowReadOnlyBanner(true);
};
window.setReadOnlyBannerVisible = setVisible => setReadOnlyBannerVisible(setVisible);

return (
access ?
<Fragment>
<NotificationPortal />
{showReadOnlyBanner && <ReadOnlyBanner />}
{isReadOnlyBannerVisible && <ReadOnlyBanner />}
<Routes />
</Fragment>
: <NoAccessPage/>
Expand Down
12 changes: 8 additions & 4 deletions src/Utilities/interceptors.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ export function errorInterceptor(error) {
if (!axios.isCancel(error)) {
const errObject = { ...error };

if (errObject?.response?.status === 503) {
window.showReadOnlyBanner();
}
errObject?.response?.status === 503 && window.setReadOnlyBannerVisible(true);

if (errObject?.response?.data?.errors) {
const data = errObject.response.data.errors[0];
Expand All @@ -24,9 +22,15 @@ export function errorInterceptor(error) {
}
}

const readOnlyBannerInterceptor = response => {
// if has updated property, then it was a successful write request
response.hasOwnProperty('updated') && window.setReadOnlyBannerVisible(false);
return response;
};

instance.interceptors.request.use(authInterceptor);
instance.interceptors.response.use(responseDataInterceptor);
instance.interceptors.response.use(null, interceptor401);
instance.interceptors.response.use(null, errorInterceptor);
instance.interceptors.response.use(readOnlyBannerInterceptor, errorInterceptor);

export default instance;

0 comments on commit 5329dca

Please sign in to comment.