簡體   English   中英

Redux傳奇中的多個發布功能

[英]multiple post function in redux saga

我想實現將設備移動到用戶創建的組的功能。 功能是當用戶選擇設備時,會出現一個創建組按鈕。 單擊創建組按鈕將打開供用戶創建組的表單,當用戶用組名填寫表單時,將運行saga的createGroup,該實現將實現POST功能,並且創建組的網址為

/ device_groups

,我已經完成並且正在工作,但是現在用戶已經創建了該組,因此所選設備應該移至該創建的組。 我該怎么辦? 將設備發布到該已創建組的網址是

/ device_group /的groupId /加

簡而言之,用戶選擇一個或多個設備並出現創建組按鈕,用戶創建一個組,然后將那些選定的設備移至該組。

並且僅接受所選設備ID的列表。

function* createGroup(action) {
  console.log('action', action);
  const group = action.group;
  const deviceId = action.deviceId;
  if (yield call(isGroupValid, fromJS(action.group))) {
    yield fork(POST(`/device_groups`, createGroupSuccess, createGroupError, group));
  }
}

case CREATE_GROUP:
  return state
    .set('loading', true)
    .set('error', null);

case CREATE_GROUP_SUCCESS:
  const deviceGroups = fromJS(action.group, idReviver);
  return state.set('loading', false).set('error', null)
    .setIn(['deviceGroups', deviceGroups.get('id')], deviceGroups);

export function createGroup(group, deviceId) {
  return {
    type: CREATE_GROUP,
    group,
    deviceId
  };
}

export function createGroupSuccess(group) {
  return {
    type: CREATE_GROUP_SUCCESS,
    group,
  };
}

如果可能的話,我會做的就是更改組創建api端點,以接受進入該組的設備ID的列表。 這樣,您只需要進行一次API調用即可。

如果那不可能,那么我將修改該傳奇,以執行以下操作:

function* createGroup(action) {
  try {
    const group = action.group;
    const deviceIds = action.deviceIds;
    if (yield call(isGroupValid, fromJS(action.group))) {
      // Create group
      const createdGroup = yield call(API.createGroup, group);
      // Add devices to new group
      yield deviceIds.map((deviceId) => call(API.addDeviceToGroup, createdGroup, deviceId));
      // Success action
      yield put(createGroupSuccess(createdGroup));
    }
  } catch (e) {
    yield put(createGroupFailed(group));
  }
}

這里的API包含一些返回諾言並執行實際rest調用的函數。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM