From dddac128fbc7ebc0990794ae7556c0fcfffa5ae6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?marcin=20miko=C5=82ajczak?= Date: Tue, 26 Nov 2024 18:46:44 +0100 Subject: [PATCH] pl-fe: types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: marcin mikołajczak --- packages/pl-api/lib/entities/token.ts | 2 +- packages/pl-fe/src/actions/auth.ts | 15 ++-- packages/pl-fe/src/actions/events.ts | 22 +++--- packages/pl-fe/src/actions/mfa.ts | 49 +++++++++---- packages/pl-fe/src/actions/pin-statuses.ts | 6 +- packages/pl-fe/src/actions/security.ts | 76 ++++++++++++++------- packages/pl-fe/src/reducers/auth.ts | 26 ++++--- packages/pl-fe/src/reducers/instance.ts | 2 +- packages/pl-fe/src/reducers/security.ts | 6 +- packages/pl-fe/src/reducers/status-lists.ts | 41 ++++++----- 10 files changed, 148 insertions(+), 97 deletions(-) diff --git a/packages/pl-api/lib/entities/token.ts b/packages/pl-api/lib/entities/token.ts index f0c790674..f55f81620 100644 --- a/packages/pl-api/lib/entities/token.ts +++ b/packages/pl-api/lib/entities/token.ts @@ -10,7 +10,7 @@ const tokenSchema = v.object({ scope: v.string(), created_at: v.fallback(v.optional(v.number()), undefined), - id: v.fallback(v.optional(v.number()), undefined), + id: v.fallback(v.optional(v.pipe(v.unknown(), v.transform(String))), undefined), refresh_token: v.fallback(v.optional(v.string()), undefined), expires_in: v.fallback(v.optional(v.number()), undefined), me: v.fallback(v.optional(v.string()), undefined), diff --git a/packages/pl-fe/src/actions/auth.ts b/packages/pl-fe/src/actions/auth.ts index 49d5f7286..a0d6da0bc 100644 --- a/packages/pl-fe/src/actions/auth.ts +++ b/packages/pl-fe/src/actions/auth.ts @@ -9,9 +9,9 @@ import { credentialAccountSchema, PlApiClient, - type Application, type CreateAccountParams, type CredentialAccount, + type CredentialApplication, type Token, } from 'pl-api'; import { defineMessages } from 'react-intl'; @@ -75,7 +75,7 @@ const createAppAndToken = () => interface AuthAppCreatedAction { type: typeof AUTH_APP_CREATED; - app: Application; + app: CredentialApplication; } /** Create an auth app, or use it from build config */ @@ -104,7 +104,7 @@ const createAuthApp = () => interface AuthAppAuthorizedAction { type: typeof AUTH_APP_AUTHORIZED; - app: Application; + app: CredentialApplication; token: Token; } @@ -295,18 +295,19 @@ const logOut = () => interface SwitchAccountAction { type: typeof SWITCH_ACCOUNT; - account?: Account; - background: boolean; + account: Account; } -const switchAccount = (accountId: string, background = false) => +const switchAccount = (accountId: string) => (dispatch: AppDispatch, getState: () => RootState) => { const account = selectAccount(getState(), accountId); + if (!account) return; + // Clear all stored cache from React Query queryClient.invalidateQueries(); queryClient.clear(); - return dispatch({ type: SWITCH_ACCOUNT, account, background }); + return dispatch({ type: SWITCH_ACCOUNT, account }); }; const fetchOwnAccounts = () => diff --git a/packages/pl-fe/src/actions/events.ts b/packages/pl-fe/src/actions/events.ts index 206361afb..944528869 100644 --- a/packages/pl-fe/src/actions/events.ts +++ b/packages/pl-fe/src/actions/events.ts @@ -436,7 +436,7 @@ const initEventEdit = (statusId: string) => (dispatch: AppDispatch, getState: () return getClient(getState()).statuses.getStatusSource(statusId).then(response => { dispatch({ type: STATUS_FETCH_SOURCE_SUCCESS, statusId }); - dispatch({ + dispatch({ type: EVENT_FORM_SET, composeId: `compose-event-modal-${statusId}`, text: response.text, @@ -453,19 +453,19 @@ const fetchRecentEvents = () => return; } - dispatch({ type: RECENT_EVENTS_FETCH_REQUEST }); + dispatch({ type: RECENT_EVENTS_FETCH_REQUEST }); return getClient(getState()).timelines.publicTimeline({ only_events: true, }).then(response => { dispatch(importEntities({ statuses: response.items })); - dispatch({ + dispatch({ type: RECENT_EVENTS_FETCH_SUCCESS, statuses: response.items, next: response.next, }); }).catch(error => { - dispatch({ type: RECENT_EVENTS_FETCH_FAIL, error }); + dispatch({ type: RECENT_EVENTS_FETCH_FAIL, error }); }); }; @@ -475,17 +475,17 @@ const fetchJoinedEvents = () => return; } - dispatch({ type: JOINED_EVENTS_FETCH_REQUEST }); + dispatch({ type: JOINED_EVENTS_FETCH_REQUEST }); getClient(getState).events.getJoinedEvents().then(response => { dispatch(importEntities({ statuses: response.items })); - dispatch({ + dispatch({ type: JOINED_EVENTS_FETCH_SUCCESS, statuses: response.items, next: response.next, }); }).catch(error => { - dispatch({ type: JOINED_EVENTS_FETCH_FAIL, error }); + dispatch({ type: JOINED_EVENTS_FETCH_FAIL, error }); }); }; @@ -520,7 +520,13 @@ type EventsAction = | ReturnType | ReturnType | ReturnType - | EventFormSetAction; + | EventFormSetAction + | { type: typeof RECENT_EVENTS_FETCH_REQUEST } + | { type: typeof RECENT_EVENTS_FETCH_SUCCESS; statuses: Array; next: (() => Promise>) | null } + | { type: typeof RECENT_EVENTS_FETCH_FAIL; error: unknown } + | { type: typeof JOINED_EVENTS_FETCH_REQUEST } + | { type: typeof JOINED_EVENTS_FETCH_SUCCESS; statuses: Array; next: (() => Promise>) | null } + | { type: typeof JOINED_EVENTS_FETCH_FAIL; error: unknown } export { EVENT_SUBMIT_REQUEST, diff --git a/packages/pl-fe/src/actions/mfa.ts b/packages/pl-fe/src/actions/mfa.ts index dbdad8d8e..94f3d97b9 100644 --- a/packages/pl-fe/src/actions/mfa.ts +++ b/packages/pl-fe/src/actions/mfa.ts @@ -1,5 +1,6 @@ import { getClient } from '../api'; +import type { PlApiClient } from 'pl-api'; import type { AppDispatch, RootState } from 'pl-fe/store'; const MFA_FETCH_REQUEST = 'MFA_FETCH_REQUEST' as const; @@ -24,62 +25,79 @@ const MFA_DISABLE_FAIL = 'MFA_DISABLE_FAIL' as const; const fetchMfa = () => (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: MFA_FETCH_REQUEST }); + dispatch({ type: MFA_FETCH_REQUEST }); return getClient(getState).settings.mfa.getMfaSettings().then((data) => { - dispatch({ type: MFA_FETCH_SUCCESS, data }); + dispatch({ type: MFA_FETCH_SUCCESS, data }); }).catch(() => { - dispatch({ type: MFA_FETCH_FAIL }); + dispatch({ type: MFA_FETCH_FAIL }); }); }; const fetchBackupCodes = () => (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: MFA_BACKUP_CODES_FETCH_REQUEST }); + dispatch({ type: MFA_BACKUP_CODES_FETCH_REQUEST }); return getClient(getState).settings.mfa.getMfaBackupCodes().then((data) => { - dispatch({ type: MFA_BACKUP_CODES_FETCH_SUCCESS, data }); + dispatch({ type: MFA_BACKUP_CODES_FETCH_SUCCESS, data }); return data; }).catch((error: unknown) => { - dispatch({ type: MFA_BACKUP_CODES_FETCH_FAIL }); + dispatch({ type: MFA_BACKUP_CODES_FETCH_FAIL }); throw error; }); }; const setupMfa = (method: 'totp') => (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: MFA_SETUP_REQUEST, method }); + dispatch({ type: MFA_SETUP_REQUEST, method }); return getClient(getState).settings.mfa.getMfaSetup(method).then((data) => { - dispatch({ type: MFA_SETUP_SUCCESS, data }); + dispatch({ type: MFA_SETUP_SUCCESS, data }); return data; }).catch((error: unknown) => { - dispatch({ type: MFA_SETUP_FAIL }); + dispatch({ type: MFA_SETUP_FAIL }); throw error; }); }; const confirmMfa = (method: 'totp', code: string, password: string) => (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: MFA_CONFIRM_REQUEST, method, code }); + dispatch({ type: MFA_CONFIRM_REQUEST, method, code }); return getClient(getState).settings.mfa.confirmMfaSetup(method, code, password).then((data) => { - dispatch({ type: MFA_CONFIRM_SUCCESS, method, code }); + dispatch({ type: MFA_CONFIRM_SUCCESS, method, code }); return data; }).catch((error: unknown) => { - dispatch({ type: MFA_CONFIRM_FAIL, method, code, error, skipAlert: true }); + dispatch({ type: MFA_CONFIRM_FAIL, method, code, error, skipAlert: true }); throw error; }); }; const disableMfa = (method: 'totp', password: string) => (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: MFA_DISABLE_REQUEST, method }); + dispatch({ type: MFA_DISABLE_REQUEST, method }); return getClient(getState).settings.mfa.disableMfa(method, password).then((data) => { - dispatch({ type: MFA_DISABLE_SUCCESS, method }); + dispatch({ type: MFA_DISABLE_SUCCESS, method }); return data; }).catch((error: unknown) => { - dispatch({ type: MFA_DISABLE_FAIL, method, skipAlert: true }); + dispatch({ type: MFA_DISABLE_FAIL, method, skipAlert: true }); throw error; }); }; +type MfaAction = + | { type: typeof MFA_FETCH_REQUEST } + | { type: typeof MFA_FETCH_SUCCESS; data: Awaited)['settings']['mfa']['getMfaSettings']>> } + | { type: typeof MFA_FETCH_FAIL } + | { type: typeof MFA_BACKUP_CODES_FETCH_REQUEST } + | { type: typeof MFA_BACKUP_CODES_FETCH_SUCCESS; data: Awaited)['settings']['mfa']['getMfaBackupCodes']>> } + | { type: typeof MFA_BACKUP_CODES_FETCH_FAIL } + | { type: typeof MFA_SETUP_REQUEST; method: 'totp' } + | { type: typeof MFA_SETUP_SUCCESS; data: Awaited)['settings']['mfa']['getMfaSetup']>> } + | { type: typeof MFA_SETUP_FAIL } + | { type: typeof MFA_CONFIRM_REQUEST; method: 'totp'; code: string } + | { type: typeof MFA_CONFIRM_SUCCESS; method: 'totp'; code: string } + | { type: typeof MFA_CONFIRM_FAIL; method: 'totp'; code: string; error: unknown; skipAlert: true } + | { type: typeof MFA_DISABLE_REQUEST; method: 'totp' } + | { type: typeof MFA_DISABLE_SUCCESS; method: 'totp' } + | { type: typeof MFA_DISABLE_FAIL; method: 'totp'; skipAlert: true }; + export { MFA_FETCH_REQUEST, MFA_FETCH_SUCCESS, @@ -101,4 +119,5 @@ export { setupMfa, confirmMfa, disableMfa, + type MfaAction, }; diff --git a/packages/pl-fe/src/actions/pin-statuses.ts b/packages/pl-fe/src/actions/pin-statuses.ts index 37a27b648..e9ddd0b92 100644 --- a/packages/pl-fe/src/actions/pin-statuses.ts +++ b/packages/pl-fe/src/actions/pin-statuses.ts @@ -4,7 +4,7 @@ import { getClient } from '../api'; import { importEntities } from './importer'; -import type { Status } from 'pl-api'; +import type { PaginatedResponse, Status } from 'pl-api'; import type { AppDispatch, RootState } from 'pl-fe/store'; const PINNED_STATUSES_FETCH_REQUEST = 'PINNED_STATUSES_FETCH_REQUEST' as const; @@ -20,7 +20,7 @@ const fetchPinnedStatuses = () => return getClient(getState()).accounts.getAccountStatuses(me as string, { pinned: true }).then(response => { dispatch(importEntities({ statuses: response.items })); - dispatch(fetchPinnedStatusesSuccess(response.items, null)); + dispatch(fetchPinnedStatusesSuccess(response.items, response.next)); }).catch(error => { dispatch(fetchPinnedStatusesFail(error)); }); @@ -30,7 +30,7 @@ const fetchPinnedStatusesRequest = () => ({ type: PINNED_STATUSES_FETCH_REQUEST, }); -const fetchPinnedStatusesSuccess = (statuses: Array, next: string | null) => ({ +const fetchPinnedStatusesSuccess = (statuses: Array, next: (() => Promise>) | null) => ({ type: PINNED_STATUSES_FETCH_SUCCESS, statuses, next, diff --git a/packages/pl-fe/src/actions/security.ts b/packages/pl-fe/src/actions/security.ts index b296ee5c7..f8a6702a5 100644 --- a/packages/pl-fe/src/actions/security.ts +++ b/packages/pl-fe/src/actions/security.ts @@ -11,6 +11,8 @@ import { normalizeUsername } from 'pl-fe/utils/input'; import { AUTH_LOGGED_OUT, messages } from './auth'; +import type { OauthToken } from 'pl-api'; +import type { Account } from 'pl-fe/normalizers/account'; import type { AppDispatch, RootState } from 'pl-fe/store'; const FETCH_TOKENS_REQUEST = 'FETCH_TOKENS_REQUEST' as const; @@ -47,32 +49,32 @@ const MOVE_ACCOUNT_FAIL = 'MOVE_ACCOUNT_FAIL' as const; const fetchOAuthTokens = () => (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: FETCH_TOKENS_REQUEST }); + dispatch({ type: FETCH_TOKENS_REQUEST }); return getClient(getState).settings.getOauthTokens().then((tokens) => { - dispatch({ type: FETCH_TOKENS_SUCCESS, tokens }); + dispatch({ type: FETCH_TOKENS_SUCCESS, tokens }); }).catch((e) => { - dispatch({ type: FETCH_TOKENS_FAIL }); + dispatch({ type: FETCH_TOKENS_FAIL }); }); }; const revokeOAuthTokenById = (tokenId: number) => (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: REVOKE_TOKEN_REQUEST, tokenId }); + dispatch({ type: REVOKE_TOKEN_REQUEST, tokenId }); return getClient(getState).settings.deleteOauthToken(tokenId).then(() => { - dispatch({ type: REVOKE_TOKEN_SUCCESS, tokenId }); + dispatch({ type: REVOKE_TOKEN_SUCCESS, tokenId }); }).catch(() => { - dispatch({ type: REVOKE_TOKEN_FAIL, tokenId }); + dispatch({ type: REVOKE_TOKEN_FAIL, tokenId }); }); }; const changePassword = (oldPassword: string, newPassword: string) => (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: CHANGE_PASSWORD_REQUEST }); + dispatch({ type: CHANGE_PASSWORD_REQUEST }); return getClient(getState).settings.changePassword(oldPassword, newPassword).then(response => { - dispatch({ type: CHANGE_PASSWORD_SUCCESS, response }); + dispatch({ type: CHANGE_PASSWORD_SUCCESS, response }); }).catch(error => { - dispatch({ type: CHANGE_PASSWORD_FAIL, error, skipAlert: true }); + dispatch({ type: CHANGE_PASSWORD_FAIL, error, skipAlert: true }); throw error; }); }; @@ -81,58 +83,83 @@ const resetPassword = (usernameOrEmail: string) => (dispatch: AppDispatch, getState: () => RootState) => { const input = normalizeUsername(usernameOrEmail); - dispatch({ type: RESET_PASSWORD_REQUEST }); + dispatch({ type: RESET_PASSWORD_REQUEST }); return getClient(getState).settings.resetPassword( input.includes('@') ? input : undefined, input.includes('@') ? undefined : input, ).then(() => { - dispatch({ type: RESET_PASSWORD_SUCCESS }); + dispatch({ type: RESET_PASSWORD_SUCCESS }); }).catch(error => { - dispatch({ type: RESET_PASSWORD_FAIL, error }); + dispatch({ type: RESET_PASSWORD_FAIL, error }); throw error; }); }; const changeEmail = (email: string, password: string) => (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: CHANGE_EMAIL_REQUEST, email }); + dispatch({ type: CHANGE_EMAIL_REQUEST, email }); return getClient(getState).settings.changeEmail(email, password).then(response => { - dispatch({ type: CHANGE_EMAIL_SUCCESS, email, response }); + dispatch({ type: CHANGE_EMAIL_SUCCESS, email, response }); }).catch(error => { - dispatch({ type: CHANGE_EMAIL_FAIL, email, error, skipAlert: true }); + dispatch({ type: CHANGE_EMAIL_FAIL, email, error, skipAlert: true }); throw error; }); }; const deleteAccount = (password: string) => (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: CHANGE_PASSWORD_REQUEST }); - const account = getLoggedInAccount(getState()); + dispatch({ type: CHANGE_PASSWORD_REQUEST }); + const account = getLoggedInAccount(getState())!; - dispatch({ type: DELETE_ACCOUNT_REQUEST }); + dispatch({ type: DELETE_ACCOUNT_REQUEST }); return getClient(getState).settings.deleteAccount(password).then(response => { - dispatch({ type: DELETE_ACCOUNT_SUCCESS, response }); - dispatch({ type: AUTH_LOGGED_OUT, account }); + dispatch({ type: DELETE_ACCOUNT_SUCCESS, response }); + dispatch({ type: AUTH_LOGGED_OUT, account }); toast.success(messages.loggedOut); }).catch(error => { - dispatch({ type: DELETE_ACCOUNT_FAIL, error, skipAlert: true }); + dispatch({ type: DELETE_ACCOUNT_FAIL, error, skipAlert: true }); throw error; }); }; const moveAccount = (targetAccount: string, password: string) => (dispatch: AppDispatch, getState: () => RootState) => { - dispatch({ type: MOVE_ACCOUNT_REQUEST }); + dispatch({ type: MOVE_ACCOUNT_REQUEST }); return getClient(getState).settings.moveAccount(targetAccount, password).then(response => { - dispatch({ type: MOVE_ACCOUNT_SUCCESS, response }); + dispatch({ type: MOVE_ACCOUNT_SUCCESS, response }); }).catch(error => { - dispatch({ type: MOVE_ACCOUNT_FAIL, error, skipAlert: true }); + dispatch({ type: MOVE_ACCOUNT_FAIL, error, skipAlert: true }); throw error; }); }; +type SecurityAction = + | { type: typeof FETCH_TOKENS_REQUEST } + | { type: typeof FETCH_TOKENS_SUCCESS; tokens: Array } + | { type: typeof FETCH_TOKENS_FAIL } + | { type: typeof REVOKE_TOKEN_REQUEST; tokenId: number } + | { type: typeof REVOKE_TOKEN_SUCCESS; tokenId: number } + | { type: typeof REVOKE_TOKEN_FAIL; tokenId: number } + | { type: typeof CHANGE_PASSWORD_REQUEST } + | { type: typeof CHANGE_PASSWORD_SUCCESS; response: {} } + | { type: typeof CHANGE_PASSWORD_FAIL; error: unknown; skipAlert: true } + | { type: typeof RESET_PASSWORD_REQUEST } + | { type: typeof RESET_PASSWORD_SUCCESS } + | { type: typeof RESET_PASSWORD_FAIL; error: unknown } + | { type: typeof CHANGE_EMAIL_REQUEST; email: string } + | { type: typeof CHANGE_EMAIL_SUCCESS; email: string; response: {} } + | { type: typeof CHANGE_EMAIL_FAIL; email: string; error: unknown; skipAlert: true } + | { type: typeof CHANGE_PASSWORD_REQUEST } + | { type: typeof DELETE_ACCOUNT_REQUEST } + | { type: typeof DELETE_ACCOUNT_SUCCESS; response: {} } + | { type: typeof AUTH_LOGGED_OUT; account: Account } + | { type: typeof DELETE_ACCOUNT_FAIL; error: unknown; skipAlert: true } + | { type: typeof MOVE_ACCOUNT_REQUEST } + | { type: typeof MOVE_ACCOUNT_SUCCESS; response: {} } + | { type: typeof MOVE_ACCOUNT_FAIL; error: unknown; skipAlert: true } + export { FETCH_TOKENS_REQUEST, FETCH_TOKENS_SUCCESS, @@ -165,4 +192,5 @@ export { changeEmail, deleteAccount, moveAccount, + type SecurityAction, }; diff --git a/packages/pl-fe/src/reducers/auth.ts b/packages/pl-fe/src/reducers/auth.ts index 9346dd14e..7d5e4a94e 100644 --- a/packages/pl-fe/src/reducers/auth.ts +++ b/packages/pl-fe/src/reducers/auth.ts @@ -23,7 +23,8 @@ import { ME_FETCH_SKIP, type MeAction } from '../actions/me'; import type { PlfeResponse } from 'pl-fe/api'; import type { Account as AccountEntity } from 'pl-fe/normalizers/account'; -import type { AnyAction } from 'redux'; + +type Action = AuthAction | MeAction | PreloadAction; const backendUrl = (isURL(BuildConfig.BACKEND_URL) ? BuildConfig.BACKEND_URL : ''); @@ -296,7 +297,7 @@ const updateState = (state: State, updater: (state: Draft) => void, clien return { ...newState, client: newClient }; }; -const reducer = (state: State, action: AnyAction | AuthAction | MeAction | PreloadAction): State => { +const reducer = (state: State, action: Action): State => { switch (action.type) { case AUTH_APP_CREATED: return updateState(state, (draft) => { @@ -304,7 +305,7 @@ const reducer = (state: State, action: AnyAction | AuthAction | MeAction | Prelo }); case AUTH_APP_AUTHORIZED: return updateState(state, (draft) => { - draft.app = ({ ...draft.app, ...action.token }); + if (draft.app) draft.app = ({ ...draft.app, ...action.token }); }); case AUTH_LOGGED_IN: return updateState(state, (draft) => { @@ -328,17 +329,20 @@ const reducer = (state: State, action: AnyAction | AuthAction | MeAction | Prelo }); case VERIFY_CREDENTIALS_FAIL: return updateState(state, (draft) => { - deleteForbiddenToken(draft, action.error, action.token); + deleteForbiddenToken(draft, action.error as any, action.token); }); case SWITCH_ACCOUNT: return updateState(state, (draft) => { draft.me = action.account.url; }, () => { + const accessToken = state.users[action.account.url]?.access_token; + if (state.client.baseURL === parseBaseURL(action.account.url)) { - state.client.accessToken = action.account.access_token; + state.client.accessToken = accessToken; return state.client; } - return new PlApiClient(parseBaseURL(action.account.url) || backendUrl, action.account.access_token); + + return new PlApiClient(parseBaseURL(action.account.url) || backendUrl, accessToken); }); case ME_FETCH_SKIP: return updateState(state, (draft) => { @@ -373,7 +377,7 @@ const userSwitched = (oldState: State, state: State) => { return stillValid && didChange && !userUpgradedUrl; }; -const maybeReload = (oldState: State, state: State, action: AnyAction) => { +const maybeReload = (oldState: State, state: State, action: Action) => { const loggedOutStandalone = action.type === AUTH_LOGGED_OUT && action.standalone; const switched = userSwitched(oldState, state); @@ -382,19 +386,13 @@ const maybeReload = (oldState: State, state: State, action: AnyAction) => { } }; -const auth = (oldState: State = initialState, action: AnyAction): State => { +const auth = (oldState: State = initialState, action: Action): State => { const state = reducer(oldState, action); if (state !== oldState) { // Persist the state in localStorage persistAuth(state); - // When middle-clicking a profile, we want to save the - // user in localStorage, but not update the reducer - if (action.background === true) { - return oldState; - } - // Persist the session persistSession(state); diff --git a/packages/pl-fe/src/reducers/instance.ts b/packages/pl-fe/src/reducers/instance.ts index a208cbeed..2822663b4 100644 --- a/packages/pl-fe/src/reducers/instance.ts +++ b/packages/pl-fe/src/reducers/instance.ts @@ -77,7 +77,7 @@ const persistInstance = (instance: { domain: string }, host: string | null = get } }; -const handleInstanceFetchFail = (state: State, error: Record) => { +const handleInstanceFetchFail = (state: State, error: any) => { if (error.response?.status === 401) { return handleAuthFetch(state); } else { diff --git a/packages/pl-fe/src/reducers/security.ts b/packages/pl-fe/src/reducers/security.ts index 634e3f0dd..3c78a5dea 100644 --- a/packages/pl-fe/src/reducers/security.ts +++ b/packages/pl-fe/src/reducers/security.ts @@ -4,11 +4,11 @@ import { MFA_FETCH_SUCCESS, MFA_CONFIRM_SUCCESS, MFA_DISABLE_SUCCESS, + type MfaAction, } from '../actions/mfa'; -import { FETCH_TOKENS_SUCCESS, REVOKE_TOKEN_SUCCESS } from '../actions/security'; +import { FETCH_TOKENS_SUCCESS, REVOKE_TOKEN_SUCCESS, type SecurityAction } from '../actions/security'; import type { OauthToken } from 'pl-api'; -import type { AnyAction } from 'redux'; interface State { tokens: Array; @@ -34,7 +34,7 @@ const enableMfa = (state: State, method: string) => state.mfa.settings = { ...st const disableMfa = (state: State, method: string) => state.mfa.settings = { ...state.mfa.settings, [method]: false }; -const security = (state = initialState, action: AnyAction) => { +const security = (state = initialState, action: MfaAction | SecurityAction) => { switch (action.type) { case FETCH_TOKENS_SUCCESS: return create(state, (draft) => { diff --git a/packages/pl-fe/src/reducers/status-lists.ts b/packages/pl-fe/src/reducers/status-lists.ts index 534664aa8..0bf038b6f 100644 --- a/packages/pl-fe/src/reducers/status-lists.ts +++ b/packages/pl-fe/src/reducers/status-lists.ts @@ -1,16 +1,5 @@ import { create } from 'mutative'; -import { - STATUS_QUOTES_EXPAND_FAIL, - STATUS_QUOTES_EXPAND_REQUEST, - STATUS_QUOTES_EXPAND_SUCCESS, - STATUS_QUOTES_FETCH_FAIL, - STATUS_QUOTES_FETCH_REQUEST, - STATUS_QUOTES_FETCH_SUCCESS, - type StatusQuotesAction, -} from 'pl-fe/actions/status-quotes'; -import { STATUS_CREATE_SUCCESS } from 'pl-fe/actions/statuses'; - import { BOOKMARKED_STATUSES_FETCH_REQUEST, BOOKMARKED_STATUSES_FETCH_SUCCESS, @@ -19,7 +8,7 @@ import { BOOKMARKED_STATUSES_EXPAND_SUCCESS, BOOKMARKED_STATUSES_EXPAND_FAIL, type BookmarksAction, -} from '../actions/bookmarks'; +} from 'pl-fe/actions/bookmarks'; import { RECENT_EVENTS_FETCH_REQUEST, RECENT_EVENTS_FETCH_SUCCESS, @@ -28,7 +17,7 @@ import { JOINED_EVENTS_FETCH_SUCCESS, JOINED_EVENTS_FETCH_FAIL, type EventsAction, -} from '../actions/events'; +} from 'pl-fe/actions/events'; import { FAVOURITED_STATUSES_FETCH_REQUEST, FAVOURITED_STATUSES_FETCH_SUCCESS, @@ -43,7 +32,7 @@ import { ACCOUNT_FAVOURITED_STATUSES_EXPAND_SUCCESS, ACCOUNT_FAVOURITED_STATUSES_EXPAND_FAIL, type FavouritesAction, -} from '../actions/favourites'; +} from 'pl-fe/actions/favourites'; import { FAVOURITE_SUCCESS, UNFAVOURITE_SUCCESS, @@ -52,8 +41,8 @@ import { PIN_SUCCESS, UNPIN_SUCCESS, type InteractionsAction, -} from '../actions/interactions'; -import { PINNED_STATUSES_FETCH_SUCCESS, type PinStatusesAction } from '../actions/pin-statuses'; +} from 'pl-fe/actions/interactions'; +import { PINNED_STATUSES_FETCH_SUCCESS, type PinStatusesAction } from 'pl-fe/actions/pin-statuses'; import { SCHEDULED_STATUSES_FETCH_REQUEST, SCHEDULED_STATUSES_FETCH_SUCCESS, @@ -63,10 +52,20 @@ import { SCHEDULED_STATUSES_EXPAND_FAIL, SCHEDULED_STATUS_CANCEL_REQUEST, SCHEDULED_STATUS_CANCEL_SUCCESS, -} from '../actions/scheduled-statuses'; + type ScheduledStatusesAction, +} from 'pl-fe/actions/scheduled-statuses'; +import { + STATUS_QUOTES_EXPAND_FAIL, + STATUS_QUOTES_EXPAND_REQUEST, + STATUS_QUOTES_EXPAND_SUCCESS, + STATUS_QUOTES_FETCH_FAIL, + STATUS_QUOTES_FETCH_REQUEST, + STATUS_QUOTES_FETCH_SUCCESS, + type StatusQuotesAction, +} from 'pl-fe/actions/status-quotes'; +import { STATUS_CREATE_SUCCESS, type StatusesAction } from 'pl-fe/actions/statuses'; import type { PaginatedResponse, ScheduledStatus, Status } from 'pl-api'; -import type { AnyAction } from 'redux'; interface StatusList { next: (() => Promise>) | null; @@ -156,7 +155,7 @@ const removeBookmarkFromLists = (state: State, status: Pick { +const statusLists = (state = initialState, action: BookmarksAction | EventsAction | FavouritesAction | InteractionsAction | PinStatusesAction | ScheduledStatusesAction | StatusesAction | StatusQuotesAction): State => { switch (action.type) { case FAVOURITED_STATUSES_FETCH_REQUEST: case FAVOURITED_STATUSES_EXPAND_REQUEST: @@ -209,9 +208,9 @@ const statusLists = (state = initialState, action: AnyAction | BookmarksAction | case SCHEDULED_STATUSES_EXPAND_FAIL: return create(state, draft => setLoading(draft, 'scheduled_statuses', false)); case SCHEDULED_STATUSES_FETCH_SUCCESS: - return create(state, draft => normalizeList(draft, 'scheduled_statuses', action.statuses, action.next)); + return create(state, draft => normalizeList(draft, 'scheduled_statuses', action.statuses, action.next as any)); case SCHEDULED_STATUSES_EXPAND_SUCCESS: - return create(state, draft => appendToList(draft, 'scheduled_statuses', action.statuses, action.next)); + return create(state, draft => appendToList(draft, 'scheduled_statuses', action.statuses, action.next as any)); case SCHEDULED_STATUS_CANCEL_REQUEST: case SCHEDULED_STATUS_CANCEL_SUCCESS: return create(state, draft => removeOneFromList(draft, 'scheduled_statuses', action.statusId));