mirror of
https://github.com/jcreek/SvelteKitSaasBoilerplate.git
synced 2026-07-12 18:43:50 +00:00
refactor(#76): Avoid using module-level variables for request-specific data
This commit is contained in:
@@ -5,8 +5,6 @@ import { getUserSubscriptions, getUserTransactions } from '$lib/utils/supabase/a
|
|||||||
import { requestAccountDeletion } from '$lib/utils/supabase/admin';
|
import { requestAccountDeletion } from '$lib/utils/supabase/admin';
|
||||||
|
|
||||||
const transactionsPerPage = 5;
|
const transactionsPerPage = 5;
|
||||||
let firstTransactionId: string;
|
|
||||||
let hasMoreThanOnePage = false;
|
|
||||||
|
|
||||||
export const load: PageServerLoad = async ({ locals: { supabase, safeGetSession } }) => {
|
export const load: PageServerLoad = async ({ locals: { supabase, safeGetSession } }) => {
|
||||||
const { session } = await safeGetSession();
|
const { session } = await safeGetSession();
|
||||||
@@ -26,14 +24,16 @@ export const load: PageServerLoad = async ({ locals: { supabase, safeGetSession
|
|||||||
const transactionsData = await getUserTransactions(session.user.id, transactionsPerPage);
|
const transactionsData = await getUserTransactions(session.user.id, transactionsPerPage);
|
||||||
|
|
||||||
const transactions = transactionsData.transactions;
|
const transactions = transactionsData.transactions;
|
||||||
firstTransactionId = transactions[0]?.id;
|
const firstTransactionId = transactions.length > 0 ? transactions[0].id : undefined;
|
||||||
hasMoreThanOnePage = transactionsData.hasNextPage;
|
const hasMoreThanOnePage = transactionsData.hasNextPage;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
session,
|
session,
|
||||||
user,
|
user,
|
||||||
subscriptions,
|
subscriptions,
|
||||||
transactions,
|
transactions,
|
||||||
|
firstTransactionId,
|
||||||
|
hasMoreThanOnePage,
|
||||||
pageSize: transactionsPerPage
|
pageSize: transactionsPerPage
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
@@ -105,6 +105,8 @@ export const actions: Actions = {
|
|||||||
if (endingBefore === 'undefined') {
|
if (endingBefore === 'undefined') {
|
||||||
endingBefore = undefined;
|
endingBefore = undefined;
|
||||||
}
|
}
|
||||||
|
const firstTransactionId = formData.get('firstTransactionId')?.toString();
|
||||||
|
const hasMoreThanOnePage = formData.get('hasMoreThanOnePage') === 'true';
|
||||||
|
|
||||||
const { session } = await safeGetSession();
|
const { session } = await safeGetSession();
|
||||||
if (!session) return fail(401, { error: 'Not authenticated' });
|
if (!session) return fail(401, { error: 'Not authenticated' });
|
||||||
@@ -117,7 +119,7 @@ export const actions: Actions = {
|
|||||||
endingBefore
|
endingBefore
|
||||||
);
|
);
|
||||||
|
|
||||||
const isFirstPage = transactions[0].id === firstTransactionId;
|
const isFirstPage = transactions.length > 0 && transactions[0].id === firstTransactionId;
|
||||||
|
|
||||||
return {
|
return {
|
||||||
transactions: JSON.stringify(transactions),
|
transactions: JSON.stringify(transactions),
|
||||||
|
|||||||
@@ -5,8 +5,8 @@
|
|||||||
export let data;
|
export let data;
|
||||||
export let form;
|
export let form;
|
||||||
|
|
||||||
let { session, user, subscriptions, transactions } = data;
|
let { session, user, subscriptions, transactions, firstTransactionId, hasMoreThanOnePage } = data;
|
||||||
$: ({ session, user, transactions } = data);
|
$: ({ session, user, transactions, firstTransactionId, hasMoreThanOnePage } = data);
|
||||||
|
|
||||||
let profileForm: HTMLFormElement;
|
let profileForm: HTMLFormElement;
|
||||||
let loading = false;
|
let loading = false;
|
||||||
@@ -42,7 +42,7 @@
|
|||||||
|
|
||||||
let startingAfter: string | undefined = undefined;
|
let startingAfter: string | undefined = undefined;
|
||||||
let endingBefore: string | undefined = undefined;
|
let endingBefore: string | undefined = undefined;
|
||||||
let hasNextPage = true;
|
let hasNextPage = hasMoreThanOnePage;
|
||||||
let hasPreviousPage = false;
|
let hasPreviousPage = false;
|
||||||
|
|
||||||
const handlePaginationSubmit: SubmitFunction = () => {
|
const handlePaginationSubmit: SubmitFunction = () => {
|
||||||
@@ -177,6 +177,8 @@
|
|||||||
>
|
>
|
||||||
<input type="hidden" name="startingAfter" value={startingAfter} />
|
<input type="hidden" name="startingAfter" value={startingAfter} />
|
||||||
<input type="hidden" name="endingBefore" value={endingBefore} />
|
<input type="hidden" name="endingBefore" value={endingBefore} />
|
||||||
|
<input type="hidden" name="firstTransactionId" value={firstTransactionId} />
|
||||||
|
<input type="hidden" name="hasMoreThanOnePage" value={hasMoreThanOnePage} />
|
||||||
|
|
||||||
<button
|
<button
|
||||||
type="submit"
|
type="submit"
|
||||||
|
|||||||
Reference in New Issue
Block a user