if im calling: // src/services/LayoutService.js import IndexedDB from…
- User
if im calling:
// src/services/LayoutService.js import IndexedDB from '../utils/IndexedDB'; import { checkOnlineStatus } from '../utils/Connectivity';
and LayoutService.js has:
const layoutDB = new IndexedDB('ReactCard', 1); export { layoutDB }
export const saveLayoutData = async (layoutId, layoutData) => { };
and within Connectivity.js I have:
//src/utils/Connectivity.js import LayoutService, { saveLayoutData, layoutDB } from '../LayoutService'; import logger from './logger';
let isOnline = navigator.onLine; let currentAlertId = null;// Track current alert ID for dismissing
const updateOnlineStatus = (showAlert, dismissAlert) => {
logger.log("Updating online status..."); isOnline = navigator.onLine; if (!isOnline) { logger.log('you are offline'); currentAlertId = showAlert('warning', "You're currently offline! Don’t worry, your data is being saved in your browser. Please avoid closing this window until you’re back online.", null); } else { logger.log('you are online'); if (currentAlertId) { synchronizeUnsyncedData();// Handle syncing when back online dismissAlert(currentAlertId); currentAlertId = null;// Clear the stored ID after dismissing } }};
const synchronizeUnsyncedData = async () => {
const unsyncedLayouts = await layoutDB.getUnsyncedData('LayoutData'); for (const layout of unsyncedLayouts) { // saveLayoutData = async (layoutId, layoutData) await saveLayoutData(layout.id, layout.blocks); }};
const setupConnectivityListeners = (showAlert, dismissAlert) => {
logger.log("Setting up connectivity listeners..."); window.addEventListener('load', () => updateOnlineStatus(showAlert, dismissAlert)); window.addEventListener('online', () => updateOnlineStatus(showAlert, dismissAlert)); window.addEventListener('offline', () => updateOnlineStatus(showAlert, dismissAlert));};
export const checkOnlineStatus = () => isOnline; export const setupConnectivityAlerts = (showAlert, dismissAlert) => {
setupConnectivityListeners(showAlert, dismissAlert);};
and Im trying to synchronizeUnsyncedData(), so I figured I needed to export { layoutDB } and import them into the Connectivity.js
But im getting this error:
Compiled with problems: ERROR in ./src/utils/Connectivity.js 4:0-75 Module not found: Error: Can't resolve '../LayoutService' in '/react-card/src/utils'
- Assistant
- User
export const saveLayoutData = async (layoutId, layoutData) => { }; does this not export it?
- Assistant
Yes, the line
export const saveLayoutData = async (layoutId, layoutData) => { };does indeed export thesaveLayoutDatafunction. This means that you can import it from another module.However, the potential issue regarding your import statements could stem from a couple of other areas. Just to clarify:
What You've Done Correctly:
- Exporting the Function:
You correctly exported
saveLayoutDatawith the syntax you used. This allows any other file that imports from to access this function.
- Exporting the Function:
You correctly exported
- User
when i go offline, the error message "you're currently..." 6 times? why and also:
Uncaught runtime errors: ERROR services_LayoutService__WEBPACK_IMPORTED_MODULE_0_.layoutDB.getUnsyncedData is not a function
- Assistant
- User
Im realizing I shouldn't even need to call: layoutDB from LayoutService.js. The synchronizeUnsyncedData should not need to: const unsyncedLayouts = await layoutDB.getUnsyncedData('LayoutData'); it will call a new const in the LayoutService.js called: export const syncLayoutData = async () => { }; and this will handle the indexedDB and updating the database. similar to saveLayoutData:
export const saveLayoutData = async (layoutId, layoutData) => {
await openDatabase(); const currentTimestamp = Date.now(); // eslint-disable-next-line no-unused-vars let response; // If online or syncing, proceed with API operations try { if (checkOnlineStatus()) { const response = await fetch(`${API_URL}/api/save/${layoutId}`, { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ ...layoutData, created_at: layoutData.created_at || currentTimestamp,// Keep original created_at if exists updated_at: currentTimestamp,// Always update this }) }); if (!response.ok) { throw new Error("Failed to save layout data to API"); } // If saving to the server succeeds, save the data to IndexedDB await layoutDB.putData('LayoutData', { id: layoutId, blocks: layoutData, created_at: layoutData.created_at || currentTimestamp, updated_at: currentTimestamp, }); logger.log(`Layout data for ${layoutId} saved successfully to API and IndexedDB.`); } else { // Offline case await layoutDB.putData('LayoutData', { id: layoutId, blocks: layoutData, created_at: layoutData.created_at || currentTimestamp, updated_at: currentTimestamp, }); logger.log(`Layout data for ${layoutId} saved successfully in IndexedDB (offline).`); return;// Early return if offline } } catch (error) { logger.error("Error saving layout data:", error); throw error; } - Assistant
- User
how would you write the syncLayoutData const
- Assistant
- User
you wrote this code:
const unsyncedLayouts = await layoutDB.getUnsyncedData('LayoutData'); // Check if there are any unsynced layouts if (unsyncedLayouts.length === 0) { logger.log("No unsynced layouts found."); return;// Exit early if there's nothing to sync }but im not sure how it works since one I do not have a way to check between the unsyned data and always treat the indexedDB as the most recent data.. so i shouldnt have to
- Assistant
- User
- Assistant
- User
- Assistant