Skip to content

Commit

Permalink
actions: get -> getFailure
Browse files Browse the repository at this point in the history
  • Loading branch information
LocalNewsTV authored and plasticviking committed Nov 4, 2024
1 parent d86b0a5 commit e83548a
Show file tree
Hide file tree
Showing 9 changed files with 66 additions and 117 deletions.
5 changes: 3 additions & 2 deletions app/src/UI/OfflineDataSync/OfflineDataSyncTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,14 @@ import { Button, LinearProgress } from '@mui/material';
import { OfflineActivityRecord, selectOfflineActivity } from 'state/reducers/offlineActivity';
import { useSelector } from 'utils/use_selector';
import { useDispatch } from 'react-redux';
import { ACTIVITY_GET_LOCAL_REQUEST, ACTIVITY_OFFLINE_DELETE_ITEM, ACTIVITY_RUN_OFFLINE_SYNC } from 'state/actions';
import { ACTIVITY_OFFLINE_DELETE_ITEM, ACTIVITY_RUN_OFFLINE_SYNC } from 'state/actions';
import Delete from '@mui/icons-material/Delete';
import './OfflineDataSync.css';
import moment from 'moment';
import { FileOpen } from '@mui/icons-material';
import { ActivitySubtypeShortLabels } from 'sharedAPI';
import { useHistory } from 'react-router-dom';
import Activity from 'state/actions/activity/Activity';

