Skip to content

Commit

Permalink
feat(Admin): Add address validation to admin pages
Browse files Browse the repository at this point in the history
Adds address autocomplete to:

- Customer page
  - Create new customer
- Orders page
  - Create new order
  - Edit existing order
  • Loading branch information
cblanc committed Jun 18, 2020
1 parent b4c63ac commit 83a111f
Show file tree
Hide file tree
Showing 9 changed files with 95 additions and 408 deletions.
19 changes: 19 additions & 0 deletions lib/admin-customers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { setupBind } from "@ideal-postcodes/jsutil";

import { Config, setupAutocomplete } from "./extension";

import { selectors } from "./billing";

const parentScope = "fieldset";
const parentTest = (e: HTMLElement) => e.className === "admin__fieldset";

const bind = (config: Config) => {
setupBind({ selectors, parentScope, parentTest }).forEach(({ targets }) => {
console.log(selectors);
setupAutocomplete(config, targets);
});
};

const pageTest = () => window.location.pathname.includes("/customer");

export const bindings = { bind, pageTest };
41 changes: 41 additions & 0 deletions lib/admin-orders.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { setupBind } from "@ideal-postcodes/jsutil";

import { Config, setupAutocomplete } from "./extension";

export const billing = {
line_1: '[name="order[billing_address][street][0]"]',
line_2: '[name="order[billing_address][street][1]"]',
line_3: '[name="order[billing_address][street][2]"]',
postcode: '[name="order[billing_address][postcode]"]',
post_town: '[name="order[billing_address][city]"]',
organisation: '[name="order[billing_address][company]"]',
county: '[name="order[billing_address][region_id]"]',
country: '[name="order[billing_address][country_id]"]'
};

export const shipping = {
line_1: '[name="order[shipping_address][street][0]"]',
line_2: '[name="order[shipping_address][street][1]"]',
line_3: '[name="order[shipping_address][street][2]"]',
postcode: '[name="order[shipping_address][postcode]"]',
post_town: '[name="order[shipping_address][city]"]',
organisation: '[name="order[shipping_address][company]"]',
county: '[name="order[shipping_address][region_id]"]',
country: '[name="order[shipping_address][country_id]"]'
};

const selectorList = [billing, shipping];

const parentScope = "fieldset";

const bind = (config: Config) => {
selectorList.forEach(selectors => {
setupBind({ selectors, parentScope }).forEach(({ targets }) => {
setupAutocomplete(config, targets);
});
});
};

const pageTest = () => window.location.pathname.includes("/sales");

export const bindings = { bind, pageTest };
2 changes: 1 addition & 1 deletion lib/billing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,6 @@ const bind = (config: Config) => {
});
};

const pageTest = () => true;
const pageTest = () => window.location.pathname.includes("/checkout");

export const bindings = { bind, pageTest };
6 changes: 6 additions & 0 deletions lib/customer.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { bindings as b } from "./multishipping";
const { bind } = b;

const pageTest = () => window.location.pathname.includes("/customer/address");

export const bindings = { bind, pageTest };
17 changes: 15 additions & 2 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,19 @@ import { setup } from "@ideal-postcodes/jsutil";

import { bindings as shipping } from "./shipping";
import { bindings as billing } from "./billing";
import { bindings as multiAndCustomer } from "./multishipping-and-customer";
import { bindings as customer } from "./customer";
import { bindings as multishipping } from "./multishipping";
import { bindings as adminOrders } from "./admin-orders";
import { bindings as adminCustomers } from "./admin-customers";

setup({ bindings: [shipping, billing, multiAndCustomer], window });
setup({
bindings: [
shipping,
billing,
customer,
multishipping,
adminOrders,
adminCustomers
],
window
});
3 changes: 1 addition & 2 deletions lib/multishipping-and-customer.ts → lib/multishipping.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ const linesIdentifier = {
};

const bind = (config: Config) => {
console.log("Calling bind");
setupBind({
selectors,
parentTest: e => e.getAttribute("id") === "form-validate"
Expand All @@ -36,6 +35,6 @@ const bind = (config: Config) => {
});
};

const pageTest = () => true;
const pageTest = () => window.location.pathname.includes("/multishipping");

export const bindings = { bind, pageTest };
14 changes: 2 additions & 12 deletions lib/shipping.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { setupBind, Binding } from "@ideal-postcodes/jsutil";
import { selectors } from "./billing";

import {
Config,
Expand All @@ -7,17 +8,6 @@ import {
hoistCountry
} from "./extension";

const selectors = {
line_1: '[name="street[0]"]',
line_2: '[name="street[1]"]',
line_3: '[name="street[2]"]',
postcode: '[name="postcode"]',
post_town: '[name="city"]',
organisation: '[name="company"]',
county: '[name="region"]',
country: '[name="country_id"]'
};

const bind = (config: Config) => {
setupBind({ selectors }).forEach(({ targets }) => {
hoistCountry(config, targets);
Expand All @@ -26,6 +16,6 @@ const bind = (config: Config) => {
});
};

const pageTest = () => true;
const pageTest = () => window.location.pathname.includes("/checkout");

export const bindings: Binding = { bind, pageTest };
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,12 @@
},
"scripts": {
"semantic-release": "semantic-release",
"start": "rollup -c",
"start": "make bootstrap && make set-base-url",
"build": "rollup -c",
"watch": "rollup -cw",
"lint": "eslint lib/**/*.ts",
"test:e2e": "cypress run --project test/e2e",
"test:all": "make bootstrap && make set-base-url && npm run test:e2e && make down",
"test:all": "npm start && npm run test:e2e && make down",
"test:snapshot": "cypress run --project test/snapshot",
"test:watch": "rollup -c test/snapshot/rollup.config.js -w",
"test": "npm run test:snapshot"
Expand Down
Loading

0 comments on commit 83a111f

Please sign in to comment.