Building E-commerce Front-Ends with AI-Assisted Coding: Evaluation and Practices

    This article explores the evaluation and practical implementation of building an e-commerce front-end using AI-assisted coding tools. It covers critical aspects such as routing configuration, design consistency, multi-level form validation, decoupling mock data, responsive design acceptance, AI generation strategies, and a troubleshooting loop for runtime errors to ensure a robust and functional web application.

    1. Why Choose an E-commerce Site for Evaluation

    An e-commerce site consists of multiple interconnected pages, including product displays, checkout, account management, orders, and assets. It involves common front-end elements such as routing, state management, forms, lists, and permission control, making it an ideal test case to evaluate whether generated code can form a complete system. During evaluation, focus on three key areas: whether the pages are complete, whether consistency is maintained across pages, and whether the project runs correctly in the target environment.

    2. Decompose Page Inventory and Routing First

    Before letting the tool generate code, outline the page inventory. The following ten pages represent a typical shopping site scope:

    • Home (Product List)
    • Product Detail Page
    • Checkout Page (Confirm product and shipping information)
    • Order Success Page
    • Login Page
    • Register Page
    • User Center: Overview Dashboard
    • User Center: Order List
    • User Center: Wallet and Coupons
    • User Center: Account Settings

    Routing should be centrally defined in a single file to prevent jump paths from scattering across different pages.

    1. // routes.js
    2. export const routes = {
    3. home: '/',
    4. product: '/product/:id',
    5. checkout: '/checkout/:id',
    6. orderSuccess: '/order/success',
    7. login: '/login',
    8. register: '/register',
    9. account: {
    10. dashboard: '/account',
    11. orders: '/account/orders',
    12. wallet: '/account/wallet',
    13. settings: '/account/settings',
    14. },
    15. };

    This article assumes that the checkout page and user center require authentication. Such routes should be handled through unified guard logic. If each page checks the login status independently, the probability of missing validation increases significantly as the number of pages grows.

    3. Ensure Overall Site Consistency

    Consistency issues typically manifest as inconsistent button colors across pages, varying spacing, or missing navigation bars. The solution is to define a set of design tokens (colors, spacing, border-radius, etc.) and have all pages reference them uniformly.

    1. :root {
    2. --color-primary: #1677ff;
    3. --color-text: #1f2328;
    4. --color-surface: #ffffff;
    5. --space-2: 8px;
    6. --space-4: 16px;
    7. --radius-md: 6px;
    8. }
    9. .btn-primary {
    10. background: var(--color-primary);
    11. color: var(--color-surface);
    12. padding: var(--space-2) var(--space-4);
    13. border-radius: var(--radius-md);
    14. }

    The contrast ratio between button text and background also needs to be checked. According to standard accessibility guidelines, the contrast ratio for normal text should be at least 4.5:1. Issues like white text on a white background can be caught early through contrast checks.

    The header, footer, and sidebar navigation should be carried by a unified layout component. The four pages in the user center share the same sidebar component, replacing only the content area when switching pages. This ensures that the navigation structure remains consistent even if individual pages are produced by different generation processes.

    4. Form Validation Across Three Levels

    Form validation can be divided into three levels:

    1. Instant validation during input, checking formats and required fields.
    2. Overall validation before submission, confirming relationships between fields.
    3. Server-side validation. Front-end validation improves user experience but cannot replace server-side validation.
    1. // validators.js
    2. export const rules = {
    3. email: (v) => /^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(v) || 'Invalid email format',
    4. phone: (v) => /^\+?\d{10,15}$/.test(v) || 'Invalid phone number format',
    5. password: (v) => v.length >= 8 || 'Password must be at least 8 characters',
    6. };
    7. export function validate(form, schema) {
    8. const errors = {};
    9. for (const field of Object.keys(schema)) {
    10. const result = schema[field](form[field] ?? '');
    11. if (result !== true) {
    12. errors[field] = result;
    13. }
    14. }
    15. return errors;
    16. }

    A rule easily overlooked on the registration page is that the confirm password must match the password field. The checkout page needs to verify that the purchase quantity is a positive integer and the shipping address is not empty. When evaluating generated results, verify these rules one by one instead of just looking at the form’s appearance.

    5. Decouple Mock Data from Business Logic

    If mock data is written directly into page components, subsequent integration with real APIs requires modifying each page, making the scope of changes difficult to control. A more robust approach is to encapsulate data access within a service layer:

    1. // services/productService.js
    2. import { mockProducts } from '../mock/products';
    3. export async function listProducts() {
    4. return mockProducts;
    5. }
    6. export async function getProduct(id) {
    7. return mockProducts.find((p) => p.id === id) ?? null;
    8. }

    Pages should only call service functions and not directly read mock data. When switching to real APIs later, only the service layer needs modification. The field structure of the mock data should also match the target API data structure; otherwise, field adaptation issues will arise during replacement.

    Images need standardized handling as well. Placeholder images should be output through a unified component, and the image field for each product should be recorded in the data. This allows for systematic replacement before launch without missing any items.

    6. Responsive Design and Visual Acceptance

    Acceptance testing should cover at least three viewport widths: mobile (approx. 375px), tablet (approx. 768px), and desktop (approx. 1280px). At each width, check for horizontal scrolling, text overlap, or button overflow.

    Deformation issues common in login and registration forms are often caused by fixed widths. Using a maximum width combined with an adaptive width solves most cases:

    1. .auth-card {
    2. width: 100%;
    3. max-width: 400px;
    4. margin: 0 auto;
    5. }

    The information density of the product detail page also requires separate inspection. A functional detail page should typically include a title, price, specification options, stock status, product description, and review entry. A page with only an image and a single line of title, even if it runs, cannot be considered complete.

    7. Choosing Generation Modes: One-Shot vs. Phased Execution

    AI-assisted coding tools typically offer two modes of operation, which can be compared on the following dimensions during evaluation:

    • One-shot generation: Outputs the entire project at once. It is fast and suitable for projects with simple page structures and low coupling. The downside is that errors are hard to locate, and interface inconsistencies between pages will be introduced globally.
    • Phased execution: Builds the skeleton (routing, layout, data layer) first, then implements pages one by one, running validation at each stage. It takes longer but allows checks at every step.

    For projects like e-commerce sites with many pages and strong interdependencies, it is recommended to generate the skeleton in one shot first, then supplement pages in phases, running a complete check after each stage. Regardless of the mode chosen, key files (routing, guards, service layers) should be manually reviewed.

    8. Troubleshooting Loop for Runtime Errors

    When a generated project fails to run, follow these steps:

    1. Install dependencies and start the project, recording only the first error, as subsequent errors are often caused by it.
    2. Locate the file and line number based on the error message.
    3. Determine the error type: dependency version mismatch, incorrect import path, or business logic error.
    4. Fix it and immediately rerun to confirm the issue is resolved.
    5. If consecutive fixes fail, narrow the scope: regenerate the failed page separately, or roll back to the last runnable version.

    Common issues include inconsistent capitalization in import paths, mismatched module export methods, and reading properties from asynchronous data without null checks. Here is a fix example for the latter:

    1. // Issue: Reading properties directly when data is not returned
    2. const product = await getProduct(id);
    3. console.log(product.name);
    4. // Fix: Check for null first, then render
    5. const product = await getProduct(id);
    6. if (!product) {
    7. renderNotFound();
    8. return;
    9. }
    10. console.log(product.name);

    Using version control tools to save each runnable version makes rollback operations straightforward.

    9. Acceptance Checklist

    • All ten pages are accessible with no dead links
    • Pages requiring login correctly redirect to the login page when unauthenticated