import { Event, Queue } from '../Services';
import React, { Component } from 'react';
import { Debounce } from '../Tools';
import { ReactiveContext } from '../Contexts/ReactiveContext';
import { ReactiveController } from './withController';
import axios from 'axios';
import { useNavigate } from 'react-router-dom';
export interface ReactiveDataEvent {
action: string;
controller: ReactiveController;
event?: string;
}
export interface ReactiveOnMountDataEvent extends ReactiveDataEvent {
state: VoidFunction;
}
export interface ReactiveControllerRegistration {
controller: ReactiveController;
state: VoidFunction;
mounted: boolean;
}
export interface ReactiveProps {
debug?: boolean;
navigate: (to: string) => undefined;
}
export function withReactive
(WrappedComponent: React.ComponentType
): any {
const Reactive = class Reactive extends Component
{
protected debug = false;
static delay = 100;
protected uuid: string;
public states: ReactiveControllerRegistration[] = [];
constructor(props: P & ReactiveProps) {
super(props);
this.debug = props?.debug ?? false;
this.uuid = 'randomid';
this.state = {
htmlResponse: null,
};
this.onMount = Debounce(this.onMount.bind(this), Reactive.delay);
this.onUnmount = this.onUnmount.bind(this);
this.onDispatch = this.onDispatch.bind(this);
this.onEvent = this.onEvent.bind(this);
this.request = this.request.bind(this);
this.renderHtml = this.renderHtml.bind(this);
}
componentWillMount(): void {
Event.subscribe(this.uuid, this.onEvent);
}
componentWillUnmount(): void {
Event.unsubscribe(this.uuid, this.onEvent);
}
request(payload: object): Promise