8000 Code refactor, introduce /repo, decommission /mine and /subscriptions by chozzz · Pull Request #34 · ingra-ai/ingra · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Code refactor, introduce /repo, decommission /mine and /subscriptions #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/(protected)/(actions)/collections.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
unsubscribeToCollection as dataUnsubscribeToCollection,
toggleCollectionSubscription as dataToggleCollectionSubscription,

} from '@/data/collections';
} from '@data/collections';

export const createCollection = async (values: z.infer<typeof CollectionSchema>) => {
const validatedValues = await validateAction(CollectionSchema, values);
Expand Down
7 changes: 5 additions & 2 deletions app/(protected)/(actions)/functions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
cloneFunction as dataCloneFunction,
toggleFunctionSubscription,
} from '@data/functions';
import { addFunctionToCollection, removeFunctionFromCollection } from '@/data/collections';
import { addFunctionToCollection, removeFunctionFromCollection } from '@data/collections';

export const upsertFunction = async (values: z.infer<typeof FunctionSchema>) => {
const validatedValues = await validateAction(FunctionSchema, values);
Expand Down Expand Up @@ -59,7 +59,10 @@ export const cloneFunction = async (functionId: string) => {
return {
status: 'ok',
message: `Function "${clonedFunction.slug}" has been cloned!`,
data: clonedFunction
data: {
...clonedFunction,
href: `/repo/${authSession.user.profile?.userName}/functions/edit/${clonedFunction.id}`
}
};
});
};
Expand Down
4 changes: 2 additions & 2 deletions app/(protected)/marketplace/MarketplaceNavRoutes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use client';
import { NavItemChild } from '@components/navs/types';
import { SquareDashedBottomCodeIcon, SquareLibraryIcon } from 'lucide-react';
import { SquareFunctionIcon, SquareLibraryIcon } from 'lucide-react';

export const MarketplaceNavRoutes: NavItemChild[] = [
{
Expand All @@ -11,6 +11,6 @@ export const MarketplaceNavRoutes: NavItemChild[] = [
{
name: 'Functions',
href: '/marketplace/functions',
icon: SquareDashedBottomCodeIcon
icon: SquareFunctionIcon
}
];

This file was deleted.

148 changes: 148 additions & 0 deletions app/(protected)/marketplace/collections/fetchPaginationData.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@

import type { FetchCommunityCollectionListPaginationType } from '@components/data/collections/types';
import clamp from 'lodash/clamp';
import db from '@lib/db';

export const fetchPaginationData = async (searchParams: Record<string, string | string[] | undefined> = {}, userId: string) => {
// Parse the query parameteres
let { page = '1' } = searchParams;
page = Array.isArray(page) ? page[0] : page;

// Validate and sanitize page and pageSize
const pageInt = Number.isInteger(Number(page)) && Number(page) > 0 ? parseInt(page, 10) : 1;
const pageSizeInt = 20;

// Calculate skip value based on page and pageSize
const skip = clamp(pageInt - 1, 0, 1e3) * pageSizeInt;

const [totalCount, allCollections] = await Promise.all([
// Fetch the total count of collections
db.collection.count({
where: {
NOT: {
userId,
},
functions: {
some: {
isPublished: true,
isPrivate: false,
},
},
},
}),

// Fetch paginated collections
db.collection.findMany({
where: {
NOT: {
userId,
},
functions: {
some: {
isPublished: true,
isPrivate: false,
},
},
},
orderBy: {
updatedAt: 'desc',
},
select: {
id: true,
name: true,
slug: true,
description: true,
owner: {
select: {
profile: {
select: {
userName: true,
}
}
}
},
functions: {
select: {
id: true,
slug: true,
code: false,
description: true,
httpVerb: true,
isPrivate: true,
isPublished: true,
ownerUserId: true,
createdAt: false,
updatedAt: true,
tags: {
select: {
id: true,
name: true,
functionId: false,
function: false,
}
},
arguments: {
select: {
id: true,
name: true,
description: false,
type: true,
defaultValue: false,
isRequired: false,
}
},
owner: {
select: {
id: true,
B93C profile: {
select: {
userName: true,
}
}
}
}
},
},
_count: {
select: {
subscribers: true,
}
},
subscribers: {
where: {
userId,
},
select: {
id: true,
},
},
},
skip,
take: pageSizeInt,
})
]);

// Transform the `subscribers` field into a boolean `isSubscribed`
const collectionsWithSubscriptionStatus = allCollections.map(collection => ({
...collection,
isSubscribed: Array.isArray( collection?.subscribers ) && collection.subscribers.length > 0
}));

// Calculate pagination details
const totalRecords = totalCount;
const nbPages = Math.ceil(totalRecords / pageSizeInt);
const hasNext = (skip + pageSizeInt) < totalRecords;
const hasPrevious = pageInt > 1;

const result: FetchCommunityCollectionListPaginationType = {
records: collectionsWithSubscriptionStatus,
page: pageInt,
pageSize: pageSizeInt,
totalRecords,
nbPages,
hasNext,
hasPrevious,
};

return result;
};
8 changes: 3 additions & 5 deletions app/(protected)/marketplace/collections/layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,9 @@ async function Layout ({
children: ReactNode
}) {
return (
<div className='relative' data-testid="marketplace-collections-layout">
<Suspense fallback={<LoadingSkeleton />}>
{ children }
</Suspense>
</div>
<Suspense fallback={<LoadingSkeleton />}>
{ children }
</Suspense>
);
}

Expand Down
Loading
0