我是redux的新手,我面临着一个问题,我甚至不知道是否有可能解决,或者我应该改变组件和存储的工作方式。
我有一个按钮,当点击时,发送这个动作:
export const updateOrganizationSettings = createAction(
'[Organization settings effect] Update organization settings via service',
props<{ organizationSettings: OrganizationSettings }>()
);
此操作会触发以下效果:
updateOrganizationSettings$ = createEffect(
() =>
this.actions$.pipe(
ofType(organizationSettingsActionTypes.updateOrganizationSettings),
concatMap((action) =>
this.organizationSettingsService.updateOrganizationSettings(
action.organizationSettings
)
)
),
{ dispatch: false }
);
它调用一个API,在成功时返回true。
我想做的是在成功后以某种方式发送另一个改变商店的操作:
export const organizationSettingsUpdated = createAction(
'[Organization settings reducer] Organization settings updated successfully',
props<{ updatedOrganizationSettings: OrganizationSettings }>()
);
问题是来自API的响应是真/假,而这个动作需要更新的模型,而我只有第一个动作才有更新的模型。
有办法做到吗?这是不是一个糟糕的工作方式与商店?作为最后一个选项,我可以更改API以返回更新的模型,但我想知道是否还有其他可能性。
感谢
===编辑===
这是API服务调用后台内部的效果:
updateOrganizationSettings(
organizationSettings: OrganizationSettings
): Promise<boolean> {
return this.httpService
.put<boolean>('/general-settings', organizationSettings)
.then((data) => data);
}
这应该是在api调用返回true后发出的操作:
export const organizationSettingsUpdated = createAction(
'[Organization settings reducer] Organization settings updated successfully',
props<{ updatedOrganizationSettings: OrganizationSettings }>()
);
最后,这应该是减少
on(
organizationSettingsActionTypes.organizationSettingsUpdated,
(state, action) => ({
...state,
organizationSettings: action.updatedOrganizationSettings
})
)
updateOrganizationSettings$ = createEffect(() =>
this.actions$.pipe(
ofType(organizationSettingsActionTypes.updateOrganizationSettings),
mergeMap((action) => {
return this.organizationSettingsService.updateOrganizationSettings(action.organizationSettings).pipe(
map(response => updateOrganizationSettingsSuccess(response)) --> the action you are asking for
catchError(error => updateOrganizationSettingsError(error))
)
})
));
当然,您应该创建一个updateOrganizationSettingsSuccess
和updateOrganizationSettingsError
操作+将其挂接到一个reducer。
这有帮助吗?