-
Notifications
You must be signed in to change notification settings - Fork 4
Refactored Timo to use XState #1
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
Open
bashlk
wants to merge
26
commits into
start-xstate-refactor
Choose a base branch
from
main
base: start-xstate-refactor
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit
Hold shift + click to select a range
38a245c
initial implementation of the userMachine
bashlk 8a1b9e1
initial implementation of userMachine
bashlk 3b1d6c2
cleaned up userMachine
bashlk 9187e80
borked implementation of customizeUserMachine
bashlk 212ef66
initial implementation of hierarchial machine
bashlk acbdab9
switched to machine per route and refactored login to use new machine
bashlk 0e91c5a
finished using customizeUser state machine
bashlk 5b7d9a8
implemented changePasswordMachine
bashlk e5a92f9
implemented profile machine
bashlk e0332b6
renamed hooks to useSystemMachine for added clarity
bashlk 890558a
implemented entriesMachine
bashlk f7d16df
implemented timerMachine
bashlk f3ce64c
added new finished state to fix bug
bashlk 90fd425
fixed bug with item status message
bashlk ddafe8d
removed unnecessary prop spread
bashlk d265052
removed unnecessary array
bashlk a549e14
initial implementation of history in rootMachine
bashlk ba61d8f
fixed bugs with history
bashlk 32e84cc
removed reference to non existing register route
bashlk bfeaa72
refactored root machine to contain route state and switched to invoke…
bashlk 43d9a21
removed unnecessary array
bashlk f51b7e8
added id
bashlk a1acda9
removed unnecessary assignment
bashlk c2c1de4
implemented useRootMachineState
bashlk 564a440
replaced initialize events with inputs
bashlk ff3da74
added robots.txt
bashlk File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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
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
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,67 +1,47 @@ | ||
| import Router from '@timo/common/components/Router'; | ||
| import UserContextProvider from '@timo/common/context/UserContextProvider'; | ||
| import Container from '@timo/common/components/Container'; | ||
| import Title from '@timo/common/components/Title'; | ||
| import TopBarWithUser from '@timo/common/contextualComponents/TopBarWithUser'; | ||
| import ProtectedRoute from '@timo/common/contextualComponents/ProtectedRoute'; | ||
|
|
||
| import Login from './routes/Login/Login'; | ||
| import Entries from './routes/Entries/Entries'; | ||
| import NewEntry from './routes/NewEntry/NewEntry'; | ||
| import Profile from './routes/Profile/Profile'; | ||
|
|
||
| const routes = [ | ||
| { path: '/', name: 'Entries' }, | ||
| { path: '/login', name: 'Login' }, | ||
| { path: '/register', name: 'Register' }, | ||
| { path: '/new', name: 'NewEntry' }, | ||
| { path: '/profile', name: 'Profile' } | ||
| ]; | ||
| import TopBarWithUser from './contextualComponents/TopBarWithUser'; | ||
| import MachineContextProvider from './context/MachineContext'; | ||
| import Router from './components/Router'; | ||
|
|
||
| const App = () => ( | ||
| <UserContextProvider> | ||
| <Router routes={routes}> | ||
| {(routeName, history) => { | ||
| <MachineContextProvider> | ||
| <Router> | ||
| {(routeName) => { | ||
| let pageComponent = null; | ||
| switch (routeName) { | ||
| case 'Login': | ||
| pageComponent = <Login history={history} />; | ||
| case 'login': | ||
| pageComponent = <Login />; | ||
| break; | ||
| case 'NewEntry': | ||
| case 'newEntry': | ||
| pageComponent = ( | ||
| <ProtectedRoute history={history}> | ||
| <NewEntry history={history} /> | ||
| </ProtectedRoute> | ||
| <NewEntry /> | ||
| ); | ||
| break; | ||
| case 'Entries': | ||
| case 'entries': | ||
| pageComponent = ( | ||
| <ProtectedRoute history={history}> | ||
| <Entries history={history} /> | ||
| </ProtectedRoute> | ||
| <Entries /> | ||
| ); | ||
| break; | ||
| case 'Profile': | ||
| case 'profile': | ||
| pageComponent = ( | ||
| <ProtectedRoute history={history}> | ||
| <Profile /> | ||
| </ProtectedRoute> | ||
| <Profile /> | ||
| ); | ||
| break; | ||
| default: | ||
| pageComponent = ( | ||
| <Title>Page not found</Title> | ||
| ); | ||
| } | ||
| return ( | ||
| <Container> | ||
| <TopBarWithUser history={history} /> | ||
| <TopBarWithUser /> | ||
| {pageComponent} | ||
| </Container> | ||
| ); | ||
| }} | ||
| </Router> | ||
| </UserContextProvider> | ||
| </MachineContextProvider> | ||
| ); | ||
|
|
||
| export default 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import PropTypes from 'prop-types'; | ||
| import useRootMachineState from '../hooks/useRootMachineState'; | ||
|
|
||
| const Router = ({ children }) => { | ||
| const state = useRootMachineState(state => state.value); | ||
| const route = state?.authenticated || state?.unauthenticated; | ||
| if (route && route !== 'unknown') { | ||
| return children(route); | ||
| } | ||
| return null; | ||
| }; | ||
|
|
||
| Router.propTypes = { | ||
| children: PropTypes.func.isRequired | ||
| }; | ||
|
|
||
| export default Router; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| import styles from '@timo/common/components/Timer/Timer.module.css'; | ||
| import useChildMachineState from '../hooks/useChildMachineState'; | ||
|
|
||
| const Timer = () => { | ||
| const timerValue = useChildMachineState('newEntry', state => state.context.timerValue); | ||
|
|
||
| // Format duration to HH:MM:SS | ||
| const formattedValue = new Date(timerValue * 1000).toISOString().slice(11, 19); | ||
|
|
||
| return ( | ||
| <div className={styles.timer}> | ||
| {formattedValue} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default Timer; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| import PropTypes from 'prop-types'; | ||
| import { createActor } from 'xstate'; | ||
| import { createContext } from 'react'; | ||
| import root from '../machines/root'; | ||
|
|
||
| const rootActor = createActor(root, { systemId: 'root' }); | ||
| rootActor.start(); | ||
|
|
||
| export const MachineContext = createContext(); | ||
|
|
||
| const MachineContextProvider = ({ children }) => { | ||
| return ( | ||
| <MachineContext.Provider value={rootActor}> | ||
| {children} | ||
| </MachineContext.Provider> | ||
| ); | ||
| }; | ||
|
|
||
| MachineContextProvider.propTypes = { | ||
| children: PropTypes.node.isRequired | ||
| }; | ||
|
|
||
| export default MachineContextProvider; |
21 changes: 21 additions & 0 deletions
21
packages/xstate/src/contextualComponents/TopBarWithUser.jsx
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| import TopBar from '@timo/common/components/TopBar'; | ||
| import useRootMachine from '../hooks/useRootMachine'; | ||
| import useRootMachineState from '../hooks/useRootMachineState'; | ||
|
|
||
| const TopBarWithUser = () => { | ||
| const rootMachine = useRootMachine(); | ||
| const userData = useRootMachineState(state => state.context.userData); | ||
|
|
||
| return ( | ||
| <TopBar | ||
| avatar={{ | ||
| character: userData?.avatar_character, | ||
| background: userData?.avatar_background | ||
| }} | ||
| onIconClick={() => rootMachine.send({ type: 'pushRoute', route: 'entries' })} | ||
| onAvatarClick={() => rootMachine.send({ type: 'pushRoute', route: 'profile' })} | ||
| /> | ||
| ); | ||
| }; | ||
|
|
||
| export default TopBarWithUser; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { useSelector } from '@xstate/react'; | ||
| import useRootMachine from './useRootMachine'; | ||
|
|
||
| const useChildMachine = (childId) => { | ||
| const rootMachine = useRootMachine(); | ||
| return useSelector(rootMachine, (state) => state.children[childId]); | ||
| }; | ||
|
|
||
| export default useChildMachine; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { useSelector } from '@xstate/react'; | ||
| import useChildMachine from './useChildMachine'; | ||
|
|
||
| const useChildMachineState = (childId, selector) => { | ||
| const machine = useChildMachine(childId); | ||
| return useSelector(machine, selector); | ||
| }; | ||
|
|
||
| export default useChildMachineState; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| import { useContext } from 'react'; | ||
| import { MachineContext } from '../context/MachineContext'; | ||
|
|
||
| const useRootMachine = () => { | ||
| return useContext(MachineContext); | ||
| }; | ||
|
|
||
| export default useRootMachine; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| import { useSelector } from '@xstate/react'; | ||
| import useRootMachine from './useRootMachine'; | ||
|
|
||
| const useRootMachineState = (selector) => { | ||
| const rootMachine = useRootMachine(); | ||
| return useSelector(rootMachine, selector); | ||
| }; | ||
|
|
||
| export default useRootMachineState; |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import { setup, assign, fromPromise } from 'xstate'; | ||
| import { updatePassword } from '@timo/common/api'; | ||
|
|
||
| const changePasswordMachine = setup({ | ||
| actors: { | ||
| updatePassword: fromPromise(async ({ input }) => updatePassword(input)) | ||
| } | ||
| }).createMachine({ | ||
| id: 'changePassword', | ||
| initial: 'idle', | ||
| context: ({ input }) => ({ | ||
| username: input.username, | ||
| statusMessage: null | ||
| }), | ||
| states: { | ||
| 'idle': { | ||
| on: { | ||
| 'save': { | ||
| target: 'saving' | ||
| } | ||
| } | ||
| }, | ||
| 'saving': { | ||
| entry: [ | ||
| assign({ | ||
| statusMessage: 'Saving...' | ||
| }) | ||
| ], | ||
| invoke: [ | ||
| { | ||
| src: 'updatePassword', | ||
| input: ({ context, event }) => ({ | ||
| username: context.username, | ||
| password: event.password, | ||
| newPassword: event.newPassword | ||
| }), | ||
| onDone: { | ||
| target: 'idle', | ||
| actions: assign({ | ||
| statusMessage: 'Password updated' | ||
| }) | ||
| }, | ||
| onError: { | ||
| target: 'idle', | ||
| actions: assign({ | ||
| statusMessage: ({ event }) => event.error.message | ||
| }) | ||
| } | ||
| } | ||
| ] | ||
| } | ||
| } | ||
| }); | ||
|
|
||
| export default changePasswordMachine; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unrelated change. This route never existed.