forked from ToolJet/ToolJet
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataSourcesStore.js
More file actions
63 lines (60 loc) · 2.35 KB
/
dataSourcesStore.js
File metadata and controls
63 lines (60 loc) · 2.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import { create, zustandDevTools } from './utils';
import { datasourceService, globalDatasourceService } from '@/_services';
import { shallow } from 'zustand/shallow';
import { DATA_SOURCE_TYPE } from '@/_helpers/constants';
const initialState = {
dataSources: [],
loadingDataSources: true,
globalDataSources: [],
sampleDataSource: null,
globalDataSourceStatus: {
isSaving: false,
isEditing: false,
unSavedModalVisible: false,
action: null,
saveAction: null,
},
};
export const useDataSourcesStore = create(
zustandDevTools(
(set, get) => ({
...initialState,
actions: {
//!DON'T REMOVE environmentId WHILE RESOLVING CONFLICTS !!!
fetchDataSources: (appId, environmentId) => {
if (appId && environmentId) {
set({ loadingDataSources: true });
datasourceService.getAll(appId, environmentId).then((data) => {
set({
dataSources: data.data_sources,
loadingDataSources: false,
});
});
}
},
fetchGlobalDataSources: (organizationId, appVersionId, environmentId) => {
globalDatasourceService.getForApp(organizationId, appVersionId, environmentId).then((data) => {
set({
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],
});
});
},
setGlobalDataSourceStatus: (status) =>
set({
globalDataSourceStatus: {
...get().globalDataSourceStatus,
...status,
},
}),
},
}),
{ name: 'Data Source Store' }
)
);
export const useDataSources = () => useDataSourcesStore((state) => state.dataSources);
export const useGlobalDataSources = () => useDataSourcesStore((state) => state.globalDataSources);
export const useSampleDataSource = () => useDataSourcesStore((state) => state.sampleDataSource);
export const useLoadingDataSources = () => useDataSourcesStore((state) => state.loadingDataSources);
export const useDataSourcesActions = () => useDataSourcesStore((state) => state.actions);
export const useGlobalDataSourcesStatus = () => useDataSourcesStore((state) => state.globalDataSourceStatus, shallow);