Skip to content

Commit

Permalink
Merge pull request #8 from wilhelmrauston/MatProPages
Browse files Browse the repository at this point in the history
Product pages
  • Loading branch information
wilhelmrauston authored Jan 7, 2024
2 parents 2f63f30 + ace21e6 commit 7366a2b
Show file tree
Hide file tree
Showing 8 changed files with 860 additions and 81 deletions.
656 changes: 596 additions & 60 deletions package-lock.json

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@
"bcrypt": "^5.1.1",
"bcryptjs": "^2.4.3",
"clsx": "^2.0.0",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"mongodb": "^6.3.0",
"mongoose": "^8.0.1",
"next": "14.0.1",
"next-auth": "^4.24.5",
Expand Down
40 changes: 20 additions & 20 deletions src/models/Product.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,43 +2,43 @@ import mongoose from 'mongoose'

const Schema = mongoose.Schema

// Define a generic schema for nested objects
const genericSchema = new Schema(
{
id: String,
dpp_class: String,
privacy: String,
},
{ _id: false, strict: false }
)

// Define a schema for individual events
const eventSchema = new Schema(
{
id: String,
id: Number,
dpp_class: String,
creation_time: Date,
creation_time: Date, // Assuming this should be a Date type
},
{ _id: false }
)

// Define the main product schema
const productSchema = new Schema(
{
_id: Schema.Types.ObjectId,
id: String,
id: Number,
name: String,
dpp_class: String,
manufactured_by: genericSchema, // Using generic schema
manufactured_by: {
owner_id: Number,
owner_name: String,
privacy: String,
},
created_at: {
creation_time: Date,
creation_time: Date, // Assuming this should be a Date type
privacy: String,
},
has_carbon_footprint: {
id: Number,
dpp_class: String,
privacy: String,
},
main_component: {
id: Number,
dpp_class: String,
privacy: String,
},
has_carbon_footprint: genericSchema, // Using generic schema
has_crm: genericSchema, // Using generic schema for has_crm
has_event_trail: {
privacy: String,
events: [eventSchema], // Array of events
events: [eventSchema], // Define a separate schema for events
},
},
{ strict: false }
Expand Down
9 changes: 9 additions & 0 deletions src/pages/Material/[materialId].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useRouter } from "next/router"

function MaterialDetails(){
const router = useRouter()
const materialId = router.query.materialId
return
}

export default MaterialDetails
5 changes: 5 additions & 0 deletions src/pages/Material/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
function Material() {
return <h1>hello</h1>
}

export default Material
42 changes: 42 additions & 0 deletions src/pages/api/v1/product/[productID].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { defaultHandler } from '@/utils/server/api-helpers'
import { ObjectId } from 'mongodb'
import Product from '@/models/Product'

const getProductByID = async (req, res) => {
try {
const { productID } = req.query

if (!ObjectId.isValid(productID)) {
return res.status(400).json({ message: 'Invalid ID' })
}

// Use 'new' to create a new instance of ObjectId
const productData = await Product.findOne({ _id: new ObjectId(productID) })

if (!productData) {
return res.status(404).json({ message: 'Product not found' })
}

res.status(200).json(productData)
} catch (error) {
console.error('Error fetching product:', error)
return res
.status(500)
.json({ message: 'Internal server error', error: error.message })
}
}

const handler = async (req, res) =>
defaultHandler(
req,
res,
{
GET: getProductByID,
},
{
requiresAuth: false,
requiresAdmin: false,
}
)

export default handler
179 changes: 179 additions & 0 deletions src/pages/product/[productId].js
Original file line number Diff line number Diff line change
@@ -0,0 +1,179 @@
import { useRouter } from 'next/router'
import LayoutGlobal from '@/components/Layout/LayoutGlobal'
import { Container } from '@/components/utils/Container'
import { useEffect, useState } from 'react'

export async function getServerSideProps(context) {
const { productID } = context.params

const baseUrl = process.env.NEXT_PUBLIC_API_BASE_URL //REDO THIS LATER WE CANT HAVE BASE URL SHOULD WORK TO JUST DO /api/v1/product/${productID}

try {
const res = await fetch(`${baseUrl}/api/v1/product/${productID}`) // Use backticks for template literal

if (!res.ok) {
throw new Error(`Failed to fetch product, status: ${res.status}`) // Template literal
}

const productData = await res.json()

return {
props: { product: productData },
}
} catch (error) {
console.error('Error fetching product:', error)
return {
notFound: true,
}
}
}

