check moving back or forw

This commit is contained in:
Sebastian 2022-04-14 16:35:00 -03:00
parent 3957f529bf
commit bf3e011183
No known key found for this signature in database
GPG Key ID: BE4FF68352439FC1

View File

@ -92,39 +92,39 @@ function ErrorBoundary(props: {
return <div>{props.children}</div>; return <div>{props.children}</div>;
} }
let currentHistoryId = 0;
export function AnastasisClientFrame(props: AnastasisClientFrameProps): VNode { export function AnastasisClientFrame(props: AnastasisClientFrameProps): VNode {
const reducer = useAnastasisContext(); const reducer = useAnastasisContext();
if (!reducer) { if (!reducer) {
return <p>Fatal: Reducer must be in context.</p>; return <p>Fatal: Reducer must be in context.</p>;
} }
const next = async (): Promise<void> => { const doBack = async (): Promise<void> => {
history.back();
if (props.onBack) {
await props.onBack();
}
};
const doNext = async (): Promise<void> => {
try {
const nextId: number =
(history.state && typeof history.state.id === "number"
? history.state.id
: 0) + 1;
currentHistoryId = nextId;
history.pushState({ id: nextId }, "unused", `#${nextId}`);
} catch (e) {
console.log(e);
}
if (props.onNext) { if (props.onNext) {
await props.onNext(); await props.onNext();
} else { } else {
await reducer.transition("next", {}); await reducer.transition("next", {});
} }
reducer.currentReducerState?.reducer_type;
const stateName = !reducer.currentReducerState
? "not-defined"
: reducer.currentReducerState.reducer_type === "backup"
? `#backup-${reducer.currentReducerState.backup_state}`
: reducer.currentReducerState.reducer_type === "recovery"
? `recovery-${reducer.currentReducerState.recovery_state}`
: reducer.currentReducerState.reducer_type === "error"
? `error-${reducer.currentReducerState.code}`
: "unknown";
const id: number =
typeof history.state.id === "number" ? history.state.id : 1;
history.pushState(
{
id: id + 1,
},
"unused",
stateName,
);
}; };
const handleKeyPress = ( const handleKeyPress = (
e: h.JSX.TargetedKeyboardEvent<HTMLDivElement>, e: h.JSX.TargetedKeyboardEvent<HTMLDivElement>,
@ -133,9 +133,14 @@ export function AnastasisClientFrame(props: AnastasisClientFrameProps): VNode {
// FIXME: By default, "next" action should be executed here // FIXME: By default, "next" action should be executed here
}; };
const browserOnBackButton = useCallback((ev: PopStateEvent) => { const browserOnBackButton = useCallback(async (ev: PopStateEvent) => {
console.log("BACK BACK", JSON.stringify(ev.state)); //check if we are going back or forward
reducer.back(); if (!ev.state || ev.state.id === 0 || ev.state.id < currentHistoryId) {
await reducer.back();
} else {
await reducer.transition("next", {});
}
// reducer // reducer
return false; return false;
}, []); }, []);
@ -164,16 +169,13 @@ export function AnastasisClientFrame(props: AnastasisClientFrameProps): VNode {
justifyContent: "space-between", justifyContent: "space-between",
}} }}
> >
<button <button class="button" onClick={doBack}>
class="button"
onClick={() => (props.onBack ?? reducer.back)()}
>
Back Back
</button> </button>
<AsyncButton <AsyncButton
class="button is-info" class="button is-info"
data-tooltip={props.hideNext} data-tooltip={props.hideNext}
onClick={next} onClick={doNext}
disabled={props.hideNext !== undefined} disabled={props.hideNext !== undefined}
> >
Next Next