Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convert pik to cik #1109

Merged
merged 11 commits into from
Feb 19, 2024
Merged
51 changes: 51 additions & 0 deletions src/client/app/actions/ciks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { ActionType, Thunk, Dispatch, GetState } from '../types/redux/actions';
import * as t from '../types/redux/ciks'
import { ciksApi } from '../utils/api';

export function requestCiksDetails(): t.RequestCiksDetailsAction {
return { type: ActionType.RequestCiksDetails };
}

export function receiveCiksDetails(data: t.CikData[]): t.ReceiveCiksDetailsAction {
return { type: ActionType.ReceiveCiksDetails, data };
}

export function confirmCiksFetchedOnce(): t.ConfirmCiksFetchedOneAction {
return { type: ActionType.ConfirmCiksFetchedOne };
}

export function fetchCiksData(): Thunk {
return async (dispatch: Dispatch, getState: GetState) => {
// make sure ciks is not being fetched
if (!getState().ciks.isFetching) {
// set isFetching to true
dispatch(requestCiksDetails());
// retrieve ciks data from database
const ciks = await ciksApi.getCiksDetails();
// update the state with the Cik details and set isFetching to false
dispatch(receiveCiksDetails(ciks));
// If this is the first fetch, inform the store that the first fetch has been made
if (!getState().ciks.hasBeenFetchedOne) {
dispatch(confirmCiksFetchedOnce());
}
}
}
}

/**
* Fetch the ciks details from the database if they have not already been fetched once
*/
export function fetchCiksIfNeeded(): Thunk {
return async (dispatch: Dispatch, getState: GetState) => {
// If ciks have not been fetched once, call the fetchCiksData
if (!getState().ciks.hasBeenFetchedOne) {
dispatch(fetchCiksData());
}
// If ciks have already been fetched, return a resolved promise
return Promise.resolve();
}
}
4 changes: 2 additions & 2 deletions src/client/app/components/InitializationComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,14 @@ import { useDispatch, useSelector } from 'react-redux';
import { State } from '../types/redux/state';
import { fetchMetersDetails, fetchMetersDetailsIfNeeded } from '../actions/meters';
import { fetchGroupsDetailsIfNeeded } from '../actions/groups';
import { ConversionArray } from '../types/conversionArray';
import { fetchPreferencesIfNeeded } from '../actions/admin';
import { fetchMapsDetails } from '../actions/map';
import { fetchUnitsDetailsIfNeeded } from '../actions/units';
import { fetchConversionsDetailsIfNeeded } from '../actions/conversions';
import { Dispatch } from 'types/redux/actions';
import { Slide, ToastContainer } from 'react-toastify';
import 'react-toastify/dist/ReactToastify.css';
import { fetchCiksIfNeeded } from '../actions/ciks';

