Skip to content

Commit

Permalink
feat(templates): Remove action creators thunks and other functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ebb-tide committed Aug 12, 2020
1 parent 982d705 commit c6de61a
Show file tree
Hide file tree
Showing 13 changed files with 4 additions and 1,597 deletions.
104 changes: 0 additions & 104 deletions ui/src/dashboards/actions/thunks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {push} from 'connected-react-router'
// APIs
import * as dashAPI from 'src/dashboards/apis'
import * as api from 'src/client'
import * as tempAPI from 'src/templates/api'
import {createCellWithView} from 'src/cells/actions/thunks'

// Schemas
Expand All @@ -29,7 +28,6 @@ import {
updateTimeRangeFromQueryParams,
} from 'src/dashboards/actions/ranges'
import {getVariables, hydrateVariables} from 'src/variables/actions/thunks'
import {setExportTemplate} from 'src/templates/actions/creators'
import {checkDashboardLimits} from 'src/cloud/actions/limits'
import {setCells, Action as CellAction} from 'src/cells/actions/creators'
import {
Expand All @@ -42,9 +40,6 @@ import {setLabelOnResource} from 'src/labels/actions/creators'
import * as creators from 'src/dashboards/actions/creators'

// Utils
import {filterUnusedVars} from 'src/shared/utils/filterUnusedVars'
import {dashboardToTemplate} from 'src/shared/utils/resourceToTemplate'
import {exportVariables} from 'src/variables/utils/exportVariables'
import {getSaveableView} from 'src/timeMachine/selectors'
import {incrementCloneName} from 'src/utils/naming'
import {isLimitError} from 'src/cloud/utils/limits'
Expand All @@ -61,18 +56,14 @@ import {
GetState,
View,
Cell,
DashboardTemplate,
Label,
RemoteDataState,
DashboardEntities,
ViewEntities,
ResourceType,
VariableEntities,
Variable,
LabelEntities,
} from 'src/types'
import {CellsWithViewProperties} from 'src/client'
import {arrayOfVariables} from 'src/schemas/variables'

type Action = creators.Action

Expand Down Expand Up @@ -285,37 +276,6 @@ export const getDashboards = () => async (
}
}

export const createDashboardFromTemplate = (
template: DashboardTemplate
) => async (dispatch, getState: GetState) => {
try {
const org = getOrg(getState())

await tempAPI.createDashboardFromTemplate(template, org.id)

const resp = await api.getDashboards({query: {orgID: org.id}})

if (resp.status !== 200) {
throw new Error(resp.data.message)
}

const dashboards = normalize<Dashboard, DashboardEntities, string[]>(
resp.data.dashboards,
arrayOfDashboards
)

dispatch(creators.setDashboards(RemoteDataState.Done, dashboards))
dispatch(notify(copy.importDashboardSucceeded()))
dispatch(checkDashboardLimits())
} catch (error) {
if (isLimitError(error)) {
dispatch(notify(copy.resourceLimitReached('dashboards')))
} else {
dispatch(notify(copy.importDashboardFailed(error)))
}
}
}

export const deleteDashboard = (dashboardID: string, name: string) => async (
dispatch
): Promise<void> => {
Expand Down Expand Up @@ -489,70 +449,6 @@ export const removeDashboardLabel = (
}
}

