shinyay / demo-azure-openai-devday

This is a demonstrataion for Azure Open AI Service Dev Day.
MIT License
0 stars 0 forks source link

My Next.js App

This is a Next.js application created using the Next.js framework.

Project Structure

The project has the following files and directories:

Getting Started

To get started with the project, follow these steps:

  1. Clone the repository: git clone <repository-url>
  2. Install the dependencies: npm install
  3. Start the development server: npm run dev

Using the Todo Application with Context

The Todo application uses React Context API for state management. The TodoContext and TodoProvider are defined in context/TodoContext.tsx. The context provides functions for adding, editing, deleting, and toggling todos.

To use the Todo application with context, follow these steps:

  1. Wrap your application with the TodoProvider in pages/_app.tsx:
import { TodoProvider } from '../context/TodoContext';

function MyApp({ Component, pageProps }: AppProps) {
  return (
    <TodoProvider>
      <Component {...pageProps} />
    </TodoProvider>
  );
}

export default MyApp;
  1. Use the context functions in your components to manage todos. For example, in pages/index.tsx:
import React, { useContext, useState } from 'react';
import { TodoContext } from '../context/TodoContext';

const Index = () => {
  const { todos, addTodo, editTodo, deleteTodo, toggleTodo } = useContext(TodoContext);
  const [newTodo, setNewTodo] = useState('');

  const handleAddTodo = () => {
    if (newTodo.trim() === '') return;

    const todo = { id: Date.now(), text: newTodo, completed: false };
    addTodo(todo);
    setNewTodo('');

    // Persist the new todo to the backend
    fetch('/api/todos', {
      method: 'POST',
      headers: { 'Content-Type': 'application/json' },
      body: JSON.stringify(todo),
    });
  };

  return (
    <div>
      <h1>Todo List</h1>
      <input
        type="text"
        value={newTodo}
        onChange={(e) => setNewTodo(e.target.value)}
        placeholder="Add a new todo"
      />
      <button onClick={handleAddTodo}>Add</button>
      <ul>
        {todos.map((todo) => (
          <li key={todo.id}>
            <input
              type="checkbox"
              checked={todo.completed}
              onChange={() => toggleTodo(todo.id)}
            />
            <span>{todo.text}</span>
            <button onClick={() => editTodo(todo.id, prompt('Edit todo:', todo.text))}>Edit</button>
            <button onClick={() => deleteTodo(todo.id)}>Delete</button>
          </li>
        ))}
      </ul>
    </div>
  );
};

export default Index;

License

This project is licensed under the MIT License. See the LICENSE file for details.