AI-Assisted Programming Practice: Building an E-commerce Frontend Project from Scratch

    This article explores using an e-commerce frontend project as a benchmark to evaluate AI-assisted programming capabilities. It covers requirement breakdown, route planning, architecture design for separating presentation, state, and data, as well as comprehensive form validation for login and registration flows.

    I. Why Use an E-commerce Frontend as a Practice Scenario

    E-commerce sites cover most typical frontend development challenges: page routing, form input and validation, list and detail data display, login state management, order status transitions, and responsive layout. A complete shopping flow requires more than a dozen pages to work together, which is sufficient to test the structure, consistency, and maintainability of the generated code. Therefore, it is suitable as a benchmark scenario for evaluating AI-assisted programming.

    Evaluation should focus on three levels. First, whether it can produce a runnable base project in one go. Second, whether the generated code conforms to project conventions. Third, whether modifications are controllable during subsequent iterations. Simply looking at “whether it can run” easily leads to one-sided conclusions.

    II. Requirement Breakdown: Page List and Route Planning

    Before starting, write the requirements as a page list. It is recommended to group them by usage scenarios:

    • Public pages: Product list or homepage, product detail page, checkout page, purchase success page
    • Account pages: Login page, registration page
    • User center: Overview (dashboard), order list, account assets and coupons, personal settings

    Each page should specify four items: route path, access permissions (whether login is required), dependent data, and main user actions. For example, all sub-pages in the user center require login. When an unauthenticated user accesses them, they should be redirected to the login page while retaining the original address, and then redirected back after successful login.

    It is recommended to use a nested structure for routing. The user center serves as the parent layout, uniformly rendering the side navigation and authentication logic, while child pages are only responsible for their own content. This prevents repeating navigation and permission checks on every page.

    Diagram structure:

    1. / Product list
    2. /products/:id Product detail
    3. /checkout/:id Checkout page
    4. /checkout/success Purchase success
    5. /login Login
    6. /register Register
    7. /account User center (requires login)
    8. /account/overview Overview
    9. /account/orders Orders
    10. /account/assets Assets and coupons
    11. /account/settings Settings

    III. Architecture Design: Separating Presentation, State, and Data

    A common problem with AI-generated code is the “one file does everything” approach: styles, data, business logic, and UI are mixed together. This structure causes numerous files to be modified simultaneously when replacing mock data with real APIs. It is recommended to divide the project into three layers:

    1. Presentation layer: Only responsible for rendering, receives properties, and does not directly access data sources.
    2. State layer: Manages cross-page shared data such as shopping cart and login state.
    3. Data layer: Uniformly encapsulates data fetching. During development, it reads mock data; during integration, it is replaced with API calls.

    Pseudocode:

    1. // Data layer: All pages fetch data only through functions here
    2. function getProduct(id):
    3. return mockProducts.find(id) // Later replaced with real requests
    4. // Page layer: Only cares about presentation
    5. page ProductDetail(id):
    6. product = getProduct(id)
    7. render(ProductInfo(product))

    The benefit of this approach is that modifying the data source only requires changes in the data layer, leaving page code untouched.

    Styles should also be managed independently. It is recommended to use unified theme variables to define colors, spacing, and font sizes. All components should reference variables instead of hardcoding color values. This ensures color consistency and makes it easier to adjust the visual style later.

    IV. Login and Registration: Comprehensive Form Validation Design

    Form validation is the most easily overlooked part. Just doing “non-empty checks” is far from enough; it should be handled in three layers:

    • Input layer validation: Email format, phone number length, password length and character combination. Provide instant feedback when the user leaves the input field.
    • Submission layer validation: Uniformly validate all fields before submission, preventing invalid requests from being sent.
    • Server-side validation: Even if the frontend has validated, the server must still validate again. Frontend validation is for improving user experience and does not constitute a security boundary.

    Rule examples:

    1. rules:
    2. email: Required; must match email format
    3. phone: Required; 11 digits
    4. password: Required; 8 to 32 characters; must contain both letters and numbers
    5. confirm: Must match password

    Error messages should be placed near the corresponding fields and explain how to fix the issue. For example, display “Password must contain both letters and numbers” instead of just “Invalid format.”

    The layout of the form also needs attention. The registration page has many fields, which can easily cause label and input box misalignment or button overflow on narrow screens. The solution is to use a flexible layout, allowing fields to stack vertically on narrow screens, and setting a maximum width for input boxes to prevent them from stretching too long on wide screens.

    V. Shopping Flow: From Product to Purchase Success

    The shopping flow is essentially a state chain: browsing products, selecting specifications and quantities, confirming…