/**
* Initializes OED redux with needed details
Expand All @@ -33,7 +33,7 @@ export default function InitializationComponent() {
dispatch(fetchMapsDetails());
dispatch(fetchUnitsDetailsIfNeeded());
dispatch(fetchConversionsDetailsIfNeeded());
ConversionArray.fetchPik();
dispatch(fetchCiksIfNeeded());
}, []);

// Rerender the route component if the user state changes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ export default function EditConversionModalComponent(props: EditConversionModalC
// If there is a difference between props and state, then a change was made
// Side note, we could probably just set a boolean when any input i
// Edit Conversion Validation: is not needed as no breaking edits can be made
const handleSaveChanges = () => {
const handleSaveChanges = async () => {
// Close the modal first to avoid repeat clicks
props.handleClose();

Expand Down
11 changes: 6 additions & 5 deletions src/client/app/components/groups/CreateGroupModalComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import { UnitData } from '../../types/redux/units';
import {
unitsCompatibleWithMeters, getMeterMenuOptionsForGroup, getGroupMenuOptionsForGroup, metersInChangedGroup
} from '../../utils/determineCompatibleUnits';
import { ConversionArray } from '../../types/conversionArray';
import { GPSPoint, isValidGPSInput } from '../../utils/calibration';
import { notifyUser, getGPSString } from '../../utils/input'
import { tooltipBaseStyle } from '../../styles/modalStyle';
Expand All @@ -50,6 +49,8 @@ export default function CreateGroupModalComponent(props: CreateGroupModalCompone
const groupsState = useSelector((state: State) => state.groups.byGroupID);
// Unit state
const unitsState = useSelector((state: State) => state.units.units);
// Cik state
const ciksState = useSelector((state: State) => state.ciks.ciks);

// Check for admin status
const currentUser = useSelector((state: State) => state.currentUser.profile);
Expand Down Expand Up @@ -247,10 +248,10 @@ export default function CreateGroupModalComponent(props: CreateGroupModalCompone
groupSelectOptions: possibleGroups
});
}
// pik is needed since the compatible units is not correct until pik is available.
// metersState normally does not change but can so include.
// groupState can change if another group is created/edited and this can change ones displayed in menus.
}, [ConversionArray.pikAvailable(), metersState, groupsState, state.defaultGraphicUnit, state.deepMeters]);
// make sure ciksState is available.
}, [metersState, groupsState, state.defaultGraphicUnit, state.deepMeters, ciksState]);

// Update compatible default graphic units set.
useEffect(() => {
Expand Down Expand Up @@ -283,8 +284,8 @@ export default function CreateGroupModalComponent(props: CreateGroupModalCompone
}
// If any of these change then it needs to be updated.
// metersState normally does not change but can so include.
// pik is needed since the compatible units is not correct until pik is available.
}, [ConversionArray.pikAvailable(), metersState, state.deepMeters]);
// make sure ciksState is available.
}, [metersState, state.deepMeters, ciksState]);

const tooltipStyle = {
...tooltipBaseStyle,
Expand Down
11 changes: 6 additions & 5 deletions src/client/app/components/groups/EditGroupModalComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ import {
unitsCompatibleWithMeters, getMeterMenuOptionsForGroup, getGroupMenuOptionsForGroup,
getCompatibilityChangeCase, GroupCase
} from '../../utils/determineCompatibleUnits';
import { ConversionArray } from '../../types/conversionArray';
import { GPSPoint, isValidGPSInput } from '../../utils/calibration';
import { notifyUser, getGPSString, nullToEmptyString } from '../../utils/input';
import { GroupDefinition } from '../../types/redux/groups';
Expand Down Expand Up @@ -60,6 +59,8 @@ export default function EditGroupModalComponent(props: EditGroupModalComponentPr

// Meter state
const metersState = useSelector((state: State) => state.meters.byMeterID);
// Cik state
const ciksState = useSelector((state: State) => state.ciks.ciks);
// Group state used on other pages
const globalGroupsState = useSelector((state: State) => state.groups.byGroupID);
// Make a local copy of the group data so we can update during the edit process.
Expand Down Expand Up @@ -352,10 +353,10 @@ export default function EditGroupModalComponent(props: EditGroupModalComponentPr
groupSelectOptions: possibleGroups
});
}
// pik is needed since the compatible units is not correct until pik is available.
// metersState normally does not change but can so include.
// globalGroupsState can change if another group is created/edited and this can change ones displayed in menus.
}, [ConversionArray.pikAvailable(), metersState, globalGroupsState, groupState.defaultGraphicUnit, groupState.deepMeters]);
// make sure ciksState is available.
}, [metersState, globalGroupsState, groupState.defaultGraphicUnit, groupState.deepMeters, ciksState]);

// Update default graphic units set.
useEffect(() => {
Expand Down Expand Up @@ -387,12 +388,12 @@ export default function EditGroupModalComponent(props: EditGroupModalComponentPr
});
}
// If any of these change then it needs to be updated.
// pik is needed since the compatible units is not correct until pik is available.
// metersState normally does not change but can so include.
// If another group that is included in this group is changed then it must be redone
// but we currently do a refresh so it is covered. It should still be okay if
// the deep meters of this group are properly updated.
}, [ConversionArray.pikAvailable(), metersState, groupState.deepMeters]);
// Make sure ciksState is available.
}, [metersState, groupState.deepMeters, ciksState]);

const tooltipStyle = {
...tooltipBaseStyle,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ import TimeZoneSelect from '../TimeZoneSelect';
import { GPSPoint, isValidGPSInput } from '../../utils/calibration';
import { UnitData } from '../../types/redux/units';
import { unitsCompatibleWithUnit } from '../../utils/determineCompatibleUnits';
import { ConversionArray } from '../../types/conversionArray';
import { AreaUnitType } from '../../utils/getAreaUnitConversion';
import { notifyUser } from '../../utils/input'
import { tooltipBaseStyle } from '../../styles/modalStyle';
Expand Down Expand Up @@ -122,6 +121,7 @@ export default function CreateMeterModalComponent(props: CreateMeterModalCompone

// Dropdowns
const [dropdownsState, setDropdownsState] = useState(dropdownsStateDefaults);
const ciksState = useSelector((state: State) => state.ciks.ciks);

/* Create Meter Validation:
Name cannot be blank
Expand Down Expand Up @@ -332,9 +332,8 @@ export default function CreateMeterModalComponent(props: CreateMeterModalCompone
compatibleUnits: new Set(compatibleUnits),
incompatibleUnits: new Set(incompatibleUnits)
});
// If either unit or the status of pik changes then this needs to be done.
// pik is needed since the compatible units is not correct until pik is available.
}, [state.unitId, state.defaultGraphicUnit, ConversionArray.pikAvailable()]);
// cik is needed since the compatible units is not correct until cik is available.
}, [state.unitId, state.defaultGraphicUnit, ciksState]);

const tooltipStyle = {
...tooltipBaseStyle,
Expand Down
12 changes: 8 additions & 4 deletions src/client/app/components/meters/EditMeterModalComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ import TimeZoneSelect from '../TimeZoneSelect';
import { GPSPoint, isValidGPSInput } from '../../utils/calibration';
import { UnitData } from '../../types/redux/units';
import { unitsCompatibleWithUnit } from '../../utils/determineCompatibleUnits';
import { ConversionArray } from '../../types/conversionArray';
import { AreaUnitType } from '../../utils/getAreaUnitConversion';
import { notifyUser, getGPSString, nullToEmptyString, noUnitTranslated } from '../../utils/input';
import { tooltipBaseStyle } from '../../styles/modalStyle';
Expand Down Expand Up @@ -119,6 +118,9 @@ export default function EditMeterModalComponent(props: EditMeterModalComponentPr
// unit state
const unitState = useSelector((state: State) => state.units.units);

// cik state
const ciksState = useSelector((state: State) => state.ciks.ciks);


/* Edit Meter Validation:
Name cannot be blank
Expand Down Expand Up @@ -176,6 +178,7 @@ export default function EditMeterModalComponent(props: EditMeterModalComponentPr
state.maxDate,
state.maxError
]);

/* End State */

