Newer
Older

Tadeusz Miesiąc
committed
import { PROJECT_ID } from '@/constants';
mateusz-winiarczyk
committed
import { openOverlaysDrawer, openSearchDrawerWithSelectedTab } from '@/redux/drawer/drawer.slice';

Tadeusz Miesiąc
committed
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';
mateusz-winiarczyk
committed
import { ZERO } from '@/constants/common';
import { getConstant } from '@/redux/constant/constant.thunks';

Tadeusz Miesiąc
committed
import { getAllBackgroundsByProjectId } from '../backgrounds/backgrounds.thunks';
import { getConfiguration, getConfigurationOptions } from '../configuration/configuration.thunks';

Tadeusz Miesiąc
committed
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';
mateusz-winiarczyk
committed
import { openPluginsDrawer, setCurrentDrawerPluginHash } from '../plugins/plugins.slice';

Tadeusz Miesiąc
committed
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,
}),
);
mateusz-winiarczyk
committed
const hash = queryData.pluginsId[ZERO];
if (hash) {
dispatch(openPluginsDrawer());
dispatch(setCurrentDrawerPluginHash(hash));
}
/** Fetch all data required for rendering map */

Tadeusz Miesiąc
committed
await Promise.all([
dispatch(getConfigurationOptions()),

Tadeusz Miesiąc
committed
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 }));
mateusz-winiarczyk
committed
if (!queryData.searchValue) {
dispatch(openOverlaysDrawer());
}