export const OfflineDataSyncTable = () => {
const { working, serializedActivities } = useSelector(selectOfflineActivity);
Expand Down Expand Up @@ -60,7 +61,7 @@ export const OfflineDataSyncTable = () => {
<Button
disabled={!(workingOffline || authenticated)}
onClick={() => {
dispatch({ type: ACTIVITY_GET_LOCAL_REQUEST, payload: { activityID: key } });
dispatch(Activity.getLocal(key));
history.push(`/Records/Activity:${key}/form`);
}}
>
Expand Down
5 changes: 0 additions & 5 deletions app/src/state/actions.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,6 @@ export const ACTIVITY_GET_INITIAL_STATE_REQUEST = 'ACTIVITY_GET_INITIAL_STATE_RE
export const ACTIVITY_GET_INITIAL_STATE_SUCCESS = 'ACTIVITY_GET_INITIAL_STATE_SUCCESS';
export const ACTIVITY_GET_INITIAL_STATE_FAILURE = 'ACTIVITY_GET_INITIAL_STATE_FAILURE';

export const ACTIVITY_GET_REQUEST = 'ACTIVITY_GET_REQUEST';
export const ACTIVITY_GET_LOCAL_REQUEST = 'ACTIVITY_GET_LOCALDB_REQUEST';
export const ACTIVITY_GET_SUCCESS = 'ACTIVITY_GET_SUCCESS';
export const ACTIVITY_GET_FAILURE = 'ACTIVITY_GET_FAILURE';

export const ACTIVITY_UPDATE_GEO_REQUEST = 'ACTIVITY_UPDATE_GEO_REQUEST';
export const ACTIVITY_UPDATE_GEO_SUCCESS = 'ACTIVITY_UPDATE_GEO_SUCCESS';
export const ACTIVITY_UPDATE_GEO_FAILURE = 'ACTIVITY_UPDATE_GEO_FAILURE';
Expand Down
16 changes: 7 additions & 9 deletions app/src/state/actions/activity/Activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,7 @@ import {
//
ACTIVITY_SET_ACTIVE_REQUEST,
ACTIVITY_SET_ACTIVE_SUCCESS,
ACTIVITY_SET_ACTIVE_FAILURE,
ACTIVITY_GET_REQUEST,
ACTIVITY_GET_LOCAL_REQUEST,
ACTIVITY_GET_SUCCESS,
ACTIVITY_GET_FAILURE
ACTIVITY_SET_ACTIVE_FAILURE
} from '../../actions';
import AutoFill from './AutoFill';
import GeoJson from './GeoJson';
Expand Down Expand Up @@ -95,10 +91,12 @@ class Activity {
static readonly copy = createAction(`${this.PREFIX}/copy`);
static readonly copySuccess = createAction<Record<string, any>>(`${this.PREFIX}/copySuccess`);

static readonly get = createAction(ACTIVITY_GET_REQUEST);
static readonly getLocal = createAction(ACTIVITY_GET_LOCAL_REQUEST);
static readonly getSuccess = createAction(ACTIVITY_GET_SUCCESS);
static readonly getFailure = createAction(ACTIVITY_GET_FAILURE);
static readonly get = createAction<string>(`${this.PREFIX}/get`);
static readonly getLocal = createAction<string>(`${this.PREFIX}/getLocal`);
static readonly getSuccess = createAction<Record<string, any>>(`${this.PREFIX}/getSuccess`);
static readonly getFailure = createAction(`${this.PREFIX}/getFailure`, (arg?: Response) => ({
payload: arg
}));

static readonly setActive = createAction(ACTIVITY_SET_ACTIVE_REQUEST);
static readonly setActiveSuccess = createAction(ACTIVITY_SET_ACTIVE_SUCCESS);
Expand Down
30 changes: 10 additions & 20 deletions app/src/state/reducers/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import { RJSFSchema, UiSchema } from '@rjsf/utils';
import {
ACTIVITY_BUILD_SCHEMA_FOR_FORM_SUCCESS,
ACTIVITY_ERRORS,
ACTIVITY_GET_FAILURE,
ACTIVITY_GET_REQUEST,
ACTIVITY_GET_SUCCESS,
ACTIVITY_ON_FORM_CHANGE_SUCCESS,
ACTIVITY_SET_CURRENT_HASH_SUCCESS,
ACTIVITY_UPDATE_GEO_SUCCESS
Expand Down Expand Up @@ -163,29 +160,23 @@ function createActivityReducer(): (ActivityState: ActivityState, AnyAction) => A
draftState.activity_copy_buffer = {
form_data: action.payload
};
} else if (Activity.get.match(action)) {
draftState.failCode = null;
draftState.loading = true;
} else if (Activity.getSuccess.match(action)) {
draftState.activity = { ...action.payload };
draftState.suggestedTreatmentIDs = [];
draftState.loading = false;
} else if (Activity.getFailure.match(action)) {
draftState.loading = false;
draftState.failCode = action.payload?.status ?? null;
} else {
switch (action.type) {
case ACTIVITY_ERRORS: {
if (action.payload.errors !== undefined)
draftState.activityErrors = getCustomErrorTransformer()(action.payload.errors);
break;
}
case ACTIVITY_GET_FAILURE: {
draftState.loading = false;
draftState.failCode = action.payload?.failNetworkObj?.status;
break;
}
case ACTIVITY_GET_REQUEST: {
draftState.failCode = null;
draftState.loading = true;
break;
}
case ACTIVITY_GET_SUCCESS: {
draftState.activity = { ...action.payload.activity };
draftState.suggestedTreatmentIDs = [];
draftState.loading = false;
break;
}
case ACTIVITY_BUILD_SCHEMA_FOR_FORM_SUCCESS: {
draftState.uiSchema = action.payload.uiSchema;
draftState.schema = action.payload.schema;
Expand Down Expand Up @@ -218,7 +209,6 @@ function createActivityReducer(): (ActivityState: ActivityState, AnyAction) => A
draftState.current_activity_hash = action.payload.current;
break;
}

default:
break;
}
Expand Down
7 changes: 2 additions & 5 deletions app/src/state/reducers/userSettings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { createNextState } from '@reduxjs/toolkit';
import { Md5 } from 'ts-md5';

import {
ACTIVITY_GET_REQUEST,
CLOSE_NEW_RECORD_MENU,
GET_API_DOC_SUCCESS,
IAPP_GET_SUCCESS,
Expand Down Expand Up @@ -119,12 +118,10 @@ function createUserSettingsReducer(configuration: AppConfig): (UserSettingsState
} else if (Activity.deleteSuccess.match(action)) {
draftState.activeActivity = null;
draftState.activeActivityDescription = null;
} else if (Activity.get.match(action)) {
draftState.activeActivity = action.payload;
} else {
switch (action.type) {
case ACTIVITY_GET_REQUEST: {
draftState.activeActivity = action.payload.activityID;
break;
}
case GET_API_DOC_SUCCESS: {
draftState.apiDocsWithViewOptions = action.payload.apiDocsWithViewOptions;
draftState.apiDocsWithSelectOptions = action.payload.apiDocsWithSelectOptions;
Expand Down
15 changes: 4 additions & 11 deletions app/src/state/sagas/activity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import {
ACTIVITY_BUILD_SCHEMA_FOR_FORM_SUCCESS,
ACTIVITY_CHEM_TREATMENT_DETAILS_FORM_ON_CHANGE_REQUEST,
ACTIVITY_DEBUG,
ACTIVITY_GET_REQUEST,
ACTIVITY_GET_SUCCESS,
ACTIVITY_LINK_RECORD_REQUEST,
ACTIVITY_ON_FORM_CHANGE_REQUEST,
ACTIVITY_ON_FORM_CHANGE_SUCCESS,
Expand Down Expand Up @@ -73,11 +71,7 @@ import GeoTracking from 'state/actions/geotracking/GeoTracking';
import Activity from 'state/actions/activity/Activity';
import { selectMap } from 'state/reducers/map';

function* handle_USER_SETTINGS_READY(action) {
// if (action.payload.activeActivity) {
// yield put({ type: ACTIVITY_GET_REQUEST, payload: { activityID: action.payload.activeActivity } });
// }
}
function* handle_USER_SETTINGS_READY(action) {}

function* handle_ACTIVITY_DEBUG(action) {}

Expand Down Expand Up @@ -144,8 +138,7 @@ function* handle_URL_CHANGE(action) {
if (afterColon) {
id = afterColon.includes('/') ? afterColon.split('/')[0] : afterColon;
}
if (id && id.length === 36 && activityPageState?.activity?.activity_id !== id)
yield put({ type: ACTIVITY_GET_REQUEST, payload: { activityID: id } });
if (id && id.length === 36 && activityPageState?.activity?.activity_id !== id) yield put(Activity.get(id));
}
}

Expand Down Expand Up @@ -414,7 +407,7 @@ function* activityPageSaga() {
yield all([
takeEvery(URL_CHANGE, handle_URL_CHANGE),
takeEvery(ACTIVITY_BUILD_SCHEMA_FOR_FORM_REQUEST, handle_ACTIVITY_BUILD_SCHEMA_FOR_FORM_REQUEST),
takeEvery(ACTIVITY_GET_REQUEST, handle_ACTIVITY_GET_REQUEST),
takeEvery(Activity.get, handle_ACTIVITY_GET_REQUEST),
takeEvery(Activity.copy, handle_ACTIVITY_COPY_REQUEST),
takeEvery(Activity.getNetworkRequest, handle_ACTIVITY_GET_NETWORK_REQUEST),
takeEvery(MAP_SET_COORDS, handle_MAP_SET_COORDS),
Expand Down Expand Up @@ -444,7 +437,7 @@ function* activityPageSaga() {
takeEvery(Activity.createSuccess, handle_ACTIVITY_CREATE_SUCCESS),
takeEvery(Activity.submit, handle_ACTIVITY_SUBMIT_REQUEST),
takeEvery(ACTIVITY_DEBUG, handle_ACTIVITY_DEBUG),
takeEvery(ACTIVITY_GET_SUCCESS, handle_ACTIVITY_GET_SUCCESS),
takeEvery(Activity.getSuccess, handle_ACTIVITY_GET_SUCCESS),
takeEvery(Activity.Photo.delete, handle_ACTIVITY_DELETE_PHOTO_REQUEST),
takeEvery(Activity.Photo.add, handle_ACTIVITY_ADD_PHOTO_REQUEST),
takeEvery(Activity.Photo.edit, handle_ACTIVITY_EDIT_PHOTO_REQUEST),
Expand Down
28 changes: 10 additions & 18 deletions app/src/state/sagas/activity/dataAccess.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ import {
import {
ACTIVITY_BUILD_SCHEMA_FOR_FORM_REQUEST,
ACTIVITY_GET_INITIAL_STATE_FAILURE,
ACTIVITY_GET_LOCAL_REQUEST,
ACTIVITY_GET_REQUEST,
ACTIVITY_ON_FORM_CHANGE_REQUEST,
ACTIVITY_ON_FORM_CHANGE_SUCCESS,
ACTIVITY_SAVE_OFFLINE,
Expand Down Expand Up @@ -52,12 +50,12 @@ import Activity, { INewActivity } from 'state/actions/activity/Activity';
import UploadedPhoto from 'interfaces/UploadedPhoto';
import { PayloadAction } from '@reduxjs/toolkit';

export function* handle_ACTIVITY_GET_REQUEST(action) {
export function* handle_ACTIVITY_GET_REQUEST(action: PayloadAction<string>) {
try {
if (MOBILE) {
yield put({ type: ACTIVITY_GET_LOCAL_REQUEST, payload: { activityID: action.payload.activityID } });
yield put(Activity.getLocal(action.payload));
} else {
yield put(Activity.getNetworkRequest(action.payload.activityID));
yield put(Activity.getNetworkRequest(action.payload));
}
} catch (e) {
console.error(e);
Expand Down Expand Up @@ -238,7 +236,7 @@ export function* handle_ACTIVITY_UPDATE_GEO_REQUEST(action: Record<string, any>)
export function* handle_ACTIVITY_SAVE_SUCCESS(action) {
const activity_id = yield select((state) => state.ActivityPage.activity.activity_id);
try {
yield put({ type: ACTIVITY_GET_REQUEST, payload: { activityID: activity_id } });
yield put(Activity.get(activity_id));
yield put(
Alerts.create({
autoClose: 5,
Expand Down Expand Up @@ -303,12 +301,7 @@ export function* handle_ACTIVITY_CREATE_SUCCESS(action: PayloadAction<string>) {
try {
yield put(UserSettings.Activity.setActiveActivityId(action.payload));
yield put({ type: CLOSE_NEW_RECORD_MENU });
yield put({
type: ACTIVITY_GET_REQUEST,
payload: {
activityID: action.payload
}
});
yield put(Activity.get(action.payload));
} catch (e) {
console.error(e);
}
Expand Down Expand Up @@ -525,7 +518,7 @@ export function* handle_PAN_AND_ZOOM_TO_ACTIVITY(action) {
}

// some form autofill on create stuff will likely need to go here
export function* handle_ACTIVITY_GET_SUCCESS(action) {
export function* handle_ACTIVITY_GET_SUCCESS(action: PayloadAction<Record<string, any>>) {
try {
const activityState = yield select(selectActivity);
const type = activityState?.activity?.activity_subtype;
Expand All @@ -534,22 +527,21 @@ export function* handle_ACTIVITY_GET_SUCCESS(action) {
yield put(Activity.Suggestions.jurisdictions(activityState.activity.geometry));

// needs to be latlng expression
const isGeo = action.payload.activity?.geometry?.[0]?.geometry?.coordinates ? true : false;
//const centerPoint = center(action.payload.activity?.geometry[0]?.geometry?.coordinates);
const isGeo = action.payload?.geometry?.[0]?.geometry?.coordinates ? true : false;

let centerPoint;
if (isGeo) {
centerPoint = center(action.payload.activity?.geometry[0]?.geometry);
centerPoint = center(action.payload?.geometry[0]?.geometry);
}
if (centerPoint && isGeo) {
yield put(UserSettings.Map.setCenter(centerPoint.geometry.coordinates));
}
if (isLinkedTreatmentSubtype(type)) {
yield put(Activity.Suggestions.treatmentIdsRequest(action.payload.activity));
yield put(Activity.Suggestions.treatmentIdsRequest(action.payload));
}
const authState = yield select(selectAuth);
const userName = authState.username;
const created_by = action.payload.activity.created_by;
const created_by = action.payload.created_by;
const createdByUser = userName === created_by;

const isViewing = !createdByUser;
Expand Down
64 changes: 26 additions & 38 deletions app/src/state/sagas/activity/offline.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
import { delay, put, select, takeEvery, takeLeading } from 'redux-saga/effects';
import { ActivityStatus } from 'sharedAPI';
import {
ACTIVITY_GET_FAILURE,
ACTIVITY_GET_LOCAL_REQUEST,
ACTIVITY_GET_REQUEST,
ACTIVITY_GET_SUCCESS,
ACTIVITY_RUN_OFFLINE_SYNC,
ACTIVITY_RUN_OFFLINE_SYNC_COMPLETE,
ACTIVITY_SAVE_OFFLINE,
Expand All @@ -19,18 +15,15 @@ import Activity, { ICreateLocal } from 'state/actions/activity/Activity';
import { PayloadAction } from '@reduxjs/toolkit';

export function* handle_ACTIVITY_SAVE_OFFLINE(action) {
//const shortId = action.payload.
// all logic handled in the reducer
yield put(
Alerts.create({
content: 'Saved locally',
severity: AlertSeverity.Info,
subject: AlertSubjects.Form
})
);

// reload the activity in case the reducer modified it (create time, etc.)
yield put({ type: ACTIVITY_GET_REQUEST, payload: { activityID: action.payload.id } });
yield put(Activity.get(action.payload.id));

// trigger a sync if we're online
const connected = yield select(selectNetworkConnected);
Expand All @@ -44,47 +37,42 @@ export function* handle_ACTIVITY_CREATE_LOCAL(action: PayloadAction<ICreateLocal
yield put(Activity.createSuccess(action.payload.data.activity_id));
}

export function* handle_ACTIVITY_GET_LOCAL_REQUEST(action) {
export function* handle_ACTIVITY_GET_LOCAL_REQUEST(action: PayloadAction<string>) {
const connected = yield select(selectNetworkConnected);
const { serializedActivities } = yield select(selectOfflineActivity);
const { activityID } = action.payload;
const activityID = action.payload;

const found = serializedActivities[activityID];

if (found) {
yield put({ type: ACTIVITY_GET_SUCCESS, payload: { activity: JSON.parse(found.data) } });
return;
} else {
yield put(Activity.getSuccess(JSON.parse(found.data)));
} else if (connected) {
// not locally, maybe we can get it from the server if we're online
try {
const networkReturn = yield InvasivesAPI_Call('GET', `/api/activity/${action.payload}`);

if (connected) {
try {
const networkReturn = yield InvasivesAPI_Call('GET', `/api/activity/${action.payload.activityID}`);

if (!(networkReturn.status === 200)) {
yield put({ type: ACTIVITY_GET_FAILURE, payload: { failNetworkObj: networkReturn } });
return;
}

const datav2 = {
...networkReturn.data,
species_positive: networkReturn.data.species_positive || [],
species_negative: networkReturn.data.species_negative || [],
species_treated: networkReturn.data.species_treated || [],
media: networkReturn.data.media || [],
media_delete_keys: networkReturn.data.media_delete_keys || []
};

yield put({ type: ACTIVITY_GET_SUCCESS, payload: { activity: datav2 } });
return;
} catch (e) {
yield put({ type: ACTIVITY_GET_FAILURE });
if (networkReturn.status !== 200) {
yield put(Activity.getFailure(networkReturn));
return;
}
} else {
yield put({ type: ACTIVITY_GET_FAILURE });

const datav2 = {
...networkReturn.data,
species_positive: networkReturn.data.species_positive || [],
species_negative: networkReturn.data.species_negative || [],
species_treated: networkReturn.data.species_treated || [],
media: networkReturn.data.media || [],
media_delete_keys: networkReturn.data.media_delete_keys || []
};
yield put(Activity.getSuccess(datav2));
return;
} catch (e) {
yield put(Activity.getFailure());
return;
}
} else {
yield put(Activity.getFailure());
return;
}
}

Expand Down Expand Up @@ -144,7 +132,7 @@ export function* handle_ACTIVITY_RUN_OFFLINE_SYNC() {
export function* handle_ACTIVITY_RESTORE_OFFLINE() {}

export const OFFLINE_ACTIVITY_SAGA_HANDLERS = [
takeEvery(ACTIVITY_GET_LOCAL_REQUEST, handle_ACTIVITY_GET_LOCAL_REQUEST),
takeEvery(Activity.getLocal, handle_ACTIVITY_GET_LOCAL_REQUEST),
takeEvery(ACTIVITY_SAVE_OFFLINE, handle_ACTIVITY_SAVE_OFFLINE),
takeEvery(Activity.createLocal, handle_ACTIVITY_CREATE_LOCAL),
takeLeading(ACTIVITY_RUN_OFFLINE_SYNC, handle_ACTIVITY_RUN_OFFLINE_SYNC)
Expand Down
Loading

0 comments on commit e83548a

Please sign in to comment.