mirror of
https://github.com/mountain-loop/yaak.git
synced 2026-04-29 03:57:12 +02:00
Generalized frontend model store (#193)
This commit is contained in:
@@ -1,17 +1,13 @@
|
||||
import type { HttpRequest } from '@yaakapp-internal/models';
|
||||
import type { HttpRequest} from '@yaakapp-internal/models';
|
||||
import { createWorkspaceModel, patchModelById } from '@yaakapp-internal/models';
|
||||
import { jotaiStore } from '../lib/jotai';
|
||||
import { invokeCmd } from '../lib/tauri';
|
||||
import { getActiveWorkspaceId } from './useActiveWorkspace';
|
||||
import { useCreateHttpRequest } from './useCreateHttpRequest';
|
||||
import { useFastMutation } from './useFastMutation';
|
||||
import { useRequestUpdateKey } from './useRequestUpdateKey';
|
||||
import { showToast } from '../lib/toast';
|
||||
import { useUpdateAnyHttpRequest } from './useUpdateAnyHttpRequest';
|
||||
import { activeWorkspaceIdAtom } from './useActiveWorkspace';
|
||||
import { useFastMutation } from './useFastMutation';
|
||||
import { wasUpdatedExternally } from './useRequestUpdateKey';
|
||||
|
||||
export function useImportCurl() {
|
||||
const updateRequest = useUpdateAnyHttpRequest();
|
||||
const createRequest = useCreateHttpRequest();
|
||||
const { wasUpdatedExternally } = useRequestUpdateKey(null);
|
||||
|
||||
return useFastMutation({
|
||||
mutationKey: ['import_curl'],
|
||||
mutationFn: async ({
|
||||
@@ -21,8 +17,8 @@ export function useImportCurl() {
|
||||
overwriteRequestId?: string;
|
||||
command: string;
|
||||
}) => {
|
||||
const workspaceId = getActiveWorkspaceId();
|
||||
const request: HttpRequest = await invokeCmd('cmd_curl_to_request', {
|
||||
const workspaceId = jotaiStore.get(activeWorkspaceIdAtom);
|
||||
const importedRequest: HttpRequest = await invokeCmd('cmd_curl_to_request', {
|
||||
command,
|
||||
workspaceId,
|
||||
});
|
||||
@@ -30,21 +26,18 @@ export function useImportCurl() {
|
||||
let verb;
|
||||
if (overwriteRequestId == null) {
|
||||
verb = 'Created';
|
||||
await createRequest.mutateAsync(request);
|
||||
await createWorkspaceModel(importedRequest);
|
||||
} else {
|
||||
verb = 'Updated';
|
||||
await updateRequest.mutateAsync({
|
||||
id: overwriteRequestId,
|
||||
update: (r: HttpRequest) => ({
|
||||
...request,
|
||||
id: r.id,
|
||||
createdAt: r.createdAt,
|
||||
workspaceId: r.workspaceId,
|
||||
folderId: r.folderId,
|
||||
name: r.name,
|
||||
sortPriority: r.sortPriority,
|
||||
}),
|
||||
});
|
||||
await patchModelById(importedRequest.model, overwriteRequestId, (r: HttpRequest) => ({
|
||||
...importedRequest,
|
||||
id: r.id,
|
||||
createdAt: r.createdAt,
|
||||
workspaceId: r.workspaceId,
|
||||
folderId: r.folderId,
|
||||
name: r.name,
|
||||
sortPriority: r.sortPriority,
|
||||
}));
|
||||
|
||||
setTimeout(() => wasUpdatedExternally(overwriteRequestId), 100);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user