export default function ProductPage({ product }) {
// Function to recursively render product properties, including nested objects/arrays
const renderProductData = data => {
if (Array.isArray(data)) {
return (
<ul>
{data.map((item, index) => (
<li key={index}>{renderProductData(item)}</li>
))}
</ul>
)
} else if (typeof data === 'object' && data !== null) {
return (
<ul>
{Object.entries(data).map(([key, value]) => (
<li key={key}>
<strong>{key}:</strong> {renderProductData(value)}
</li>
))}
</ul>
)
} else {
return data.toString()
}
}

return (
<div>
<h1>Product Details</h1>
{renderProductData(product)}
</div>
)
}

/* function ProductDetails() {
const router = useRouter()
const ProductId = router.query.productId
const [productData, setProductData] = useState({
_id,
id,
name,
dpp_class,
creation_time,
privacy,
})
const fetchproduct = async () => {
try {
// Make a GET request to the API route
const response = await fetch('/api/v1/product/export-product')
// Check if the response is successful (status code 200)
if (response.ok) {
// Parse the response body as JSON
const result = await response.json()
// Update the component state with the fetched data
setData(result)
} else {
// Handle non-successful responses (e.g., log an error)
console.error(
'Error fetching data:',
response.status,
response.statusText
)
}
} catch (error) {
// Handle network errors or other exceptions
console.error('Error fetching data:', error.message)
}
}
fetchproduct()
return (
<LayoutGlobal>
<Container className={'pt-20'}>
<div className='rounded-md border-2 shadow-md'>
<div className='px-4 py-4'>
<div className='px-4 sm:px-0'>
<h3 className='text-base font-semibold leading-7 text-gray-900'>
Battery
</h3>
<p className='mt-1 max-w-2xl text-sm leading-6 text-gray-500'>
Text.
</p>
</div>
<div className='mt-6 border-t border-gray-100'>
<dl className='divide-y divide-gray-100'>
<div className='px-4 py-6 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0'>
<dt className='text-sm font-medium leading-6 text-gray-900'>
ProductID:
</dt>
<dd className='mt-1 text-sm leading-6 text-gray-700 sm:col-span-2 sm:mt-0'>
198454
</dd>
</div>
<div className='px-4 py-6 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0'>
<dt className='text-sm font-medium leading-6 text-gray-900'>
Main Component:
</dt>
<dd className='mt-1 text-sm leading-6 text-gray-700 sm:col-span-2 sm:mt-0'>
Lithium
</dd>
</div>
<div className='px-4 py-6 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0'>
<dt className='text-sm font-medium leading-6 text-gray-900'>
MaterialID:
</dt>
<dd className='mt-1 text-sm leading-6 text-gray-700 sm:col-span-2 sm:mt-0'>
223411
</dd>
</div>
<div className='px-4 py-6 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0'>
<dt className='text-sm font-medium leading-6 text-gray-900'>
Owner
</dt>
<dd className='mt-1 text-sm leading-6 text-gray-700 sm:col-span-2 sm:mt-0'>
Scania
</dd>
</div>
<div className='px-4 py-6 sm:grid sm:grid-cols-3 sm:gap-4 sm:px-0'>
<dt className='text-sm font-medium leading-6 text-gray-900'>
Creation time
</dt>
<dd className='mt-1 text-sm leading-6 text-gray-700 sm:col-span-2 sm:mt-0'>
aug 2023
</dd>
</div>
</dl>
</div>
</div>
</div>
<div className='mt-16 flex flex-row content-center'>
<div className='flex h-56 w-72 rounded-md border-2'>
<div className='flex border-b-2 px-2'>
<dl className=''>
<dt>Weight</dt>
</dl>
</div>
</div>
</div>
</Container>
</LayoutGlobal>
)
}
export default ProductDetails
*/
7 changes: 7 additions & 0 deletions src/pages/product/index.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@

function Product() {

return <h1>hello</h1>
}

export default Product

0 comments on commit 7366a2b

Please sign in to comment.