Skip to content

Instantly share code, notes, and snippets.

@NoMan2000
Last active June 10, 2021 20:06
Show Gist options
  • Save NoMan2000/4f34af639ff08245149fea5d5ac0b132 to your computer and use it in GitHub Desktop.
Save NoMan2000/4f34af639ff08245149fea5d5ac0b132 to your computer and use it in GitHub Desktop.
Generated by XState Viz: https://xstate.js.org/viz
var AdCreationPages;
(function (AdCreationPages) {
AdCreationPages["CHOOSE_ORGANIC_OR_PAID"] = "organicOrPaid";
AdCreationPages["AD_CREATION_FORM"] = "adCreationForm";
AdCreationPages["ORGANIC_POSTS"] = "organicPosts";
AdCreationPages["PAID_POSTS"] = "paidPosts";
AdCreationPages["PAID_POSTS_CURATED"] = "paidPostsCuration";
AdCreationPages["PAID_POSTS_BESPOKE"] = "paidPostsBespoke";
AdCreationPages["POSTS_PUBLISHED"] = "postsPublished";
AdCreationPages["SAVE_DRAFT"] = "saveDraft";
})(AdCreationPages || (AdCreationPages = {}));
var PaidPostsCurationTypes;
(function (PaidPostsCurationTypes) {
PaidPostsCurationTypes["CREDITS"] = "credits";
PaidPostsCurationTypes["PRODUCTS"] = "products";
PaidPostsCurationTypes["COMPETITORS"] = "competitors";
PaidPostsCurationTypes["AUDIENCE"] = "audience";
})(PaidPostsCurationTypes || (PaidPostsCurationTypes = {}));
var PaidPostsBespokeTypes;
(function (PaidPostsBespokeTypes) {
PaidPostsBespokeTypes["OBJECTIVES"] = "objectives";
PaidPostsBespokeTypes["AUDIENCE"] = "audience";
PaidPostsBespokeTypes["CREATIVE"] = "creative";
PaidPostsBespokeTypes["PAYMENT"] = "payment";
PaidPostsBespokeTypes["OVERVIEW"] = "overview";
})(PaidPostsBespokeTypes || (PaidPostsBespokeTypes = {}));
// States specific to an action.
var AdCreationEventNames;
(function (AdCreationEventNames) {
AdCreationEventNames["CHOOSE_ORGANIC_OR_PAID"] = "chooseOrganicOrPaid";
// Can do one of the two below
AdCreationEventNames["CHOOSE_ORGANIC"] = "chooseOrganic";
AdCreationEventNames["CHOOSE_PAID"] = "choosePaid";
// If they choose paid, can do one of the two below.
AdCreationEventNames["CHOOSE_PAID_CURATED"] = "choosePaidCurated";
AdCreationEventNames["CHOOSE_PAID_BESPOKE"] = "choosePaidBespoke";
// If they choose curated, they can choose one of the four paths below
AdCreationEventNames["CHOOSE_PAID_CURATED_CREDITS"] = "choosePaidCuratedCredits";
AdCreationEventNames["CHOOSE_PAID_CURATED_PRODUCTS"] = "choosePaidCuratedProducts";
AdCreationEventNames["CHOOSE_PAID_CURATED_COMPETITORS"] = "choosePaidCuratedCompetitors";
AdCreationEventNames["CHOOSE_PAID_CURATED_AUDIENCE"] = "choosePaidCuratedAudience";
// Final curated states
AdCreationEventNames["CHOOSE_PAID_CURATED_POST_DETAILS"] = "choosePaidCuratedPostDetails";
// If they choose bespoke, they can choosen one of the five paths below.
AdCreationEventNames["CHOOSE_PAID_BESPOKE_OBJECTIVES"] = "choosePaidBespokeObjectives";
AdCreationEventNames["CHOOSE_PAID_BESPOKE_AUDIENCE"] = "choosePaidBespokeAudience";
AdCreationEventNames["CHOOSE_PAID_BESPOKE_CREATIVE"] = "choosePaidBespokeCreative";
AdCreationEventNames["CHOOSE_PAID_BESPOKE_PAYMENT"] = "choosePaidBespokePayment";
AdCreationEventNames["CHOOSE_PAID_BESPOKE_OVERVIEW"] = "choosePaidBespokeOverview";
AdCreationEventNames["CHOOSE_PAID_BESPOKE_POST_DETAILS"] = "choosePaidBespokePostDetails";
// Paid final state.
AdCreationEventNames["SUBMITTED_PREVIEW"] = "submittedPreview";
// GENERIC, will require useContext to make it do what it needs to.
AdCreationEventNames["FETCH"] = "fetch";
// FINAL PAGE
AdCreationEventNames["DONE"] = "DONE";
})(AdCreationEventNames || (AdCreationEventNames = {}));
var ExternalMachineStates;
(function (ExternalMachineStates) {
ExternalMachineStates["FETCH_ACTION"] = "FETCH_ACTION";
ExternalMachineStates["RESOLVE_ACTION"] = "RESOLVE_ACTION";
ExternalMachineStates["REJECT_ACTION"] = "REJECT_ACTION";
})(ExternalMachineStates || (ExternalMachineStates = {}));
var AdGuards;
(function (AdGuards) {
AdGuards["IS_FETCHING"] = "isFetching";
AdGuards["HAS_FAILED"] = "hasFailed";
AdGuards["NOT_FAILING"] = "notFailing";
})(AdGuards || (AdGuards = {}));
const isFetching = (context) => {
return context.isLoading;
};
const hasFailed = (context) => {
return Boolean(context.errorMessage);
};
const successActions = [
assign({
isLoading: false,
}),
send(ExternalMachineStates.RESOLVE_ACTION),
assign({
errorMessage: '',
results: (_ctx, evt) => {
return evt.data;
},
}),
];
const failActions = [
assign({
isLoading: false,
}),
send(ExternalMachineStates.REJECT_ACTION),
assign({
results: [],
errorMessage: (_ctx, evt) => {
return evt.data || 'Unknown error occurred!';
},
}),
];
const targetCreator = (name) => {
return [
{
id: `${name}_REQUEST`,
actions: [
assign({
isLoading: true,
}),
send(ExternalMachineStates.FETCH_ACTION),
],
on: {
[`${name}_FAILURE`]: {
actions: failActions,
target: `${name}_FAILURE`,
},
[`${name}_SUCCESS`]: {
actions: successActions,
target: `${name}_SUCCESS`,
cond: AdGuards.NOT_FAILING,
},
},
},
{
id: `${name}_FAILURE`,
actions: failActions,
on: {
[`${name}_REQUEST`]: {
actions: [
assign({
isLoading: true,
errorMessage: '',
}),
],
target: `#${name}_REQUEST`,
},
},
},
];
};
const stateCreator = (name, onArr) => {
const on = onArr.reduce((acc, curr) => {
return Object.assign(Object.assign({}, acc), { [curr]: { target: `#${curr}` } });
}, {});
const targets = targetCreator(name);
return {
[`${name}_REQUEST`]: Object.assign({}, targets[0]),
[`${name}_FAILURE`]: Object.assign({}, targets[1]),
[`${name}_SUCCESS`]: {
id: `${name}_SUCCESS`,
actions: successActions,
on,
},
};
};
const AD_CREATION_MACHINE_ID = 'AdCreationMachine';
const defaultCtx = {
results: [],
errorMessage: '',
isLoading: false,
};
const adCreationMachine = Machine({
id: AD_CREATION_MACHINE_ID,
initial: AdCreationEventNames.CHOOSE_ORGANIC_OR_PAID,
context: Object.assign({}, defaultCtx),
states: {
[AdCreationEventNames.CHOOSE_ORGANIC_OR_PAID]: {
id: AdCreationEventNames.CHOOSE_ORGANIC_OR_PAID,
initial: `${AdCreationEventNames.CHOOSE_ORGANIC_OR_PAID}_REQUEST`,
states: Object.assign({}, stateCreator(AdCreationEventNames.CHOOSE_ORGANIC_OR_PAID, [
AdCreationEventNames.CHOOSE_ORGANIC,
AdCreationEventNames.CHOOSE_PAID,
])),
},
[AdCreationEventNames.CHOOSE_ORGANIC]: {
id: AdCreationEventNames.CHOOSE_ORGANIC,
initial: `${AdCreationEventNames.CHOOSE_ORGANIC}_REQUEST`,
states: Object.assign({}, stateCreator(AdCreationEventNames.CHOOSE_ORGANIC, [
AdCreationEventNames.CHOOSE_ORGANIC_OR_PAID,
AdCreationEventNames.DONE,
])),
},
[AdCreationEventNames.CHOOSE_PAID]: {
id: AdCreationEventNames.CHOOSE_PAID,
initial: `${AdCreationEventNames.CHOOSE_PAID}_REQUEST`,
states: Object.assign({}, stateCreator(AdCreationEventNames.CHOOSE_PAID, [
AdCreationEventNames.CHOOSE_ORGANIC_OR_PAID,
AdCreationEventNames.CHOOSE_PAID_CURATED,
AdCreationEventNames.CHOOSE_PAID_BESPOKE,
])),
},
[AdCreationEventNames.CHOOSE_PAID_CURATED]: {
id: AdCreationEventNames.CHOOSE_PAID_CURATED,
initial: `${AdCreationEventNames.CHOOSE_PAID_CURATED}_REQUEST`,
states: Object.assign({}, stateCreator(AdCreationEventNames.CHOOSE_PAID_CURATED, [
AdCreationEventNames.CHOOSE_PAID,
AdCreationEventNames.CHOOSE_PAID_CURATED_CREDITS,
AdCreationEventNames.CHOOSE_PAID_CURATED_PRODUCTS,
AdCreationEventNames.CHOOSE_PAID_CURATED_COMPETITORS,
AdCreationEventNames.CHOOSE_PAID_CURATED_AUDIENCE,
])),
},
[AdCreationEventNames.CHOOSE_PAID_CURATED_CREDITS]: {
id: AdCreationEventNames.CHOOSE_PAID_CURATED_CREDITS,
initial: `${AdCreationEventNames.CHOOSE_PAID_CURATED_CREDITS}_REQUEST`,
states: Object.assign({}, stateCreator(AdCreationEventNames.CHOOSE_PAID_CURATED_CREDITS, [
AdCreationEventNames.CHOOSE_PAID_CURATED,
AdCreationEventNames.CHOOSE_PAID_CURATED_POST_DETAILS,
])),
},
[AdCreationEventNames.CHOOSE_PAID_CURATED_PRODUCTS]: {
id: AdCreationEventNames.CHOOSE_PAID_CURATED_PRODUCTS,
initial: `${AdCreationEventNames.CHOOSE_PAID_CURATED_PRODUCTS}_REQUEST`,
states: Object.assign({}, stateCreator(AdCreationEventNames.CHOOSE_PAID_CURATED_PRODUCTS, [
AdCreationEventNames.CHOOSE_PAID_CURATED,
AdCreationEventNames.CHOOSE_PAID_CURATED_POST_DETAILS,
])),
},
[AdCreationEventNames.CHOOSE_PAID_CURATED_COMPETITORS]: {
id: AdCreationEventNames.CHOOSE_PAID_CURATED_COMPETITORS,
initial: `${AdCreationEventNames.CHOOSE_PAID_CURATED_COMPETITORS}_REQUEST`,
states: Object.assign({}, stateCreator(AdCreationEventNames.CHOOSE_PAID_CURATED_COMPETITORS, [
AdCreationEventNames.CHOOSE_PAID_CURATED,
AdCreationEventNames.CHOOSE_PAID_CURATED_POST_DETAILS,
])),
},
[AdCreationEventNames.CHOOSE_PAID_CURATED_AUDIENCE]: {
id: AdCreationEventNames.CHOOSE_PAID_CURATED_AUDIENCE,
initial: `${AdCreationEventNames.CHOOSE_PAID_CURATED_AUDIENCE}_REQUEST`,
states: Object.assign({}, stateCreator(AdCreationEventNames.CHOOSE_PAID_CURATED_AUDIENCE, [
AdCreationEventNames.CHOOSE_PAID_CURATED,
AdCreationEventNames.CHOOSE_PAID_CURATED_POST_DETAILS,
])),
},
[AdCreationEventNames.CHOOSE_PAID_CURATED_POST_DETAILS]: {
id: AdCreationEventNames.CHOOSE_PAID_CURATED_POST_DETAILS,
initial: `${AdCreationEventNames.CHOOSE_PAID_CURATED_POST_DETAILS}_REQUEST`,
states: Object.assign({}, stateCreator(AdCreationEventNames.CHOOSE_PAID_CURATED_POST_DETAILS, [AdCreationEventNames.SUBMITTED_PREVIEW])),
},
[AdCreationEventNames.CHOOSE_PAID_BESPOKE]: {
id: AdCreationEventNames.CHOOSE_PAID_BESPOKE,
initial: `${AdCreationEventNames.CHOOSE_PAID_BESPOKE}_REQUEST`,
states: Object.assign({}, stateCreator(AdCreationEventNames.CHOOSE_PAID_BESPOKE, [
AdCreationEventNames.CHOOSE_PAID,
AdCreationEventNames.CHOOSE_PAID_BESPOKE_OBJECTIVES,
AdCreationEventNames.CHOOSE_PAID_BESPOKE_AUDIENCE,
AdCreationEventNames.CHOOSE_PAID_CURATED_COMPETITORS,
AdCreationEventNames.CHOOSE_PAID_CURATED_AUDIENCE,
AdCreationEventNames.CHOOSE_PAID_BESPOKE_OVERVIEW,
])),
},
[AdCreationEventNames.CHOOSE_PAID_BESPOKE_OBJECTIVES]: {
id: AdCreationEventNames.CHOOSE_PAID_BESPOKE_OBJECTIVES,
initial: `${AdCreationEventNames.CHOOSE_PAID_BESPOKE_OBJECTIVES}_REQUEST`,
states: Object.assign({}, stateCreator(AdCreationEventNames.CHOOSE_PAID_BESPOKE_OBJECTIVES, [
AdCreationEventNames.CHOOSE_PAID_BESPOKE,
AdCreationEventNames.CHOOSE_PAID_BESPOKE_POST_DETAILS,
])),
},
[AdCreationEventNames.CHOOSE_PAID_BESPOKE_AUDIENCE]: {
id: AdCreationEventNames.CHOOSE_PAID_BESPOKE_AUDIENCE,
initial: `${AdCreationEventNames.CHOOSE_PAID_BESPOKE_AUDIENCE}_REQUEST`,
states: Object.assign({}, stateCreator(AdCreationEventNames.CHOOSE_PAID_BESPOKE_AUDIENCE, [
AdCreationEventNames.CHOOSE_PAID_BESPOKE,
AdCreationEventNames.CHOOSE_PAID_BESPOKE_POST_DETAILS,
])),
},
[AdCreationEventNames.CHOOSE_PAID_BESPOKE_CREATIVE]: {
id: AdCreationEventNames.CHOOSE_PAID_BESPOKE_CREATIVE,
initial: `${AdCreationEventNames.CHOOSE_PAID_BESPOKE_CREATIVE}_REQUEST`,
states: Object.assign({}, stateCreator(AdCreationEventNames.CHOOSE_PAID_BESPOKE_CREATIVE, [
AdCreationEventNames.CHOOSE_PAID_BESPOKE,
AdCreationEventNames.CHOOSE_PAID_BESPOKE_POST_DETAILS,
])),
},
[AdCreationEventNames.CHOOSE_PAID_BESPOKE_PAYMENT]: {
id: AdCreationEventNames.CHOOSE_PAID_BESPOKE_PAYMENT,
initial: `${AdCreationEventNames.CHOOSE_PAID_BESPOKE_PAYMENT}_REQUEST`,
states: Object.assign({}, stateCreator(AdCreationEventNames.CHOOSE_PAID_BESPOKE_PAYMENT, [
AdCreationEventNames.CHOOSE_PAID_BESPOKE,
AdCreationEventNames.CHOOSE_PAID_BESPOKE_POST_DETAILS,
])),
},
[AdCreationEventNames.CHOOSE_PAID_BESPOKE_OVERVIEW]: {
id: AdCreationEventNames.CHOOSE_PAID_BESPOKE_OVERVIEW,
initial: `${AdCreationEventNames.CHOOSE_PAID_BESPOKE_OVERVIEW}_REQUEST`,
states: Object.assign({}, stateCreator(AdCreationEventNames.CHOOSE_PAID_BESPOKE_OVERVIEW, [
AdCreationEventNames.CHOOSE_PAID_BESPOKE,
AdCreationEventNames.CHOOSE_PAID_BESPOKE_POST_DETAILS,
])),
},
[AdCreationEventNames.CHOOSE_PAID_BESPOKE_POST_DETAILS]: {
id: AdCreationEventNames.CHOOSE_PAID_BESPOKE_POST_DETAILS,
initial: `${AdCreationEventNames.CHOOSE_PAID_BESPOKE_POST_DETAILS}_REQUEST`,
states: Object.assign({}, stateCreator(AdCreationEventNames.CHOOSE_PAID_BESPOKE_POST_DETAILS, [AdCreationEventNames.SUBMITTED_PREVIEW])),
},
[AdCreationEventNames.SUBMITTED_PREVIEW]: {
id: AdCreationEventNames.SUBMITTED_PREVIEW,
initial: `${AdCreationEventNames.SUBMITTED_PREVIEW}_REQUEST`,
states: Object.assign({}, stateCreator(AdCreationEventNames.SUBMITTED_PREVIEW, [
AdCreationEventNames.DONE,
])),
},
[AdCreationEventNames.DONE]: {
id: AdCreationEventNames.DONE,
type: 'final',
},
},
}, {
guards: {
[AdGuards.IS_FETCHING]: (ctx) => {
return ctx.isLoading;
},
[AdGuards.HAS_FAILED]: (ctx) => {
return Boolean(ctx.errorMessage);
},
[AdGuards.NOT_FAILING]: (ctx) => {
return !ctx.errorMessage;
},
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment