Skip to content

Commit

Permalink
fix: added bucket error handling for 401 status (issue #425) (#487)
Browse files Browse the repository at this point in the history
  • Loading branch information
denys-kolomiitsev authored Jan 10, 2024
1 parent 139fc87 commit 75cdd07
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 21 deletions.
13 changes: 7 additions & 6 deletions src/pages/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ export default function Home({ initialState }: Props) {
SettingsSelectors.selectEnabledFeatures,
);

const shouldLogin =
!authDisabled &&
(session.status !== 'authenticated' || !isClientSessionValid(session));

// EFFECTS --------------------------------------------
useEffect(() => {
if (!isIframe && !authDisabled && !isClientSessionValid(session)) {
Expand All @@ -80,8 +84,8 @@ export default function Home({ initialState }: Props) {
handleSetProperVHPoints();
window.addEventListener('resize', handleSetProperVHPoints);

dispatch(SettingsActions.initApp());
}, [dispatch, initialState]);
dispatch(SettingsActions.initApp({ shouldLogin }));
}, [dispatch, initialState, shouldLogin]);

const handleIframeAuth = async () => {
const timeout = 30 * 1000;
Expand Down Expand Up @@ -115,10 +119,7 @@ export default function Home({ initialState }: Props) {
]);
};

const shouldIframeLogin =
isIframe &&
!authDisabled &&
(session.status !== 'authenticated' || !isClientSessionValid(session));
const shouldIframeLogin = isIframe && shouldLogin;

return (
<>
Expand Down
1 change: 0 additions & 1 deletion src/store/conversations/conversations.epics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,6 @@ const initConversationsEpic: AppEpic = (action$) =>
}),
),
);

if (!conversations.length || !selectedConversationsIds.length) {
actions.push(
of(
Expand Down
19 changes: 12 additions & 7 deletions src/store/files/files.epics.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,18 @@ const getBucketEpic: AppEpic = (action$) =>
map(({ bucket }) => {
return FilesActions.setBucket({ bucket });
}),
catchError(() => {
return of(
UIActions.showToast({
message: errorsMessages.errorGettingUserFileBucket,
type: 'error',
}),
);
catchError((error) => {
if (error.status === 401) {
window.location.assign('api/auth/signin');
return EMPTY;
} else {
return of(
UIActions.showToast({
message: errorsMessages.errorGettingUserFileBucket,
type: 'error',
}),
);
}
}),
);
}),
Expand Down
17 changes: 11 additions & 6 deletions src/store/settings/settings.epic.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { concat, filter, of, switchMap, tap } from 'rxjs';
import { EMPTY, concat, delay, filter, of, switchMap, tap } from 'rxjs';

import { combineEpics } from 'redux-observable';

Expand All @@ -16,19 +16,24 @@ import { SettingsActions, SettingsSelectors } from './settings.reducers';

const initEpic: AppEpic = (action$, state$) =>
action$.pipe(
filter((action) => SettingsActions.initApp.match(action)),
filter(SettingsActions.initApp.match),
tap(() => {
const storageType = SettingsSelectors.selectStorageType(state$.value);
DataService.init(storageType);
}),
switchMap(() =>
delay(100),
switchMap(({ payload }) =>
concat(
of(ModelsActions.init()),
of(AddonsActions.init()),
of(ConversationsActions.init()),
of(PromptsActions.init()),
of(UIActions.init()),
of(FilesActions.init()),
payload.shouldLogin
? EMPTY
: concat(
of(ModelsActions.init()),
of(AddonsActions.init()),
of(FilesActions.init()),
),
),
),
);
Expand Down
2 changes: 1 addition & 1 deletion src/store/settings/settings.reducers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export const settingsSlice = createSlice({
name: 'settings',
initialState,
reducers: {
initApp: (state) => state,
initApp: (state, _action: PayloadAction<{ shouldLogin: boolean }>) => state,
setAppName: (
state,
{ payload }: PayloadAction<SettingsState['appName']>,
Expand Down

0 comments on commit 75cdd07

Please sign in to comment.