diff --git a/api/src/graphql/parts.sdl.ts b/api/src/graphql/parts.sdl.ts
index 7395a1e..3e5334a 100644
--- a/api/src/graphql/parts.sdl.ts
+++ b/api/src/graphql/parts.sdl.ts
@@ -8,7 +8,29 @@ export const schema = gql`
createdAt: DateTime!
}
+ type PartPage {
+ parts: [Part!]!
+ count: Int!
+ page: Int!
+ sort: SortMethod!
+ order: SortOrder!
+ }
+
+ enum SortMethod {
+ id
+ name
+ description
+ stock
+ createdAt
+ }
+
+ enum SortOrder {
+ ascending
+ descending
+ }
+
type Query {
+ partPage(page: Int, sort: SortMethod, order: SortOrder): PartPage @skipAuth
parts: [Part!]! @skipAuth
part(id: Int!): Part @skipAuth
}
diff --git a/api/src/services/parts/parts.ts b/api/src/services/parts/parts.ts
index 63b31e6..8f6b409 100644
--- a/api/src/services/parts/parts.ts
+++ b/api/src/services/parts/parts.ts
@@ -1,8 +1,18 @@
import * as Filestack from 'filestack-js'
-import type { QueryResolvers, MutationResolvers } from 'types/graphql'
+import type {
+ QueryResolvers,
+ MutationResolvers,
+ SortMethod,
+ SortOrder,
+} from 'types/graphql'
import { db } from 'src/lib/db'
+const PARTS_PER_PAGE = 8
+
+const removeEnding = (input: string): string =>
+ input.endsWith('ending') ? input.slice(0, -6) : input
+
export const parts: QueryResolvers['parts'] = () => {
return db.part.findMany()
}
@@ -13,6 +23,59 @@ export const part: QueryResolvers['part'] = ({ id }) => {
})
}
+export const partPage = ({
+ page = 1,
+ sort = 'id',
+ order = 'ascending',
+}: {
+ page: number
+ sort: SortMethod
+ order: SortOrder
+}) => {
+ const offset = (page - 1) * PARTS_PER_PAGE
+ let orderByCase
+
+ switch (sort) {
+ case 'id':
+ orderByCase = { id: removeEnding(order) }
+ break
+
+ case 'name':
+ orderByCase = { name: removeEnding(order) }
+ break
+
+ case 'createdAt':
+ orderByCase = { createdAt: removeEnding(order) }
+ break
+
+ case 'description':
+ orderByCase = { description: removeEnding(order) }
+ break
+
+ case 'stock':
+ orderByCase = {
+ availableStock: removeEnding(order),
+ }
+ break
+
+ default:
+ orderByCase = { id: removeEnding(order) }
+ break
+ }
+
+ return {
+ parts: db.part.findMany({
+ take: PARTS_PER_PAGE,
+ skip: offset,
+ orderBy: orderByCase,
+ }),
+ count: db.part.count(),
+ page,
+ sort,
+ order,
+ }
+}
+
export const createPart: MutationResolvers['createPart'] = ({ input }) => {
return db.part.create({
data: input,
diff --git a/web/src/Routes.tsx b/web/src/Routes.tsx
index 4268f80..0c34cb6 100644
--- a/web/src/Routes.tsx
+++ b/web/src/Routes.tsx
@@ -1,12 +1,3 @@
-// In this file, all Page components from 'src/pages` are auto-imported. Nested
-// directories are supported, and should be uppercase. Each subdirectory will be
-// prepended onto the component name.
-//
-// Examples:
-//
-// 'src/pages/HomePage/HomePage.js' -> HomePage
-// 'src/pages/Admin/BooksPage/BooksPage.js' -> AdminBooksPage
-
import { Router, Route, Set, Private } from '@redwoodjs/router'
import NavbarLayout from 'src/layouts/NavbarLayout'
@@ -21,6 +12,7 @@ const Routes = () => {
+
@@ -29,9 +21,12 @@ const Routes = () => {
+
+
+
)
diff --git a/web/src/components/NavbarAccountIcon/NavbarAccountIcon.tsx b/web/src/components/NavbarAccountIcon/NavbarAccountIcon.tsx
index 1c5634a..c1571f5 100644
--- a/web/src/components/NavbarAccountIcon/NavbarAccountIcon.tsx
+++ b/web/src/components/NavbarAccountIcon/NavbarAccountIcon.tsx
@@ -11,7 +11,7 @@ interface Props {
}
const NavbarAccountIcon = ({ mobile, className }: Props) => {
- const { isAuthenticated, currentUser, logOut } = useAuth()
+ const { isAuthenticated, currentUser, logOut, hasRole } = useAuth()
return isAuthenticated ? (
@@ -21,11 +21,16 @@ const NavbarAccountIcon = ({ mobile, className }: Props) => {
}`}
>
-
+
- Hello, {currentUser.firstName}!
+ {currentUser ? `Hello, ${currentUser.firstName}!` : ``}