UNPKG

analytica-frontend-lib

Version:

Repositório público dos componentes utilizados nas plataformas da Analytica Ensino

173 lines (169 loc) 5.76 kB
"use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _optionalChain(ops) { let lastAccessLHS = undefined; let value = ops[0]; let i = 1; while (i < ops.length) { const op = ops[i]; const fn = ops[i + 1]; i += 2; if ((op === 'optionalAccess' || op === 'optionalCall') && value == null) { return undefined; } if (op === 'access' || op === 'optionalAccess') { lastAccessLHS = value; value = fn(value); } else if (op === 'call' || op === 'optionalCall') { value = fn((...args) => value.call(lastAccessLHS, ...args)); lastAccessLHS = undefined; } } return value; }// src/hooks/useQuestionsList.ts var _react = require('react'); var useQuestionsListImpl = (apiClient) => { const [state, setState] = _react.useState.call(void 0, { questions: [], pagination: null, loading: false, loadingMore: false, error: null, currentFilters: null }); const updateState = _react.useCallback.call(void 0, (updates) => { setState((prev) => ({ ...prev, ...updates })); }, []); const handleError = _react.useCallback.call(void 0, (error) => { console.error("Erro ao carregar quest\xF5es:", error); let errorMessage = "Erro ao carregar quest\xF5es"; if (error && typeof error === "object" && "response" in error) { const axiosError = error; errorMessage = _optionalChain([axiosError, 'optionalAccess', _ => _.response, 'optionalAccess', _2 => _2.data, 'optionalAccess', _3 => _3.message]) || _optionalChain([axiosError, 'optionalAccess', _4 => _4.message]) || errorMessage; } else if (error instanceof Error) { errorMessage = error.message; } updateState({ loading: false, loadingMore: false, error: errorMessage }); }, [updateState] ); const convertPagination = (pagination) => { if (!pagination) return null; return { page: pagination.page, pageSize: pagination.limit, total: pagination.total, totalPages: pagination.totalPages, hasNext: pagination.hasNext, hasPrevious: pagination.hasPrev }; }; const fetchQuestions = _react.useCallback.call(void 0, async (filters, append = false) => { if (append) { setState((prev) => ({ ...prev, loadingMore: true, error: null })); } else { updateState({ loading: true, error: null, questions: [] }); } try { const validatedFilters = { ...filters }; const response = await apiClient.post( "/questions/list", validatedFilters ); setState((prev) => ({ loading: false, loadingMore: false, questions: append ? [...prev.questions, ...response.data.data.questions] : response.data.data.questions, pagination: convertPagination(response.data.data.pagination), error: null, currentFilters: validatedFilters })); } catch (error) { setState((prev) => ({ ...prev, loading: false, loadingMore: false })); handleError(error); } }, [apiClient, updateState, handleError] ); const fetchRandomQuestions = _react.useCallback.call(void 0, async (count, filters) => { try { const validatedFilters = { ...filters, randomQuestions: count }; const response = await apiClient.post( "/questions/list", validatedFilters ); return response.data.data.questions; } catch (error) { handleError(error); return []; } }, [apiClient, handleError] ); const fetchQuestionsByIds = _react.useCallback.call(void 0, async (questionIds) => { try { const body = { questionsIds: questionIds }; const response = await apiClient.post( "/questions/by-ids", body ); return response.data.data.questions; } catch (error) { handleError(error); return []; } }, [apiClient, handleError] ); const loadMore = _react.useCallback.call(void 0, async (externalFilters, externalPagination) => { setState((prev) => { const { currentFilters, pagination: internalPagination, loadingMore: isLoadingMore } = prev; const effectiveFilters = currentFilters || externalFilters; const effectivePagination = internalPagination || externalPagination; if (isLoadingMore || !effectiveFilters || !_optionalChain([effectivePagination, 'optionalAccess', _5 => _5.hasNext])) { return prev; } const nextPageFilters = { ...effectiveFilters, page: effectivePagination.page + 1 }; fetchQuestions(nextPageFilters, true).catch((error) => { console.error("Erro ao carregar mais quest\xF5es:", error); }); return { ...prev, loadingMore: true }; }); }, [fetchQuestions] ); const reset = _react.useCallback.call(void 0, () => { setState({ questions: [], pagination: null, loading: false, loadingMore: false, error: null, currentFilters: null }); }, []); return { ...state, fetchQuestions, fetchRandomQuestions, fetchQuestionsByIds, loadMore, reset }; }; var createUseQuestionsList = (apiClient) => { return () => useQuestionsListImpl(apiClient); }; var createQuestionsListHook = (apiClient) => { return createUseQuestionsList(apiClient); }; exports.createUseQuestionsList = createUseQuestionsList; exports.createQuestionsListHook = createQuestionsListHook; //# sourceMappingURL=chunk-ABMVSNYE.js.map