您的位置:首页 > 文旅 > 旅游 > Superset二次开发之Dashboard状态变更源码解读

Superset二次开发之Dashboard状态变更源码解读

2024/10/5 13:56:18 来源:https://blog.csdn.net/u013985879/article/details/140698758  浏览:    关键词:Superset二次开发之Dashboard状态变更源码解读
接口PUTapi/v1/dashboard/<dashboard_ID>

前端文件

superset-frontend\src\dashboard\components\PublishedStatus\index.jsx

69行 togglePublished()

  togglePublished() {
    this.props.savePublished(this.props.dashboardId, !this.props.isPublished);
  }

    superset-frontend\src\dashboard\actions\dashboardState.js

export function savePublished(id, isPublished) {return function savePublishedThunk(dispatch) {return SupersetClient.put({endpoint: `/api/v1/dashboard/${id}`,headers: { 'Content-Type': 'application/json' },body: JSON.stringify({published: isPublished,}),}).then(() => {dispatch(addSuccessToast(isPublished? t('This dashboard is now published'): t('This dashboard is now hidden'),),);dispatch(togglePublished(isPublished));}).catch(() => {dispatch(addDangerToast(t('You do not have permissions to edit this dashboard.'),),);});};

代码解读

这个函数的核心逻辑是通过API请求更新仪表板的发布状态,然后根据请求的结果更新应用的UI状态和通知用户。它使用了Redux的Thunk中间件来处理异步操作,并根据请求的成功或失败情况显示相应的通知。

  • 函数定义

    • savePublished(id, isPublished):这是一个带有两个参数的函数。id是仪表板的唯一标识符,isPublished是一个布尔值,表示仪表板是否被发布。
  • Thunk Action Creator

    • savePublishedThunk(dispatch):这是一个Thunk action creator,返回一个函数,接收dispatch作为参数。Thunk是Redux中的一个中间件,用于处理异步操作。
  • API 请求

    • SupersetClient.put({...}):使用SupersetClient发送一个PUT请求,更新指定仪表板的发布状态。请求的endpoint/api/v1/dashboard/${id},其中${id}代表仪表板的唯一标识符。headers指定了请求的内容类型为JSON,body包含了将published状态设置为isPublished的JSON字符串。
  • Promise 处理

    • .then(...):如果请求成功,执行以下操作:

      • 调用addSuccessToast函数,根据isPublished的值显示成功消息:如果仪表板已发布,显示“仪表板现在已发布”;如果未发布,显示“仪表板现在已隐藏”。
      • 调用togglePublished(isPublished)更新Redux状态,以反映仪表板的发布状态变化。
    • .catch(...):如果请求失败,执行以下操作:

      • 调用addDangerToast函数,显示错误消息“您没有权限编辑此仪表板”。

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com