import { PROJECT_ID } from '@/constants'; import { openOverlaysDrawer, openSearchDrawerWithSelectedTab } from '@/redux/drawer/drawer.slice'; import { AppDispatch } from '@/redux/store'; import { QueryData } from '@/types/query'; import { getDefaultSearchTab } from '@/components/FunctionalArea/TopBar/SearchBar/SearchBar.utils'; import { PluginsManager } from '@/services/pluginsManager'; import { createAsyncThunk } from '@reduxjs/toolkit'; import { ZERO } from '@/constants/common'; import { getConstant } from '@/redux/constant/constant.thunks'; import { getAllBackgroundsByProjectId } from '../backgrounds/backgrounds.thunks'; import { getConfiguration, getConfigurationOptions } from '../configuration/configuration.thunks'; import { initMapBackground, initMapPosition, initMapSizeAndModelId, initOpenedMaps, } from '../map/map.thunks'; import { getModels } from '../models/models.thunks'; import { getInitOverlays } from '../overlayBioEntity/overlayBioEntity.thunk'; import { getAllPublicOverlaysByProjectId, getAllUserOverlaysByCreator, } from '../overlays/overlays.thunks'; import { getAllPlugins, getInitPlugins } from '../plugins/plugins.thunks'; import { getProjectById, setProjectId } from '../project/project.thunks'; import { setPerfectMatch } from '../search/search.slice'; import { getSearchData } from '../search/search.thunks'; import { getStatisticsById } from '../statistics/statistics.thunks'; import { getSessionValid } from '../user/user.thunks'; import { openPluginsDrawer, setCurrentDrawerPluginHash } from '../plugins/plugins.slice'; interface InitializeAppParams { queryData: QueryData; } export const fetchInitialAppData = createAsyncThunk< void, InitializeAppParams, { dispatch: AppDispatch } >('appInit/fetchInitialAppData', async ({ queryData }, { dispatch }): Promise<void> => { dispatch(setProjectId({ queryData })); if (queryData.pluginsId) { await dispatch( getInitPlugins({ pluginsId: queryData.pluginsId, setHashedPlugin: PluginsManager.setHashedPlugin, }), ); const hash = queryData.pluginsId[ZERO]; if (hash) { dispatch(openPluginsDrawer()); dispatch(setCurrentDrawerPluginHash(hash)); } } /** Fetch all data required for rendering map */ await Promise.all([ dispatch(getConstant()), dispatch(getConfiguration()), dispatch(getConfigurationOptions()), dispatch(getProjectById(PROJECT_ID)), dispatch(getAllBackgroundsByProjectId(PROJECT_ID)), dispatch(getAllPublicOverlaysByProjectId(PROJECT_ID)), dispatch(getModels()), ]); /** Set map properties to allow rendering. If map params (modelId,backgroundId,position) are not provided in query -> it will be set to map default */ await Promise.all([ dispatch(initMapSizeAndModelId({ queryData })), dispatch(initMapPosition({ queryData })), dispatch(initMapBackground({ queryData })), ]); /** Create tabs for maps / submaps */ dispatch(initOpenedMaps({ queryData })); // Check if auth token is valid await dispatch(getSessionValid()); // Fetch data needed for export dispatch(getStatisticsById(PROJECT_ID)); // Fetch plugins list dispatch(getAllPlugins()); /** Trigger search */ if (queryData.searchValue) { dispatch(setPerfectMatch(queryData.perfectMatch)); dispatch( getSearchData({ searchQueries: queryData.searchValue, isPerfectMatch: queryData.perfectMatch, }), ); dispatch(openSearchDrawerWithSelectedTab(getDefaultSearchTab(queryData.searchValue))); } await dispatch(getAllUserOverlaysByCreator()); /** fetch overlays */ if (queryData.overlaysId) { dispatch(getInitOverlays({ overlaysId: queryData.overlaysId })); if (!queryData.searchValue) { dispatch(openOverlaysDrawer()); } } });