import * as React from "react";
import axios from "axios";
import withProgressHandlers from "./withProgressHandlers";
import { CallStatus, ValueType } from "./../enums";
import { toQueue } from "./../network";
import { Those } from "../util";
import CreateImmutable from "../util/CreateImmutable";
import ReservedKeys from "../util/ReservedKeys";
import { ImmutableStoreValue } from "./../types/ValueMap";
import { WithCallConfig } from "./../types/NetworkQueue";
export default (
key: string,
callConfig?: WithCallConfig
) => (Component: any) => (props: any) => {
if (!ReservedKeys.includes(key)) {
const obj: ImmutableStoreValue = props.store[key] || CreateImmutable(undefined);
const getLatestStore = () => props.store;
if (
obj.get("type") === ValueType.STORE_VALUE &&
obj.get("status") !== CallStatus.NONE
) {
const config = callConfig && callConfig.config ? callConfig.config : {};
const reducer = callConfig && callConfig.reducer ? callConfig.reducer : undefined;
let queueKey = key;
if (typeof callConfig !== "undefined" && typeof callConfig.config !== "undefined") {
if (typeof callConfig.config.url !== "undefined") queueKey += window.btoa(callConfig.config.url);
// if (typeof callConfig.config.url !== "undefined" && callConfig.config.url.indexOf("?") !== -1) console.log(queueKey, callConfig.config.url);
}
toQueue(getLatestStore, key, obj, Those(axios, config), queueKey, reducer);
}
let newProps = props;
if (callConfig && callConfig.progressHandlers) {
newProps = Object.assign({}, props, {
__ion_progress_status: obj.get("status")
});
if (callConfig && callConfig.progressHandlers && typeof callConfig.progressHandlers !== "boolean") {
return withProgressHandlers(callConfig.progressHandlers)(Component)(newProps);
}
}
return ;
} else return ;
};