diff --git a/src/extensions/activities/tota/TOTAExtension.js b/src/extensions/activities/tota/TOTAExtension.js index 1976dc48..fd3720ce 100644 --- a/src/extensions/activities/tota/TOTAExtension.js +++ b/src/extensions/activities/tota/TOTAExtension.js @@ -25,6 +25,7 @@ import { totaFindAllByLocation, totaFindOneByReference, registerTOTADataFile } f import { TOTAActivityOptions } from './TOTAActivityOptions' import { TOTALoggingControl } from './TOTALoggingControl' import { TOTAPostSelfSpot } from './TOTAPostSelfSpot' +import { TOTAPostOtherSpot } from './TOTAPostOtherSpot' const Extension = { ...Info, @@ -55,6 +56,7 @@ const ActivityHook = { } }, postSelfSpot: TOTAPostSelfSpot, + postOtherSpot: TOTAPostOtherSpot, Options: TOTAActivityOptions, generalHuntingType: ({ operation, settings }) => Info.huntingType, sampleOperations: ({ t, settings, callInfo }) => { diff --git a/src/extensions/activities/tota/TOTAPostOtherSpot.js b/src/extensions/activities/tota/TOTAPostOtherSpot.js new file mode 100644 index 00000000..30085b9f --- /dev/null +++ b/src/extensions/activities/tota/TOTAPostOtherSpot.js @@ -0,0 +1,49 @@ +/* + * Copyright ©️ 2024 Sebastian Delmont + * + * This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. + * If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/. + */ + +import { Alert } from 'react-native' + +import { reportError } from '../../../distro' +import GLOBAL from '../../../GLOBAL' + +import { findRef } from '../../../tools/refTools' +import { apiTOTA } from '../../../store/apis/apiTOTA' +import { Info } from './TOTAInfo' + +export const TOTAPostOtherSpot = ({ t, comments, qso }) => async (dispatch) => { + if (GLOBAL?.flags?.services?.tota === false) return false + + const ref = findRef(qso, Info.huntingType) + + if (ref && ref.ref) { + const spot = { + callsign: qso.their.call, + frequency: qso.freq, + mode: qso.mode, + tower_ref: ref.ref, + comment: comments, + } + + try { + const apiPromise = await dispatch(apiTOTA.endpoints.spot.initiate(spot, { forceRefetch: true })) + await Promise.all(dispatch(apiTOTA.util.getRunningQueriesThunk())) + const apiResults = await dispatch((_dispatch, _getState) => apiTOTA.endpoints.spot.select(spot)(_getState())) + apiPromise.unsubscribe && apiPromise.unsubscribe() + // Don't worry about duplicates + if (apiResults?.error && !apiResults?.error?.data?.message?.match(/Duplicate self-spot/)) { + Alert.alert(t('extensions.activities.tota.postSpotAPI.error', 'Error posting TOTA spot'), + t('extensions.tota.postSpotAPI.serverResponse', 'Server responded with status {{status}} {{message}}', { status: apiResults.error?.status, message: apiResults.error?.data?.message })) + return false + } + } catch (error) { + Alert.alert(t('extensions.tota.postSpotAPI.error', 'Error posting TOTA spot'), error.message) + reportError('Error posting TOTA spot', error) + return false + } + return true + } +}