Skip to content
Snippets Groups Projects
Unverified Commit 0ef26e64 authored by Anton Kulyk's avatar Anton Kulyk Committed by GitHub
Browse files

Convert native QB actions to TypeScript (#33234)

parent a47039c6
No related merge requests found
...@@ -5,6 +5,17 @@ import { createThunkAction } from "metabase/lib/redux"; ...@@ -5,6 +5,17 @@ import { createThunkAction } from "metabase/lib/redux";
import Questions from "metabase/entities/questions"; import Questions from "metabase/entities/questions";
import { getMetadata } from "metabase/selectors/metadata"; import { getMetadata } from "metabase/selectors/metadata";
import type {
CardId,
NativeQuerySnippet,
Parameter,
TemplateTag,
} from "metabase-types/api";
import type { Dispatch, GetState } from "metabase-types/store";
import type NativeQuery from "metabase-lib/queries/NativeQuery";
import { import {
getDataReferenceStack, getDataReferenceStack,
getNativeEditorCursorOffset, getNativeEditorCursorOffset,
...@@ -27,7 +38,7 @@ export const setDataReferenceStack = createAction(SET_DATA_REFERENCE_STACK); ...@@ -27,7 +38,7 @@ export const setDataReferenceStack = createAction(SET_DATA_REFERENCE_STACK);
export const POP_DATA_REFERENCE_STACK = "metabase/qb/POP_DATA_REFERENCE_STACK"; export const POP_DATA_REFERENCE_STACK = "metabase/qb/POP_DATA_REFERENCE_STACK";
export const popDataReferenceStack = createThunkAction( export const popDataReferenceStack = createThunkAction(
POP_DATA_REFERENCE_STACK, POP_DATA_REFERENCE_STACK,
() => (dispatch, getState) => { () => (dispatch: Dispatch, getState: GetState) => {
const stack = getDataReferenceStack(getState()); const stack = getDataReferenceStack(getState());
dispatch(setDataReferenceStack(stack.slice(0, -1))); dispatch(setDataReferenceStack(stack.slice(0, -1)));
}, },
...@@ -37,17 +48,18 @@ export const PUSH_DATA_REFERENCE_STACK = ...@@ -37,17 +48,18 @@ export const PUSH_DATA_REFERENCE_STACK =
"metabase/qb/PUSH_DATA_REFERENCE_STACK"; "metabase/qb/PUSH_DATA_REFERENCE_STACK";
export const pushDataReferenceStack = createThunkAction( export const pushDataReferenceStack = createThunkAction(
PUSH_DATA_REFERENCE_STACK, PUSH_DATA_REFERENCE_STACK,
item => (dispatch, getState) => { (item: { type: string; item: unknown }) =>
const stack = getDataReferenceStack(getState()); (dispatch: Dispatch, getState: GetState) => {
dispatch(setDataReferenceStack(stack.concat([item]))); const stack = getDataReferenceStack(getState());
}, dispatch(setDataReferenceStack(stack.concat([item])));
},
); );
export const OPEN_DATA_REFERENCE_AT_QUESTION = export const OPEN_DATA_REFERENCE_AT_QUESTION =
"metabase/qb/OPEN_DATA_REFERENCE_AT_QUESTION"; "metabase/qb/OPEN_DATA_REFERENCE_AT_QUESTION";
export const openDataReferenceAtQuestion = createThunkAction( export const openDataReferenceAtQuestion = createThunkAction(
OPEN_DATA_REFERENCE_AT_QUESTION, OPEN_DATA_REFERENCE_AT_QUESTION,
id => async (dispatch, getState) => { (id: CardId) => async (dispatch: Dispatch, getState: GetState) => {
const action = await dispatch( const action = await dispatch(
Questions.actions.fetch( Questions.actions.fetch(
{ id }, { id },
...@@ -79,7 +91,9 @@ export const toggleTemplateTagsEditor = createAction( ...@@ -79,7 +91,9 @@ export const toggleTemplateTagsEditor = createAction(
export const SET_IS_SHOWING_TEMPLATE_TAGS_EDITOR = export const SET_IS_SHOWING_TEMPLATE_TAGS_EDITOR =
"metabase/qb/SET_IS_SHOWING_TEMPLATE_TAGS_EDITOR"; "metabase/qb/SET_IS_SHOWING_TEMPLATE_TAGS_EDITOR";
export const setIsShowingTemplateTagsEditor = isShowingTemplateTagsEditor => ({ export const setIsShowingTemplateTagsEditor = (
isShowingTemplateTagsEditor: boolean,
) => ({
type: SET_IS_SHOWING_TEMPLATE_TAGS_EDITOR, type: SET_IS_SHOWING_TEMPLATE_TAGS_EDITOR,
isShowingTemplateTagsEditor, isShowingTemplateTagsEditor,
}); });
...@@ -91,12 +105,14 @@ export const toggleSnippetSidebar = createAction(TOGGLE_SNIPPET_SIDEBAR, () => { ...@@ -91,12 +105,14 @@ export const toggleSnippetSidebar = createAction(TOGGLE_SNIPPET_SIDEBAR, () => {
export const SET_IS_SHOWING_SNIPPET_SIDEBAR = export const SET_IS_SHOWING_SNIPPET_SIDEBAR =
"metabase/qb/SET_IS_SHOWING_SNIPPET_SIDEBAR"; "metabase/qb/SET_IS_SHOWING_SNIPPET_SIDEBAR";
export const setIsShowingSnippetSidebar = isShowingSnippetSidebar => ({ export const setIsShowingSnippetSidebar = (
isShowingSnippetSidebar: boolean,
) => ({
type: SET_IS_SHOWING_SNIPPET_SIDEBAR, type: SET_IS_SHOWING_SNIPPET_SIDEBAR,
isShowingSnippetSidebar, isShowingSnippetSidebar,
}); });
export const setIsNativeEditorOpen = isNativeEditorOpen => ({ export const setIsNativeEditorOpen = (isNativeEditorOpen: boolean) => ({
type: SET_UI_CONTROLS, type: SET_UI_CONTROLS,
payload: { isNativeEditorOpen }, payload: { isNativeEditorOpen },
}); });
...@@ -114,60 +130,68 @@ export const SET_SNIPPET_COLLECTION_ID = ...@@ -114,60 +130,68 @@ export const SET_SNIPPET_COLLECTION_ID =
"metabase/qb/SET_SNIPPET_COLLECTION_ID"; "metabase/qb/SET_SNIPPET_COLLECTION_ID";
export const setSnippetCollectionId = createAction(SET_SNIPPET_COLLECTION_ID); export const setSnippetCollectionId = createAction(SET_SNIPPET_COLLECTION_ID);
export const openSnippetModalWithSelectedText = () => (dispatch, getState) => { export const openSnippetModalWithSelectedText =
const state = getState(); () => (dispatch: Dispatch, getState: GetState) => {
const content = getNativeEditorSelectedText(state); const state = getState();
const collection_id = getSnippetCollectionId(state); const content = getNativeEditorSelectedText(state);
dispatch(setModalSnippet({ content, collection_id })); const collection_id = getSnippetCollectionId(state);
}; dispatch(setModalSnippet({ content, collection_id }));
};
export const closeSnippetModal = () => dispatch => { export const closeSnippetModal = () => (dispatch: Dispatch) => {
dispatch(setModalSnippet(null)); dispatch(setModalSnippet(null));
}; };
export const insertSnippet = snip => (dispatch, getState) => { export const insertSnippet =
const name = snip.name; (snippet: NativeQuerySnippet) => (dispatch: Dispatch, getState: GetState) => {
const question = getQuestion(getState()); const name = snippet.name;
const query = question.query();
const nativeEditorCursorOffset = getNativeEditorCursorOffset(getState());
const nativeEditorSelectedText = getNativeEditorSelectedText(getState());
const selectionStart =
nativeEditorCursorOffset - (nativeEditorSelectedText || "").length;
const newText =
query.queryText().slice(0, selectionStart) +
`{{snippet: ${name}}}` +
query.queryText().slice(nativeEditorCursorOffset);
const datasetQuery = query
.setQueryText(newText)
.updateSnippetsWithIds([snip])
.datasetQuery();
dispatch(updateQuestion(question.setDatasetQuery(datasetQuery)));
};
export const SET_TEMPLATE_TAG = "metabase/qb/SET_TEMPLATE_TAG";
export const setTemplateTag = createThunkAction(SET_TEMPLATE_TAG, tag => {
return (dispatch, getState) => {
const question = getQuestion(getState()); const question = getQuestion(getState());
const newQuestion = question if (!question) {
.query() return;
.setTemplateTag(tag.name, tag) }
.question(); const query = question.query() as NativeQuery;
const nativeEditorCursorOffset = getNativeEditorCursorOffset(getState());
dispatch(updateQuestion(newQuestion)); const nativeEditorSelectedText = getNativeEditorSelectedText(getState());
const selectionStart =
nativeEditorCursorOffset - (nativeEditorSelectedText || "").length;
const newText =
query.queryText().slice(0, selectionStart) +
`{{snippet: ${name}}}` +
query.queryText().slice(nativeEditorCursorOffset);
const datasetQuery = query
.setQueryText(newText)
.updateSnippetsWithIds([snippet])
.datasetQuery();
dispatch(updateQuestion(question.setDatasetQuery(datasetQuery)));
}; };
});
export const SET_TEMPLATE_TAG = "metabase/qb/SET_TEMPLATE_TAG";
export const setTemplateTag = createThunkAction(
SET_TEMPLATE_TAG,
(tag: TemplateTag) => {
return (dispatch: Dispatch, getState: GetState) => {
const question = getQuestion(getState());
if (!question) {
return;
}
const query = question.query() as NativeQuery;
const newQuestion = query.setTemplateTag(tag.name, tag).question();
dispatch(updateQuestion(newQuestion));
};
},
);
export const SET_TEMPLATE_TAG_CONFIG = "metabase/qb/SET_TEMPLATE_TAG_CONFIG"; export const SET_TEMPLATE_TAG_CONFIG = "metabase/qb/SET_TEMPLATE_TAG_CONFIG";
export const setTemplateTagConfig = createThunkAction( export const setTemplateTagConfig = createThunkAction(
SET_TEMPLATE_TAG_CONFIG, SET_TEMPLATE_TAG_CONFIG,
(tag, parameter) => { (tag: TemplateTag, parameter: Parameter) => {
return (dispatch, getState) => { return (dispatch: Dispatch, getState: GetState) => {
const question = getQuestion(getState()); const question = getQuestion(getState());
const newQuestion = question if (!question) {
.query() return;
.setTemplateTagConfig(tag, parameter) }
.question(); const query = question.query() as NativeQuery;
const newQuestion = query.setTemplateTagConfig(tag, parameter).question();
dispatch(updateQuestion(newQuestion)); dispatch(updateQuestion(newQuestion));
}; };
}, },
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment