import React, { Fragment, useEffect, useRef } from 'react'; import { useDB } from 'react-pouchdb'; import { IonButton, IonButtons, IonContent, IonIcon, IonInput, IonItem, IonLabel, IonList, IonListHeader, IonModal, IonTitle, IonToolbar, } from '@ionic/react'; import { options } from 'ionicons/icons'; import { enterAnimation, leaveAnimation } from '../../lib/animation'; import { useDispatch, useSelector } from 'react-redux'; import { settingsActions, SettingsState } from '../../store/settings'; import uri from '../../lib/ids'; import _ from 'lodash'; const Settings: React.FC<{}> = () => { const dispatch = useDispatch(); const db = useDB(); useEffect(() => { const initFromDb = async () => { try { const settingsFromDb = await db.get(uri('settings', {})); console.log( `settingsFromDb: ${JSON.stringify(settingsFromDb.settings)}` ); dispatch(settingsActions.saveSettings(settingsFromDb.settings)); } catch {} }; initFromDb(); }, [db, dispatch]); const settingsState = useSelector( (state: { settings: SettingsState }) => state.settings ); useEffect(() => { if (!settingsState.default) { const settingsFromRedux = async () => { const id = uri('settings', {}); var settingsFromDb; try { settingsFromDb = await db.get(id); } catch (error) { console.log( `Error getting settings from db: ${JSON.stringify(error)}` ); settingsFromDb = { _id: id, settings: {} }; } if (!_.isEqual(settingsState, settingsFromDb.settings)) { console.log(`settingsFromRedux: ${JSON.stringify(settingsState)}`); settingsFromDb.settings = settingsState; db.put(settingsFromDb); } }; settingsFromRedux(); } }, [db, settingsState]); const pseudo = useRef(null); const minTimeInterval = useRef(null); const minDistance = useRef(null); const modal = useRef(null); const dismiss = () => { modal.current?.dismiss(); }; const save = () => { dispatch( settingsActions.saveSettings({ user: { pseudo: pseudo.current?.value, }, geolocation: { minTimeInterval: minTimeInterval.current?.value, minDistance: minDistance.current?.value, }, }) ); dismiss(); }; return ( Settings dismiss()}>Close User Pseudo: Geolocation Minimal time interval (ms): Minimal distance (m): save()} > Save dismiss()} > Cancel ); }; export default Settings;