Skip to content

Instantly share code, notes, and snippets.

@germanescobar
Created February 27, 2024 19:07
Show Gist options
  • Save germanescobar/2a3331f84b8cde7a469aa82bd901008d to your computer and use it in GitHub Desktop.
Save germanescobar/2a3331f84b8cde7a469aa82bd901008d to your computer and use it in GitHub Desktop.
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