Skip to content

Commit

Permalink
Add cart endpoints, handle woo session
Browse files Browse the repository at this point in the history
  • Loading branch information
dbertella committed Nov 22, 2020
1 parent 11df5af commit 4b7bc3e
Show file tree
Hide file tree
Showing 15 changed files with 298 additions and 117 deletions.
8 changes: 6 additions & 2 deletions lib/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const API_URL = `${process.env.WORDPRESS_API_URL}`;
const API_URL = `${process.env.NEXT_PUBLIC_WORDPRESS_API_URL}`;

export async function fetchAPI(query: string, options?: { variables: object }) {
const headers = { "Content-Type": "application/json" };
Expand All @@ -9,6 +9,11 @@ export async function fetchAPI(query: string, options?: { variables: object }) {
] = `Bearer ${process.env.WORDPRESS_AUTH_REFRESH_TOKEN}`;
}

const session = process.browser ? localStorage.getItem("woo-session") : null;
if (session) {
console.log(session);
headers["woocommerce-session"] = `Session ${session}`;
}
const { variables } = options ?? {};

const res = await fetch(API_URL, {
Expand Down Expand Up @@ -45,4 +50,3 @@ export async function getPreviewPost(id: string, idType = "DATABASE_ID") {
);
return data.post;
}

File renamed without changes.
72 changes: 72 additions & 0 deletions lib/clientGql/add-to-cart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { fetchAPI } from "lib";
import { AddToCartInput } from "lib/graphql";

export async function addToCart(input: AddToCartInput) {
const data = await fetchAPI(
`
mutation AddToCart($input: AddToCartInput!) {
addToCart(input: $input) {
cartItem {
key
product {
id
name
description
type
onSale
slug
averageRating
reviewCount
image {
id
sourceUrl
altText
}
galleryImages {
nodes {
id
sourceUrl
altText
}
}
}
variation {
id
name
description
type
onSale
price
regularPrice
salePrice
image {
id
sourceUrl
altText
}
attributes {
nodes {
id
attributeId
name
value
}
}
}
quantity
total
subtotal
subtotalTax
}
}
}
`,
{
variables: {
input,
},
}
);

return data;
}
File renamed without changes.
File renamed without changes.
90 changes: 90 additions & 0 deletions lib/clientGql/get-cart.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { fetchAPI } from "lib";

export async function getCart() {
const data = await fetchAPI(
`
query GetCart {
cart {
contents {
nodes {
key
product {
id
name
description
type
onSale
slug
averageRating
reviewCount
image {
sourceUrl
srcSet
altText
}
}
variation {
id
name
description
type
onSale
price
regularPrice
salePrice
image {
id
sourceUrl
srcSet
altText
title
}
attributes {
nodes {
id
name
value
}
}
}
quantity
total
subtotal
subtotalTax
}
}
appliedCoupons {
nodes {
id
discountType
amount
dateExpiry
products {
nodes {
id
}
}
productCategories {
nodes {
id
}
}
}
}
subtotal
subtotalTax
shippingTax
shippingTotal
total
totalTax
feeTax
feeTotal
discountTax
discountTotal
}
}
`
);

return data;
}
File renamed without changes.
4 changes: 4 additions & 0 deletions lib/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,3 +9,7 @@ export * from "./queries/products";
export * from "./queries/home";
export * from "./queries/news";
export * from "./queries/page-by-slug";

// Ecommerce
export * from "./clientGql/get-cart";
export * from "./clientGql/add-to-cart";
101 changes: 0 additions & 101 deletions lib/queries/get-cart.ts

This file was deleted.

12 changes: 12 additions & 0 deletions lib/queries/product-by-slug.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export async function getProductBySlug(slug: string) {
fragment ProductFields on Product {
name
slug
databaseId
seo {
canonical
title
Expand Down Expand Up @@ -54,6 +55,17 @@ export async function getProductBySlug(slug: string) {
shortDescription
type
...ProductFields
productElements {
productGallery {
image {
id
sourceUrl
srcSet
altText
title
}
}
}
productTypes {
edges {
node {
Expand Down
11 changes: 3 additions & 8 deletions lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,25 +74,20 @@ export type PageSingle = {
};

export type ProductListItem = {
id: string;
id: number;
name: string;
slug: string;
price: string;
image: MediaImage;
};

export type ProductSingle = {
id: string;
name: string;
productId: string;
export type ProductSingle = ProductListItem & {
averageRating: string;
slug: string;
description: string;
shortDescription: string;
image: MediaImage;
price: string;
onSale: boolean;
type: string;
databaseId: number;
seo: Seo;
productCategories: {
edges: CategoryListItem[];
Expand Down
8 changes: 7 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,25 @@
},
"dependencies": {
"-": "^0.0.1",
"@types/cookie": "^0.4.0",
"@types/lodash": "^4.14.164",
"D": "^1.0.0",
"cookie": "^0.4.1",
"dayjs": "^1.9.5",
"graphql": "^15.4.0",
"graphql-import": "^1.0.2",
"graphql-tag": "^2.11.0",
"immer": "^7.0.14",
"js-cookie": "^2.2.1",
"lodash": "^4.17.20",
"next": "10.0.1",
"react": "17.0.1",
"react-cookie-consent": "^6.0.0",
"react-dom": "17.0.1",
"react-image-gallery": "^1.0.8",
"theme-ui": "^0.3.1"
"react-query": "^2.26.3",
"theme-ui": "^0.3.1",
"uuid": "^8.3.1"
},
"devDependencies": {
"@graphql-codegen/cli": "1.19.1",
Expand All @@ -35,6 +40,7 @@
"@types/react": "^16.9.55",
"@types/react-image-gallery": "^1.0.0",
"@types/theme-ui": "^0.3.7",
"@types/uuid": "^8.3.0",
"globby": "^11.0.1",
"typescript": "^4.0.5"
}
Expand Down
Loading

0 comments on commit 4b7bc3e

Please sign in to comment.