export const convertToTemplate = (dashboardID: string) => async (
dispatch,
getState: GetState
): Promise<void> => {
try {
dispatch(setExportTemplate(RemoteDataState.Loading))
const state = getState()
const org = getOrg(state)

const dashResp = await api.getDashboard({dashboardID})

if (dashResp.status !== 200) {
throw new Error(dashResp.data.message)
}

const {entities, result} = normalize<Dashboard, DashboardEntities, string>(
dashResp.data,
dashboardSchema
)

const dashboard = entities.dashboards[result]
const cells = dashboard.cells.map(cellID => entities.cells[cellID])

const pendingViews = dashboard.cells.map(cellID =>
dashAPI.getView(dashboardID, cellID)
)

const views = await Promise.all(pendingViews)
const resp = await api.getVariables({query: {orgID: org.id}})
if (resp.status !== 200) {
throw new Error(resp.data.message)
}

let vars = []

// dumb bug
// https://github.com/paularmstrong/normalizr/issues/290
if (resp.data.variables.length) {
const normVars = normalize<Variable, VariableEntities, string>(
resp.data.variables,
arrayOfVariables
)

vars = Object.values(normVars.entities.variables)
}

const variables = filterUnusedVars(vars, views)
const exportedVariables = exportVariables(variables, vars)
const dashboardTemplate = dashboardToTemplate(
state,
dashboard,
cells,
views,
exportedVariables
)

dispatch(setExportTemplate(RemoteDataState.Done, dashboardTemplate))
} catch (error) {
console.error(error)
dispatch(setExportTemplate(RemoteDataState.Error))
dispatch(notify(copy.createTemplateFailed(error)))
}
}

export const saveVEOView = (dashboardID: string) => async (
dispatch,
getState: GetState
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
Task,
Variable,
Label,
Template,
Bucket,
Telegraf,
Scraper,
Expand All @@ -17,7 +16,6 @@ export type DashboardSortKey = keyof Dashboard | 'meta.updatedAt'
export type TaskSortKey = keyof Task
export type VariableSortKey = keyof Variable | 'arguments.type'
export type LabelSortKey = keyof Label | 'properties.description'
export type TemplateSortKey = keyof Template | 'meta.name' | 'meta.description'
export type BucketSortKey = keyof Bucket | 'retentionRules[0].everySeconds'
export type TelegrafSortKey = keyof Telegraf
export type ScraperSortKey = keyof Scraper
Expand All @@ -28,7 +26,6 @@ export type SortKey =
| TaskSortKey
| VariableSortKey
| LabelSortKey
| TemplateSortKey
| BucketSortKey
| TelegrafSortKey
| ScraperSortKey
Expand Down Expand Up @@ -177,33 +174,6 @@ export const generateSortItems = (
sortDirection: Sort.Descending,
},
]
case ResourceType.Templates:
return [
{
label: 'Name (A → Z)',
sortKey: 'meta.name',
sortType: SortTypes.String,
sortDirection: Sort.Ascending,
},
{
label: 'Name (Z → A)',
sortKey: 'meta.name',
sortType: SortTypes.String,
sortDirection: Sort.Descending,
},
{
label: 'Description (A → Z)',
sortKey: 'meta.description',
sortType: SortTypes.String,
sortDirection: Sort.Ascending,
},
{
label: 'Description (Z → A)',
sortKey: 'meta.description',
sortType: SortTypes.String,
sortDirection: Sort.Descending,
},
]
case ResourceType.Buckets:
return [
{
Expand Down
30 changes: 0 additions & 30 deletions ui/src/shared/copy/notifications.ts
Original file line number Diff line number Diff line change
Expand Up @@ -375,36 +375,6 @@ export const createResourceFromTemplateFailed = (
message: `Failed to create from template: ${error}`,
})

export const updateTemplateSucceeded = (): Notification => ({
...defaultSuccessNotification,
message: `Successfully updated template.`,
})

export const updateTemplateFailed = (error: string): Notification => ({
...defaultErrorNotification,
message: `Failed to update template: ${error}`,
})

export const deleteTemplateFailed = (error: string): Notification => ({
...defaultErrorNotification,
message: `Failed to delete template: ${error}`,
})

export const deleteTemplateSuccess = (): Notification => ({
...defaultSuccessNotification,
message: 'Template was deleted successfully',
})

export const cloneTemplateFailed = (error: string): Notification => ({
...defaultErrorNotification,
message: `Failed to clone template: ${error}`,
})

export const cloneTemplateSuccess = (): Notification => ({
...defaultSuccessNotification,
message: 'Template cloned successfully',
})

export const resourceSavedAsTemplate = (
resourceName: string
): Notification => ({
Expand Down
Loading

0 comments on commit c6de61a

Please sign in to comment.