Rent-My-Ride Guide

Rent-My-Ride Guide

Rent My Ride SE Project guidance and references

4 minute read

Rent-My-Ride Project Directory

Project Directory ทั้งหมดควรหน้าตาเป็นแบบนี้

Rent-My-Ride Project Directories
├── README.md
├── package.json
├── next.config.ts
├── tsconfig.json
├── tailwind.config.ts
├── postcss.config.mjs
├── eslint.config.mjs
├── components.json
├── next-env.d.ts
├── middleware.ts
├── app/
│   ├── layout.tsx
│   ├── page.tsx
│   ├── globals.css
│   ├── favicon.ico
│   ├── opengraph-image.png
│   ├── twitter-image.png
│   ├── auth/
│   │   ├── login/
│   │   │   └── page.tsx
│   │   ├── sign-up/
│   │   │   └── page.tsx
│   │   ├── sign-up-success/
│   │   │   └── page.tsx
│   │   ├── forgot-password/
│   │   │   └── page.tsx
│   │   ├── update-password/
│   │   │   └── page.tsx
│   │   ├── confirm/
│   │   │   └── route.ts
│   │   └── error/
│   │       └── page.tsx
│   ├── dashboard/
│   │   ├── page.tsx
│   │   ├── admin/
│   │   │   └── page.tsx
│   │   ├── cars/
│   │   │   ├── page.tsx
│   │   │   ├── add/
│   │   │   │   └── page.tsx
│   │   │   └── [id]/
│   │   │       ├── page.tsx
│   │   │       └── edit/
│   │   │           └── page.tsx
│   │   ├── bookings/
│   │   │   └── page.tsx
│   │   └── profile/
│   │       └── page.tsx
│   ├── browse/
│   │   └── page.tsx
│   ├── cars/
│   │   └── [id]/
│   │       └── page.tsx
│   ├── checkout/
│   │   └── page.tsx
│   ├── success/
│   │   └── page.tsx
│   └── api/
│       ├── cars/
│       │   ├── route.ts
│       │   └── [id]/
│       │       └── route.ts
│       ├── bookings/
│       │   └── route.ts
│       └── admin/
│           └── approve/
│               └── route.ts
├── components/
│   ├── auth-button.tsx
│   ├── car-card.tsx
│   ├── env-var-warning.tsx
│   ├── footer.tsx
│   ├── header.tsx
│   ├── hero.tsx
│   ├── login-form.tsx
│   ├── logout-button.tsx
│   ├── sign-up-form.tsx
│   ├── forgot-password-form.tsx
│   ├── update-password-form.tsx
│   ├── car-form.tsx
│   ├── car-list.tsx
│   ├── booking-card.tsx
│   ├── profile-form.tsx
│   ├── admin-car-approval.tsx
│   ├── payment-form.tsx
│   └── ui/
│       ├── badge.tsx
│       ├── button.tsx
│       ├── card.tsx
│       ├── checkbox.tsx
│       ├── dropdown-menu.tsx
│       ├── input.tsx
│       ├── label.tsx
│       ├── select.tsx
│       ├── textarea.tsx
│       └── toast.tsx
├── lib/
│   ├── utils.ts
│   ├── auth.ts
│   ├── types.ts
│   └── supabase/
│       ├── client.ts
│       ├── server.ts
│       └── middleware.ts
├── public/
│   ├── logo/
│   │   └── logo.png
│   └── images/
│       └── placeholder-car.jpg
└── node_modules/

ส่วน Flow ของ Web-Application ควรเป็นแบบนี้นะ

ปล. ตอนนี้ของเรามีประมาณนี้ ตอนแรกว่าจะมีระบบ approve user ด้วย แต่ว่าเหนื่อยไป ค่อยเพิ่มได้ ไม่เป็นไร

