mirror of
https://github.com/vercel/commerce.git
synced 2025-07-23 04:36:49 +00:00
polish order confirmation in orders list and order details pages
This commit is contained in:
0
lib/shopify/fragments/order-card.ts
Normal file
0
lib/shopify/fragments/order-card.ts
Normal file
@@ -37,7 +37,7 @@ import {
|
||||
import { getCustomerQuery } from './queries/customer';
|
||||
import { getMenuQuery } from './queries/menu';
|
||||
import { getMetaobjectQuery, getMetaobjectsQuery } from './queries/metaobject';
|
||||
import { getImageQuery, getMetaobjectsByIdsQuery } from './queries/node';
|
||||
import { getFileQuery, getImageQuery, getMetaobjectsByIdsQuery } from './queries/node';
|
||||
import { getCustomerOrdersQuery } from './queries/orders';
|
||||
import { getPageQuery, getPagesQuery } from './queries/page';
|
||||
import {
|
||||
@@ -52,6 +52,7 @@ import {
|
||||
Collection,
|
||||
Connection,
|
||||
Customer,
|
||||
File,
|
||||
FileCreateInput,
|
||||
Filter,
|
||||
Fulfillment,
|
||||
@@ -313,7 +314,7 @@ export async function shopifyCustomerFetch<T>({
|
||||
}
|
||||
}
|
||||
|
||||
const removeEdgesAndNodes = (array: Connection<any>) => {
|
||||
const removeEdgesAndNodes = <T = any>(array: Connection<T>) => {
|
||||
return array.edges.map((edge) => edge?.node);
|
||||
};
|
||||
|
||||
@@ -439,7 +440,7 @@ const reshapeImages = (images: Connection<Image>, productTitle: string) => {
|
||||
const flattened = removeEdgesAndNodes(images);
|
||||
|
||||
return flattened.map((image) => {
|
||||
const filename = image.url.match(/.*\/(.*)\..*/)[1];
|
||||
const filename = (image.url.match(/.*\/(.*)\..*/) || [])[1];
|
||||
return {
|
||||
...image,
|
||||
altText: image.altText || `${productTitle} - ${filename}`
|
||||
@@ -621,25 +622,21 @@ function reshapeOrder(shopifyOrder: ShopifyOrder): Order {
|
||||
shippingMethod: {
|
||||
name: shopifyOrder.shippingLine?.title,
|
||||
price: reshapeMoney(shopifyOrder.shippingLine.originalPrice)!
|
||||
}
|
||||
},
|
||||
warrantyActivationDeadline: shopifyOrder.warrantyActivationDeadline,
|
||||
warrantyStatus: shopifyOrder.warrantyStatus,
|
||||
warrantyActivationInstallation: shopifyOrder.warrantyActivationInstallation,
|
||||
warrantyActivationMileage: shopifyOrder.warrantyActivationMileage,
|
||||
warrantyActivationOdometer: shopifyOrder.warrantyActivationOdometer,
|
||||
warrantyActivationSelfInstall: shopifyOrder.warrantyActivationSelfInstall,
|
||||
warrantyActivationVIN: shopifyOrder.warrantyActivationVIN,
|
||||
orderConfirmation: shopifyOrder.orderConfirmation
|
||||
};
|
||||
|
||||
if (shopifyOrder.customer) {
|
||||
order.customer = reshapeCustomer(shopifyOrder.customer);
|
||||
}
|
||||
|
||||
if (shopifyOrder.warrantyStatus) {
|
||||
order.warrantyStatus = shopifyOrder.warrantyStatus.value as WarrantyStatus;
|
||||
}
|
||||
|
||||
if (shopifyOrder.warrantyActivationDeadline) {
|
||||
order.warrantyActivationDeadline = new Date(shopifyOrder.warrantyActivationDeadline.value);
|
||||
}
|
||||
|
||||
if (shopifyOrder.orderConfirmation) {
|
||||
order.orderConfirmation = shopifyOrder.orderConfirmation.value;
|
||||
}
|
||||
|
||||
return order;
|
||||
}
|
||||
|
||||
@@ -891,6 +888,31 @@ export async function getMetaobjects(type: string) {
|
||||
return reshapeMetaobjects(removeEdgesAndNodes(res.body.data.metaobjects));
|
||||
}
|
||||
|
||||
export async function getAllMetaobjects(type: string) {
|
||||
const allMetaobjects: Metaobject[] = [];
|
||||
let hasNextPage = true;
|
||||
let after: string | undefined;
|
||||
|
||||
while (hasNextPage) {
|
||||
const res = await shopifyFetch<ShopifyMetaobjectsOperation>({
|
||||
query: getMetaobjectsQuery,
|
||||
tags: [TAGS.collections, TAGS.products],
|
||||
variables: { type, after }
|
||||
});
|
||||
|
||||
const metaobjects = reshapeMetaobjects(removeEdgesAndNodes(res.body.data.metaobjects));
|
||||
|
||||
for (const metaobject of metaobjects) {
|
||||
allMetaobjects.push(metaobject);
|
||||
}
|
||||
|
||||
hasNextPage = res.body.data.metaobjects.pageInfo?.hasNextPage || false;
|
||||
after = res.body.data.metaobjects.pageInfo?.endCursor;
|
||||
}
|
||||
|
||||
return allMetaobjects;
|
||||
}
|
||||
|
||||
export async function getMetaobjectsByIds(ids: string[]) {
|
||||
if (!ids.length) return [];
|
||||
|
||||
@@ -1147,3 +1169,19 @@ export const updateOrderMetafields = async ({
|
||||
|
||||
return response.body.data.orderUpdate.order.id;
|
||||
};
|
||||
|
||||
export const getFile = async (id: string) => {
|
||||
const res = await shopifyFetch<{
|
||||
data: {
|
||||
node: File;
|
||||
};
|
||||
variables: {
|
||||
id: string;
|
||||
};
|
||||
}>({
|
||||
query: getFileQuery,
|
||||
variables: { id }
|
||||
});
|
||||
|
||||
return res.body.data.node;
|
||||
};
|
||||
|
@@ -1,6 +1,6 @@
|
||||
export const getMetaobjectsQuery = /* GraphQL */ `
|
||||
query getMetaobjects($type: String!) {
|
||||
metaobjects(type: $type, first: 200) {
|
||||
query getMetaobjects($type: String!, $after: String) {
|
||||
metaobjects(type: $type, first: 200, after: $after) {
|
||||
edges {
|
||||
node {
|
||||
id
|
||||
@@ -16,6 +16,10 @@ export const getMetaobjectsQuery = /* GraphQL */ `
|
||||
}
|
||||
}
|
||||
}
|
||||
pageInfo {
|
||||
hasNextPage
|
||||
endCursor
|
||||
}
|
||||
}
|
||||
}
|
||||
`;
|
||||
|
@@ -3,6 +3,7 @@ export type Maybe<T> = T | null;
|
||||
|
||||
export type Connection<T> = {
|
||||
edges: Array<Edge<T>>;
|
||||
pageInfo?: PageInfo;
|
||||
};
|
||||
|
||||
export type Edge<T> = {
|
||||
@@ -152,10 +153,7 @@ export type Order = {
|
||||
name: string;
|
||||
price: Money;
|
||||
};
|
||||
warrantyStatus?: WarrantyStatus | null;
|
||||
warrantyActivationDeadline?: Date | null;
|
||||
orderConfirmation?: string | null;
|
||||
};
|
||||
} & ShopifyOrderMetafield;
|
||||
|
||||
export type ShopifyOrder = {
|
||||
id: string;
|
||||
@@ -184,10 +182,7 @@ export type ShopifyOrder = {
|
||||
requiresShipping: boolean;
|
||||
shippingLine: ShopifyShippingLine;
|
||||
note: string | null;
|
||||
warrantyStatus?: ShopifyMetafield;
|
||||
warrantyActivationDeadline?: ShopifyMetafield;
|
||||
orderConfirmation?: ShopifyMetafield;
|
||||
};
|
||||
} & ShopifyOrderMetafield;
|
||||
|
||||
type ShopifyShippingLine = {
|
||||
title: string;
|
||||
@@ -685,7 +680,7 @@ export type ShopifyImageOperation = {
|
||||
|
||||
export type ShopifyMetaobjectsOperation = {
|
||||
data: { metaobjects: Connection<ShopifyMetaobject> };
|
||||
variables: { type: string };
|
||||
variables: { type: string; after?: string };
|
||||
};
|
||||
|
||||
export type ShopifyPagesOperation = {
|
||||
@@ -878,20 +873,15 @@ export enum WarrantyStatus {
|
||||
LimitedActivated = 'Limited Activation'
|
||||
}
|
||||
|
||||
export type OrderMetafieldValue<T = string> = {
|
||||
value: T;
|
||||
id: string;
|
||||
key: string;
|
||||
};
|
||||
|
||||
export type ShopifyOrderMetafield = {
|
||||
warrantyStatus: OrderMetafieldValue | null;
|
||||
warrantyActivationDeadline: OrderMetafieldValue | null;
|
||||
warrantyActivationOdometer: OrderMetafieldValue | null;
|
||||
warrantyActivationInstallation: OrderMetafieldValue | null;
|
||||
warrantyActivationSelfInstall: OrderMetafieldValue | null;
|
||||
warrantyActivationVIN: OrderMetafieldValue | null;
|
||||
warrantyActivationMileage: OrderMetafieldValue | null;
|
||||
orderConfirmation: ShopifyMetafield | null;
|
||||
warrantyStatus: ShopifyMetafield | null;
|
||||
warrantyActivationDeadline: ShopifyMetafield | null;
|
||||
warrantyActivationOdometer: ShopifyMetafield | null;
|
||||
warrantyActivationInstallation: ShopifyMetafield | null;
|
||||
warrantyActivationSelfInstall: ShopifyMetafield | null;
|
||||
warrantyActivationVIN: ShopifyMetafield | null;
|
||||
warrantyActivationMileage: ShopifyMetafield | null;
|
||||
};
|
||||
|
||||
export type File = {
|
||||
|
Reference in New Issue
Block a user