export function PortfolioProvider(props: ProviderProps) {
const { user, isAuthenticated } = useUser();
const [loading, setLoading] = createSignal(false);
const [transactions, setTransactions] = createSignal<Transaction[]>();
const [pagination, setPagination] = createSignal<Pagination>();
const [currentRequest, setCurrentRequest] = createSignal<TransactionRequest>();
createEffect(() => {
console.log("Fetching transactions", currentRequest());
if (isAuthenticated() && currentRequest()) {
_fetch(currentRequest());
}
});
const _fetch = async (request: TransactionRequest | undefined) => {
if (!request) return;
setLoading(true);
try {
let response = await urqlClient(ContextHelpers.getAuthToken()).query(Queries.GetTransactions, request).toPromise();
ContextHelpers.verifyGraphQLReponse(response);
setTransactions(response.data.transactions.transactions as Transaction[]);
setPagination(response.data.transactions.pagination as Pagination);
} finally {
setLoading(false);
}
};
const response = {
transactions,
loading,
fetch: (request: TransactionRequest) => {
setCurrentRequest(request);
},
};
return <TransactionContext.Provider value={response}>{props.children}</TransactionContext.Provider>;
}