This file contains hidden or 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
| import { RequestOptions, Transport } from "./HttpTransportProvider"; | |
| import { | |
| createMutationRequest, | |
| createQueryRequest, | |
| } from "./HttpTransportService"; | |
| export class ApiClient { | |
| readonly baseUrl: string; | |
| readonly transport: Transport; |
This file contains hidden or 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
| import React, { useEffect, useState } from "react"; | |
| import { DateTimeFormat } from "./DateTimeProvider"; | |
| import { FormattedDateTime } from "./FormattedDateTime"; | |
| import { ServiceProvider } from "./ServiceProvider"; | |
| import { Services, services } from "./Services"; | |
| import { useTimer } from "./Timer"; | |
| export const now = () => new Date(); | |
| export function App() { |
This file contains hidden or 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
| import { createContext, ReactNode, useContext, useEffect, useState } from "react"; | |
| export enum DateTimeFormat { | |
| TIME, | |
| ISO, | |
| RELATIVE | |
| } | |
| export interface DateTimeFormatterProps { | |
| format: DateTimeFormat; |
This file contains hidden or 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
| import { | |
| DateTimeFormat, | |
| DateTimeFormatterProps, | |
| useDateTimeFormatter, | |
| } from "./utils/datetime"; | |
| export const FormattedDateTime = ({ format, date }: DateTimeFormatterProps) => { | |
| const { formatDateTime } = useDateTimeFormatter(); | |
| return ( |
This file contains hidden or 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
| function MessageView () { | |
| const { | |
| context: { | |
| message | |
| } | |
| } = useMachineState() | |
| return ( | |
| <> | |
| <p>Your message to {message.recipient} has been sent</p> |
This file contains hidden or 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
| function ComposerView () { | |
| const saveDraft = useMachineTransition('save_draft') | |
| const send = useMachineTransition('send') | |
| const { | |
| context: { | |
| message | |
| } | |
| } = useMachineState() |
This file contains hidden or 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
| function App () { | |
| return ( | |
| <StateMachine | |
| states={states} | |
| initialContext={{ | |
| message: { | |
| recipient: '', | |
| text: '', | |
| } | |
| }} |
This file contains hidden or 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
| export function Transition | |
| ({ | |
| transitionName, | |
| payload = {} | |
| }) { | |
| const transition = useMachineTransition(transitionName) | |
| useEffect(() => { | |
| transition.execute(payload) | |
| }) |
This file contains hidden or 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
| export function useMachineTransition (transitionName) { | |
| const { | |
| states, | |
| state, | |
| setState, | |
| context, | |
| setContext, | |
| } = useContext(StateMachineContext) | |
| return useAsyncCallback(async (payload = {}) => { |
This file contains hidden or 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
| const loadDraft = ({ context, setContext }) => { | |
| console.log('Context before action', context); | |
| const draft = localStorage.getItem('draft') | |
| if (draft) { | |
| setContext((state) => { | |
| return { | |
| ...state, | |
| message: JSON.parse(draft) |