Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Update Tags CRUD API #24839

Merged
merged 30 commits into from
Aug 25, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
30 commits
Select commit Hold shift + click to select a range
0e099cc
add create relationship dao + test
hughhhh Jul 27, 2023
9ffd7c7
aadd create command for relationships
hughhhh Jul 28, 2023
6b75e34
add api + test
hughhhh Jul 30, 2023
98ecc81
create tags modal
hughhhh Jul 31, 2023
c9ecff7
save for now
hughhhh Aug 1, 2023
93dc089
Merge branch 'master' of https://github.com/preset-io/superset into t…
hughhhh Aug 9, 2023
68d8ebf
remove unneeded migration
hughhhh Aug 9, 2023
815fe05
Merge branch 'master' of https://github.com/preset-io/superset into t…
hughhhh Aug 11, 2023
b6da96f
fix fe linting
hughhhh Aug 14, 2023
55d2e12
update test
hughhhh Aug 14, 2023
b474daf
fix test
hughhhh Aug 14, 2023
80d6090
added test
hughhhh Aug 15, 2023
423b069
fixed python test
hughhhh Aug 15, 2023
53c4590
fix linting
hughhhh Aug 17, 2023
da3a783
Update superset-frontend/src/features/tags/TagModal.tsx
hughhhh Aug 21, 2023
18a81d1
refactory
hughhhh Aug 21, 2023
417fae1
update exception
hughhhh Aug 21, 2023
c2e069d
fix update test
hughhhh Aug 22, 2023
b7cfbc6
ok
hughhhh Aug 22, 2023
748d0cb
updating status codes
hughhhh Aug 22, 2023
e41b9ac
Merge branch 'tags-modal-api' of https://github.com/preset-io/superse…
hughhhh Aug 23, 2023
1c67ce5
Merge branch 'master' of https://github.com/preset-io/superset into t…
hughhhh Aug 23, 2023
7077a12
Update superset/tags/exceptions.py
hughhhh Aug 24, 2023
bf02054
Update superset/tags/exceptions.py
hughhhh Aug 24, 2023
922c10f
update exception inherittance
hughhhh Aug 24, 2023
f035592
remove unneeded exceptions
hughhhh Aug 24, 2023
826aed4
refactored commands
hughhhh Aug 24, 2023
450a649
linting
hughhhh Aug 24, 2023
2fedeca
Update superset/tags/commands/create.py
hughhhh Aug 24, 2023
385840c
Update superset/tags/commands/create.py
hughhhh Aug 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions superset-frontend/src/features/tags/TagModal.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import React from 'react';
import { render, screen } from 'spec/helpers/testing-library';
import TagModal from 'src/features/tags/TagModal';
import fetchMock from 'fetch-mock';
import { Tag } from 'src/views/CRUD/types';

const mockedProps = {
onHide: () => {},
refreshData: () => {},
addSuccessToast: () => {},
addDangerToast: () => {},
show: true,
};

const fetchEditFetchObjects = `glob:*/api/v1/tag/get_objects/?tags=*`;

test('should render', () => {
const { container } = render(<TagModal {...mockedProps} />);
expect(container).toBeInTheDocument();
});

test('renders correctly in create mode', () => {
const { getByPlaceholderText, getByText } = render(
<TagModal {...mockedProps} />,
);

expect(getByPlaceholderText('Name of your tag')).toBeInTheDocument();
expect(getByText('Create Tag')).toBeInTheDocument();
});

test('renders correctly in edit mode', () => {
fetchMock.get(fetchEditFetchObjects, [[]]);
const editTag: Tag = {
id: 1,
name: 'Test Tag',
description: 'A test tag',
type: 'dashboard',
changed_on_delta_humanized: '',
created_by: {},
};

render(<TagModal {...mockedProps} editTag={editTag} />);
expect(screen.getByPlaceholderText(/name of your tag/i)).toHaveValue(
editTag.name,
);
});
321 changes: 321 additions & 0 deletions superset-frontend/src/features/tags/TagModal.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,321 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
import React, { ChangeEvent, useState, useEffect } from 'react';
import rison from 'rison';
import Modal from 'src/components/Modal';
import AsyncSelect from 'src/components/Select/AsyncSelect';
import { FormLabel } from 'src/components/Form';
import { t, SupersetClient } from '@superset-ui/core';
import { Input } from 'antd';
import { Divider } from 'src/components';
import Button from 'src/components/Button';
import { Tag } from 'src/views/CRUD/types';
import { fetchObjects } from 'src/features/tags/tags';

