51 lines
1.5 KiB
JavaScript
51 lines
1.5 KiB
JavaScript
import { createContext, useContext, useState } from 'react';
|
|
|
|
const SubscriptionContext = createContext(null);
|
|
|
|
const PLANS = [
|
|
{ id: 'basic', name: 'Basic', price: 9.99, description: '2 livres par mois' },
|
|
{ id: 'standard', name: 'Standard', price: 14.99, description: '5 livres par mois' },
|
|
{ id: 'premium', name: 'Premium', price: 24.99, description: 'Livres illimités par mois' },
|
|
];
|
|
|
|
export { PLANS };
|
|
|
|
export function SubscriptionProvider({ children }) {
|
|
const [subscription, setSubscription] = useState(() => {
|
|
const saved = localStorage.getItem('subscription');
|
|
return saved ? JSON.parse(saved) : null;
|
|
});
|
|
|
|
function subscribe(planId, phoneNumber) {
|
|
const plan = PLANS.find(p => p.id === planId);
|
|
const newSubscription = {
|
|
subscriptionId: crypto.randomUUID(),
|
|
planId,
|
|
planName: plan.name,
|
|
price: plan.price,
|
|
phoneNumber,
|
|
status: 'ACTIVE',
|
|
startDate: new Date().toISOString(),
|
|
};
|
|
setSubscription(newSubscription);
|
|
localStorage.setItem('subscription', JSON.stringify(newSubscription));
|
|
return newSubscription;
|
|
}
|
|
|
|
function cancelSubscription() {
|
|
const updated = { ...subscription, status: 'CANCELLED' };
|
|
setSubscription(updated);
|
|
localStorage.setItem('subscription', JSON.stringify(updated));
|
|
}
|
|
|
|
return (
|
|
<SubscriptionContext.Provider value={{ subscription, subscribe, cancelSubscription, PLANS }}>
|
|
{children}
|
|
</SubscriptionContext.Provider>
|
|
);
|
|
}
|
|
|
|
export function useSubscription() {
|
|
return useContext(SubscriptionContext);
|
|
}
|