Skip to content

Commit

Permalink
Modify the mode functions to encompass all types of labels (purpose, …
Browse files Browse the repository at this point in the history
…replaced_mode ...) functions to em
  • Loading branch information
jiji14 committed Jan 5, 2024
1 parent 189f79d commit 643ae90
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 42 deletions.
5 changes: 4 additions & 1 deletion www/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@
"services-save": "Save",
"default-mode": "Default Mode",
"custom-mode": "Custom Mode",
"default-purpose": "Default Purpose"
"default-purpose": "Default Purpose",
"custom-purpose": "Custom Purpose",
"default-replace-mode": "Default Replace Mode",
"custom-replace-mode": "Custom Replace Mode"
},

"control": {
Expand Down
12 changes: 7 additions & 5 deletions www/js/diary/LabelTab.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,21 @@ import { fillLocationNamesOfTrip, resetNominatimLimiter } from './addressNamesHe
import { getLabelOptions } from '../survey/multilabel/confirmHelper';
import { displayError, displayErrorMsg, logDebug, logWarn } from '../plugin/logger';
import { useTheme } from 'react-native-paper';
import { getPipelineRangeTs, getUserCustomModes } from '../services/commHelper';
import { getPipelineRangeTs, getUserCustomLabels } from '../services/commHelper';
import { mapInputsToTimelineEntries } from '../survey/inputMatcher';
import { configuredFilters as multilabelConfiguredFilters } from '../survey/multilabel/infinite_scroll_filters';
import { configuredFilters as enketoConfiguredFilters } from '../survey/enketo/infinite_scroll_filters';
import LabelTabContext, {
TimelineLabelMap,
TimelineMap,
TimelineNotesMap,
CustomLabelKey,
} from './LabelTabContext';

let showPlaces;
const ONE_DAY = 24 * 60 * 60; // seconds
const ONE_WEEK = ONE_DAY * 7; // seconds
const CUSTOM_LABEL_KEYS = ['mode', 'purpose', 'replaced_mode'];

const LabelTab = () => {
const appConfig = useAppConfig();
Expand All @@ -56,7 +58,7 @@ const LabelTab = () => {
const [displayedEntries, setDisplayedEntries] = useState(null);
const [refreshTime, setRefreshTime] = useState(null);
const [isLoading, setIsLoading] = useState<string | false>('replace');
const [customModes, setCustomModes] = useState<string[]>([]);
const [customLabel, setCustomLabel] = useState<CustomLabelKey>({});

const Timeline = getAngularService('Timeline');

Expand All @@ -66,7 +68,7 @@ const LabelTab = () => {
if (!appConfig) return;
showPlaces = appConfig.survey_info?.buttons?.['place-notes'];
getLabelOptions(appConfig).then((labelOptions) => setLabelOptions(labelOptions));
getUserCustomModes().then((res) => setCustomModes(res['modes'] as string[]));
getUserCustomLabels(CUSTOM_LABEL_KEYS).then((res) => setCustomLabel(res));
// we will show filters if 'additions' are not configured
// https://github.com/e-mission/e-mission-docs/issues/894
if (appConfig.survey_info?.buttons == undefined) {
Expand Down Expand Up @@ -323,8 +325,8 @@ const LabelTab = () => {
loadSpecificWeek,
refresh,
repopulateTimelineEntry,
customModes,
setCustomModes,
customLabel,
setCustomLabel,
};

const Tab = createStackNavigator();
Expand Down
7 changes: 5 additions & 2 deletions www/js/diary/LabelTabContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ export type TimelineLabelMap = {
export type TimelineNotesMap = {
[k: string]: UserInputEntry[];
};
export type CustomLabelKey = {
[k: string]: string[];
};

type ContextProps = {
labelOptions: any;
Expand All @@ -34,8 +37,8 @@ type ContextProps = {
loadSpecificWeek: any; // TODO
refresh: any; // TODO
repopulateTimelineEntry: any; // TODO
customModes: string[];
setCustomModes: Dispatch<SetStateAction<string[]>>;
customLabel: CustomLabelKey;
setCustomLabel: Dispatch<SetStateAction<CustomLabelKey>>;
};

export default createContext<ContextProps>(null);
31 changes: 19 additions & 12 deletions www/js/services/commHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -230,31 +230,38 @@ export function putOne(key, data) {
});
}

export function getUserCustomModes() {
export function getUserCustomLabels(keys) {
return new Promise((rs, rj) => {
window['cordova'].plugins.BEMServerComm.getUserPersonalData('/mode/get', rs, rj);
window['cordova'].plugins.BEMServerComm.postUserPersonalData(
'/customlabel/get',
'keys',
keys,
rs,
rj,
);
}).catch((error) => {
error = 'While getting modes, ' + error;
error = 'While getting labels, ' + error;
throw error;
});
}

export function updateUserCustomMode(oldMode, newMode, isNewModeMustAdded) {
const updatedMode = {
old_mode: oldMode,
new_mode: newMode,
is_new_mode_must_added: isNewModeMustAdded,
export function updateUserCustomLabel(key, oldLabel, newLabel, isNewLabelMustAdded) {
const updatedLabel = {
key: key,
old_label: oldLabel,
new_label: newLabel,
is_new_label_must_added: isNewLabelMustAdded,
};
return new Promise((rs, rj) => {
window['cordova'].plugins.BEMServerComm.postUserPersonalData(
'/mode/update',
'updated_mode',
updatedMode,
'/customlabel/update',
'updated_label',
updatedLabel,
rs,
rj,
);
}).catch((error) => {
error = 'While putting one mode, ' + error;
error = 'While putting one label, ' + error;
throw error;
});
}
49 changes: 27 additions & 22 deletions www/js/survey/multilabel/MultiLabelButtonGroup.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ import {
verifiabilityForTrip,
} from './confirmHelper';
import useAppConfig from '../../useAppConfig';
import { updateUserCustomMode } from '../../services/commHelper';
import { updateUserCustomLabel } from '../../services/commHelper';

const MultilabelButtonGroup = ({ trip, buttonsInline = false }) => {
const { colors } = useTheme();
const { t } = useTranslation();
const appConfig = useAppConfig();
const { repopulateTimelineEntry, labelOptions, timelineLabelMap, customModes, setCustomModes } =
const { repopulateTimelineEntry, labelOptions, timelineLabelMap, customLabel, setCustomLabel } =
useContext(LabelTabContext);
const { height: windowHeight } = useWindowDimensions();
// modal visible for which input type? (mode or purpose or replaced_mode, null if not visible)
Expand Down Expand Up @@ -80,21 +80,22 @@ const MultilabelButtonGroup = ({ trip, buttonsInline = false }) => {
(i.e. lowercase, and with underscores instead of spaces) */
newLabel = readableLabelToKey(newLabel);
}
// If a user saves a new customized mode or makes changes to/from customized modes, the modes need to be updated.
if (isOther || customModes.indexOf(initialLabel) > -1 || customModes.indexOf(newLabel) > -1) {
if (inputType === 'MODE') {
updateUserCustomMode(initialLabel ?? '', newLabel, isOther)
.then((res) => {
setCustomModes(res['modes'] as string[]);
logDebug('Successfuly stored custom mode ' + JSON.stringify(res));
})
.catch((e) => {
displayErrorMsg(e, 'Create Mode Error');
});
}
if (inputType === 'PURPOSE') {
// TODO : update custom purpose
}
// If a user saves a new customized label or makes changes to/from customized labels, the labels need to be updated.
const key = inputType.toLowerCase();
if (
isOther ||
customLabel[key].indexOf(initialLabel) > -1 ||
customLabel[key].indexOf(newLabel) > -1
) {
updateUserCustomLabel(key, initialLabel ?? '', newLabel, isOther)
.then((res) => {
customLabel[key] = res['label'];
setCustomLabel(customLabel);
logDebug('Successfuly stored custom label ' + JSON.stringify(res));
})
.catch((e) => {
displayErrorMsg(e, 'Create Label Error');
});
}
const inputDataToStore = {
start_ts: trip.start_ts,
Expand Down Expand Up @@ -171,6 +172,7 @@ const MultilabelButtonGroup = ({ trip, buttonsInline = false }) => {
<Text style={{ fontSize: 12, color: colors.onSurface, paddingVertical: 4 }}>
{modalVisibleFor === 'MODE' && t('trip-confirm.default-mode')}
{modalVisibleFor === 'PURPOSE' && t('trip-confirm.default-purpose')}
{modalVisibleFor === 'REPLACED_MODE' && t('trip-confirm.default-replace-mode')}
</Text>
<RadioButton.Group onValueChange={(val) => onChooseLabel(val)} value={initialLabel}>
{labelOptions?.[modalVisibleFor]?.map((o, i) => {
Expand All @@ -182,17 +184,20 @@ const MultilabelButtonGroup = ({ trip, buttonsInline = false }) => {
style={{ paddingVertical: 2 }}
/>
);
/* if this is the 'other' option and there are some custom modes,
show the custom modes section before 'other' */
if (modalVisibleFor == 'MODE' && o.value == 'other' && customModes?.length) {
/* if this is the 'other' option and there are some custom labels,
show the custom labels section before 'other' */
if (o.value == 'other' && customLabel[modalVisibleFor.toLowerCase()]?.length) {
return (
<>
<Divider style={{ marginVertical: 10 }} />
<Text
style={{ fontSize: 12, color: colors.onSurface, paddingVertical: 4 }}>
{t('trip-confirm.custom-mode')}
{modalVisibleFor === 'MODE' && t('trip-confirm.custom-mode')}
{modalVisibleFor === 'PURPOSE' && t('trip-confirm.custom-purpose')}
{modalVisibleFor === 'REPLACED_MODE' &&
t('trip-confirm.custom-replace-mode')}
</Text>
{customModes.map((key, i) => (
{customLabel[modalVisibleFor.toLowerCase()].map((key, i) => (
<RadioButton.Item
key={i}
label={labelKeyToReadable(key)}
Expand Down

0 comments on commit 643ae90

Please sign in to comment.