graph TD
    subgraph "Guest Flow (ผู้ใช้ทั่วไปที่ยังไม่ Login)"
        A["Homepage /"] --> B["Browse Cars /browse"];
        A --> C["Login /auth/login"];
        A --> D["Sign Up /auth/sign-up"];
        B --> E{"Car Detail /cars/[id]"};
        E -- กดจองรถ --> C;
    end

    subgraph "Authentication Flow (ขั้นตอนการยืนยันตัวตน)"
        C -- ลืมรหัสผ่าน --> F["Forgot Password /auth/forgot-password"];
        F -- ส่งฟอร์ม --> G["Email Sent..."];
        G -- คลิกลิงก์ในอีเมล --> H["Update Password /auth/update-password"];
        H -- อัปเดตรหัสผ่านสำเร็จ --> C;
        
        D -- กรอกข้อมูลสมัคร --> I["Sign Up Success /auth/sign-up-success"];
        I -- ยืนยันตัวตนสำเร็จ --> C;
        
        C -- Login สำเร็จ --> J["User Dashboard /dashboard"];
    end

    subgraph "Logged-in User Flow (ผู้ใช้ที่ Login แล้ว)"
        J --> B;
        J --> K["My Profile /dashboard/profile"];
        J --> L["My Bookings /dashboard/bookings"];
        J --> M["My Cars /dashboard/cars"];

        M --> N["Add New Car /dashboard/cars/add"];
        M --> O{"View My Car /dashboard/cars/[id]"};
        O --> P["Edit My Car /dashboard/cars/[id]/edit"];

        E -- กดจองรถ (Login แล้ว) --> Q["Checkout /checkout"];
        Q -- ชำระเงินสำเร็จ --> R["Success Page /success"];
        R --> J;
    end
    
    subgraph "Admin Flow (ผู้ใช้ที่เป็น Admin)"
       J -- เป็น Admin --> S["Admin Dashboard /dashboard/admin"];
       S -- จัดการรถ --> M;
       S -- ดูรถที่รออนุมัติ --> T["Approve Cars UI"];
    end

ส่วนนี้จะเป็น flow ของพวก API use-state, use-effect ต่าง ๆ

ทำมาให้ดู เพื่อให้เห็นภาพมากยิ่งขึ้นคับ

sequenceDiagram
    participant Client as Client (Next.js Component)
    participant Server as Next.js Server (API Route / Server Component)
    participant DB as Supabase (DB & Auth)

    %% Case 1: Fetching Public Data (Server Component)
    Note over Client, DB: Case 1: การดึงข้อมูล Public (เช่น หน้า /browse)
    Client->>Server: 1. ผู้ใช้เรียกหน้าเว็บ /browse
    Server->>DB: 2. Server Component เรียกใช้ Supabase Client (ฝั่ง Server)<br>await supabase.from('cars').select('*')
    DB-->>Server: 3. Supabase ส่งข้อมูลรถทั้งหมดกลับมา
    Server-->>Client: 4. Next.js Server สร้างหน้าเว็บ (SSR) ที่มีข้อมูลรถแล้วส่งเป็น HTML ให้ Client

    %% Case 2: Creating/Updating Authenticated Data (Client Component + API Route)
    Note over Client, DB: Case 2: การสร้างข้อมูลใหม่ (เช่น การเพิ่มรถในหน้า /dashboard/cars/add)
    Client->>Client: 5. ผู้ใช้กรอกฟอร์มในหน้าเว็บ
    Client->>Server: 6. ส่ง Request (POST) ไปที่ /api/cars <br>พร้อมข้อมูลจากฟอร์มและ Auth Token (JWT)
    Server->>DB: 7. API Route ตรวจสอบสิทธิ์ผู้ใช้จาก Token กับ Supabase Auth
    DB-->>Server: 8. ยืนยันว่าผู้ใช้มีสิทธิ์
    Server->>DB: 9. สั่งเพิ่มข้อมูลรถลง Database<br>supabase.from('cars').insert({...})
    DB-->>Server: 10. ยืนยันการเพิ่มข้อมูลสำเร็จ
    Server-->>Client: 11. ส่ง Response กลับไปว่าสำเร็จ { success: true }

    %% Case 3: Authentication Flow (Client-side)
    Note over Client, DB: Case 3: การยืนยันตัวตน (เช่น Login)
    Client->>Client: 12. ผู้ใช้กรอกอีเมล/รหัสผ่าน
    Client->>DB: 13. เรียกใช้ supabase.auth.signInWithPassword() โดยตรง<br>Supabase Client (JS) จะส่ง Request ไปยัง Supabase Auth
    DB-->>Client: 14. Supabase Auth ตรวจสอบข้อมูลและส่ง Session กลับมา<br>พร้อมตั้ง Cookie ให้โดยอัตโนมัติ
    Client->>Server: 15. การเรียกหน้าเว็บครั้งถัดไป (เช่น /dashboard) จะมี Cookie ติดไปด้วย
    Server->>Server: 16. Middleware หรือ Server Component <br>อ่านค่า Cookie เพื่อตรวจสอบ Session ก่อนแสดงผลหน้า

Tinnapat Sittisuwan

Tinnapat Sittisuwan

Computer Engineering Student at Chulalongkorn University