102 lines
3.0 KiB
TypeScript
Raw Normal View History

2021-02-16 21:51:59 -08:00
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import { createSlice } from '@reduxjs/toolkit';
import { useQuery } from 'react-query';
import Client, { AccountInfo, ErrorInfo, MapInfo } from '../classes/client';
import MockClient from '../classes/client/mock-client';
import RestClient from '../classes/client/rest-client';
import { useSelector } from 'react-redux';
2021-02-13 10:52:15 -08:00
2021-02-03 14:27:32 -08:00
interface ConfigInfo {
apiBaseUrl: string;
2020-12-25 21:39:54 -08:00
}
2021-02-03 14:27:32 -08:00
class RutimeConfig {
private config: ConfigInfo;
2021-02-22 22:37:29 -08:00
load() {
// Config can be inserted in the html page to define the global properties ...
// eslint-disable-next-line @typescript-eslint/no-explicit-any
this.config = (window as any).serverconfig;
return this;
2021-02-22 22:37:29 -08:00
}
2021-02-03 14:27:32 -08:00
2021-02-22 22:37:29 -08:00
buildClient(): Client {
let result: Client;
2021-02-22 22:37:29 -08:00
if (this.config) {
result = new RestClient(this.config.apiBaseUrl, () => {
sessionExpired();
});
console.log('Service using rest client. ' + JSON.stringify(this.config));
2021-02-22 22:37:29 -08:00
} else {
console.log('Warning:Service using mockservice client');
result = new MockClient();
2021-02-22 22:37:29 -08:00
}
return result;
2020-12-25 21:39:54 -08:00
}
}
2021-02-04 23:05:46 -08:00
export interface ClientStatus {
state: 'healthy' | 'session-expired';
msg?: string;
2021-02-04 23:05:46 -08:00
}
export interface ClientState {
instance: Client;
status: ClientStatus;
2020-12-25 21:39:54 -08:00
}
2021-02-04 23:05:46 -08:00
const initialState: ClientState = {
2021-02-22 22:37:29 -08:00
instance: new RutimeConfig().load().buildClient(),
status: { state: 'healthy' },
};
2020-12-25 21:39:54 -08:00
2021-02-04 23:05:46 -08:00
export const clientSlice = createSlice({
2021-02-22 22:37:29 -08:00
name: 'client',
initialState: initialState,
reducers: {
sessionExpired(state) {
state.status = {
state: 'session-expired',
msg: 'Sessions has expired. You need to login again',
};
2021-02-22 22:37:29 -08:00
},
},
});
2021-02-13 10:52:15 -08:00
type MapLoadResult = {
isLoading: boolean;
error: ErrorInfo | null;
map: MapInfo | null;
};
2021-02-13 10:52:15 -08:00
export const fetchMapById = (id: number): MapLoadResult => {
const client: Client = useSelector(activeInstance);
2021-02-22 22:37:29 -08:00
const { isLoading, error, data } = useQuery<unknown, ErrorInfo, MapInfo[]>('maps', () => {
return client.fetchAllMaps();
});
2021-02-22 22:37:29 -08:00
const result = data?.find((m) => m.id == id);
const map = result || null;
return { isLoading: isLoading, error: error, map: map };
};
2021-02-13 10:52:15 -08:00
export const fetchAccount = (): AccountInfo | undefined => {
const client: Client = useSelector(activeInstance);
2021-02-22 22:37:29 -08:00
const { data } = useQuery<unknown, ErrorInfo, AccountInfo>('account', () => {
return client.fetchAccountInfo();
});
return data;
};
2021-02-13 10:52:15 -08:00
2021-02-16 21:51:59 -08:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2021-02-01 22:15:32 -08:00
export const activeInstance = (state: any): Client => {
return state.client.instance;
};
2021-02-04 23:05:46 -08:00
2021-02-16 21:51:59 -08:00
// eslint-disable-next-line @typescript-eslint/no-explicit-any
2021-02-04 23:05:46 -08:00
export const activeInstanceStatus = (state: any): ClientStatus => {
return state.client.status;
};
2021-02-03 14:27:32 -08:00
export const { sessionExpired } = clientSlice.actions;
export default clientSlice.reducer;