File size: 2,474 Bytes
3c29fcc |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
import { useState, useCallback } from 'react';
import { useDispatch } from 'react-redux';
import { analyzeKeyword as analyzeKeywordThunk } from '../store/reducers/sourcesSlice';
import sourceService from '../services/sourceService';
const useKeywordAnalysis = () => {
const [keyword, setKeyword] = useState('');
const [analysisData, setAnalysisData] = useState(null);
const [patternAnalysis, setPatternAnalysis] = useState(null);
const [loading, setLoading] = useState(false);
const [patternLoading, setPatternLoading] = useState(false);
const [error, setError] = useState(null);
const dispatch = useDispatch();
// Function to call the backend API for keyword analysis
const analyzeKeyword = async () => {
if (!keyword.trim()) {
setError('Please enter a keyword');
return;
}
setLoading(true);
setError(null);
try {
// Call the Redux thunk to analyze keyword trends
const result = await dispatch(analyzeKeywordThunk({ keyword: keyword, date_range: 'monthly' }));
if (analyzeKeywordThunk.fulfilled.match(result)) {
setAnalysisData(result.payload.data);
} else {
setError('Failed to analyze keyword. Please try again.');
}
} catch (err) {
setError('Failed to analyze keyword. Please try again.');
console.error('Keyword analysis error:', err);
} finally {
setLoading(false);
}
};
// Function to call the backend API for keyword frequency pattern analysis
const analyzeKeywordPattern = async () => {
if (!keyword.trim()) {
setError('Please enter a keyword');
return;
}
setPatternLoading(true);
setError(null);
try {
// Call the new service method for frequency pattern analysis
const response = await sourceService.analyzeKeywordPattern({ keyword });
setPatternAnalysis(response.data.data);
return response.data;
} catch (err) {
setError('Failed to analyze keyword frequency pattern. Please try again.');
console.error('Keyword frequency pattern analysis error:', err);
throw err;
} finally {
setPatternLoading(false);
}
};
return {
keyword,
setKeyword,
analysisData,
setAnalysisData,
patternAnalysis,
setPatternAnalysis,
loading,
setLoading,
patternLoading,
setPatternLoading,
error,
setError,
analyzeKeyword,
analyzeKeywordPattern
};
};
export default useKeywordAnalysis; |