Skip to content

Commit

Permalink
Formatting example
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkpiano committed May 19, 2024
1 parent d647285 commit 01cfc0a
Show file tree
Hide file tree
Showing 11 changed files with 84 additions and 83 deletions.
2 changes: 1 addition & 1 deletion examples/7guis-flight-booker-react/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ The 7GUIs Fligt Booker App built with:
- Typescript
- Vite

Visit the [7GUIs project](https://eugenkiss.github.io/7guis/tasks#flight/ "Flight Booker") for more info.
Visit the [7GUIs project](https://eugenkiss.github.io/7guis/tasks#flight/ 'Flight Booker') for more info.

## Screenshots

Expand Down
26 changes: 13 additions & 13 deletions examples/7guis-flight-booker-react/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import FlightContext from "./machines/flightMachine";
import { BookButton, Header } from "./components";
import { DateSelector, TripSelector } from "./components";
import { TODAY } from "./utils";
import FlightContext from './machines/flightMachine';
import { BookButton, Header } from './components';
import { DateSelector, TripSelector } from './components';
import { TODAY } from './utils';

export default function App() {
const { send } = FlightContext.useActorRef();
const state = FlightContext.useSelector((state) => state);
const { departDate, returnDate } = state.context;
const isRoundTrip = state.matches({ scheduling: "roundTrip" });
const isBooking = state.matches("booking");
const isBooked = state.matches("booked");
const isRoundTrip = state.matches({ scheduling: 'roundTrip' });
const isBooking = state.matches('booking');
const isBooked = state.matches('booked');

const isValidDepartDate = departDate >= TODAY;
const isValidReturnDate = returnDate >= departDate;
Expand All @@ -21,7 +21,7 @@ export default function App() {
id="Trip Type"
isBooking={isBooking}
isBooked={isBooked}
tripType={isRoundTrip ? "roundTrip" : "oneWay"}
tripType={isRoundTrip ? 'roundTrip' : 'oneWay'}
/>
<DateSelector
id="Depart Date"
Expand All @@ -30,8 +30,8 @@ export default function App() {
disabled={isBooking || isBooked}
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
send({
type: "CHANGE_DEPART_DATE",
value: e.currentTarget.value,
type: 'CHANGE_DEPART_DATE',
value: e.currentTarget.value
})
}
/>
Expand All @@ -42,13 +42,13 @@ export default function App() {
disabled={!isRoundTrip}
onChange={(e: React.ChangeEvent<HTMLInputElement>) =>
send({
type: "CHANGE_RETURN_DATE",
value: e.currentTarget.value,
type: 'CHANGE_RETURN_DATE',
value: e.currentTarget.value
})
}
/>
<BookButton
eventType={isRoundTrip ? "BOOK_RETURN" : "BOOK_DEPART"}
eventType={isRoundTrip ? 'BOOK_RETURN' : 'BOOK_DEPART'}
isBooking={isBooking}
isBooked={isBooked}
/>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import FlightContext from "../machines/flightMachine";
import FlightContext from '../machines/flightMachine';

type Props = {
isBooking: boolean;
Expand Down Expand Up @@ -32,7 +32,7 @@ export default function BookButton({ eventType, isBooking, isBooked }: Props) {
{isBooking ? <p>Booking...</p> : successMessage}
</dialog>
<button onClick={bookFlight} disabled={!canBook}>
{isBooking ? "Booking" : isBooked ? "Booked!" : "Book"}
{isBooking ? 'Booking' : isBooked ? 'Booked!' : 'Book'}
</button>
</>
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ function DateInput({ isValidDate, ...props }: Props) {
return (
<label>
<span className="visually-hidden">{props.id}</span>
<input type="date" {...props} className={isValidDate ? "" : "error"} />
<input type="date" {...props} className={isValidDate ? '' : 'error'} />
</label>
);
}
Expand Down
8 changes: 4 additions & 4 deletions examples/7guis-flight-booker-react/src/components/index.ts
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 examples/7guis-flight-booker-react/src/machines/flightMachine.ts
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);
14 changes: 7 additions & 7 deletions examples/7guis-flight-booker-react/src/main.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import "./styles/reset.css";
import "./styles/styles.css";
import './styles/reset.css';
import './styles/styles.css';

import React from "react";
import ReactDOM from "react-dom/client";
import FlightContext from "./machines/flightMachine.ts";
import App from "./App.tsx";
import React from 'react';
import ReactDOM from 'react-dom/client';
import FlightContext from './machines/flightMachine.ts';
import App from './App.tsx';

ReactDOM.createRoot(document.getElementById("root")!).render(
ReactDOM.createRoot(document.getElementById('root')!).render(
<React.StrictMode>
<FlightContext.Provider>
<App />
Expand Down
2 changes: 1 addition & 1 deletion examples/7guis-flight-booker-react/src/styles/reset.css
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ blockquote:before,
blockquote:after,
q:before,
q:after {
content: "";
content: '';
content: none;
}
table {
Expand Down
2 changes: 1 addition & 1 deletion examples/7guis-flight-booker-react/src/styles/styles.css
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ dialog {
}
}

input[type="date"] {
input[type='date'] {
cursor: pointer;
box-shadow: var(--inner-shadow);
color: var(--black);
Expand Down
4 changes: 2 additions & 2 deletions examples/7guis-flight-booker-react/src/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
// CONSTANTS
export const TODAY = new Date().toISOString().split("T")[0];
export const TODAY = new Date().toISOString().split('T')[0];

export const TOMORROW = new Date(Date.now() + 86400000)
.toISOString()
.split("T")[0];
.split('T')[0];

// Emulate async operation
export function sleep(ms: number) {
Expand Down
7 changes: 4 additions & 3 deletions examples/7guis-flight-booker-react/types.d.ts
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>;
}

0 comments on commit 01cfc0a

Please sign in to comment.