fleet/frontend/services/entities/queries.ts
jacobshandling 841a425bb0
UI – Display whitespace of existing, trim names on create/update of team and query names (#22524)
## #22212

- Trim whitespace from names on field blur, form submit, and in API
calls when:
   - Creating a team
   - Updating a team
   - Creating a query
   - Updating a query
- Refactor `AutoResizeInputField` to remove its internal state-based
management of its value, leaving its `value` prop as the single source
of truth for the field's value at all times.

[Loom
demo](https://www.loom.com/share/882f4a803b1540db985c987adbd9f441?sid=67caf100-4711-41a3-971f-bc8f67beeae7)

- [x] Changes file added for user-visible changes in `changes/`, 
- [x] Manual QA for all new/changed functionality

---------

Co-authored-by: Jacob Shandling <jacob@fleetdm.com>
2024-10-03 15:59:10 -07:00

91 lines
2.4 KiB
TypeScript

/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
import sendRequest from "services";
import endpoints from "utilities/endpoints";
import { getErrorReason } from "interfaces/errors";
import { ISelectedTargetsForApi } from "interfaces/target";
import {
ICreateQueryRequestBody,
IModifyQueryRequestBody,
} from "interfaces/schedulable_query";
import { buildQueryStringFromParams } from "utilities/url";
export default {
create: (createQueryRequestBody: ICreateQueryRequestBody) => {
const { QUERIES } = endpoints;
if (createQueryRequestBody.name) {
createQueryRequestBody.name = createQueryRequestBody.name.trim();
}
return sendRequest("POST", QUERIES, createQueryRequestBody);
},
destroy: (id: string | number) => {
const { QUERIES } = endpoints;
const path = `${QUERIES}/id/${id}`;
return sendRequest("DELETE", path);
},
bulkDestroy: (ids: number[]) => {
const { QUERIES } = endpoints;
const path = `${QUERIES}/delete`;
return sendRequest("POST", path, { ids });
},
load: (id: number) => {
const { QUERIES } = endpoints;
const path = `${QUERIES}/${id}`;
return sendRequest("GET", path);
},
loadAll: (teamId?: number, mergeInherited = false) => {
const { QUERIES } = endpoints;
const queryString = buildQueryStringFromParams({
team_id: teamId,
merge_inherited: mergeInherited || null,
});
const path = `${QUERIES}`;
return sendRequest(
"GET",
queryString ? path.concat(`?${queryString}`) : path
);
},
run: async ({
query,
queryId,
selected,
}: {
query: string;
queryId: number | null;
selected: ISelectedTargetsForApi;
}) => {
const { LIVE_QUERY } = endpoints;
try {
const { campaign } = await sendRequest("POST", LIVE_QUERY, {
query,
query_id: queryId,
selected,
});
return Promise.resolve({
...campaign,
hosts_count: {
successful: 0,
failed: 0,
total: 0,
},
});
} catch (e) {
throw new Error(
getErrorReason(e) || `run query: parse server error ${e}`
);
}
},
update: (id: number, updateParams: IModifyQueryRequestBody) => {
const { QUERIES } = endpoints;
const path = `${QUERIES}/${id}`;
if (updateParams.name) {
updateParams.name = updateParams.name.trim();
}
return sendRequest("PATCH", path, updateParams);
},
};