Skip to content

Commit

Permalink
front: add switch for paced train
Browse files Browse the repository at this point in the history
  - add a switch in user setting
  - update userSlice with pacedTrain property

Co-authored-by: Math_R_ <[email protected]>
Signed-off-by: Uriel-Sautron <[email protected]>
  • Loading branch information
Uriel-Sautron and Math-R committed Feb 7, 2025
1 parent a69c32d commit 14fa21b
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 5 deletions.
3 changes: 3 additions & 0 deletions front/public/locales/en/operationalStudies/scenario.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
"modifyScenario": "Modify scenario",
"moreDetails": "more details",
"noElectricalProfileSet": "No electrical profile",
"pacedTrain": {
"pacedTrain": "Paced train"
},
"scenarioCancel": "Cancel",
"scenarioCreateButton": "Create a scenario",
"scenarioCreationTitle": "Create a scenario",
Expand Down
3 changes: 3 additions & 0 deletions front/public/locales/fr/operationalStudies/scenario.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@
"modifyScenario": "Modifier le scénario",
"moreDetails": "plus de détails",
"noElectricalProfileSet": "Pas de profil électrique",
"pacedTrain": {
"pacedTrain": "Mission"
},
"scenarioCancel": "Annuler",
"scenarioCreateButton": "Créer le scénario",
"scenarioCreationTitle": "Créer un scénario",
Expand Down
29 changes: 27 additions & 2 deletions front/src/common/UserSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,25 @@ import { useSelector } from 'react-redux';
import InputSNCF from 'common/BootstrapSNCF/InputSNCF';
import { ModalBodySNCF, ModalHeaderSNCF } from 'common/BootstrapSNCF/ModalSNCF';
import { updateUserPreferences } from 'reducers/user';
import { getUserPreferences } from 'reducers/user/userSelectors';
import { getIsSuperUser, getUserPreferences } from 'reducers/user/userSelectors';
import { useAppDispatch } from 'store';
import { useDebounce } from 'utils/helpers';

import SwitchSNCF from './BootstrapSNCF/SwitchSNCF/SwitchSNCF';

const UserSettings = () => {
const userPreferences = useSelector(getUserPreferences);
const [safeWordText, setSafeWordText] = useState(userPreferences.safeWord);
const dispatch = useAppDispatch();
const isSuperUser = useSelector(getIsSuperUser);

const debouncedSafeWord = useDebounce(safeWordText, 500);

useEffect(() => {
dispatch(updateUserPreferences({ ...userPreferences, safeWord: debouncedSafeWord }));
}, [debouncedSafeWord]);

const { t } = useTranslation(['home/navbar']);
const { t } = useTranslation(['home/navbar', 'operationalStudies/scenario']);
return (
<>
<ModalHeaderSNCF withCloseButton>
Expand Down Expand Up @@ -55,6 +58,28 @@ const UserSettings = () => {
<small id="safeWordHelpBlock" className="form-text text-muted">
{t('safeWordHelp')}
</small>
{
// TODO PACEDTRAIN: Remove switch after development pacedTrain feature
isSuperUser && (
<div className="d-flex align-items-center mt-2">
<SwitchSNCF
id="paced-train-switch"
type="switch"
name={t('operationalStudies/scenario:pacedTrain.pacedTrain')}
checked={userPreferences.showPacedTrains}
onChange={() =>
dispatch(
updateUserPreferences({
...userPreferences,
showPacedTrains: !userPreferences.showPacedTrains,
})
)
}
/>
<div className="ml-2">{t('operationalStudies/scenario:pacedTrain.pacedTrain')}</div>
</div>
)
}
</ModalBodySNCF>
</>
);
Expand Down
12 changes: 9 additions & 3 deletions front/src/reducers/user/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export interface UserState {
isLogged: boolean;
loginError?: ApiError;
username: string;
userPreferences: { safeWord: string };
// TODO PACEDTRAIN: Remove pacedTrain after development pacedTrain feature
userPreferences: { safeWord: string; showPacedTrains?: boolean };
userRoles: BuiltinRole[];
account: Record<string, string>;
}
Expand All @@ -16,7 +17,8 @@ export const userInitialState: UserState = {
isLogged: false,
loginError: undefined,
username: '',
userPreferences: { safeWord: '' },
// TODO PACEDTRAIN: Remove pacedTrain after development pacedTrain feature
userPreferences: { safeWord: '', showPacedTrains: false },
userRoles: [],
account: {},
};
Expand Down Expand Up @@ -45,7 +47,11 @@ export const userSlice = createSlice({
setUserRoles(state, action: PayloadAction<BuiltinRole[] | undefined>) {
state.userRoles = action.payload || [];
},
updateUserPreferences(state, action: PayloadAction<{ safeWord: string }>) {
updateUserPreferences(
state,
// TODO PACEDTRAIN: Remove pacedTrain after development pacedTrain feature
action: PayloadAction<{ safeWord: string; showPacedTrains?: boolean }>
) {
state.userPreferences = action.payload;
},
},
Expand Down
3 changes: 3 additions & 0 deletions front/src/reducers/user/userSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ export const getUsername = makeUserSelector('username');
export const getUserRoles = makeUserSelector('userRoles');
export const getIsSuperUser = (state: RootState) => getUserRoles(state).includes('Superuser');

// TODO PACEDTRAIN: Remove pacedTrain after development pacedTrain feature
export const getShowPacedTrains = makeUserPreferencesSelector('showPacedTrains');

const makeUserHasAllRequiredRolesSelector =
(requiredRoles: BuiltinRole[]) => (state: RootState) => {
const userRoles = getUserRoles(state);
Expand Down

0 comments on commit 14fa21b

Please sign in to comment.