Skip to content

Commit

Permalink
Added functions to handle line, fromStop and toStop when ever transport
Browse files Browse the repository at this point in the history
mode and line is set.
  • Loading branch information
jonasbrunvoll committed Sep 27, 2024
1 parent 92169c7 commit 79b4d08
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 12 deletions.
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,6 +79,12 @@ export const meansOfTransportFormMachine = setup({
} else {
context.errorMessages[inputName] = [];
}

if (inputName === 'transportMode')
return setTransportModeAndResetLineAndStops(context, value);

if (inputName === 'line') return setLineAndResetStops(context, value);

return {
...context,
[inputName]: value,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Typo } from '@atb/components/typography';
import { FileInput } from '../../components/input/file';
import { ticketControlFormEvents } from '../events';
import { ContextProps } from '../ticket-control-form-machine';
import { useEffect } from 'react';

type FeedbackFormProps = {
state: { context: ContextProps };
Expand All @@ -20,6 +21,10 @@ export const FeedbackForm = ({ state, send }: FeedbackFormProps) => {
const { t } = useTranslation();
const { getLinesByMode, getQuaysByLine } = useLines();

useEffect(() => {
console.log('state.context.line: ', state.context.line);
}, [state]);

return (
<div>
<SectionCard title={t(PageText.Contact.ticketControl.feedback.title)}>
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,17 @@ 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);

if (inputName === 'transportMode')
return setTransportModeAndResetLineAndStops(context, value);

if (inputName === 'line') return setLineAndResetStops(context, value);

context.errorMessages[inputName] = [];
return {
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,12 @@ export const fetchMachine = setup({
onInputChange: assign(({ context, event }) => {
if (event.type === 'ON_INPUT_CHANGE') {
let { inputName, value } = event;

if (inputName === 'transportMode')
return setTransportModeAndResetLineAndStops(context, value);

if (inputName === 'line') return setLineAndResetStops(context, value);

context.errorMessages[inputName] = [];
return {
...context,
Expand Down
25 changes: 25 additions & 0 deletions src/page-modules/contact/utils.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 shouldShowContactPage = (): boolean => {
return process.env.NEXT_PUBLIC_CONTACT_API_URL ? true : false;
};
Expand Down Expand Up @@ -36,3 +39,25 @@ export const getCurrentDateString = (): string =>

export const getCurrentTimeString = (): string =>
`${String(new Date().getHours()).padStart(2, '0')}:${String(new Date().getMinutes()).padStart(2, '0')}`;

export const setTransportModeAndResetLineAndStops = (
context: any,
transporMode: TransportModeType,
) => {
return {
...context,
transportMode: transporMode,
line: undefined,
fromStop: undefined,
toStop: undefined,
};
};

export const setLineAndResetStops = (context: any, line: Line) => {
return {
...context,
line: line,
fromStop: undefined,
toStop: undefined,
};
};

0 comments on commit 79b4d08

Please sign in to comment.