2023-05-10 10:14:38 +00:00
|
|
|
import { create, zustandDevTools } from './utils';
|
|
|
|
|
import { datasourceService, globalDatasourceService } from '@/_services';
|
2024-04-30 16:19:37 +00:00
|
|
|
import { DATA_SOURCE_TYPE } from '@/_helpers/constants';
|
2023-05-10 10:14:38 +00:00
|
|
|
|
2024-04-29 11:06:10 +00:00
|
|
|
const initialState = {
|
|
|
|
|
dataSources: [],
|
|
|
|
|
loadingDataSources: true,
|
|
|
|
|
globalDataSources: [],
|
2024-04-30 16:19:37 +00:00
|
|
|
sampleDataSource: null,
|
2024-04-29 11:06:10 +00:00
|
|
|
globalDataSourceStatus: {
|
|
|
|
|
isSaving: false,
|
|
|
|
|
isEditing: false,
|
|
|
|
|
unSavedModalVisible: false,
|
|
|
|
|
action: null,
|
|
|
|
|
saveAction: null,
|
|
|
|
|
},
|
|
|
|
|
};
|
2023-05-10 10:14:38 +00:00
|
|
|
|
2024-04-29 11:06:10 +00:00
|
|
|
export const useDataSourcesStore = create(
|
|
|
|
|
zustandDevTools(
|
|
|
|
|
(set, get) => ({
|
|
|
|
|
...initialState,
|
|
|
|
|
actions: {
|
|
|
|
|
fetchDataSources: (appId) => {
|
|
|
|
|
set({ loadingDataSources: true });
|
|
|
|
|
datasourceService.getAll(appId).then((data) => {
|
|
|
|
|
set({
|
|
|
|
|
dataSources: data.data_sources,
|
|
|
|
|
loadingDataSources: false,
|
2023-06-06 04:54:54 +00:00
|
|
|
});
|
2024-04-29 11:06:10 +00:00
|
|
|
});
|
|
|
|
|
},
|
|
|
|
|
fetchGlobalDataSources: (organizationId) => {
|
|
|
|
|
globalDatasourceService.getAll(organizationId).then((data) => {
|
2024-02-05 13:44:14 +00:00
|
|
|
set({
|
2024-04-30 16:19:37 +00:00
|
|
|
globalDataSources: data.data_sources?.filter((source) => source?.type != DATA_SOURCE_TYPE.SAMPLE),
|
|
|
|
|
sampleDataSource: data.data_sources?.filter((source) => source?.type == DATA_SOURCE_TYPE.SAMPLE)[0],
|
2024-04-29 11:06:10 +00:00
|
|
|
});
|
|
|
|
|
});
|
2023-06-06 04:54:54 +00:00
|
|
|
},
|
2024-04-29 11:06:10 +00:00
|
|
|
setGlobalDataSourceStatus: (status) =>
|
|
|
|
|
set({
|
|
|
|
|
globalDataSourceStatus: {
|
|
|
|
|
...get().globalDataSourceStatus,
|
|
|
|
|
...status,
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
},
|
|
|
|
|
}),
|
|
|
|
|
{ name: 'Data Source Store' }
|
|
|
|
|
)
|
|
|
|
|
);
|
2023-05-10 10:14:38 +00:00
|
|
|
|
|
|
|
|
export const useDataSources = () => useDataSourcesStore((state) => state.dataSources);
|
|
|
|
|
export const useGlobalDataSources = () => useDataSourcesStore((state) => state.globalDataSources);
|
2024-04-30 16:19:37 +00:00
|
|
|
export const useSampleDataSource = () => useDataSourcesStore((state) => state.sampleDataSource);
|
2023-05-10 10:14:38 +00:00
|
|
|
export const useLoadingDataSources = () => useDataSourcesStore((state) => state.loadingDataSources);
|
|
|
|
|
export const useDataSourcesActions = () => useDataSourcesStore((state) => state.actions);
|
2023-08-28 11:44:26 +00:00
|
|
|
export const useGlobalDataSourcesStatus = () => useDataSourcesStore((state) => state.globalDataSourceStatus);
|