Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add cart endpoints, handle woo session #11

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 19 additions & 2 deletions lib/api.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
const API_URL = `${process.env.WORDPRESS_API_URL}`;
const API_URL = `${process.env.NEXT_PUBLIC_WORDPRESS_API_URL}`;

const WooKey = {
LocalStorage: "woo-session",
HeaderKey: "woocommerce-session",
};

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

const session = process.browser
? localStorage.getItem(WooKey.LocalStorage)
: null;
if (session) {
headers[WooKey.HeaderKey] = `Session ${session}`;
}
const { variables } = options ?? {};

const res = await fetch(API_URL, {
Expand All @@ -20,6 +31,13 @@ export async function fetchAPI(query: string, options?: { variables: object }) {
}),
});

if (process.browser && res.headers.get(WooKey.HeaderKey)) {
localStorage.setItem(
WooKey.LocalStorage,
res.headers.get(WooKey.HeaderKey) as string
);
}

const json = await res.json();
if (json.errors) {
console.error(json.errors);
Expand All @@ -45,4 +63,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
Loading