import { createAsyncThunk } from '@reduxjs/toolkit' import type { I18nState } from 'domains/i18n/i18n.types' import { ThunkAPI } from 'domains/redux/redux.types' export const setLocale = createAsyncThunk< { translations: I18nState['translations'] userLocale: I18nState['userLocale'] }, string, ThunkAPI >( 'setLocale', async (userLocale, { extra: { api }, rejectWithValue }) => { try { const response = await api.getTranslations(userLocale) return { translations: response, userLocale, } } catch (error: any) { return rejectWithValue(error) } }, { condition: (locale, { getState }) => { const { i18n: { isLoading, userLocale }, } = getState() if (locale === userLocale || isLoading) { // Already fetched or in progress, don't need to re-fetch return false } return true }, }, )