Skip to content

:hedgehog: PostHog is the leading platform for building self-driving products. Our developer tools – AI observability, analytics, session replay, flags, experiments, error tracking, logs, and more – capture all the context agents need to diagnose problems, uncover opportunities, and ship fixes. Steer it all from Slack, web, desktop, or the MCP.

4.3 KB
Raw Blame
import { MakeLogicType, actions, connect, kea, listeners, path, reducers } from 'kea'
import { forms } from 'kea-forms'
import type { DeepPartial, DeepPartialMap, FieldName, ValidationErrorType } from 'kea-forms'
import { router } from 'kea-router'

import { urls } from 'scenes/urls'

import { dashboardsModel } from '~/models/dashboardsModel'
import { groupsModel } from '~/models/groupsModel'

export interface DeleteDashboardForm {
    dashboardId: number | null
    deleteInsights: boolean
}

const defaultFormValues: DeleteDashboardForm = {
    dashboardId: null,
    deleteInsights: false,
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface deleteDashboardLogicValues {
    deleteDashboard: DeleteDashboardForm
    deleteDashboardAllErrors: Record<string, any>
    deleteDashboardChanged: boolean
    deleteDashboardErrors: DeepPartialMap<DeleteDashboardForm, ValidationErrorType>
    deleteDashboardHasErrors: boolean
    deleteDashboardManualErrors: Record<string, any>
    deleteDashboardModalVisible: boolean
    deleteDashboardTouched: boolean
    deleteDashboardTouches: Record<string, boolean>
    deleteDashboardValidationErrors: DeepPartialMap<DeleteDashboardForm, ValidationErrorType>
    isDeleteDashboardSubmitting: boolean
    isDeleteDashboardValid: boolean
    showDeleteDashboardErrors: boolean
}

// Generated by kea-typegen. Update if you're an agent, ignore if you're human.
export interface deleteDashboardLogicActions {
    removeDetailDashboard: (dashboardId: number) => number // groupsModel
    hideDeleteDashboardModal: () => {
        value: true
    }
    resetDeleteDashboard: (values?: DeleteDashboardForm) => {
        values?: DeleteDashboardForm
    }
    setDeleteDashboardManualErrors: (errors: Record<string, any>) => {
        errors: Record<string, any>
    }
    setDeleteDashboardValue: (
        key: FieldName,
        value: any
    ) => {
        name: FieldName
        value: any
    }
    setDeleteDashboardValues: (values: DeepPartial<DeleteDashboardForm>) => {
        values: DeepPartial<DeleteDashboardForm>
    }
    showDeleteDashboardModal: (id: number) => {
        id: number
    }
    submitDeleteDashboard: () => {
        value: boolean
    }
    submitDeleteDashboardFailure: (
        error: Error,
        errors: Record<string, any>
    ) => {
        error: Error
        errors: Record<string, any>
    }
    submitDeleteDashboardRequest: (deleteDashboard: DeleteDashboardForm) => {
        deleteDashboard: DeleteDashboardForm
    }
    submitDeleteDashboardSuccess: (deleteDashboard: DeleteDashboardForm) => {
        deleteDashboard: DeleteDashboardForm
    }
    touchDeleteDashboardField: (key: string) => {
        key: string
    }
}

export type deleteDashboardLogicType = MakeLogicType<deleteDashboardLogicValues, deleteDashboardLogicActions>

export const deleteDashboardLogic = kea<deleteDashboardLogicType>([
    path(['scenes', 'dashboard', 'deleteDashboardLogic']),
    connect(() => ({
        actions: [groupsModel, ['removeDetailDashboard']],
    })),
    actions({
        showDeleteDashboardModal: (id: number) => ({ id }),
        hideDeleteDashboardModal: true,
    }),
    reducers({
        deleteDashboardModalVisible: [
            false,
            {
                showDeleteDashboardModal: () => true,
                hideDeleteDashboardModal: () => false,
            },
        ],
    }),
    forms(() => ({
        deleteDashboard: {
            defaults: defaultFormValues,
            errors: () => ({}),
            submit: async ({ dashboardId, deleteInsights }) => {
                dashboardsModel.actions.deleteDashboard({ id: dashboardId, deleteInsights })
            },
        },
    })),
    listeners(({ actions }) => ({
        showDeleteDashboardModal: ({ id }) => {
            actions.setDeleteDashboardValues({ dashboardId: id })
        },
        hideDeleteDashboardModal: () => {
            actions.resetDeleteDashboard()
        },
        [dashboardsModel.actionTypes.deleteDashboardSuccess]: ({ dashboard }) => {
            actions.hideDeleteDashboardModal()

            actions.removeDetailDashboard(dashboard.id)

            if (router.values.currentLocation.pathname !== urls.dashboards()) {
                router.actions.push(urls.dashboards())
            }
        },
    })),
])