BUG FIX: Dont set status to `SAVED` on 422s

pull/593/head
Rick Carlino 2017-12-26 10:45:08 -06:00
parent ae17f30615
commit a8aeab1a82
4 changed files with 24 additions and 11 deletions

View File

@ -93,8 +93,9 @@ export function initSave(resource: TaggedResource) {
export function save(uuid: string) {
return function (dispatch: Function, getState: GetState) {
const resource = findByUuid(getState().resources.index, uuid);
const oldStatus = resource.specialStatus;
dispatch({ type: Actions.SAVE_RESOURCE_START, payload: resource });
return dispatch(update(uuid));
return dispatch(update(uuid, oldStatus));
};
}
@ -135,10 +136,19 @@ export function refreshNO(payload: GeneralizedError): ReduxAction<GeneralizedErr
return { type: Actions.REFRESH_RESOURCE_NO, payload };
}
function update(uuid: string) {
interface AjaxUpdatePayload {
index: ResourceIndex;
uuid: string;
dispatch: Function;
statusBeforeError: SpecialStatus;
}
function update(uuid: string, statusBeforeError: SpecialStatus) {
return function (dispatch: Function, getState: GetState) {
maybeStartTracking(uuid);
return updateViaAjax(getState().resources.index, uuid, dispatch);
const { index } = getState().resources;
const payl: AjaxUpdatePayload = { index, uuid, dispatch, statusBeforeError };
return updateViaAjax(payl);
};
}
@ -206,9 +216,8 @@ export function urlFor(tag: ResourceName) {
}
/** Shared functionality in create() and update(). */
function updateViaAjax(index: ResourceIndex,
uuid: string,
dispatch: Function) {
function updateViaAjax(payl: AjaxUpdatePayload) {
const { uuid, statusBeforeError, dispatch, index } = payl;
const resource = findByUuid(index, uuid);
const { body, kind } = resource;
let verb: "post" | "put";
@ -220,7 +229,6 @@ function updateViaAjax(index: ResourceIndex,
verb = "post";
}
maybeStartTracking(uuid);
const statusBeforeError = resource.specialStatus;
return axios[verb](url, body)
.then(function (resp: HttpData<typeof resource.body>) {
const r1 = defensiveClone(resource);

View File

@ -3,6 +3,7 @@ import { UnsafeError } from "../interfaces";
import { Actions } from "../constants";
import { toastErrors } from "../toast_errors";
import { stopTracking } from "../connectivity/data_consistency";
import { catchErrors } from "../util";
export function createOK(payload: TaggedResource) {
return { type: Actions.SAVE_RESOURCE_OK, payload };
@ -27,6 +28,11 @@ export interface GeneralizedError {
/** Generalized error handler when there are not special error handling
* requirements */
export const generalizedError = (payload: GeneralizedError) => {
const badStatus = payload.statusBeforeError == SpecialStatus.SAVING;
if (badStatus) {
/** If, somehow, a `SAVING` status sneaks in, default it to DIRTY. */
payload.statusBeforeError = SpecialStatus.DIRTY;
}
toastErrors(payload);
stopTracking(payload.uuid);
return { type: Actions._RESOURCE_NO, payload };

View File

@ -170,12 +170,11 @@ export let resourceReducer = generateReducer
throw new Error("BAD UUID IN UPDATE_RESOURCE_OK");
}
})
.add<TaggedResource>(Actions._RESOURCE_NO, (s, { payload }) => {
.add<GeneralizedError>(Actions._RESOURCE_NO, (s, { payload }) => {
fancyDebug(payload);
const uuid = payload.uuid;
debugger;
const tr = merge(findByUuid(s.index, uuid), payload);
tr.specialStatus = SpecialStatus.SAVED;
tr.specialStatus = payload.statusBeforeError;
sanityCheck(tr);
return s;
})

View File

@ -473,7 +473,7 @@ export function minFwVersionCheck(current: string | undefined, min: string) {
}
}
export const catchErrors = (error: Error, errorInfo: ErrorInfo) => {
export const catchErrors = (error: Error, errorInfo: ErrorInfo | undefined) => {
Rollbar && Rollbar.error && Rollbar.error(error as any);
};