// Reset the state to default values
Expand Down Expand Up @@ -370,9 +373,9 @@ export default function EditMeterModalComponent(props: EditMeterModalComponentPr
compatibleUnits: new Set(compatibleUnits),
incompatibleUnits: new Set(incompatibleUnits)
});
// If either unit or the status of pik changes then this needs to be done.
// pik is needed since the compatible units is not correct until pik is available.
}, [state.unitId, state.defaultGraphicUnit, ConversionArray.pikAvailable()]);
// If either unit changes then this needs to be done.
// make sure ciksState is available.
}, [state.unitId, state.defaultGraphicUnit, ciksState]);

// If you edit and return to this page then want to see the DB result formatted for users
// for the readingFrequency. Since the update on save is to the global state, need to
Expand Down Expand Up @@ -642,6 +645,7 @@ export default function EditMeterModalComponent(props: EditMeterModalComponentPr
</FormGroup></Col>
</Row>
<Row xs='1' lg='2'>

{/* cumulativeResetStart input */}
<Col><FormGroup>
<Label for='cumulativeResetStart'>{translate('meter.cumulativeResetStart')}</Label>
Expand Down
3 changes: 1 addition & 2 deletions src/client/app/components/unit/CreateUnitModalComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@ export default function CreateUnitModalComponent() {
// The client code makes the id for the selected unit and default graphic unit be -99
// so it can tell it is not yet assigned and do the correct logic for that case.
// The units API expects these values to be undefined on call so that the database can assign their values.
id: -99,
unitIndex: -99
id: -99
}

/* State */
Expand Down
3 changes: 1 addition & 2 deletions src/client/app/components/unit/EditUnitModalComponent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,7 @@ export default function EditUnitModalComponent(props: EditUnitModalComponentProp
secInRate: props.unit.secInRate,
suffix: props.unit.suffix,
note: props.unit.note,
id: props.unit.id,
unitIndex: props.unit.unitIndex
id: props.unit.id
}

/* State */
Expand Down
36 changes: 36 additions & 0 deletions src/client/app/reducers/ciks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { CiksAction, CiksState } from '../types/redux/ciks';
import { ActionType } from '../types/redux/actions';

const defaultState: CiksState = {
hasBeenFetchedOne: false,
isFetching: false,
ciks: []
}

export default function ciks(state = defaultState, action: CiksAction) {
switch (action.type) {
case ActionType.ConfirmCiksFetchedOne:
return {
...state,
hasBeenFetchedOne: true
};
case ActionType.RequestCiksDetails:
return {
...state,
isFetching: true
};
case ActionType.ReceiveCiksDetails:
return {
...state,
isFetching: false,
ciks: action.data
}
default:
return state;
}
}

4 changes: 3 additions & 1 deletion src/client/app/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import unsavedWarning from './unsavedWarning';
import units from './units';
import conversions from './conversions';
import options from './options';
import ciks from './ciks';


export default combineReducers({
Expand All @@ -39,5 +40,6 @@ export default combineReducers({
unsavedWarning,
units,
conversions,
options
options,
ciks
});
28 changes: 0 additions & 28 deletions src/client/app/types/conversionArray.ts

This file was deleted.

4 changes: 4 additions & 0 deletions src/client/app/types/redux/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,10 @@ export enum ActionType {
DeleteSubmittedConversion = 'DELETE_SUBMITTED_CONVERSION',
DeleteConversion = 'DELETE_CONVERSION',
ConfirmConversionsFetchedOnce = 'CONFIRM_CONVERSIONS_FETCHED_ONCE',

RequestCiksDetails = 'REQUEST_CIKS_DETAILS',
ReceiveCiksDetails = 'RECEIVE_CIKS_DETAILS',
ConfirmCiksFetchedOne = 'CONFIRM_CIKS_FETCHED_ONE',
}

/**
Expand Down
35 changes: 35 additions & 0 deletions src/client/app/types/redux/ciks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

import { ActionType } from './actions';

export interface RequestCiksDetailsAction {
type: ActionType.RequestCiksDetails;
}

export interface ReceiveCiksDetailsAction {
type: ActionType.ReceiveCiksDetails;
data: CikData[];
}

export interface ConfirmCiksFetchedOneAction {
type: ActionType.ConfirmCiksFetchedOne;
}

export type CiksAction = RequestCiksDetailsAction
| ReceiveCiksDetailsAction
| ConfirmCiksFetchedOneAction;

export interface CikData {
meterUnitId: number;
nonMeterUnitId: number;
slope: number;
intercept: number;
}

export interface CiksState {
hasBeenFetchedOne: boolean;
isFetching: boolean;
ciks: CikData[];
}
Loading
Loading