AI-Assisted Programming for E-Commerce Frontends: A Practical 0-to-1 Guide with Quality Assessment

    This article explains how to build an e-commerce frontend with AI-assisted programming, covering task decomposition across ten pages, common pitfalls in generated code, prompt engineering as verifiable specifications, project structure with a shared foundation layer, code examples for data access, form validation, and auth guards, plus a quantifiable acceptance checklist for quality evaluation.

    1. Background: Why E-Commerce Sites Are a Good Test for AI Coding

    E-commerce sites cover a broad range of business scenarios. Product browsing, order placement, account management, and asset viewing all coexist, with clear state dependencies between pages: the login state determines whether the user center is accessible, the checkout page depends on product data, and the order success page depends on the order result. A project like this tests not only the quality of individual page generation, but also cross-page consistency.

    With AI-assisted programming, single-page generation usually produces a runnable interface. The real challenge is multi-page coordination: whether routes form a closed loop, whether visual standards are unified, whether data flows end to end, and whether the project structure can support future iteration. This article addresses these questions with actionable methods.

    2. Task Decomposition: Boundaries and Dependencies of Ten Pages

    The ten pages can be divided into three groups by business domain:

    • Browsing domain: home page (product list), product detail page, checkout page, order success page
    • Account domain: login page, registration page
    • User center domain: dashboard, orders, wallet (cards and vouchers), settings

    The dependencies between pages are as follows:

    1. The product list page links to the product detail page, and the detail page carries the product identifier into the checkout page.
    2. After the checkout page submits an order, it navigates to the success page, which displays the order number.
    3. After successful registration, the page either guides the user to the login page or completes automatic login directly.
    4. The four user center pages share the same login state; unauthenticated users must be redirected to the login page.

    When decomposing tasks, first clarify each page’s inputs (route parameters, login state, API data) and outputs (navigation targets, state changes). Only with clear boundaries can the interfaces between pages be aligned.

    3. Common Problems and Root Cause Analysis

    3.1 Site Completeness and Route Consistency

    When AI tools generate code page by page, they typically do not maintain a global route table automatically. Common consequences include links pointing to nonexistent paths, inconsistent parameter names across pages (e.g., page A uses productId while page B uses id), and login guards covering only some pages.

    The solution is to define a route manifest and agree on parameter naming rules before generating any pages.

    3.2 Visual and Interaction Consistency

    If colors, font sizes, and spacing are not defined as unified variables, every page ends up with its own hardcoded values. The result is that the same type of button appears in different colors on different pages, and in some cases text and background colors match, making the text unreadable.

    Visual standards should be defined centrally as design tokens (colors, font sizes, spacing, border radii). All pages should reference tokens only, never raw color values.

    3.3 Form Validation

    The login, registration, and checkout pages all contain forms. Complete validation typically covers:

    • Required field checks
    • Format checks, e.g., phone numbers and email addresses
    • Length and password strength checks
    • Disabling the submit button during submission to prevent duplicate submissions
    • Echoing server-returned error messages back to the corresponding fields

    Many generated results implement only the first two. When validation rules are scattered across pages, changing one rule requires edits in multiple places and is prone to omissions.

    3.4 Data-View Coupling

    Writing mock data directly inside components is the most common structural problem in generated code. When replacing mock data with real APIs, every page needs individual modification, which is labor-intensive and error-prone. The correct approach is to encapsulate data access in a separate service layer, with pages calling only service functions.

    3.5 Responsive Adaptation

    Styles written only for mobile widths cause stretched or broken layouts on desktop. When login and registration pages use fixed widths, forms often deform on narrow screens. Flexible layouts should be used, with breakpoints defined for common widths.

    3.6 The Impact of Placeholder Assets

    When product images use placeholders, their aspect ratios may not match real assets. Product cards then show inconsistent heights and misaligned grids, and these issues are hard to spot during the placeholder phase. Use containers with fixed aspect ratios for images, and ensure the layout remains unchanged after real images are swapped in.

    3.7 Project Structure

    Messy directory hierarchies, same-name directories nested inside each other, and empty directories reserved for hooks or context can directly prevent the project from starting. When fixing these issues, investigating them one by one often costs more than regenerating the affected modules.

    4. Prompt Design: Write Requirements as Verifiable Specifications

    The role of a prompt is to provide verifiable specifications, not just describe the desired effect. A complete prompt typically includes the following parts:

    1. [Technical Conventions]
    2. - Frontend framework: component-based framework, function components
    3. - Styling: global design tokens + page-level style files; no hardcoded color values
    4. - Data: all data access goes through the services directory; no hardcoded mock data in pages
    5. [Global Constraints]
    6. - Route table defined centrally in the routes directory; parameter names unified as productId / orderId
    7. - Login state managed in the stores directory; user center pages must have login guards
    8. [Page List]
    9. 1. Home page: product list with category filtering; clicking navigates to detail page
    10. 2. Product detail page: displays images, price, and variants; provides a purchase entry
    11. 3. Checkout page: shipping info form; on submit, generates an order and navigates to the success page
    12. ... (remaining pages in the same format)
    13. [Acceptance Criteria]
    14. - All internal links reachable, no 404s
    15. - Form validation covers required, format, and submit-disable rules
    16. - No horizontal scrolling at 375, 768, and 1280 widths

    The benefit of this format is that every requirement can be checked item by item. If the generated output fails a requirement, you can pinpoint the exact page and rule.

    5. Engineering Constraints: Build the Shared Layer First, Then Generate Pages

    It is recommended to generate the shared foundation first, then generate business pages one by one. A reference directory structure:

    1. src/
    2. ├── pages/ # Business pages, one directory per page
    3. │ ├── home/
    4. │ ├── product-detail/
    5. │ ├── checkout/
    6. │ ├── checkout-success/
    7. │ ├── login/
    8. │ ├── register/
    9. │ └── user-center/ # Includes dashboard, orders, wallet, settings
    10. ├── components/ # Reusable components across pages
    11. ├── layouts/ # Header, footer, user-center sidebar, etc.
    12. ├── routes/ # Route table and login guards
    13. ├── services/ # Data access layer, with mock/real API switching
    14. ├── stores/ # Global state such as login state and cart
    15. ├── styles/ # Design tokens and global styles
    16. └── utils/ # Utility functions such as form validators

    The directory structure must satisfy two conditions: each concept lives in exactly one place, and each directory name maps one-to-one with its responsibility. Same-name nested directories usually indicate a path misunderstanding during generation; fix it promptly rather than layering more generation on top.

    6. Key Module Examples

    6.1 Data Access Layer: Separating Mock Data from Real APIs

    Pages should not care where data comes from. A configuration flag switches the data source, so replacing APIs only requires changes in the service layer.

    1. // services/productService.js
    2. import { mockProducts } from './mock/products.js';
    3. import { request } from './http.js';
    4. const USE_MOCK = true; // Boolean injected by the build configuration
    5. export async function fetchProducts() {
    6. if (USE_MOCK) {
    7. return mockProducts;
    8. }
    9. return request('/api/products');
    10. }

    Pages only call fetchProducts() and never reference mockProducts directly. This keeps mock data and page logic independent of each other.

    6.2 Centralized Form Validation Rules

    Validation rules live in one place, and pages reference them through a declarative rule table.

    1. // utils/validators.js
    2. export const rules = {
    3. phone: (value = '') =>
    4. /^1[3-9]\d{9}$/.test(value) || 'Please enter a valid 11-digit phone number',
    5. email: (value = '') =>
    6. /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(value) || 'Invalid email format',
    7. password: (value = '') =>
    8. value.length >= 8 || 'Password must be at least 8 characters',
    9. };
    10. export function validateForm(values, schema) {
    11. const errors = {};
    12. for (const [field, checks] of Object.entries(schema)) {
    13. for (const check of checks) {
    14. const result = check(values[field]);
    15. if (result !== true) {
    16. errors[field] = result;
    17. break;
    18. }
    19. }
    20. }
    21. return errors;
    22. }

    Usage on the registration page:

    1. const schema = {
    2. phone: [rules.phone],
    3. password: [rules.password],
    4. };
    5. const errors = validateForm(formValues, schema);

    Client-side validation only improves interaction experience; it cannot replace server-side validation. Rules involving money, inventory, and account security must be re-validated on the server.

    6.3 Login State and User Center Guards

    The four user center pages share the same guard logic. Guards should be handled uniformly at the routing layer instead of being checked in each page individually.

    1. // routes/guards.js
    2. export function requireLogin(next, isLoggedIn, redirectTo = '/login') {
    3. return isLoggedIn ? next() : redirectTo;
    4. }

    With guard logic in a single place, adjusting redirect rules later stays within a controlled scope.

    7. Quality Assessment: A Quantifiable Acceptance Checklist

    Whether generated output is usable should be verified item by item against a checklist, not judged by visual impression alone.

    Dimension Check Item Pass Criteria
    Site completeness All ten pages accessible No 404s in routes; all internal links reachable
    Data flow Data passed from checkout to success page The order number shown on the success page matches the submitted one
    Visual consistency Colors, font sizes, spacing reference unified tokens No hardcoded color values in spot-checked pages
    Readability Contrast between button text and background Meets common accessibility standards; body text contrast at least 4.5:1
    Responsiveness Layout at 375, 768, and 1280 widths No horizontal scrolling