Skip to content

Instantly share code, notes, and snippets.

@gsong
Last active May 24, 2020 14:35
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gsong/99951fdf5f58e9e56d6b09457964d90c to your computer and use it in GitHub Desktop.
Save gsong/99951fdf5f58e9e56d6b09457964d90c to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
const context = { useLoadingStatus: false };
const queryMachine = Machine(
{
id: "query",
context,
on: {
UPDATE: [
{ target: "loading", cond: "isLoading" },
{ target: "notFound", cond: "isNotFound" },
{ target: "error", cond: "isError" },
{ target: "success", cond: "isSuccess" },
],
},
initial: "loading",
states: {
error: {},
loading: {},
notFound: {},
success: {},
},
},
{
guards: {
isError: (_, { queryInfo: { status } }) => status === "error",
isLoading: (
{ useLoadingStatus },
{ queryInfo: { status, isFetching } },
) => (useLoadingStatus ? status === "loading" : isFetching),
isNotFound: (_, { queryInfo: { error } }) =>
error?.response?.status === 404,
isSuccess: (_, { queryInfo: { status, data } }) =>
status === "success" && !isEmpty(data),
},
},
);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment