193 lines
No EOL
7.1 KiB
TypeScript
193 lines
No EOL
7.1 KiB
TypeScript
import React, { useState } from 'react';
|
|
import { Link, useNavigate } from 'react-router-dom';
|
|
import { useMutation, useQuery } from '@tanstack/react-query';
|
|
import { apiClient, type FeedRequest, type Language } from '../../../lib/api';
|
|
|
|
const FeedStep: React.FC = () => {
|
|
const navigate = useNavigate();
|
|
const [formData, setFormData] = useState<FeedRequest>({
|
|
name: '',
|
|
url: '',
|
|
type: 'rss',
|
|
language_id: 0,
|
|
description: ''
|
|
});
|
|
const [errors, setErrors] = useState<Record<string, string[]>>({});
|
|
|
|
// Get onboarding options (languages)
|
|
const { data: options, isLoading: optionsLoading } = useQuery({
|
|
queryKey: ['onboarding-options'],
|
|
queryFn: () => apiClient.getOnboardingOptions()
|
|
});
|
|
|
|
const createFeedMutation = useMutation({
|
|
mutationFn: (data: FeedRequest) => apiClient.createFeedForOnboarding(data),
|
|
onSuccess: () => {
|
|
navigate('/onboarding/channel');
|
|
},
|
|
onError: (error: any) => {
|
|
if (error.response?.data?.errors) {
|
|
setErrors(error.response.data.errors);
|
|
} else {
|
|
setErrors({ general: [error.response?.data?.message || 'An error occurred'] });
|
|
}
|
|
}
|
|
});
|
|
|
|
const handleSubmit = (e: React.FormEvent) => {
|
|
e.preventDefault();
|
|
setErrors({});
|
|
createFeedMutation.mutate(formData);
|
|
};
|
|
|
|
const handleChange = (field: keyof FeedRequest, value: string | number) => {
|
|
setFormData(prev => ({ ...prev, [field]: value }));
|
|
// Clear field error when user starts typing
|
|
if (errors[field]) {
|
|
setErrors(prev => ({ ...prev, [field]: [] }));
|
|
}
|
|
};
|
|
|
|
if (optionsLoading) {
|
|
return <div className="text-center">Loading...</div>;
|
|
}
|
|
|
|
return (
|
|
<div className="text-center mb-8">
|
|
<h1 className="text-2xl font-bold text-gray-900 mb-2">Add Your First Feed</h1>
|
|
<p className="text-gray-600">
|
|
Add a RSS feed or website to monitor for new articles
|
|
</p>
|
|
|
|
{/* Progress indicator */}
|
|
<div className="flex justify-center mt-6 space-x-2">
|
|
<div className="w-6 h-6 bg-green-500 text-white rounded-full flex items-center justify-center text-xs font-semibold">✓</div>
|
|
<div className="w-6 h-6 bg-blue-500 text-white rounded-full flex items-center justify-center text-xs font-semibold">2</div>
|
|
<div className="w-6 h-6 bg-gray-300 text-gray-600 rounded-full flex items-center justify-center text-xs font-semibold">3</div>
|
|
<div className="w-6 h-6 bg-gray-300 text-gray-600 rounded-full flex items-center justify-center text-xs font-semibold">4</div>
|
|
</div>
|
|
|
|
<form onSubmit={handleSubmit} className="space-y-6 mt-8 text-left">
|
|
{errors.general && (
|
|
<div className="p-3 bg-red-50 border border-red-200 rounded-md">
|
|
<p className="text-red-600 text-sm">{errors.general[0]}</p>
|
|
</div>
|
|
)}
|
|
|
|
<div>
|
|
<label htmlFor="name" className="block text-sm font-medium text-gray-700 mb-2">
|
|
Feed Name
|
|
</label>
|
|
<input
|
|
type="text"
|
|
id="name"
|
|
value={formData.name}
|
|
onChange={(e) => handleChange('name', e.target.value)}
|
|
placeholder="My News Feed"
|
|
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
|
|
required
|
|
/>
|
|
{errors.name && (
|
|
<p className="text-red-600 text-sm mt-1">{errors.name[0]}</p>
|
|
)}
|
|
</div>
|
|
|
|
<div>
|
|
<label htmlFor="url" className="block text-sm font-medium text-gray-700 mb-2">
|
|
Feed URL
|
|
</label>
|
|
<input
|
|
type="url"
|
|
id="url"
|
|
value={formData.url}
|
|
onChange={(e) => handleChange('url', e.target.value)}
|
|
placeholder="https://example.com/rss.xml"
|
|
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
|
|
required
|
|
/>
|
|
{errors.url && (
|
|
<p className="text-red-600 text-sm mt-1">{errors.url[0]}</p>
|
|
)}
|
|
</div>
|
|
|
|
<div>
|
|
<label htmlFor="type" className="block text-sm font-medium text-gray-700 mb-2">
|
|
Feed Type
|
|
</label>
|
|
<select
|
|
id="type"
|
|
value={formData.type}
|
|
onChange={(e) => handleChange('type', e.target.value)}
|
|
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
|
|
required
|
|
>
|
|
<option value="">Select feed type</option>
|
|
<option value="rss">RSS Feed</option>
|
|
<option value="website">Website</option>
|
|
</select>
|
|
{errors.type && (
|
|
<p className="text-red-600 text-sm mt-1">{errors.type[0]}</p>
|
|
)}
|
|
</div>
|
|
|
|
<div>
|
|
<label htmlFor="language_id" className="block text-sm font-medium text-gray-700 mb-2">
|
|
Language
|
|
</label>
|
|
<select
|
|
id="language_id"
|
|
value={formData.language_id}
|
|
onChange={(e) => handleChange('language_id', parseInt(e.target.value))}
|
|
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
|
|
required
|
|
>
|
|
<option value="">Select language</option>
|
|
{options?.languages.map((language: Language) => (
|
|
<option key={language.id} value={language.id}>
|
|
{language.name}
|
|
</option>
|
|
))}
|
|
</select>
|
|
{errors.language_id && (
|
|
<p className="text-red-600 text-sm mt-1">{errors.language_id[0]}</p>
|
|
)}
|
|
</div>
|
|
|
|
<div>
|
|
<label htmlFor="description" className="block text-sm font-medium text-gray-700 mb-2">
|
|
Description (Optional)
|
|
</label>
|
|
<textarea
|
|
id="description"
|
|
rows={3}
|
|
value={formData.description || ''}
|
|
onChange={(e) => handleChange('description', e.target.value)}
|
|
placeholder="Brief description of this feed"
|
|
className="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
|
|
/>
|
|
{errors.description && (
|
|
<p className="text-red-600 text-sm mt-1">{errors.description[0]}</p>
|
|
)}
|
|
</div>
|
|
|
|
<div className="flex justify-between">
|
|
<Link
|
|
to="/onboarding/platform"
|
|
className="px-4 py-2 text-gray-600 hover:text-gray-800 transition duration-200"
|
|
>
|
|
← Back
|
|
</Link>
|
|
<button
|
|
type="submit"
|
|
disabled={createFeedMutation.isPending}
|
|
className="bg-blue-600 text-white py-2 px-6 rounded-md hover:bg-blue-700 transition duration-200 disabled:opacity-50"
|
|
>
|
|
{createFeedMutation.isPending ? 'Creating...' : 'Continue'}
|
|
</button>
|
|
</div>
|
|
</form>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
export default FeedStep; |