Adding Subscription Pages
Guide for adding new subscription page components to your Fireact SaaS application
Overview
The subscription system is built with several key components:
- Route configuration in saasConfig.json
- Permission-based access control
- i18n translation support
- Component integration in the subscription layout
Step 1: Create the Component
- Create a new component file in the
src/components
directory:
import { useTranslation } from 'react-i18next';
import { useConfig } from '@fireact.dev/core';
import { useSubscription } from '../contexts/SubscriptionContext';
export default function YourNewComponent() {
const { subscription, loading, error } = useSubscription();
const { t } = useTranslation();
const config = useConfig();
if (loading) {
return (
<div className="flex justify-center items-center h-64">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-indigo-600"></div>
</div>
);
}
if (error || !subscription) {
return <Navigate to={config.pages.home} replace />;
}
return (
// Your component JSX
);
}
Step 2: Add Route Configuration
- Add the new page route to
saasConfig.json
under thepages
section:
{
"pages": {
"yourNewPage": "/subscription/:id/your-new-page"
}
}
Step 3: Add the Route to App.tsx
- Import your component:
import YourNewComponent from './components/YourNewComponent';
- Add the route within the subscription layout section:
<Route path={paths.yourNewPage} element={
<ProtectedSubscriptionRoute requiredPermissions={['your-required-permission']}>
<YourNewComponent />
</ProtectedSubscriptionRoute>
} />
Step 4: Configure Permissions
- Add any new permissions to
saasConfig.json
under thepermissions
section:
{
"permissions": {
"your-permission": {
"label": "Your Permission",
"default": false,
"admin": false
}
}
}
The permission system has three levels:
access
: Basic access permission (default: true)admin
: Administrator permission for managing users and settingsowner
: Highest level for billing and critical operations
Choose the appropriate permission level for your component, or add a new permission level:
- Use
access
for general subscription features - Use
admin
for management features - Use
owner
for billing/critical operations
Step 5: Add i18n Translations
- Add new translation keys to your locale files (e.g.,
src/i18n/locales/saas/en.ts
):
{
"yourComponent": {
"title": "Your Component Title",
"description": "Your component description",
// Add other needed translations
}
}
- Use translations in your component:
const { t } = useTranslation();
// ...
<h2>{t('yourComponent.title')}</h2>
Step 6: Add Navigation (Optional)
- To add navigation to your new page, update the SubscriptionMenuItems components:
// In SubscriptionDesktopMenu.tsx
<MenuItem
to={`/subscription/${subscription.id}/your-new-page`}
icon={YourIcon}
label={t('yourComponent.title')}
/>
Best Practices
Permission Handling:
- Always wrap your routes with
ProtectedSubscriptionRoute
- Choose appropriate permission levels based on functionality
- Use the most restrictive permission level necessary
- Always wrap your routes with
Component Structure:
- Handle loading states
- Handle error states
- Use the subscription context for data
- Implement proper navigation
Translations:
- Keep translation keys organized by component
- Use descriptive key names
- Provide translations for all supported languages
Route Naming:
- Use consistent naming patterns
- Include the subscription ID parameter when needed
- Group related functionality under similar paths
Example: Adding a Reports Page
Here’s a complete example of adding a reports page:
- Create
src/components/SubscriptionReports.tsx
:
import { Navigate } from 'react-router-dom';
import { useTranslation } from 'react-i18next';
import { useConfig } from '@fireact.dev/core';
import { useSubscription } from '../contexts/SubscriptionContext';
export default function SubscriptionReports() {
const { subscription, loading, error } = useSubscription();
const { t } = useTranslation();
const config = useConfig();
if (loading) {
return (
<div className="flex justify-center items-center h-64">
<div className="animate-spin rounded-full h-8 w-8 border-b-2 border-indigo-600"></div>
</div>
);
}
if (error || !subscription) {
return <Navigate to={config.pages.home} replace />;
}
return (
<div className="space-y-6">
<div className="bg-white rounded-lg shadow">
<div className="border-b border-gray-200">
<h2 className="text-xl font-semibold p-6">
{t('reports.title')}
</h2>
</div>
<div className="p-6">
{/* Reports content */}
</div>
</div>
</div>
);
}
- Update
saasConfig.json
:
{
"pages": {
"reports": "/subscription/:id/reports"
},
"permissions": {
"view-reports": {
"label": "View Reports",
"default": false,
"admin": true
}
}
}
- Update
App.tsx
:
<Route path={paths.reports} element={
<ProtectedSubscriptionRoute requiredPermissions={['view-reports']}>
<SubscriptionReports />
</ProtectedSubscriptionRoute>
} />
- Add translations:
// in en.ts
{
"reports": {
"title": "Subscription Reports",
"description": "View detailed reports for your subscription"
}
}
This documentation provides a comprehensive guide for adding new subscription pages while maintaining consistency with the existing architecture and best practices.
Feedback
Was this page helpful?
Glad to hear it! Please tell us how we can improve.
Sorry to hear that. Please tell us how we can improve.