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

Sample app frontend #5931

Merged
merged 11 commits into from Apr 5, 2024
Merged
Show file tree
Hide file tree
Changes from 8 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
@@ -0,0 +1,3 @@
{
"extends": "next/core-web-vitals"
}
36 changes: 36 additions & 0 deletions code-samples/eventing/bookstore-sample-app/frontend/.gitignore
@@ -0,0 +1,36 @@
# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.

# dependencies
/node_modules
/.pnp
.pnp.js
.yarn/install-state.gz

# testing
/coverage

# next.js
/.next/
/out/

# production
/build

# misc
.DS_Store
*.pem

# debug
npm-debug.log*
yarn-debug.log*
yarn-error.log*

# local env files
.env*.local

# vercel
.vercel

# typescript
*.tsbuildinfo
next-env.d.ts
85 changes: 85 additions & 0 deletions code-samples/eventing/bookstore-sample-app/frontend/README.md
ctmphuongg marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The README.md looks great! But just a note here: it might need to be changed when the sample app approaches to the end!! If it got changed, it doesn't mean your README.md is wrong!!

@@ -0,0 +1,85 @@
# Getting Started

This app use Next.js and TailwindCSS as main packages. Use this command to install all dependencies:

```bash
npm install
```

To run application, use:

```bash
npm run dev
# or
yarn dev
# or
pnpm dev
# or
bun dev
```

