From de82cf813fc0c90bbd2458b809d5a92bb69837fd Mon Sep 17 00:00:00 2001 From: MuslemRahimi Date: Mon, 17 Jun 2024 09:43:32 +0200 Subject: [PATCH] feature: change from monthly to annual plan --- src/routes/community/profile/+page.server.ts | 48 ++++++++- src/routes/community/profile/+page.svelte | 105 ++++++++++++++++--- src/routes/pricing/+page.svelte | 16 ++- 3 files changed, 139 insertions(+), 30 deletions(-) diff --git a/src/routes/community/profile/+page.server.ts b/src/routes/community/profile/+page.server.ts index 3b0dda6b..9c9be189 100644 --- a/src/routes/community/profile/+page.server.ts +++ b/src/routes/community/profile/+page.server.ts @@ -33,8 +33,6 @@ export const actions = { const apiKey = import.meta.env.VITE_LEMON_SQUEEZY_API_KEY; const subscriptionId = formData?.get('subscriptionId'); - - try { const url = `https://api.lemonsqueezy.com/v1/subscriptions/${subscriptionId}`; @@ -49,8 +47,6 @@ export const actions = { headers: headers }); - - } catch (err) { console.log("Error: ", err); error(err.status, err.message); @@ -104,6 +100,50 @@ export const actions = { redirect(302, '/community/profile'); }, + + changeSubscription: async ({ request, locals }) => { + const formData = await request?.formData(); + + const apiKey = import.meta.env.VITE_LEMON_SQUEEZY_API_KEY; + const subscriptionId = formData?.get('subscriptionId'); + + try { + const url = `https://api.lemonsqueezy.com/v1/subscriptions/${subscriptionId}`; + const headers = { + 'Accept': 'application/vnd.api+json', + 'Content-Type': 'application/vnd.api+json', + 'Authorization': `Bearer ${apiKey}` + }; + + // Create the data payload + const payload = { + data: { + type: "subscriptions", + id: subscriptionId, + attributes: { + variant_id: import.meta.env.VITE_LEMON_SQUEEZY_ANNUAL_VARIANT_ID // Change from monthly to annually plan + } + } + }; + + const response = await fetch(url, { + method: 'PATCH', + headers: headers, + body: JSON.stringify(payload) + }); + + console.log(await response.json()) + + + } catch (err) { + console.log("Error: ", err); + error(err.status, err.message); + } + + + redirect(302, '/community/profile'); + + }, diff --git a/src/routes/community/profile/+page.svelte b/src/routes/community/profile/+page.svelte index 45c238d8..e91a252b 100644 --- a/src/routes/community/profile/+page.svelte +++ b/src/routes/community/profile/+page.svelte @@ -15,7 +15,6 @@ import { pb } from '$lib/pocketbase'; import { z } from 'zod'; import { updatePersonalDataSchema, updatePasswordSchema} from '$lib/schemas'; - import communityBanner from '$lib/images/community_banner.jpg'; import { enhance } from '$app/forms'; export let data; @@ -147,21 +146,48 @@ setTimeout(() => { -async function handleReactivateSubscription() { +const submitChangePlan = () => { + return async ({ result, update}) => { + switch (result.type) { + case 'success': + toast.success('Changing to Annual Plan successfully!', { + style: 'border-radius: 200px; background: #333; color: #fff;'}); + await update(); + break; + case 'redirect': + toast.success('Changing to Annual Plan successfully!', { + style: 'border-radius: 200px; background: #333; color: #fff;'}); + await update(); + break; + case 'failure': + toast.error('Something went wrong.', { + style: 'border-radius: 200px; background: #333; color: #fff;'}); + await update(); + break; + case 'error': + toast.error(result.error.message, { + style: 'border-radius: 200px; background: #333; color: #fff;'}); + break; + default: + await update(); + } + +setTimeout(() => { + if (result.type === 'redirect') { + const anchor = document.createElement('a'); + anchor.href = '/community/profile'; + anchor.dataset.sveltekitReload = true; + document.body.appendChild(anchor); + anchor.dispatchEvent(new MouseEvent('click')); + } + }, 5000); - // make the POST request to the endpoint - const response = await fetch('/api/reactivate-subscription', { - method: 'GET', - headers: { - 'Content-Type': 'application/json', - }, - }); - - const output = await response.json(); - + } } + + async function updateAvatar(event) { @@ -446,7 +472,7 @@ async function getPost() { -let showTab = 'post'; +let showTab = 'subscription'; let settingsTab = 'personalData'; @@ -897,9 +923,18 @@ onDestroy(async () => { {#if subscriptionData?.status_formatted === 'Active' || subscriptionData?.status_formatted === 'On Trial'} - +
+ + {#if subscriptionData?.product_name?.includes('Monthly')} + + {/if} +
+ + {:else if subscriptionData?.status_formatted === 'Cancelled'}