UNPKG

analytica-frontend-lib

Version:

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

57 lines (52 loc) 2.34 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; } var _chunkAOUX6XXNjs = require('./chunk-AOUX6XXN.js'); // src/hooks/useDraftAutoSave.ts var _react = require('react'); function useDraftAutoSave({ apiClient, enabled, endpoint = "/activities/{activityId}/draft" }) { const { setDraftApiClient, saveDraft } = _chunkAOUX6XXNjs.useQuizStore.call(void 0, ); const draftApiClient = _react.useMemo.call(void 0, () => ({ saveDraft: async (activityId, payload) => { const url = endpoint.replace("{activityId}", activityId); await apiClient.post(url, payload); return { success: true }; }, loadDraft: async (activityId) => { const url = endpoint.replace("{activityId}", activityId); const response = await apiClient.get(url); const data = _optionalChain([response, 'access', _ => _.data, 'optionalAccess', _2 => _2.data]); return data ? { hasDraft: data.hasDraft, answers: data.answers || [] } : null; } }), [apiClient, endpoint] ); _react.useEffect.call(void 0, () => { if (enabled) { setDraftApiClient(draftApiClient); } else { setDraftApiClient(null); } return () => { setDraftApiClient(null); }; }, [enabled, draftApiClient, setDraftApiClient]); _react.useEffect.call(void 0, () => { if (!enabled) return; const handleVisibilityChange = () => { if (document.visibilityState === "hidden") { saveDraft(); } }; document.addEventListener("visibilitychange", handleVisibilityChange); return () => document.removeEventListener("visibilitychange", handleVisibilityChange); }, [enabled, saveDraft]); } exports.useDraftAutoSave = useDraftAutoSave; //# sourceMappingURL=chunk-M4VITEXC.js.map