Skip to content
This repository has been archived by the owner on Apr 11, 2024. It is now read-only.

Commit

Permalink
Update project to use new Customer Account API
Browse files Browse the repository at this point in the history
  • Loading branch information
paul-phan committed Feb 22, 2024
1 parent 68d077a commit bb9ac66
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 0 deletions.
58 changes: 58 additions & 0 deletions app/graphql/customer-account/CustomerAddressMutations.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// NOTE: https://shopify.dev/docs/api/customer/latest/mutations/customerAddressUpdate
export const UPDATE_ADDRESS_MUTATION = `#graphql
mutation customerAddressUpdate(
$address: CustomerAddressInput!
$addressId: ID!
$defaultAddress: Boolean
) {
customerAddressUpdate(
address: $address
addressId: $addressId
defaultAddress: $defaultAddress
) {
userErrors {
code
field
message
}
}
}
` as const;

// NOTE: https://shopify.dev/docs/api/customer/latest/mutations/customerAddressDelete
export const DELETE_ADDRESS_MUTATION = `#graphql
mutation customerAddressDelete(
$addressId: ID!,
) {
customerAddressDelete(addressId: $addressId) {
deletedAddressId
userErrors {
code
field
message
}
}
}
` as const;

// NOTE: https://shopify.dev/docs/api/customer/latest/mutations/customerAddressCreate
export const CREATE_ADDRESS_MUTATION = `#graphql
mutation customerAddressCreate(
$address: CustomerAddressInput!
$defaultAddress: Boolean
) {
customerAddressCreate(
address: $address
defaultAddress: $defaultAddress
) {
customerAddress {
id
}
userErrors {
code
field
message
}
}
}
` as const;
83 changes: 83 additions & 0 deletions app/graphql/customer-account/CustomerDetailsQuery.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
const CUSTOMER_FRAGMENT = `#graphql
fragment OrderCard on Order {
id
number
processedAt
financialStatus
fulfillments(first: 1) {
nodes {
status
}
}
totalPrice {
amount
currencyCode
}
lineItems(first: 2) {
edges {
node {
title
image {
altText
height
url
width
}
}
}
}
}
fragment AddressPartial on CustomerAddress {
id
formatted
firstName
lastName
company
address1
address2
territoryCode
zoneCode
city
zip
phoneNumber
}
fragment CustomerDetails on Customer {
firstName
lastName
phoneNumber {
phoneNumber
}
emailAddress {
emailAddress
}
defaultAddress {
...AddressPartial
}
addresses(first: 6) {
edges {
node {
...AddressPartial
}
}
}
orders(first: 250, sortKey: PROCESSED_AT, reverse: true) {
edges {
node {
...OrderCard
}
}
}
}
` as const;

// NOTE: https://shopify.dev/docs/api/customer/latest/queries/customer
export const CUSTOMER_DETAILS_QUERY = `#graphql
query CustomerDetails {
customer {
...CustomerDetails
}
}
${CUSTOMER_FRAGMENT}
` as const;
87 changes: 87 additions & 0 deletions app/graphql/customer-account/CustomerOrderQuery.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// NOTE: https://shopify.dev/docs/api/customer/latest/queries/order
export const CUSTOMER_ORDER_QUERY = `#graphql
fragment OrderMoney on MoneyV2 {
amount
currencyCode
}
fragment DiscountApplication on DiscountApplication {
value {
__typename
... on MoneyV2 {
...OrderMoney
}
... on PricingPercentageValue {
percentage
}
}
}
fragment OrderLineItemFull on LineItem {
id
title
quantity
price {
...OrderMoney
}
discountAllocations {
allocatedAmount {
...OrderMoney
}
discountApplication {
...DiscountApplication
}
}
totalDiscount {
...OrderMoney
}
image {
altText
height
url
id
width
}
variantTitle
}
fragment Order on Order {
id
name
statusPageUrl
processedAt
fulfillments(first: 1) {
nodes {
status
}
}
totalTax {
...OrderMoney
}
totalPrice {
...OrderMoney
}
subtotal {
...OrderMoney
}
shippingAddress {
name
formatted(withName: true)
formattedArea
}
discountApplications(first: 100) {
nodes {
...DiscountApplication
}
}
lineItems(first: 100) {
nodes {
...OrderLineItemFull
}
}
}
query Order($orderId: ID!) {
order(id: $orderId) {
... on Order {
...Order
}
}
}
` as const;
11 changes: 11 additions & 0 deletions app/graphql/customer-account/CustomerUpdateMutation.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
export const CUSTOMER_UPDATE_MUTATION = `#graphql
mutation customerUpdate($customer: CustomerUpdateInput!) {
customerUpdate(input: $customer) {
userErrors {
code
field
message
}
}
}
`;
13 changes: 13 additions & 0 deletions app/routes/($locale).account.$.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {redirect, type LoaderFunctionArgs} from '@shopify/remix-oxygen';

// fallback wild card for all unauthenticated routes in account section
export async function loader({context, params}: LoaderFunctionArgs) {
await context.customerAccount.handleAuthStatus();

const locale = params.locale;
return redirect(locale ? `/${locale}/account` : '/account', {
headers: {
'Set-Cookie': await context.session.commit(),
},
});
}
5 changes: 5 additions & 0 deletions app/routes/($locale).account_.authorize.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type {LoaderFunctionArgs} from '@shopify/remix-oxygen';

export async function loader({context, params}: LoaderFunctionArgs) {
return context.customerAccount.authorize();
}
5 changes: 5 additions & 0 deletions app/routes/($locale).account_.login.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import type {LoaderFunctionArgs} from '@shopify/remix-oxygen';

export async function loader({params, request, context}: LoaderFunctionArgs) {
return context.customerAccount.login();
}
20 changes: 20 additions & 0 deletions app/routes/($locale).account_.logout.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import {
redirect,
type ActionFunction,
type AppLoadContext,
type LoaderFunctionArgs,
type ActionFunctionArgs,
} from '@shopify/remix-oxygen';

export async function doLogout(context: AppLoadContext) {
return context.customerAccount.logout();
}

export async function loader({params}: LoaderFunctionArgs) {
const locale = params.locale;
return redirect(locale ? `/${locale}` : '/');
}

export const action: ActionFunction = async ({context}: ActionFunctionArgs) => {
return doLogout(context);
};

0 comments on commit bb9ac66

Please sign in to comment.