Skip to content
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

Refactor #33

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
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
10 changes: 6 additions & 4 deletions components/Button.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
//button

export default function Button({ children, onClick }) {
return <button onClick={onClick}>{children}</button>;
export default function Button({ children, onClick, className }) {
return (
<button className={className} onClick={onClick}>
{children}
</button>
);
}
9 changes: 4 additions & 5 deletions components/Cart.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import ctx from '@/store/ctx-obj';

export default function Cart() {
const cartCTX = useContext(ctx);
console.log(cartCTX, ' context in cart');
const items = cartCTX.items;

return (
Expand All @@ -12,10 +11,10 @@ export default function Cart() {
<div>
<h1>Cart</h1>
<ul>
{items.map((el) => (
<li key={el.id + Math.random()}>
<p>{el.title}</p>
<p>items: {el.items}</p>
{items.map((element) => (
<li key={element.id + Math.random()}>
<p>{element.title}</p>
<p>items: {element.items}</p>
</li>
))}
</ul>
Expand Down
29 changes: 9 additions & 20 deletions components/Fruit.js
Original file line number Diff line number Diff line change
@@ -1,36 +1,25 @@
//individual fruit page
//more info on the fruit like description, price and add to cart option
//need to import image

import Image from 'next/image';
import Button from './Button';
import ctx from '@/store/ctx-obj';
import React from 'react';
import { useContext } from 'react';
import React, { useContext } from 'react';

export default function FruitPage(props) {
const { title, image_path, description, price } = props;

export default function Fruitpage(props) {
const cartCTX = useContext(ctx);
const addToBasket = () => {
console.log('added to basket ', props);
const addToBasket = () => cartCTX.addItem(props);

cartCTX.addItem(props);
};
return (
<>
<h1>{props.title}</h1>
<h1>{title}</h1>
<div>
<Image
src={props.image_path}
alt={props.title}
width="300"
height="300"
/>
<Image src={image_path} alt={title} width="300" height="300" />
</div>
<div>
<p>{props.description}</p>
<p>{description}</p>
</div>
<div>
<p>£ {props.price}</p>
<p>£ {price}</p>
</div>
<Button onClick={addToBasket}>Add to Cart</Button>
</>
Expand Down
19 changes: 9 additions & 10 deletions components/FruitItem.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,24 @@ import React, { useContext } from 'react';
import ctx from '@/store/ctx-obj';

export default function FruitItem(props) {
const cartCTX = useContext(ctx);
const { id, title, image, price, link } = props;

const AddToBasketHandler = () => {
cartCTX.addItem(props);
};
const cartCTX = useContext(ctx);
const addToBasket = () => cartCTX.addItem(props);

return (
<li id={props.id}>
<h3>{props.title}</h3>
<li id={id}>
<h3>{title}</h3>
<div>
<div>
<Image src={props.image} alt={props.title} width="40" height="40" />
<Image src={image} alt={title} width="40" height="40" />
</div>
<p>£ {props.price}</p>
<p>£ {price}</p>
</div>
<div>
<Link href={props.link}>See more at {props.title}</Link>
<Link href={link}>See more at {title}</Link>
</div>
<Button onClick={AddToBasketHandler}>Add to Cart</Button>
<Button onClick={addToBasket}>Add to Cart</Button>
</li>
);
}
6 changes: 1 addition & 5 deletions components/Navigation.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//nav bar
import Link from 'next/link';

import classes from './Navigation.module.css';

function Navigation() {
export default function Navigation() {
return (
<header className={classes.header}>
<Link href="/">
Expand All @@ -25,5 +23,3 @@ function Navigation() {
</header>
);
}

export default Navigation;
2 changes: 1 addition & 1 deletion database/schema.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ BEGIN;
CREATE TABLE IF NOT EXISTS fruits (
id INTEGER PRIMARY KEY AUTOINCREMENT,
title TEXT,
image_path TEXT ,
image_path TEXT,
price DECIMAL,
fruit_description TEXT,
fruit_type INTEGER REFERENCES fruit_types(id)
Expand Down
4 changes: 1 addition & 3 deletions database/seed.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
//id, title, image, price, desc, type: ref field
// type, id, name

import { readFileSync } from 'node:fs';
import { join } from 'node:path';
import db from './db.js';

const seedPath = join('database', 'seed.sql');
const seed = readFileSync(seedPath, 'utf-8');
db.exec(seed);
Expand Down
2 changes: 0 additions & 2 deletions database/seed.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,3 @@ VALUES (1, 'Citrus'),
ON CONFLICT DO NOTHING;

COMMIT;

PRAGMA foreign_keys = ON;
Binary file modified db.sqlite
Binary file not shown.
6 changes: 2 additions & 4 deletions layout/layout.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@ import Cart from '@/components/Cart';
import CartProvider from '@/store/ContextProvider';
import Navigation from '../components/Navigation';

function Layout(props) {
export default function Layout({ children }) {
return (
<CartProvider>
<Navigation />
<main className="layout-main">{props.children}</main>
<main className="layout-main">{children}</main>
<Cart />
</CartProvider>
);
}

export default Layout;
56 changes: 0 additions & 56 deletions lib/data.js

This file was deleted.

24 changes: 12 additions & 12 deletions model/fruits.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ const select_all_fruits = db.prepare(
`
);

export function getAllFruits() {
return select_all_fruits.all();
}

const select_fruit_by_id = db.prepare(
/*sql*/
`
Expand All @@ -34,10 +30,6 @@ SELECT
`
);

export function getFruitById(id) {
return select_fruit_by_id.get(id);
}

const select_fruit_by_type = db.prepare(
/*sql*/
`
Expand All @@ -54,10 +46,6 @@ SELECT
`
);

export function getFruitByType(type) {
return select_fruit_by_type.all(type);
}

const select_all_ids = db.prepare(
/*sql*/
`
Expand All @@ -67,6 +55,18 @@ const select_all_ids = db.prepare(
`
);

export function getAllFruits() {
return select_all_fruits.all();
}

export function getAllIds() {
return select_all_ids.all();
}

export function getFruitById(id) {
return select_fruit_by_id.get(id);
}

export function getFruitByType(type) {
return select_fruit_by_type.all(type);
}
7 changes: 0 additions & 7 deletions model/types.js

This file was deleted.

3 changes: 1 addition & 2 deletions pages/_app.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import Navigation from '@/components/Navigation';
import Layout from '@/layout/layout';
import '@/styles/globals.css';

export default function App({ Component, pageProps }) {
return (
<Layout>
<Component {...pageProps} />{' '}
<Component {...pageProps} />
</Layout>
);
}
16 changes: 7 additions & 9 deletions pages/fruits/[fruit_id].js
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,20 @@ import { getAllIds, getFruitById } from '@/model/fruits';
import Fruit from '@/components/Fruit';

export async function getStaticPaths() {
const arrIds = getAllIds().map((idObj) => ({
const fruitIDs = getAllIds().map((fruit) => ({
params: {
fruit_id: idObj.id.toString(),
fruit_id: fruit.id.toString(),
},
}));

return {
paths: arrIds,
paths: fruitIDs,
fallback: false,
};
}

export async function getStaticProps(context) {
const fruitId = context.params.fruit_id; //we receive this data when user visit this path, not during the build process
//fetch data from db for single fruit
const fruit = getFruitById(fruitId);
export async function getStaticProps({ params }) {
const fruit = getFruitById(params.fruit_id);

return {
props: {
Expand All @@ -25,8 +24,7 @@ export async function getStaticProps(context) {
};
}

export default function FruitDetail(props) {
const { fruitData } = props;
export default function FruitDetail({ fruitData }) {
return (
<Fruit
id={fruitData.id}
Expand Down
1 change: 0 additions & 1 deletion pages/fruits/berries.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import Link from 'next/link';
import Head from 'next/head';
import FruitList from '@/components/FruitList';
import { getFruitByType } from '@/model/fruits.js';
Expand Down
Loading