123 lines
4.3 KiB
JavaScript
123 lines
4.3 KiB
JavaScript
import axiosInstance from '@/plugins/axios-plugin';
|
|
import { mountStoreDevtool } from 'simple-zustand-devtools';
|
|
import { toast } from 'sonner';
|
|
import { route } from 'ziggy-js';
|
|
import { create } from 'zustand';
|
|
import { devtools } from 'zustand/middleware';
|
|
|
|
const usePricingStore = create(
|
|
devtools((set, get) => ({
|
|
// Pricing Plans
|
|
subscription: null,
|
|
one_times: [],
|
|
isFetchingPricing: false,
|
|
|
|
isCheckingOut: false,
|
|
|
|
checkoutPurchase: async (price_id) => {
|
|
console.log('checkoutPurchase', price_id);
|
|
|
|
set({ isCheckingOut: true });
|
|
try {
|
|
const response = await axiosInstance.post(route('api.user.purchase'), { price_id: price_id });
|
|
console.log(response);
|
|
if (response?.data?.success?.data) {
|
|
if (response.data.success.data.redirect) {
|
|
window.location.href = response.data.success.data.redirect;
|
|
}
|
|
} else {
|
|
throw 'Invalid API response';
|
|
}
|
|
} catch (error) {
|
|
console.error(route('api.user.purchase'));
|
|
console.error('Error fetching:', error);
|
|
set({ isCheckingOut: false });
|
|
if (error?.response?.data?.error?.message?.length > 0) {
|
|
toast.error(error.response.data.error.message);
|
|
}
|
|
throw error;
|
|
} finally {
|
|
set({ isCheckingOut: false });
|
|
}
|
|
},
|
|
|
|
checkoutSubscribe: async (price_id) => {
|
|
console.log('checkoutSubscribe', price_id);
|
|
|
|
set({ isCheckingOut: true });
|
|
try {
|
|
const response = await axiosInstance.post(route('api.user.subscribe'), { price_id: price_id });
|
|
|
|
if (response?.data?.success?.data) {
|
|
if (response.data.success.data.redirect) {
|
|
window.location.href = response.data.success.data.redirect;
|
|
}
|
|
} else {
|
|
throw 'Invalid API response';
|
|
}
|
|
} catch (error) {
|
|
console.error(route('api.user.subscribe'));
|
|
console.error('Error fetching:', error);
|
|
set({ isCheckingOut: false });
|
|
if (error?.response?.data?.error?.message?.length > 0) {
|
|
toast.error(error.response.data.error.message);
|
|
}
|
|
throw error;
|
|
} finally {
|
|
set({ isCheckingOut: false });
|
|
}
|
|
},
|
|
|
|
// Fetch backgrounds
|
|
fetchPricing: async () => {
|
|
set({ isFetchingPricing: true });
|
|
try {
|
|
const response = await axiosInstance.post(route('api.pricing_page'));
|
|
|
|
if (response?.data?.success?.data) {
|
|
set({
|
|
subscription: response.data.success.data.subscription,
|
|
one_times: response.data.success.data.one_times,
|
|
});
|
|
return response.data.success.data;
|
|
} else {
|
|
throw 'Invalid API response';
|
|
}
|
|
} catch (error) {
|
|
console.error(route('api.pricing_page'));
|
|
console.error('Error fetching:', error);
|
|
set({ isFetchingPricing: false });
|
|
if (error?.response?.data?.error?.message?.length > 0) {
|
|
toast.error(error.response.data.error.message);
|
|
}
|
|
throw error;
|
|
} finally {
|
|
set({ isFetchingPricing: false });
|
|
}
|
|
},
|
|
|
|
// Reset store to default state
|
|
restoreMemeStateToDefault: () => {
|
|
console.log('restoreMemeStateToDefault');
|
|
set({
|
|
memes: [],
|
|
backgrounds: [],
|
|
isFetchingMemes: false,
|
|
isFetchingBackgrounds: false,
|
|
selectedMeme: null,
|
|
selectedBackground: null,
|
|
});
|
|
},
|
|
})),
|
|
{
|
|
name: 'MemeStore',
|
|
store: 'MemeStore',
|
|
},
|
|
);
|
|
|
|
if (import.meta.env.APP_ENV === 'local') {
|
|
mountStoreDevtool('PricingStore', usePricingStore);
|
|
}
|
|
|
|
export default usePricingStore;
|