Development Environment Setup
Complete guide for setting up a development environment to contribute to LegoCity.
Overview
This guide is for developers who want to:
- 🛠️ Build new features
- 🐛 Fix bugs and issues
- 🧪 Write and run tests
- 📦 Create custom blocks and plugins
- 🔍 Debug and profile code
Prerequisites
Required Tools
Install these before proceeding:
Node.js & pnpm:
# Check Node.js version (need 18.x or 20.x)
node --version
# Install pnpm globally
npm install -g pnpm
# Verify pnpm
pnpm --versionGit:
# Check Git installation
git --version
# Configure Git
git config --global user.name "Your Name"
git config --global user.email "your.email@example.com"MongoDB:
Option 1: Local Installation
# Windows (via Chocolatey)
choco install mongodb
# macOS (via Homebrew)
brew install mongodb-community
# Linux (Ubuntu/Debian)
sudo apt install mongodbOption 2: Docker
docker run -d -p 27017:27017 --name mongodb mongo:6VS Code (Recommended):
Install Visual Studio Code with extensions:
- ESLint
- Prettier
- TypeScript and JavaScript Language Features
- Tailwind CSS IntelliSense
- MongoDB for VS Code
Optional Tools
Enhance your development experience:
- Docker Desktop - For containerized services
- Postman or Insomnia - API testing
- MongoDB Compass - Database GUI
- Redux DevTools - State debugging
Initial Setup
1. Fork & Clone Repository
# Fork on GitHub first, then:
git clone https://github.com/YOUR_USERNAME/LegoCity.git
cd LegoCity
# Add upstream remote
git remote add upstream https://github.com/CTU-SematX/LegoCity.git
# Verify remotes
git remote -v2. Install Dependencies
cd dashboard
pnpm installThis installs:
- Next.js framework
- PayloadCMS and plugins
- React and UI libraries
- Development tools
3. Set Up Environment
# Copy example env file
cp .env.example .envEdit .env with development settings:
# Database
DATABASE_URI=mongodb://127.0.0.1/legocity-dev
# Security (generate with: openssl rand -hex 32)
PAYLOAD_SECRET=dev-secret-min-32-characters-long-replace-in-production
# Server
NEXT_PUBLIC_SERVER_URL=http://localhost:3000
# Mapbox (get token from mapbox.com)
NEXT_PUBLIC_MAPBOX_TOKEN=pk.your_mapbox_token
# NGSI-LD Context Broker (if running locally)
NGSI_LD_BROKER_URL=http://localhost:1026
# Development
NODE_ENV=development4. Start Development Server
# Start with hot reload
pnpm dev
# Or with turbopack (faster)
pnpm dev --turboAccess at:
- Frontend: http://localhost:3000
- Admin: http://localhost:3000/admin
- API: http://localhost:3000/api
5. Create Admin Account
On first visit to /admin, you'll create the first user:
Email: dev@example.com
Password: [choose strong password]Development Workflow
Branch Strategy
# Always start from latest main
git checkout main
git pull upstream main
# Create feature branch
git checkout -b feature/your-feature-name
# Or bug fix branch
git checkout -b fix/issue-123Making Changes
- Edit Code - Make your changes in
dashboard/src/ - Hot Reload - Changes appear automatically
- Check Console - Watch for errors
- Test Changes - Verify functionality works
Code Style
LegoCity uses ESLint and Prettier:
# Check linting
pnpm lint
# Auto-fix issues
pnpm lint:fix
# Format code
pnpm formatType Checking
# Check TypeScript types
pnpm type-check
# Build to verify no errors
pnpm buildProject Structure
dashboard/
├── src/
│ ├── app/ # Next.js App Router
│ │ ├── (frontend)/ # Public pages
│ │ └── (payload)/ # Admin routes
│ ├── blocks/ # PayloadCMS blocks
│ ├── collections/ # Content types
│ ├── components/ # React components
│ ├── fields/ # Custom fields
│ ├── hooks/ # React hooks
│ ├── plugins/ # PayloadCMS plugins
│ ├── providers/ # Context providers
│ └── utilities/ # Helper functions
├── public/ # Static assets
├── payload.config.ts # CMS configuration
└── next.config.js # Next.js configDevelopment Tasks
Adding a New Block
Create block directory:
bashmkdir -p src/blocks/MyBlockCreate config:
typescript// src/blocks/MyBlock/config.ts import { Block } from "payload/types"; export const MyBlock: Block = { slug: "myBlock", fields: [ { name: "title", type: "text", required: true, }, ], };Create component:
tsx// src/blocks/MyBlock/Component.tsx export const MyBlockComponent = ({ title }) => <div>{title}</div>;Register in
RenderBlocks.tsx
See Creating Blocks Guide for details.
Creating a Collection
// src/collections/MyCollection.ts
import { CollectionConfig } from "payload/types";
export const MyCollection: CollectionConfig = {
slug: "my-collection",
admin: {
useAsTitle: "name",
},
fields: [
{
name: "name",
type: "text",
required: true,
},
],
};Register in payload.config.ts:
collections: [
// ... existing
MyCollection,
],Writing Tests
# Run all tests
pnpm test
# Run specific test
pnpm test blocks
# Watch mode
pnpm test:watch
# Coverage report
pnpm test:coverageExample test:
// src/blocks/MyBlock/MyBlock.test.ts
import { render } from "@testing-library/react";
import { MyBlockComponent } from "./Component";
describe("MyBlock", () => {
it("renders title", () => {
const { getByText } = render(<MyBlockComponent title="Test" />);
expect(getByText("Test")).toBeInTheDocument();
});
});Debugging
VS Code Debug Configuration
Create .vscode/launch.json:
{
"version": "0.2.0",
"configurations": [
{
"name": "Next.js: debug server-side",
"type": "node-terminal",
"request": "launch",
"command": "pnpm dev"
},
{
"name": "Next.js: debug client-side",
"type": "chrome",
"request": "launch",
"url": "http://localhost:3000"
}
]
}Browser DevTools
- React DevTools - Inspect component tree
- Network Tab - Debug API calls
- Console - View logs and errors
Logging
// Development logging
if (process.env.NODE_ENV === "development") {
console.log("Debug info:", data);
}
// PayloadCMS logging
payload.logger.info("Info message");
payload.logger.error("Error message");Working with MongoDB
MongoDB Compass
Connect to: mongodb://127.0.0.1:27017/legocity-dev
View and edit:
- Collections (Pages, Posts, Media, etc.)
- Documents
- Indexes
Command Line
# Connect to database
mongosh legocity-dev
# List collections
show collections
# Find documents
db.pages.find().pretty()
# Clear collection
db.pages.deleteMany({})Seed Data
# Load sample data
pnpm seed
# Custom seed script
pnpm seed:customBuilding & Testing
Development Build
# Build for development
pnpm build
# Start production server
pnpm startProduction Build
# Build optimized for production
NODE_ENV=production pnpm build
# Analyze bundle size
pnpm build --analyzeQuality Checks
# Run all checks before committing
pnpm lint # ESLint
pnpm type-check # TypeScript
pnpm test # Jest tests
pnpm build # Production buildGit Workflow
Commit Changes
# Stage changes
git add .
# Commit with descriptive message
git commit -m "feat: add new block component"
# Follow conventional commits:
# feat: new feature
# fix: bug fix
# docs: documentation
# style: formatting
# refactor: code restructuring
# test: adding tests
# chore: maintenancePush & Create PR
# Push to your fork
git push origin feature/your-feature
# Create Pull Request on GitHub
# Fill out PR template
# Wait for reviewKeep Fork Updated
# Fetch upstream changes
git fetch upstream
# Update main branch
git checkout main
git merge upstream/main
git push origin main
# Rebase feature branch
git checkout feature/your-feature
git rebase mainCommon Issues
Port 3000 Already in Use
# Kill process
npx kill-port 3000
# Or use different port
PORT=3001 pnpm devBuild Errors
# Clear cache
rm -rf .next node_modules
pnpm install
pnpm devMongoDB Connection Issues
# Check MongoDB is running
mongosh --eval "db.version()"
# Restart MongoDB
# Windows: net restart MongoDB
# Linux: sudo systemctl restart mongodType Errors
# Regenerate Payload types
pnpm payload generate:types
# Check tsconfig.json pathsBest Practices
Code Quality
✅ Write TypeScript, avoid any
✅ Follow ESLint rules
✅ Write tests for new features
✅ Document complex logic
Performance
✅ Use React Server Components when possible
✅ Optimize images with next/image
✅ Lazy load heavy components
✅ Monitor bundle size
Security
✅ Never commit secrets
✅ Validate user inputs
✅ Use environment variables
✅ Keep dependencies updated
Git
✅ Write clear commit messages
✅ Keep PRs focused and small
✅ Rebase before pushing
✅ Review your own PR first
Resources
Getting Help
- 💬 GitHub Discussions
- 🐛 Report Issues
- 📧 Email: CTU-SematX Team
Environment ready? Start building with the Development Guide.