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

fix: handling reseting transport mode and line. #371

Merged
merged 9 commits into from
Sep 30, 2024
13 changes: 11 additions & 2 deletions src/page-modules/contact/commoneEvents.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
import { TransportModeType } from '@atb-as/config-specs';
import { Line } from '.';

export const commonEvents = {} as
| { type: 'VALIDATE' }
| {
Expand All @@ -12,14 +15,20 @@ export const commonEvents = {} as
| 'firstName'
| 'feedback'
| 'IBAN'
| 'line'
| 'lastName'
| 'phoneNumber'
| 'postalCode'
| 'SWIFT'
| 'toStop'
| 'transportMode'
| 'fromStop'
| 'plannedDepartureTime';
value: any;
}
| {
type: 'ON_TRANSPORTMODE_CHANGE';
value: TransportModeType;
}
| {
type: 'ON_LINE_CHANGE';
value: Line;
};
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,7 @@ export const DelayForm = ({ state, send }: DelayFormProps) => {
value={state.context.transportMode}
onChange={(value) =>
send({
type: 'ON_INPUT_CHANGE',
inputName: 'transportMode',
type: 'ON_TRANSPORTMODE_CHANGE',
value: value as TransportModeType,
})
}
Expand All @@ -68,8 +67,7 @@ export const DelayForm = ({ state, send }: DelayFormProps) => {
onChange={(value: Line | undefined) => {
if (!value) return;
send({
type: 'ON_INPUT_CHANGE',
inputName: 'line',
type: 'ON_LINE_CHANGE',
value: value,
});
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,7 @@ export const DriverForm = ({ state, send }: DriverFormProps) => {
value={state.context.transportMode}
onChange={(value) =>
send({
type: 'ON_INPUT_CHANGE',
inputName: 'transportMode',
type: 'ON_TRANSPORTMODE_CHANGE',
value: value as TransportModeType,
})
}
Expand All @@ -91,8 +90,7 @@ export const DriverForm = ({ state, send }: DriverFormProps) => {
onChange={(value: Line | undefined) => {
if (!value) return;
send({
type: 'ON_INPUT_CHANGE',
inputName: 'line',
type: 'ON_LINE_CHANGE',
value: value,
});
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,7 @@ export const InjuryForm = ({ state, send }: InjuryFormProps) => {
value={state.context.transportMode}
onChange={(value) =>
send({
type: 'ON_INPUT_CHANGE',
inputName: 'transportMode',
type: 'ON_TRANSPORTMODE_CHANGE',
value: value as TransportModeType,
})
}
Expand All @@ -90,8 +89,7 @@ export const InjuryForm = ({ state, send }: InjuryFormProps) => {
onChange={(value: Line | undefined) => {
if (!value) return;
send({
type: 'ON_INPUT_CHANGE',
inputName: 'line',
type: 'ON_LINE_CHANGE',
value: value,
});
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,7 @@ export const ServiceOfferingForm = ({
value={state.context.transportMode}
onChange={(value) =>
send({
type: 'ON_INPUT_CHANGE',
inputName: 'transportMode',
type: 'ON_TRANSPORTMODE_CHANGE',
value: value as TransportModeType,
})
}
Expand All @@ -95,8 +94,7 @@ export const ServiceOfferingForm = ({
onChange={(value: Line | undefined) => {
if (!value) return;
send({
type: 'ON_INPUT_CHANGE',
inputName: 'line',
type: 'ON_LINE_CHANGE',
value: value,
});
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ export const StopForm = ({ state, send }: StopFormProps) => {
value={state.context.transportMode}
onChange={(value) =>
send({
type: 'ON_INPUT_CHANGE',
inputName: 'transportMode',
type: 'ON_TRANSPORTMODE_CHANGE',
value: value as TransportModeType,
})
}
Expand All @@ -64,8 +63,7 @@ export const StopForm = ({ state, send }: StopFormProps) => {
onChange={(value: Line | undefined) => {
if (!value) return;
send({
type: 'ON_INPUT_CHANGE',
inputName: 'line',
type: 'ON_LINE_CHANGE',
value: value,
});
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,7 @@ export const TransportationForm = ({
value={state.context.transportMode}
onChange={(value) =>
send({
type: 'ON_INPUT_CHANGE',
inputName: 'transportMode',
type: 'ON_TRANSPORTMODE_CHANGE',
value: value as TransportModeType,
})
}
Expand All @@ -72,8 +71,7 @@ export const TransportationForm = ({
onChange={(value: Line | undefined) => {
if (!value) return;
send({
type: 'ON_INPUT_CHANGE',
inputName: 'line',
type: 'ON_LINE_CHANGE',
value: value,
});
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ import {
convertFilesToBase64,
getCurrentDateString,
getCurrentTimeString,
setLineAndResetStops,
setTransportModeAndResetLineAndStops,
} from '../utils';
import { Area, meansOfTransportFormEvents } from './events';

Expand Down Expand Up @@ -77,13 +79,27 @@ export const meansOfTransportFormMachine = setup({
} else {
context.errorMessages[inputName] = [];
}

return {
...context,
[inputName]: value,
};
}
return context;
}),

onTransportModeChange: assign(({ context, event }) => {
if (event.type === 'ON_TRANSPORTMODE_CHANGE')
return setTransportModeAndResetLineAndStops(context, event.value);
return context;
}),

onLineChange: assign(({ context, event }) => {
if (event.type === 'ON_LINE_CHANGE')
return setLineAndResetStops(context, event.value);
return context;
}),

navigateToErrorPage: () => {
window.location.href = '/contact/error';
},
Expand Down Expand Up @@ -167,6 +183,12 @@ export const meansOfTransportFormMachine = setup({
ON_INPUT_CHANGE: {
actions: 'onInputChange',
},
ON_TRANSPORTMODE_CHANGE: {
actions: 'onTransportModeChange',
},
ON_LINE_CHANGE: {
actions: 'onLineChange',
},

VALIDATE: {
guard: 'validateInputs',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ export const FeedbackForm = ({ state, send }: FeedbackFormProps) => {
value={state.context.transportMode}
onChange={(value) =>
send({
type: 'ON_INPUT_CHANGE',
inputName: 'transportMode',
type: 'ON_TRANSPORTMODE_CHANGE',
value: value as TransportModeType,
})
}
Expand All @@ -56,8 +55,7 @@ export const FeedbackForm = ({ state, send }: FeedbackFormProps) => {
onChange={(value: Line | undefined) => {
if (!value) return;
send({
type: 'ON_INPUT_CHANGE',
inputName: 'line',
type: 'ON_LINE_CHANGE',
value: value,
});
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
convertFilesToBase64,
getCurrentDateString,
getCurrentTimeString,
setLineAndResetStops,
setTransportModeAndResetLineAndStops,
} from '../utils';
import { TransportModeType } from '@atb-as/config-specs';
import { Line } from '../server/journey-planner/validators';
Expand Down Expand Up @@ -88,7 +90,7 @@ export type ContextProps = {
};

// Function to reset the agreement fields and error messages
const resetAgreementFieldsAndErrors = (
const setFormTypeAndInitialContext = (
context: ContextProps,
formType: FormType,
) => {
Expand All @@ -103,6 +105,14 @@ const resetAgreementFieldsAndErrors = (
};
};

const disagreeAgreements = (context: ContextProps) => {
return {
...context,
agreesFirstAgreement: false,
agreesSecondAgreement: false,
};
};

const setInputToValidate = (context: ContextProps) => {
// Destructure the needed fields from context
const {
Expand Down Expand Up @@ -202,18 +212,12 @@ export const ticketControlFormMachine = setup({
if (event.type === 'ON_INPUT_CHANGE') {
const { inputName, value } = event;

if (inputName === 'formType') {
return resetAgreementFieldsAndErrors(context, value as FormType);
}
if (inputName === 'formType')
return setFormTypeAndInitialContext(context, value as FormType);

// Set both agreements to false if agreesFirstAgreement is set to false.
if (inputName === 'agreesFirstAgreement' && !value) {
return {
...context,
['agreesFirstAgreement']: false,
['agreesSecondAgreement']: false,
};
}
if (inputName === 'agreesFirstAgreement' && !value)
return disagreeAgreements(context);

context.errorMessages[inputName] = [];
return {
Expand All @@ -223,6 +227,17 @@ export const ticketControlFormMachine = setup({
}
return context;
}),
onTransportModeChange: assign(({ context, event }) => {
if (event.type === 'ON_TRANSPORTMODE_CHANGE')
return setTransportModeAndResetLineAndStops(context, event.value);
return context;
}),

onLineChange: assign(({ context, event }) => {
if (event.type === 'ON_LINE_CHANGE')
return setLineAndResetStops(context, event.value);
return context;
}),
},
actors: {
submit: fromPromise(
Expand Down Expand Up @@ -317,6 +332,12 @@ export const ticketControlFormMachine = setup({
ON_INPUT_CHANGE: {
actions: 'onInputChange',
},
ON_TRANSPORTMODE_CHANGE: {
actions: 'onTransportModeChange',
},
ON_LINE_CHANGE: {
actions: 'onLineChange',
},

VALIDATE: {
guard: 'validateInputs',
Expand Down
6 changes: 2 additions & 4 deletions src/page-modules/contact/travel-guarantee/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ export const RefundForm = () => {
value={state.context.transportMode}
onChange={(value) =>
send({
type: 'ON_INPUT_CHANGE',
inputName: 'transportMode',
type: 'ON_TRANSPORTMODE_CHANGE',
value: value as TransportModeType,
})
}
Expand All @@ -71,8 +70,7 @@ export const RefundForm = () => {
onChange={(value: Line | undefined) => {
if (!value) return;
send({
type: 'ON_INPUT_CHANGE',
inputName: 'line',
type: 'ON_LINE_CHANGE',
value: value,
});
}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,12 @@ import {
InputErrorMessages,
travelGuaranteeInputValidator,
} from '../validation';
import { getCurrentDateString, getCurrentTimeString } from '../utils';
import {
getCurrentDateString,
getCurrentTimeString,
setLineAndResetStops,
setTransportModeAndResetLineAndStops,
} from '../utils';
import { TravelGuaranteeFormEvents } from './events';

type APIParams = {
Expand Down Expand Up @@ -75,6 +80,7 @@ export const fetchMachine = setup({
onInputChange: assign(({ context, event }) => {
if (event.type === 'ON_INPUT_CHANGE') {
let { inputName, value } = event;

context.errorMessages[inputName] = [];
return {
...context,
Expand All @@ -84,6 +90,18 @@ export const fetchMachine = setup({
return context;
}),

onTransportModeChange: assign(({ context, event }) => {
if (event.type === 'ON_TRANSPORTMODE_CHANGE')
return setTransportModeAndResetLineAndStops(context, event.value);
return context;
}),

onLineChange: assign(({ context, event }) => {
if (event.type === 'ON_LINE_CHANGE')
return setLineAndResetStops(context, event.value);
return context;
}),

cleanErrorMessages: assign({
errorMessages: () => ({}),
}),
Expand Down Expand Up @@ -187,10 +205,15 @@ export const fetchMachine = setup({
OTHER: {
target: 'editing.other',
},

ON_INPUT_CHANGE: {
actions: 'onInputChange',
},
ON_TRANSPORTMODE_CHANGE: {
actions: 'onTransportModeChange',
},
ON_LINE_CHANGE: {
actions: 'onLineChange',
},
VALIDATE: {
guard: 'validateInputs',
target: 'editing.readyForSubmit',
Expand Down
Loading
Loading