Skip to content

Commit

Permalink
test the sync URL
Browse files Browse the repository at this point in the history
  • Loading branch information
mark-prins committed Jul 28, 2024
1 parent 9309097 commit b67ba26
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 16 deletions.
31 changes: 24 additions & 7 deletions src/features/Sync/SyncSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,11 @@ const reducers = {
tryIntegrating: () => {},
tryTestConnection: {
prepare: (
loginUrl: string,
serverUrl: string,
username: string,
password: string
): PrepareActionReturn<AuthenticateActionPayload> => ({
payload: { username, password, loginUrl },
payload: { username, password, serverUrl },
}),
reducer: () => {},
},
Expand Down Expand Up @@ -90,8 +90,8 @@ const reducers = {
},
},
authenticate: {
prepare: (loginUrl: string, username: string, password: string) => ({
payload: { loginUrl, username, password },
prepare: (serverUrl: string, username: string, password: string) => ({
payload: { serverUrl, username, password },
}),
reducer: () => {},
},
Expand Down Expand Up @@ -177,11 +177,12 @@ const SyncSelector = {
};

function* authenticate({
payload: { loginUrl, username, password },
payload: { serverUrl, username, password },
}: PayloadAction<AuthenticateActionPayload>): SagaIterator {
const syncOutManager: SyncOutManager = yield call(getDependency, 'syncOutManager');

try {
const loginUrl = `${serverUrl}/${ENDPOINT.LOGIN}`;
yield call(syncOutManager.login, loginUrl, username, password);
yield put(SyncAction.authenticateSuccess());
} catch (e) {
Expand Down Expand Up @@ -322,12 +323,28 @@ function* enablePassiveSync(): SagaIterator {
}

function* tryTestConnection({
payload: { loginUrl, username, password },
payload: { serverUrl, username, password },
}: PayloadAction<AuthenticateActionPayload>): SagaIterator {
const syncOutManager: SyncOutManager = yield call(getDependency, 'syncOutManager');
ToastAndroid.show('Testing Connection', ToastAndroid.SHORT);
try {
yield call(syncOutManager.login, loginUrl, username, password);
const loginUrl = `${serverUrl}/${ENDPOINT.LOGIN}`;
const loginResponse = yield call(syncOutManager.login, loginUrl, username, password);
const { success } = loginResponse?.data ?? {};

if (success !== true) {
yield put(SyncAction.testConnectionFailure('Invalid response returned from login'));
return;
}

// login returned correctly, now test for a valid response from the sensor endpoint
const sensorUrl = `${serverUrl}/${ENDPOINT.SENSOR}`;
const sensorResponse = yield call(syncOutManager.syncSensors, sensorUrl, []);
const sensors = sensorResponse?.data;
if (!Array.isArray(sensors) || sensors.length !== 0) {
yield put(SyncAction.testConnectionFailure('Does not appear to be a cold chain server'));
return;
}
yield put(SyncAction.testConnectionSuccess());
} catch (e) {
yield put(SyncAction.testConnectionFailure((e as Error).message));
Expand Down
2 changes: 1 addition & 1 deletion src/features/Sync/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export interface UpdateSyncErrorActionPayload {
}

export interface AuthenticateActionPayload {
loginUrl: string;
serverUrl: string;
username: string;
password: string;
}
Expand Down
9 changes: 1 addition & 8 deletions src/ui/screens/settings/SyncSettingsScreen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import {
import { SettingAction, SettingSelector, SyncAction, SyncSelector } from '~features';
import { t } from '~common/translations';
import { useFormatter } from '~hooks';
import { ENDPOINT } from '~features/Sync/SyncSlice';
import { QRCodeScanner } from './QRCodeScanner';
import { IconButton } from '~components/buttons';
import { ToastAndroid } from 'react-native';
Expand Down Expand Up @@ -120,13 +119,7 @@ export const SyncSettingsScreen: FC = () => {
label={t('TEST_CONNECTION')}
subtext={t('TEST_CONNECTION_SUBTEXT')}
onPress={() =>
dispatch(
SyncAction.tryTestConnection(
`${serverUrl}/${ENDPOINT.LOGIN}`,
authUsername,
authPassword
)
)
dispatch(SyncAction.tryTestConnection(serverUrl, authUsername, authPassword))
}
/>
<SettingsGroup title="Info">
Expand Down

0 comments on commit b67ba26

Please sign in to comment.