2021-01-05 16:17:44 +00:00
|
|
|
import http, { FractalResponseData, getPaginationSet, PaginatedResult } from '@/api/http';
|
2021-07-19 20:34:10 +00:00
|
|
|
import { useContext } from 'react';
|
2021-01-05 16:17:44 +00:00
|
|
|
import useSWR from 'swr';
|
2021-07-19 20:37:09 +00:00
|
|
|
import { createContext } from '@/api/admin';
|
2021-02-16 01:48:10 +00:00
|
|
|
import { Database, rawDataToDatabase } from '@/api/admin/databases/getDatabases';
|
2021-01-06 16:52:07 +00:00
|
|
|
import { Location, rawDataToLocation } from '@/api/admin/locations/getLocations';
|
2021-01-05 16:17:44 +00:00
|
|
|
|
|
|
|
export interface Node {
|
|
|
|
id: number;
|
|
|
|
uuid: string;
|
|
|
|
public: boolean;
|
|
|
|
name: string;
|
|
|
|
description: string | null;
|
|
|
|
locationId: number;
|
2021-02-16 01:48:10 +00:00
|
|
|
databaseHostId: number | null;
|
2021-01-05 16:17:44 +00:00
|
|
|
fqdn: string;
|
2021-02-11 17:21:49 +00:00
|
|
|
listenPortHTTP: number;
|
|
|
|
publicPortHTTP: number;
|
|
|
|
listenPortSFTP: number;
|
|
|
|
publicPortSFTP: number;
|
2021-01-05 16:17:44 +00:00
|
|
|
scheme: string;
|
|
|
|
behindProxy: boolean;
|
|
|
|
maintenanceMode: boolean;
|
|
|
|
memory: number;
|
|
|
|
memoryOverallocate: number;
|
|
|
|
disk: number;
|
|
|
|
diskOverallocate: number;
|
|
|
|
uploadSize: number;
|
|
|
|
daemonBase: string;
|
|
|
|
createdAt: Date;
|
|
|
|
updatedAt: Date;
|
2021-01-06 16:52:07 +00:00
|
|
|
|
|
|
|
relations: {
|
2021-02-16 01:48:10 +00:00
|
|
|
databaseHost: Database | undefined;
|
2021-01-06 16:52:07 +00:00
|
|
|
location: Location | undefined;
|
2021-07-19 20:34:10 +00:00
|
|
|
}
|
2021-01-05 16:17:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
export const rawDataToNode = ({ attributes }: FractalResponseData): Node => ({
|
|
|
|
id: attributes.id,
|
|
|
|
uuid: attributes.uuid,
|
|
|
|
public: attributes.public,
|
|
|
|
name: attributes.name,
|
|
|
|
description: attributes.description,
|
|
|
|
locationId: attributes.location_id,
|
2021-02-16 01:48:10 +00:00
|
|
|
databaseHostId: attributes.database_host_id,
|
2021-01-05 16:17:44 +00:00
|
|
|
fqdn: attributes.fqdn,
|
2021-02-11 17:21:49 +00:00
|
|
|
listenPortHTTP: attributes.listen_port_http,
|
|
|
|
publicPortHTTP: attributes.public_port_http,
|
|
|
|
listenPortSFTP: attributes.listen_port_sftp,
|
|
|
|
publicPortSFTP: attributes.public_port_sftp,
|
2021-01-05 16:17:44 +00:00
|
|
|
scheme: attributes.scheme,
|
|
|
|
behindProxy: attributes.behind_proxy,
|
|
|
|
maintenanceMode: attributes.maintenance_mode,
|
|
|
|
memory: attributes.memory,
|
|
|
|
memoryOverallocate: attributes.memory_overallocate,
|
|
|
|
disk: attributes.disk,
|
|
|
|
diskOverallocate: attributes.disk_overallocate,
|
|
|
|
uploadSize: attributes.upload_size,
|
|
|
|
daemonBase: attributes.daemon_base,
|
|
|
|
createdAt: new Date(attributes.created_at),
|
|
|
|
updatedAt: new Date(attributes.updated_at),
|
2021-01-06 16:52:07 +00:00
|
|
|
|
|
|
|
relations: {
|
2021-02-16 20:03:14 +00:00
|
|
|
// eslint-disable-next-line camelcase
|
|
|
|
databaseHost: attributes.relationships?.database_host !== undefined && attributes.relationships?.database_host.object !== 'null_resource' ? rawDataToDatabase(attributes.relationships.database_host as FractalResponseData) : undefined,
|
2021-01-06 16:52:07 +00:00
|
|
|
location: attributes.relationships?.location !== undefined ? rawDataToLocation(attributes.relationships.location as FractalResponseData) : undefined,
|
|
|
|
},
|
2021-01-05 16:17:44 +00:00
|
|
|
});
|
|
|
|
|
2021-05-20 22:00:46 +00:00
|
|
|
export interface Filters {
|
2021-07-14 22:43:59 +00:00
|
|
|
id?: string;
|
2021-05-20 22:00:46 +00:00
|
|
|
uuid?: string;
|
|
|
|
name?: string;
|
|
|
|
image?: string;
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
external_id?: string;
|
|
|
|
/* eslint-enable camelcase */
|
|
|
|
}
|
|
|
|
|
2021-07-19 20:34:10 +00:00
|
|
|
export const Context = createContext<Filters>();
|
2021-01-05 16:17:44 +00:00
|
|
|
|
2021-01-06 16:52:07 +00:00
|
|
|
export default (include: string[] = []) => {
|
2021-05-20 22:00:46 +00:00
|
|
|
const { page, filters, sort, sortDirection } = useContext(Context);
|
|
|
|
|
|
|
|
const params = {};
|
|
|
|
if (filters !== null) {
|
|
|
|
Object.keys(filters).forEach(key => {
|
|
|
|
// @ts-ignore
|
|
|
|
params['filter[' + key + ']'] = filters[key];
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (sort !== null) {
|
|
|
|
// @ts-ignore
|
|
|
|
params.sort = (sortDirection ? '-' : '') + sort;
|
|
|
|
}
|
2021-01-05 16:17:44 +00:00
|
|
|
|
2021-05-20 22:00:46 +00:00
|
|
|
return useSWR<PaginatedResult<Node>>([ 'nodes', page, filters, sort, sortDirection ], async () => {
|
|
|
|
const { data } = await http.get('/api/application/nodes', { params: { include: include.join(','), page, ...params } });
|
2021-01-05 16:17:44 +00:00
|
|
|
|
|
|
|
return ({
|
|
|
|
items: (data.data || []).map(rawDataToNode),
|
|
|
|
pagination: getPaginationSet(data.meta.pagination),
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|