-
Notifications
You must be signed in to change notification settings - Fork 14.4k
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
feat: Update Tags CRUD API #24839
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 9ffd7c7
aadd create command for relationships
hughhhh 6b75e34
add api + test
hughhhh 98ecc81
create tags modal
hughhhh c9ecff7
save for now
hughhhh 93dc089
Merge branch 'master' of https://github.com/preset-io/superset into t…
hughhhh 68d8ebf
remove unneeded migration
hughhhh 815fe05
Merge branch 'master' of https://github.com/preset-io/superset into t…
hughhhh b6da96f
fix fe linting
hughhhh 55d2e12
update test
hughhhh b474daf
fix test
hughhhh 80d6090
added test
hughhhh 423b069
fixed python test
hughhhh 53c4590
fix linting
hughhhh da3a783
Update superset-frontend/src/features/tags/TagModal.tsx
hughhhh 18a81d1
refactory
hughhhh 417fae1
update exception
hughhhh c2e069d
fix update test
hughhhh b7cfbc6
ok
hughhhh 748d0cb
updating status codes
hughhhh e41b9ac
Merge branch 'tags-modal-api' of https://github.com/preset-io/superse…
hughhhh 1c67ce5
Merge branch 'master' of https://github.com/preset-io/superset into t…
hughhhh 7077a12
Update superset/tags/exceptions.py
hughhhh bf02054
Update superset/tags/exceptions.py
hughhhh 922c10f
update exception inherittance
hughhhh f035592
remove unneeded exceptions
hughhhh 826aed4
refactored commands
hughhhh 450a649
linting
hughhhh 2fedeca
Update superset/tags/commands/create.py
hughhhh 385840c
Update superset/tags/commands/create.py
hughhhh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> = ({ | ||
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?