interface TaggableResourceOption {
label: string;
value: number;
key: number;
}

export enum TaggableResources {
Chart = 'chart',
Dashboard = 'dashboard',
SavedQuery = 'query',
}

interface TagModalProps {
onHide: () => void;
refreshData: () => void;
addSuccessToast: (msg: string) => void;
addDangerToast: (msg: string) => void;
show: boolean;
editTag?: Tag | null;
}

const TagModal: React.FC<TagModalProps> = ({

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we add some tests for this component?

show,
onHide,
editTag,
refreshData,
addSuccessToast,
addDangerToast,
}) => {
const [dashboardsToTag, setDashboardsToTag] = useState<
TaggableResourceOption[]
>([]);
const [chartsToTag, setChartsToTag] = useState<TaggableResourceOption[]>([]);
const [savedQueriesToTag, setSavedQueriesToTag] = useState<
TaggableResourceOption[]
>([]);

const [tagName, setTagName] = useState<string>('');
const [description, setDescription] = useState<string>('');

const isEditMode = !!editTag;
const modalTitle = isEditMode ? 'Edit Tag' : 'Create Tag';

const clearResources = () => {
setDashboardsToTag([]);
setChartsToTag([]);
setSavedQueriesToTag([]);
};

useEffect(() => {
const resourceMap: { [key: string]: TaggableResourceOption[] } = {
[TaggableResources.Dashboard]: [],
[TaggableResources.Chart]: [],
[TaggableResources.SavedQuery]: [],
};

const updateResourceOptions = ({ id, name, type }: Tag) => {
const resourceOptions = resourceMap[type];
if (resourceOptions) {
resourceOptions.push({
hughhhh marked this conversation as resolved.
Show resolved Hide resolved
value: id,
label: name,
key: id,
});
}
};
clearResources();
if (isEditMode) {
fetchObjects(
{ tags: editTag.name, types: null },
(data: Tag[]) => {
data.forEach(updateResourceOptions);
setDashboardsToTag(resourceMap[TaggableResources.Dashboard]);
setChartsToTag(resourceMap[TaggableResources.Chart]);
setSavedQueriesToTag(resourceMap[TaggableResources.SavedQuery]);
},
(error: Response) => {
addDangerToast('Error Fetching Tagged Objects');
},
);
setTagName(editTag.name);
setDescription(editTag.description);
}
}, [editTag]);

const loadData = async (
search: string,
page: number,
pageSize: number,
columns: string[],
filterColumn: string,
orderColumn: string,
endpoint: string,
) => {
const queryParams = rison.encode({
columns,
filters: [
{
col: filterColumn,
opr: 'ct',
value: search,
},
],
page,
order_column: orderColumn,
});

const { json } = await SupersetClient.get({
endpoint: `/api/v1/${endpoint}/?q=${queryParams}`,
});
const { result, count } = json;

return {
data: result.map((item: { id: number }) => ({
value: item.id,
label: item[filterColumn],
})),
totalCount: count,
};
};

const loadCharts = async (search: string, page: number, pageSize: number) =>
loadData(
search,
page,
pageSize,
['id', 'slice_name'],
'slice_name',
'slice_name',
'chart',
);

const loadDashboards = async (
search: string,
page: number,
pageSize: number,
) =>
loadData(
search,
page,
pageSize,
['id', 'dashboard_title'],
'dashboard_title',
'dashboard_title',
'dashboard',
);

const loadQueries = async (search: string, page: number, pageSize: number) =>
loadData(
search,
page,
pageSize,
['id', 'label'],
'label',
'label',
'saved_query',
);

const handleOptionChange = (resource: TaggableResources, data: any) => {
if (resource === TaggableResources.Dashboard) setDashboardsToTag(data);
else if (resource === TaggableResources.Chart) setChartsToTag(data);
else if (resource === TaggableResources.SavedQuery)
setSavedQueriesToTag(data);
};

const handleTagNameChange = (ev: ChangeEvent<HTMLInputElement>) =>
setTagName(ev.target.value);
const handleDescriptionChange = (ev: ChangeEvent<HTMLInputElement>) =>
setDescription(ev.target.value);

const onSave = () => {
const dashboards = dashboardsToTag.map(dash => ['dashboard', dash.value]);
const charts = chartsToTag.map(chart => ['chart', chart.value]);
const savedQueries = savedQueriesToTag.map(q => ['query', q.value]);

if (isEditMode) {
SupersetClient.put({
endpoint: `/api/v1/tag/${editTag.id}`,
jsonPayload: {
description,
name: tagName,
objects_to_tag: [...dashboards, ...charts, ...savedQueries],
},
}).then(({ json = {} }) => {
refreshData();
addSuccessToast(t('Tag updated'));
});
} else {
SupersetClient.post({
endpoint: `/api/v1/tag/`,
jsonPayload: {
description,
name: tagName,
objects_to_tag: [...dashboards, ...charts, ...savedQueries],
},
}).then(({ json = {} }) => {
refreshData();
addSuccessToast(t('Tag created'));
});
}
onHide();
};

return (
<Modal
title={modalTitle}
onHide={() => {
setTagName('');
setDescription('');
setDashboardsToTag([]);
setChartsToTag([]);
setSavedQueriesToTag([]);
onHide();
}}
show={show}
footer={
<div>
<Button
data-test="modal-save-dashboard-button"
buttonStyle="secondary"
onClick={onHide}
>
{t('Cancel')}
</Button>
<Button
data-test="modal-save-dashboard-button"
buttonStyle="primary"
onClick={onSave}
>
{t('Save')}
</Button>
</div>
}
>
<>
<FormLabel>{t('Tag Name')}</FormLabel>
<Input
onChange={handleTagNameChange}
placeholder={t('Name of your tag')}
value={tagName}
/>
<FormLabel>{t('Description')}</FormLabel>
<Input
onChange={handleDescriptionChange}
placeholder={t('Add description of your tag')}
value={description}
/>
<Divider />
<AsyncSelect
ariaLabel={t('Select Dashboards')}
mode="multiple"
name="dashboards"
// @ts-ignore
value={dashboardsToTag}
options={loadDashboards}
onChange={value =>
handleOptionChange(TaggableResources.Dashboard, value)
}
header={<FormLabel>{t('Dashboards')}</FormLabel>}
allowClear
/>
<AsyncSelect
ariaLabel={t('Select Charts')}
mode="multiple"
name="charts"
// @ts-ignore
value={chartsToTag}
options={loadCharts}
onChange={value => handleOptionChange(TaggableResources.Chart, value)}
header={<FormLabel>{t('Charts')}</FormLabel>}
allowClear
/>
<AsyncSelect
ariaLabel={t('Select Saved Queries')}
mode="multiple"
name="savedQueries"
// @ts-ignore
value={savedQueriesToTag}
options={loadQueries}
onChange={value =>
handleOptionChange(TaggableResources.SavedQuery, value)
}
header={<FormLabel>{t('Saved Queries')}</FormLabel>}
allowClear
/>
</>
</Modal>
);
};

export default TagModal;
10 changes: 10 additions & 0 deletions superset-frontend/src/features/tags/tags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,16 @@ export function fetchAllTags(
.catch(response => error(response));
}

export function fetchSingleTag(
name: string,
callback: (json: JsonObject) => void,
error: (response: Response) => void,
) {
SupersetClient.get({ endpoint: `/api/v1/tag` })
.then(({ json }) => callback(json))
.catch(response => error(response));
}

export function fetchTags(
{
objectType,
Expand Down
Loading