-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d647285
commit 01cfc0a
Showing
11 changed files
with
84 additions
and
83 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
import Header from "./Header"; | ||
import BookButton from "./BookButton"; | ||
import DateSelector from "./DateInput"; | ||
import TripSelector from "./TripSelector"; | ||
import Header from './Header'; | ||
import BookButton from './BookButton'; | ||
import DateSelector from './DateInput'; | ||
import TripSelector from './TripSelector'; | ||
|
||
export { Header, BookButton, DateSelector, TripSelector }; |
96 changes: 48 additions & 48 deletions
96
examples/7guis-flight-booker-react/src/machines/flightMachine.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,107 +1,107 @@ | ||
import { setup, assign, assertEvent, fromPromise } from "xstate"; | ||
import { createActorContext } from "@xstate/react"; | ||
import { TODAY, TOMORROW } from "../utils"; | ||
import { sleep } from "../utils"; | ||
import { setup, assign, assertEvent, fromPromise } from 'xstate'; | ||
import { createActorContext } from '@xstate/react'; | ||
import { TODAY, TOMORROW } from '../utils'; | ||
import { sleep } from '../utils'; | ||
|
||
export const flightBookerMachine = setup({ | ||
types: { | ||
context: {} as FlightData, | ||
events: {} as | ||
| { type: "BOOK_DEPART" } | ||
| { type: "BOOK_RETURN" } | ||
| { type: "CHANGE_TRIP_TYPE" } | ||
| { type: "CHANGE_DEPART_DATE"; value: string } | ||
| { type: "CHANGE_RETURN_DATE"; value: string }, | ||
| { type: 'BOOK_DEPART' } | ||
| { type: 'BOOK_RETURN' } | ||
| { type: 'CHANGE_TRIP_TYPE' } | ||
| { type: 'CHANGE_DEPART_DATE'; value: string } | ||
| { type: 'CHANGE_RETURN_DATE'; value: string } | ||
}, | ||
actions: { | ||
setDepartDate: assign(({ event }) => { | ||
assertEvent(event, "CHANGE_DEPART_DATE"); | ||
assertEvent(event, 'CHANGE_DEPART_DATE'); | ||
return { departDate: event.value }; | ||
}), | ||
setReturnDate: assign(({ event }) => { | ||
assertEvent(event, "CHANGE_RETURN_DATE"); | ||
assertEvent(event, 'CHANGE_RETURN_DATE'); | ||
return { returnDate: event.value }; | ||
}), | ||
}) | ||
}, | ||
actors: { | ||
Booker: fromPromise(() => { | ||
return sleep(2000); | ||
}), | ||
}) | ||
}, | ||
guards: { | ||
"isValidDepartDate?": ({ context: { departDate } }) => { | ||
'isValidDepartDate?': ({ context: { departDate } }) => { | ||
return departDate >= TODAY; | ||
}, | ||
"isValidReturnDate?": ({ context: { departDate, returnDate } }) => { | ||
'isValidReturnDate?': ({ context: { departDate, returnDate } }) => { | ||
return departDate >= TODAY && returnDate > departDate; | ||
}, | ||
}, | ||
} | ||
} | ||
}).createMachine({ | ||
id: "flightBookerMachine", | ||
id: 'flightBookerMachine', | ||
context: { | ||
departDate: TODAY, | ||
returnDate: TOMORROW, | ||
returnDate: TOMORROW | ||
}, | ||
initial: "scheduling", | ||
initial: 'scheduling', | ||
states: { | ||
scheduling: { | ||
initial: "oneWay", | ||
initial: 'oneWay', | ||
on: { | ||
CHANGE_DEPART_DATE: { | ||
actions: { | ||
type: "setDepartDate", | ||
}, | ||
}, | ||
type: 'setDepartDate' | ||
} | ||
} | ||
}, | ||
states: { | ||
oneWay: { | ||
on: { | ||
CHANGE_TRIP_TYPE: { | ||
target: "roundTrip", | ||
target: 'roundTrip' | ||
}, | ||
BOOK_DEPART: { | ||
target: "#flightBookerMachine.booking", | ||
target: '#flightBookerMachine.booking', | ||
guard: { | ||
type: "isValidDepartDate?", | ||
}, | ||
}, | ||
}, | ||
type: 'isValidDepartDate?' | ||
} | ||
} | ||
} | ||
}, | ||
roundTrip: { | ||
on: { | ||
CHANGE_TRIP_TYPE: { | ||
target: "oneWay", | ||
target: 'oneWay' | ||
}, | ||
CHANGE_RETURN_DATE: { | ||
actions: { | ||
type: "setReturnDate", | ||
}, | ||
type: 'setReturnDate' | ||
} | ||
}, | ||
BOOK_RETURN: { | ||
target: "#flightBookerMachine.booking", | ||
target: '#flightBookerMachine.booking', | ||
guard: { | ||
type: "isValidReturnDate?", | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
type: 'isValidReturnDate?' | ||
} | ||
} | ||
} | ||
} | ||
} | ||
}, | ||
booking: { | ||
invoke: { | ||
src: "Booker", | ||
src: 'Booker', | ||
onDone: { | ||
target: "booked", | ||
target: 'booked' | ||
}, | ||
onError: { | ||
target: "scheduling", | ||
}, | ||
}, | ||
target: 'scheduling' | ||
} | ||
} | ||
}, | ||
booked: { | ||
type: "final", | ||
}, | ||
}, | ||
type: 'final' | ||
} | ||
} | ||
}); | ||
|
||
export default createActorContext(flightBookerMachine); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,18 +1,19 @@ | ||
import React from "react"; | ||
import React from 'react'; | ||
|
||
declare global { | ||
type Input = React.InputHTMLAttributes<HTMLInputElement>; | ||
type Select = React.SelectHTMLAttributes<HTMLSelectElement>; | ||
|
||
type EventType = "BOOK_DEPART" | "BOOK_RETURN"; | ||
type EventType = 'BOOK_DEPART' | 'BOOK_RETURN'; | ||
|
||
type FlightData = { | ||
departDate: string; | ||
returnDate: string; | ||
}; | ||
|
||
type TripSelectorProps = { | ||
isBooking: boolean; | ||
isBooked: boolean; | ||
tripType: "oneWay" | "roundTrip"; | ||
tripType: 'oneWay' | 'roundTrip'; | ||
} & React.InputHTMLAttributes<HTMLSelectElement>; | ||
} |