Farmbot-Web-App/frontend/resources/actions.ts

39 lines
1.3 KiB
TypeScript
Raw Normal View History

2018-08-01 07:03:35 -06:00
import { TaggedResource, SpecialStatus } from "farmbot";
2017-06-29 12:54:02 -06:00
import { UnsafeError } from "../interfaces";
2017-07-17 12:34:46 -06:00
import { Actions } from "../constants";
import { toastErrors } from "../toast_errors";
2017-11-21 14:48:24 -07:00
import { stopTracking } from "../connectivity/data_consistency";
2017-06-29 12:54:02 -06:00
export function saveOK(payload: TaggedResource) {
2017-07-17 12:34:46 -06:00
return { type: Actions.SAVE_RESOURCE_OK, payload };
2017-06-29 12:54:02 -06:00
}
export function destroyOK(payload: TaggedResource) {
2017-07-17 12:34:46 -06:00
return { type: Actions.DESTROY_RESOURCE_OK, payload };
2017-06-29 12:54:02 -06:00
}
export interface GeneralizedError {
err: UnsafeError;
uuid: string;
/** Used to rollback the status of the failed resource.
* If we didn't do this, resources would go from `DIRTY => NONE` even though
* they are still DIRTY. */
statusBeforeError: SpecialStatus;
}
2017-06-29 12:54:02 -06:00
/** Generalized error handler when there are not special error handling
* requirements */
2017-11-21 14:11:25 -07:00
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;
}
2017-06-29 12:54:02 -06:00
toastErrors(payload);
2017-11-21 14:48:24 -07:00
stopTracking(payload.uuid);
2017-11-20 12:44:46 -07:00
return { type: Actions._RESOURCE_NO, payload };
2017-11-21 14:11:25 -07:00
};
2017-06-29 12:54:02 -06:00
export const destroyNO = generalizedError;
export const createNO = generalizedError;
export const updateNO = generalizedError;