ui(admin): add allocation table, implement allocation creator
This commit is contained in:
parent
6b746440fc
commit
3c01dbbcc5
14 changed files with 397 additions and 87 deletions
|
@ -0,0 +1,16 @@
|
|||
import http from '@/api/http';
|
||||
import { Allocation, rawDataToAllocation } from '@/api/admin/nodes/allocations/getAllocations';
|
||||
|
||||
export interface Values {
|
||||
ip: string;
|
||||
ports: number[];
|
||||
alias?: string;
|
||||
}
|
||||
|
||||
export default (id: string | number, values: Values, include: string[] = []): Promise<Allocation[]> => {
|
||||
return new Promise((resolve, reject) => {
|
||||
http.post(`/api/application/nodes/${id}/allocations`, values, { params: { include: include.join(',') } })
|
||||
.then(({ data }) => resolve((data || []).map(rawDataToAllocation)))
|
||||
.catch(reject);
|
||||
});
|
||||
};
|
|
@ -0,0 +1,65 @@
|
|||
import { Server, rawDataToServer } from '@/api/admin/servers/getServers';
|
||||
import http, { FractalResponseData, getPaginationSet, PaginatedResult } from '@/api/http';
|
||||
import { useContext } from 'react';
|
||||
import useSWR from 'swr';
|
||||
import { createContext } from '@/api/admin';
|
||||
|
||||
export interface Allocation {
|
||||
id: number;
|
||||
ip: string;
|
||||
port: number;
|
||||
alias: string | null;
|
||||
serverId: number | null;
|
||||
assigned: boolean;
|
||||
|
||||
relations: {
|
||||
server?: Server;
|
||||
}
|
||||
}
|
||||
|
||||
export const rawDataToAllocation = ({ attributes }: FractalResponseData): Allocation => ({
|
||||
id: attributes.id,
|
||||
ip: attributes.ip,
|
||||
port: attributes.port,
|
||||
alias: attributes.ip_alias || null,
|
||||
serverId: attributes.server_id,
|
||||
assigned: attributes.assigned,
|
||||
|
||||
relations: {
|
||||
server: attributes.relationships?.server?.object === 'server' ? rawDataToServer(attributes.relationships.server as FractalResponseData) : undefined,
|
||||
},
|
||||
});
|
||||
|
||||
export interface Filters {
|
||||
id?: string;
|
||||
ip?: string;
|
||||
port?: string;
|
||||
}
|
||||
|
||||
export const Context = createContext<Filters>();
|
||||
|
||||
export default (id: string | number, include: string[] = []) => {
|
||||
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;
|
||||
}
|
||||
|
||||
return useSWR<PaginatedResult<Allocation>>([ 'allocations', page, filters, sort, sortDirection ], async () => {
|
||||
const { data } = await http.get(`/api/application/nodes/${id}/allocations`, { params: { include: include.join(','), page, ...params } });
|
||||
|
||||
return ({
|
||||
items: (data.data || []).map(rawDataToAllocation),
|
||||
pagination: getPaginationSet(data.meta.pagination),
|
||||
});
|
||||
});
|
||||
};
|
|
@ -47,9 +47,9 @@ export interface Server {
|
|||
updatedAt: Date;
|
||||
|
||||
relations: {
|
||||
egg: Egg | undefined;
|
||||
node: Node | undefined;
|
||||
user: User | undefined;
|
||||
egg?: Egg;
|
||||
node?: Node;
|
||||
user?: User;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -94,11 +94,11 @@ export const rawDataToServer = ({ attributes }: FractalResponseData): Server =>
|
|||
updatedAt: new Date(attributes.updated_at),
|
||||
|
||||
relations: {
|
||||
egg: attributes.relationships?.egg !== undefined ? rawDataToEgg(attributes.relationships.egg as FractalResponseData) : undefined,
|
||||
node: attributes.relationships?.node !== undefined ? rawDataToNode(attributes.relationships.node as FractalResponseData) : undefined,
|
||||
user: attributes.relationships?.user !== undefined ? rawDataToUser(attributes.relationships.user as FractalResponseData) : undefined,
|
||||
egg: attributes.relationships?.egg?.object === 'egg' ? rawDataToEgg(attributes.relationships.egg as FractalResponseData) : undefined,
|
||||
node: attributes.relationships?.node?.object === 'node' ? rawDataToNode(attributes.relationships.node as FractalResponseData) : undefined,
|
||||
user: attributes.relationships?.user?.object === 'user' ? rawDataToUser(attributes.relationships.user as FractalResponseData) : undefined,
|
||||
},
|
||||
});
|
||||
}) as Server;
|
||||
|
||||
export interface Filters {
|
||||
id?: string;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue