Skip to content
Snippets Groups Projects
Unverified Commit 0ed94c99 authored by Alexander Polyankin's avatar Alexander Polyankin Committed by GitHub
Browse files

Use typed createAsyncThunk (#31133)

parent 7d235203
No related branches found
No related tags found
No related merge requests found
import { createAsyncThunk } from "@reduxjs/toolkit";
import { push } from "react-router-redux"; import { push } from "react-router-redux";
import { getIn } from "icepick"; import { getIn } from "icepick";
import { SessionApi, UtilApi } from "metabase/services"; import { SessionApi, UtilApi } from "metabase/services";
import { getSetting } from "metabase/selectors/settings"; import { getSetting } from "metabase/selectors/settings";
import { createAsyncThunk } from "metabase/lib/redux";
import MetabaseSettings from "metabase/lib/settings"; import MetabaseSettings from "metabase/lib/settings";
import { loadLocalization } from "metabase/lib/i18n"; import { loadLocalization } from "metabase/lib/i18n";
import { deleteSession } from "metabase/lib/auth"; import { deleteSession } from "metabase/lib/auth";
...@@ -10,7 +10,6 @@ import * as Urls from "metabase/lib/urls"; ...@@ -10,7 +10,6 @@ import * as Urls from "metabase/lib/urls";
import { clearCurrentUser, refreshCurrentUser } from "metabase/redux/user"; import { clearCurrentUser, refreshCurrentUser } from "metabase/redux/user";
import { refreshSiteSettings } from "metabase/redux/settings"; import { refreshSiteSettings } from "metabase/redux/settings";
import { getUser } from "metabase/selectors/user"; import { getUser } from "metabase/selectors/user";
import { State } from "metabase-types/store";
import { import {
trackLogin, trackLogin,
trackLoginGoogle, trackLoginGoogle,
...@@ -19,12 +18,8 @@ import { ...@@ -19,12 +18,8 @@ import {
} from "./analytics"; } from "./analytics";
import { LoginData } from "./types"; import { LoginData } from "./types";
interface ThunkConfig {
state: State;
}
export const REFRESH_LOCALE = "metabase/user/REFRESH_LOCALE"; export const REFRESH_LOCALE = "metabase/user/REFRESH_LOCALE";
export const refreshLocale = createAsyncThunk<void, void, ThunkConfig>( export const refreshLocale = createAsyncThunk(
REFRESH_LOCALE, REFRESH_LOCALE,
async (_, { getState }) => { async (_, { getState }) => {
const userLocale = getUser(getState())?.locale; const userLocale = getUser(getState())?.locale;
...@@ -34,7 +29,7 @@ export const refreshLocale = createAsyncThunk<void, void, ThunkConfig>( ...@@ -34,7 +29,7 @@ export const refreshLocale = createAsyncThunk<void, void, ThunkConfig>(
); );
export const REFRESH_SESSION = "metabase/auth/REFRESH_SESSION"; export const REFRESH_SESSION = "metabase/auth/REFRESH_SESSION";
export const refreshSession = createAsyncThunk<void, void, ThunkConfig>( export const refreshSession = createAsyncThunk(
REFRESH_SESSION, REFRESH_SESSION,
async (_, { dispatch }) => { async (_, { dispatch }) => {
await Promise.all([ await Promise.all([
...@@ -51,9 +46,12 @@ interface LoginPayload { ...@@ -51,9 +46,12 @@ interface LoginPayload {
} }
export const LOGIN = "metabase/auth/LOGIN"; export const LOGIN = "metabase/auth/LOGIN";
export const login = createAsyncThunk<void, LoginPayload, ThunkConfig>( export const login = createAsyncThunk(
LOGIN, LOGIN,
async ({ data, redirectUrl = "/" }, { dispatch, rejectWithValue }) => { async (
{ data, redirectUrl = "/" }: LoginPayload,
{ dispatch, rejectWithValue },
) => {
try { try {
await SessionApi.create(data); await SessionApi.create(data);
await dispatch(refreshSession()).unwrap(); await dispatch(refreshSession()).unwrap();
...@@ -71,13 +69,12 @@ interface LoginGooglePayload { ...@@ -71,13 +69,12 @@ interface LoginGooglePayload {
} }
export const LOGIN_GOOGLE = "metabase/auth/LOGIN_GOOGLE"; export const LOGIN_GOOGLE = "metabase/auth/LOGIN_GOOGLE";
export const loginGoogle = createAsyncThunk< export const loginGoogle = createAsyncThunk(
void,
LoginGooglePayload,
ThunkConfig
>(
LOGIN_GOOGLE, LOGIN_GOOGLE,
async ({ credential, redirectUrl = "/" }, { dispatch, rejectWithValue }) => { async (
{ credential, redirectUrl = "/" }: LoginGooglePayload,
{ dispatch, rejectWithValue },
) => {
try { try {
await SessionApi.createWithGoogleAuth({ token: credential }); await SessionApi.createWithGoogleAuth({ token: credential });
await dispatch(refreshSession()).unwrap(); await dispatch(refreshSession()).unwrap();
...@@ -90,9 +87,9 @@ export const loginGoogle = createAsyncThunk< ...@@ -90,9 +87,9 @@ export const loginGoogle = createAsyncThunk<
); );
export const LOGOUT = "metabase/auth/LOGOUT"; export const LOGOUT = "metabase/auth/LOGOUT";
export const logout = createAsyncThunk<void, string | undefined, ThunkConfig>( export const logout = createAsyncThunk(
LOGOUT, LOGOUT,
async (redirectUrl, { dispatch, rejectWithValue }) => { async (redirectUrl: string | undefined, { dispatch, rejectWithValue }) => {
try { try {
await deleteSession(); await deleteSession();
await dispatch(clearCurrentUser()); await dispatch(clearCurrentUser());
...@@ -124,13 +121,12 @@ interface ResetPasswordPayload { ...@@ -124,13 +121,12 @@ interface ResetPasswordPayload {
} }
export const RESET_PASSWORD = "metabase/auth/RESET_PASSWORD"; export const RESET_PASSWORD = "metabase/auth/RESET_PASSWORD";
export const resetPassword = createAsyncThunk< export const resetPassword = createAsyncThunk(
void,
ResetPasswordPayload,
ThunkConfig
>(
RESET_PASSWORD, RESET_PASSWORD,
async ({ token, password }, { dispatch, rejectWithValue }) => { async (
{ token, password }: ResetPasswordPayload,
{ dispatch, rejectWithValue },
) => {
try { try {
await SessionApi.reset_password({ token, password }); await SessionApi.reset_password({ token, password });
await dispatch(refreshSession()).unwrap(); await dispatch(refreshSession()).unwrap();
......
export * from "./utils"; export * from "./utils";
export * from "./typed-utils";
export * from "./hooks"; export * from "./hooks";
import { createAsyncThunk as createAsyncThunkOriginal } from "@reduxjs/toolkit";
import type { State } from "metabase-types/store";
interface ThunkConfig {
state: State;
}
export const createAsyncThunk =
createAsyncThunkOriginal.withTypes<ThunkConfig>();
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment