Superset二次开发之Dashboard状态变更源码解读
创始人
2024-12-01 13:06:50
接口PUTapi/v1/dashboard/

前端文件

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函数,显示错误消息“您没有权限编辑此仪表板”。

相关内容

热门资讯

四部门印发行动方案,促进人工智... 近日,国家能源局会同国家发展改革委、工业和信息化部、国家数据局印发《关于促进人工智能与能源双向赋能的...
瞄准8时13分!一起看发射场准... 我国将于5月11日8时13分发射天舟十号货运飞船。目前,长征七号遥十一运载火箭已完成推进剂加注。 距...
天舟十号货运飞船点火发射 今天上午,搭载天舟十号货运飞船的长征七号遥十一运载火箭,在我国文昌航天发射场点火发射。
原创 微... 前段时间 iOS 微信发布了 8.0.73 正式版,安卓微信发布了 8.0.72 测试版,这次的更新...
任正非罕见出镜:华为芯片基础技... 感谢IT之家网友 的线索投递! 5 月 10 日消息,在 5 月 8 日播出的《新闻联播》节目中,...