diff --git a/src/client/features/keywords/state/keywordControllerActions.ts b/src/client/features/keywords/state/keywordControllerActions.ts index a2a45fd..64fc67b 100644 --- a/src/client/features/keywords/state/keywordControllerActions.ts +++ b/src/client/features/keywords/state/keywordControllerActions.ts @@ -4,20 +4,17 @@ import { getStandardErrorMessage } from "@/client/lib/error-messages"; import { captureClientEvent } from "@/client/lib/posthog"; import { getLanguageCode } from "@/client/features/keywords/utils"; import type { KeywordResearchRow } from "@/types/keywords"; +import type { SaveKeywordsInput } from "@/types/schemas/keywords"; import type { SortDir, SortField } from "@/client/features/keywords/components"; import type { KeywordResearchControllerInput } from "./useKeywordResearchController"; type SaveExportActionParams = { selectedRows: Set; + rows: KeywordResearchRow[]; filteredRows: KeywordResearchRow[]; input: KeywordResearchControllerInput; saveKeywordsMutate: ( - variables: { - projectId: string; - keywords: string[]; - locationCode: number; - languageCode: string; - }, + variables: SaveKeywordsInput, options: { onSuccess: () => void; onError: (error: unknown) => void; @@ -51,6 +48,7 @@ export function getNextSortParams( export function useSaveAndExportActions(params: SaveExportActionParams) { const { selectedRows, + rows, filteredRows, input, saveKeywordsMutate, @@ -66,12 +64,25 @@ export function useSaveAndExportActions(params: SaveExportActionParams) { }; const confirmSave = () => { + const metrics = rows + .filter((row) => selectedRows.has(row.keyword)) + .map((row) => ({ + keyword: row.keyword, + searchVolume: row.searchVolume, + cpc: row.cpc, + competition: row.competition, + keywordDifficulty: row.keywordDifficulty, + intent: row.intent, + monthlySearches: row.trend, + })); + saveKeywordsMutate( { projectId: input.projectId, keywords: [...selectedRows], locationCode: input.locationCode, languageCode: getLanguageCode(input.locationCode), + metrics, }, { onSuccess: () => { diff --git a/src/client/features/keywords/state/keywordControllerInternals.ts b/src/client/features/keywords/state/keywordControllerInternals.ts index 9abbbad..2891d14 100644 --- a/src/client/features/keywords/state/keywordControllerInternals.ts +++ b/src/client/features/keywords/state/keywordControllerInternals.ts @@ -3,6 +3,7 @@ import { useMutation, useQueryClient } from "@tanstack/react-query"; import { useState } from "react"; import { usePreferredKeywordLocation } from "@/client/features/keywords/hooks/usePreferredKeywordLocation"; import { saveKeywords } from "@/serverFunctions/keywords"; +import type { SaveKeywordsInput } from "@/types/schemas/keywords"; import type { KeywordResearchRow } from "@/types/keywords"; import type { KeywordResearchControllerInput } from "./useKeywordResearchController"; @@ -53,12 +54,7 @@ export function useKeywordSaveMutation(projectId: string) { const queryClient = useQueryClient(); return useMutation({ - mutationFn: (data: { - projectId: string; - keywords: string[]; - locationCode: number; - languageCode: string; - }) => saveKeywords({ data }), + mutationFn: (data: SaveKeywordsInput) => saveKeywords({ data }), onSuccess: () => { void queryClient.invalidateQueries({ queryKey: ["savedKeywords", projectId], diff --git a/src/client/features/keywords/state/useKeywordResearchController.ts b/src/client/features/keywords/state/useKeywordResearchController.ts index 4dbb69f..23d0585 100644 --- a/src/client/features/keywords/state/useKeywordResearchController.ts +++ b/src/client/features/keywords/state/useKeywordResearchController.ts @@ -78,6 +78,7 @@ export function useKeywordResearchController( const { handleSaveKeywords, confirmSave, exportCsv } = useSaveAndExportActions({ selectedRows: state.selectedRows, + rows: state.rows, filteredRows: state.filteredRows, input, saveKeywordsMutate: state.saveMutation.mutate,