Open [http://localhost:3000](http://localhost:3000) with your browser to see the result.

# Project Structures

- app/: Contains the main layout, page, and global styling.
- client/: Contains components and pages used in the application.
- public/images/: Contains image files.
- next-env.d.ts, next.config.mjs, package-lock.json, package.json, postcss.config.js, tailwind.config.js, tsconfig.json: Configuration files for Next.js, Tailwind CSS, and TypeScript.

# Containerize Application

This repository contains a Next.js application that utilizes next-themes and Tailwind CSS. This README file provides instructions on how to containerize the application using Docker.

## Prerequisites

- Docker installed on your machine. You can download and install Docker from [here](https://www.docker.com/get-started).

## Dockerization Steps

1. Clone this repository to your local machine.
2. Navigate to the root directory of the cloned repository.

### Building the Docker Image

Run the following command to build the Docker image:

```bash
docker build -t frontend .
```

## Running the Docker Container

Once the image is built, you can run a container using the following command:

```bash
docker run -d -p 3000:3000 frontend
```

## Dockerfile for containerization

```Dockerfile
ctmphuongg marked this conversation as resolved.
Show resolved Hide resolved
# Use a base image with Node.js LTS
FROM node:lts-alpine

# Set the working directory inside the container
WORKDIR /app

# Copy package.json and package-lock.json to the working directory
COPY package*.json ./

# Install dependencies
RUN npm install

# Copy the rest of your application code to the working directory
COPY . .

# Build the Next.js application
RUN npm run build

# Expose the port your app runs on
EXPOSE 3000

# Define the command to run your app
CMD ["npm", "run dev"]
```
ctmphuongg marked this conversation as resolved.
Show resolved Hide resolved
Binary file not shown.
@@ -0,0 +1,22 @@
@tailwind base;
@tailwind components;
@tailwind utilities;

@import url('https://fonts.googleapis.com/css2?family=Poppins:wght@400;500;600;700&display=swap');

#__next {
min-height: 100vh;
width: 100%;
display: flex;
flex-direction: column;
}

body {
font-family: 'Poppins', sans-serif;
}

@layer utilities {
.text-balance {
text-wrap: balance;
}
}
22 changes: 22 additions & 0 deletions code-samples/eventing/bookstore-sample-app/frontend/app/layout.tsx
@@ -0,0 +1,22 @@
import type { Metadata } from 'next';
import { Inter } from 'next/font/google';
import './globals.css';

const inter = Inter({ subsets: ['latin'] });

export const metadata: Metadata = {
title: 'Knative Bookstore',
description: 'Bookstore Sample Application from Knative',
};

export default function RootLayout({
children,
}: Readonly<{
children: React.ReactNode;
}>) {
return (
<html lang='en'>
<body className={inter.className}>{children}</body>
</html>
);
}
14 changes: 14 additions & 0 deletions code-samples/eventing/bookstore-sample-app/frontend/app/page.tsx
@@ -0,0 +1,14 @@
'use client';
import Image from 'next/image';
import Main from '../client/pages/Main';
// import { keepTheme } from '../client/utils/themes';
ctmphuongg marked this conversation as resolved.
Show resolved Hide resolved
import { useEffect } from 'react';
import { ThemeProvider } from 'next-themes';

export default function Home() {
return (
<ThemeProvider attribute='class'>
<Main />
</ThemeProvider>
);
}
@@ -0,0 +1,43 @@
const BookDetail = ({ book }) => {
return (
<div className='flex flex-col md:flex-row md:items-start p-8 space-x-8 justify-center items-center font-sans'>
<div className='mb-4 md:mb-0 md:mr-8'>
<img src={book.img} alt='Book Cover' className='w-48 h-auto' />
</div>
<div>
<div className='space-y-4'>
<div className='grid grid-cols-[auto_1fr] gap-x-2 items-center'>
<span className='font-bold text-left'>Title:</span>
<span className='text-left'>{book.title}</span>
</div>
<div className='grid grid-cols-[auto_1fr] gap-x-2 items-center'>
<span className='font-bold text-left'>Author:</span>
<span className='text-left'>{book.author}</span>
</div>
<div className='grid grid-cols-[auto_1fr] gap-x-2 items-center'>
<span className='font-bold text-left'>ISBN:</span>
<span>{book.ISBN}</span>
</div>
<div className='grid grid-cols-[auto_1fr] gap-x-2 items-center'>
<span className='font-bold text-left'>Publisher:</span>
<span>{book.publisher}</span>
</div>
<div className='grid grid-cols-[auto_1fr] gap-x-2 items-center'>
<span className='font-bold text-left'>Published Date:</span>
<span>{book.publishedDate}</span>
</div>
<div className='grid grid-cols-[auto_1fr] gap-x-2 items-center'>
<span className='font-bold text-left'>Description:</span>
<span>{book.description}</span>
</div>
<div className='grid grid-cols-[auto_1fr] gap-x-2 items-center'>
<span className='font-bold text-left'>Price:</span>
<span>{book.price}</span>
</div>
</div>
</div>
</div>
);
};

export default BookDetail;
@@ -0,0 +1,30 @@
import Emoji from './Emoji';
const CommentDisplay = ({ comment }) => {
// Assume receiving a comment object
return (
<div className='flex my-4 p-4 justify-center align-middle items-center'>
<div className='comment-display w-full w-7/12 flex flex-row rounded-lg p-4 bg-gray-800 text-white dark:bg-white dark:text-black'>
<div className='flex items-center justify-center md:w-1/12'>
<img
src={comment.avatar}
alt='Avatar'
className='rounded-full w-8 h-8'
/>
</div>
<div className='md:w-1/12 text-sm text-gray-200 dark:text-black'>
{comment.time}
</div>
<div className='md:w-9/12'> {comment.text} </div>
<div className='md:w-1/12 text-4xl'>
<Emoji
symbol={comment.emotion}
label={comment.label}
size='text-2xl'
/>
</div>
</div>
</div>
);
};

export default CommentDisplay;
ctmphuongg marked this conversation as resolved.
Show resolved Hide resolved
@@ -0,0 +1,17 @@
import SubmitButton from './SubmitButton';
const CommentForm = () => {
return (
<div className='flex my-4 p-4 justify-center'>
<form className='w-full w-8/12 flex flex-col items-end'>
<textarea
className='form-textarea w-full mb-2 p-2 border border-2 border-black rounded-lg p-4'
rows='3'
placeholder='Leave your comment here...'
></textarea>
<SubmitButton />
</form>
</div>
);
};

export default CommentForm;
@@ -0,0 +1,13 @@
import CommentDisplay from './CommentDisplay';
const CommentList = () => {
const comment = {
avatar: '/images/avatar.jpg',
time: '10:05',
text: 'I used this provider to insert a different theme object depending on a person ',
emotion: '😃',
ctmphuongg marked this conversation as resolved.
Show resolved Hide resolved
label: 'grinning face',
};
return <CommentDisplay comment={comment} />;
};

export default CommentList;
@@ -0,0 +1,12 @@
import React from 'react';
const Emoji = (props) => (
<span
className='emoji'
role='img'
aria-label={props.label ? props.label : ''}
aria-hidden={props.label ? 'false' : 'true'}
>
{props.symbol}
</span>
);
export default Emoji;
@@ -0,0 +1,22 @@
import Image from 'next/image';
import Toggle from './Toggle';

const Header = () => {
return (
<div className='flex justify-between p-4'>
<header className='flex items-center p-4'>
<Image
src='/images/knative-logo.png'
alt='Knative BookStore Logo'
width={50}
height={50}
className='mr-2'
/>
<h1 className='text-3xl font-bold'>Knative BookStore</h1>
</header>
<Toggle />
</div>
);
};

export default Header;
@@ -0,0 +1,18 @@
'use-client';
import { useState } from 'react';

export default function SubmitButton() {
const [hover, setHover] = useState(false);

ctmphuongg marked this conversation as resolved.
Show resolved Hide resolved
return (
<button
type='submit'
className={`font-bold py-2 px-9 rounded ${hover ? '' : 'bg-blue-600'}`}
style={{ backgroundColor: hover ? '#A0DDFF' : '#A5D8FF' }}
onMouseEnter={() => setHover(true)}
onMouseLeave={() => setHover(false)}
>
Submit
</button>
);
}
@@ -0,0 +1,19 @@
'use client';
import React from 'react';
import { useTheme } from 'next-themes';

const Toggle = () => {
const { systemTheme, theme, setTheme } = useTheme();
const currentTheme = theme === 'system' ? systemTheme : theme;

ctmphuongg marked this conversation as resolved.
Show resolved Hide resolved
return (
<button
onClick={() => (theme == 'dark' ? setTheme('light') : setTheme('dark'))}
className='bg-gray-800 dark:bg-gray-50 hover:bg-gray-600 dark:hover:bg-gray-300 transition-all duration-100 text-white dark:text-gray-800 px-6 py-1 text-xl md:text-2xl rounded-md'
>
Mode
</button>
);
};

export default Toggle;