Created
February 27, 2024 19:07
-
-
Save germanescobar/2a3331f84b8cde7a469aa82bd901008d to your computer and use it in GitHub Desktop.
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
type MachineContext = { | |
fromDate: Date; | |
toDate: Date; | |
glucose: GlucoseReading[]; | |
}; | |
type MachineEvent = | |
| { | |
type: 'SET_FROM_DATE'; | |
fromDate: Date; | |
} | |
| { | |
type: 'SET_TO_DATE'; | |
toDate: Date; | |
}; | |
const machine = createMachine<MachineContext, MachineEvent>({ | |
id: 'desktop-app', | |
context: { | |
fromDate, | |
toDate, | |
glucose: [], | |
}, | |
initial: 'loading', | |
states: { | |
loading: { | |
invoke: [ | |
{ | |
id: 'data-loader', | |
src: async ctx => { | |
const data = await trpcClient.getGlucoseData.query({ fromDate: ctx.fromDate, toDate: ctx.toDate }); | |
return { | |
glucose: data, | |
}; | |
}, | |
onDone: [{ target: 'ready' }], | |
onError: { | |
actions: [ | |
(_, evt) => { | |
console.log('Error finding or creating cart', { message: evt.data }); | |
}, | |
], | |
target: 'error', | |
}, | |
}, | |
], | |
}, | |
ready: { | |
on: { | |
'SET_FROM_DATE': { | |
actions: assign({ | |
fromDate: (_, evt) => evt.fromDate, | |
}), | |
}, | |
'SET_TO_DATE': { | |
actions: assign({ | |
fromDate: (_, evt) => evt.toDate, | |
}), | |
}, | |
}, | |
}, | |
error: {}, | |
}, | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment