Skip to content

Commit

Permalink
feat: create adress action
Browse files Browse the repository at this point in the history
  • Loading branch information
soutofernando authored and marcoferreiradev committed Oct 14, 2024
1 parent b617008 commit d9c45c6
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 76 deletions.
91 changes: 91 additions & 0 deletions vtex/actions/address/createAddress.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import { PostalAddress } from "../../../commerce/types.ts";
import { AppContext } from "../../mod.ts";
import { parseCookie } from "../../utils/vtexId.ts";

interface Address {
name?: string;
addressName: string;
addressType?: string;
city?: string;
complement?: string;
country?: string;
geoCoordinates?: number[];
neighborhood?: string;
number?: string;
postalCode?: string;
receiverName?: string;
reference?: string;
state?: string;
street?: string;
}

interface AddressInput {
receiverName?: string;
complement?: string | null;
neighborhood?: string | null;
country?: string;
state?: string;
number?: string | null;
street?: string;
geoCoordinates?: number[];
postalCode?: string;
city?: string;
reference?: string | null;
addressName: string;
addressType?: string;
}

async function loader(
props: Address,
req: Request,
ctx: AppContext,
): Promise<
| PostalAddress & {
receiverName?: string | null;
complement?: string | null;
}
| null
> {
const { io } = ctx;
const { cookie } = parseCookie(req.headers, ctx.account);

const mutation = `
mutation SaveAddress($address: AddressInput!) {
saveAddress(address: $address) @context(provider: "vtex.store-graphql") {
id
cacheId
}
}`;

try {
const { saveAddress: updatedAddress } = await io.query<
{ saveAddress: Address },
{ address: AddressInput }
>(
{
query: mutation,
operationName: "SaveAddress",
variables: {
address: props,
},
},
{ headers: { cookie } },
);

return {
"@type": "PostalAddress",
addressCountry: updatedAddress?.country,
addressLocality: updatedAddress?.city,
addressRegion: updatedAddress?.state,
postalCode: updatedAddress?.postalCode,
streetAddress: updatedAddress?.street,
receiverName: updatedAddress?.receiverName,
complement: updatedAddress?.complement,
};
} catch (error) {
console.error("Error saving address:", error);
return null;
}
}

export default loader;
22 changes: 5 additions & 17 deletions vtex/actions/address/updateAddress.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,27 +39,14 @@ async function loader(
props: Address,
req: Request,
ctx: AppContext,
): Promise<PostalAddress & { receiverName?: string } | null> {
): Promise<
| PostalAddress & { receiverName?: string | null; complement?: string | null }
| null
> {
const { io } = ctx;
const { cookie } = parseCookie(req.headers, ctx.account);
const id = props.addressName;

const moc: AddressInput = {
addressName: "l1ov8h0kea",
addressType: "residential",
city: "SAINT GEORGE",
complement: null,
country: "USA",
geoCoordinates: [-113.57, 37.06],
neighborhood: null,
number: null,
postalCode: "84790",
receiverName: "Joao maria",
reference: null,
state: "UT",
street: "SAINT GEORGE, UT - 4555",
};

const mutation = `
mutation UpdateAddress($addressId: String, $addressFields: AddressInput) {
updateAddress(id: $addressId, fields: $addressFields)
Expand Down Expand Up @@ -108,6 +95,7 @@ async function loader(
postalCode: updatedAddress?.postalCode,
streetAddress: updatedAddress?.street,
receiverName: updatedAddress?.receiverName,
complement: updatedAddress?.complement,
};
} catch (error) {
console.error("Error updating address:", error);
Expand Down
8 changes: 7 additions & 1 deletion vtex/loaders/address/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,11 @@ async function loader(
req: Request,
ctx: AppContext,
): Promise<
PostalAddress[] & { receiverName?: string; addressName?: string } | null
PostalAddress[] & {
receiverName?: string;
addressName?: string;
complement?: string | null;
} | null
> {
const { io } = ctx;
const { cookie, payload } = parseCookie(req.headers, ctx.account);
Expand All @@ -46,6 +50,7 @@ async function loader(
street
receiverName
addressName
complement
}
}
}`;
Expand All @@ -68,6 +73,7 @@ async function loader(
streetAddress: address?.street,
receiverName: address?.receiverName,
addressName: address?.addressName,
complement: address?.complement,
}));
} catch (_) {
return null;
Expand Down
118 changes: 60 additions & 58 deletions vtex/manifest.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,36 @@
// This file SHOULD be checked into source version control.
// This file is automatically updated during development when running `dev.ts`.

import * as $$$$$$$$$0 from "./actions/address/updateAddress.ts";
import * as $$$$$$$$$1 from "./actions/analytics/sendEvent.ts";
import * as $$$$$$$$$2 from "./actions/cart/addItems.ts";
import * as $$$$$$$$$3 from "./actions/cart/addOfferings.ts";
import * as $$$$$$$$$4 from "./actions/cart/clearOrderformMessages.ts";
import * as $$$$$$$$$5 from "./actions/cart/getInstallment.ts";
import * as $$$$$$$$$6 from "./actions/cart/removeItemAttachment.ts";
import * as $$$$$$$$$7 from "./actions/cart/removeItems.ts";
import * as $$$$$$$$$8 from "./actions/cart/removeOffering.ts";
import * as $$$$$$$$$9 from "./actions/cart/simulation.ts";
import * as $$$$$$$$$10 from "./actions/cart/updateAttachment.ts";
import * as $$$$$$$$$11 from "./actions/cart/updateCoupons.ts";
import * as $$$$$$$$$12 from "./actions/cart/updateGifts.ts";
import * as $$$$$$$$$13 from "./actions/cart/updateItemAttachment.ts";
import * as $$$$$$$$$14 from "./actions/cart/updateItemPrice.ts";
import * as $$$$$$$$$15 from "./actions/cart/updateItems.ts";
import * as $$$$$$$$$16 from "./actions/cart/updateProfile.ts";
import * as $$$$$$$$$17 from "./actions/cart/updateUser.ts";
import * as $$$$$$$$$18 from "./actions/masterdata/createDocument.ts";
import * as $$$$$$$$$19 from "./actions/newsletter/subscribe.ts";
import * as $$$$$$$$$20 from "./actions/notifyme.ts";
import * as $$$$$$$$$21 from "./actions/payments/delete.ts";
import * as $$$$$$$$$22 from "./actions/profile/newsletterProfile.ts";
import * as $$$$$$$$$23 from "./actions/profile/updateProfile.ts";
import * as $$$$$$$$$24 from "./actions/review/submit.ts";
import * as $$$$$$$$$25 from "./actions/sessions/delete.ts";
import * as $$$$$$$$$26 from "./actions/trigger.ts";
import * as $$$$$$$$$27 from "./actions/wishlist/addItem.ts";
import * as $$$$$$$$$28 from "./actions/wishlist/removeItem.ts";
import * as $$$$$$$$$0 from "./actions/address/createAddress.ts";
import * as $$$$$$$$$1 from "./actions/address/updateAddress.ts";
import * as $$$$$$$$$2 from "./actions/analytics/sendEvent.ts";
import * as $$$$$$$$$3 from "./actions/cart/addItems.ts";
import * as $$$$$$$$$4 from "./actions/cart/addOfferings.ts";
import * as $$$$$$$$$5 from "./actions/cart/clearOrderformMessages.ts";
import * as $$$$$$$$$6 from "./actions/cart/getInstallment.ts";
import * as $$$$$$$$$7 from "./actions/cart/removeItemAttachment.ts";
import * as $$$$$$$$$8 from "./actions/cart/removeItems.ts";
import * as $$$$$$$$$9 from "./actions/cart/removeOffering.ts";
import * as $$$$$$$$$10 from "./actions/cart/simulation.ts";
import * as $$$$$$$$$11 from "./actions/cart/updateAttachment.ts";
import * as $$$$$$$$$12 from "./actions/cart/updateCoupons.ts";
import * as $$$$$$$$$13 from "./actions/cart/updateGifts.ts";
import * as $$$$$$$$$14 from "./actions/cart/updateItemAttachment.ts";
import * as $$$$$$$$$15 from "./actions/cart/updateItemPrice.ts";
import * as $$$$$$$$$16 from "./actions/cart/updateItems.ts";
import * as $$$$$$$$$17 from "./actions/cart/updateProfile.ts";
import * as $$$$$$$$$18 from "./actions/cart/updateUser.ts";
import * as $$$$$$$$$19 from "./actions/masterdata/createDocument.ts";
import * as $$$$$$$$$20 from "./actions/newsletter/subscribe.ts";
import * as $$$$$$$$$21 from "./actions/notifyme.ts";
import * as $$$$$$$$$22 from "./actions/payments/delete.ts";
import * as $$$$$$$$$23 from "./actions/profile/newsletterProfile.ts";
import * as $$$$$$$$$24 from "./actions/profile/updateProfile.ts";
import * as $$$$$$$$$25 from "./actions/review/submit.ts";
import * as $$$$$$$$$26 from "./actions/sessions/delete.ts";
import * as $$$$$$$$$27 from "./actions/trigger.ts";
import * as $$$$$$$$$28 from "./actions/wishlist/addItem.ts";
import * as $$$$$$$$$29 from "./actions/wishlist/removeItem.ts";
import * as $$$$0 from "./handlers/sitemap.ts";
import * as $$$0 from "./loaders/address/list.ts";
import * as $$$1 from "./loaders/cart.ts";
Expand Down Expand Up @@ -130,35 +131,36 @@ const manifest = {
"vtex/sections/Analytics/Vtex.tsx": $$$$$$0,
},
"actions": {
"vtex/actions/address/updateAddress.ts": $$$$$$$$$0,
"vtex/actions/analytics/sendEvent.ts": $$$$$$$$$1,
"vtex/actions/cart/addItems.ts": $$$$$$$$$2,
"vtex/actions/cart/addOfferings.ts": $$$$$$$$$3,
"vtex/actions/cart/clearOrderformMessages.ts": $$$$$$$$$4,
"vtex/actions/cart/getInstallment.ts": $$$$$$$$$5,
"vtex/actions/cart/removeItemAttachment.ts": $$$$$$$$$6,
"vtex/actions/cart/removeItems.ts": $$$$$$$$$7,
"vtex/actions/cart/removeOffering.ts": $$$$$$$$$8,
"vtex/actions/cart/simulation.ts": $$$$$$$$$9,
"vtex/actions/cart/updateAttachment.ts": $$$$$$$$$10,
"vtex/actions/cart/updateCoupons.ts": $$$$$$$$$11,
"vtex/actions/cart/updateGifts.ts": $$$$$$$$$12,
"vtex/actions/cart/updateItemAttachment.ts": $$$$$$$$$13,
"vtex/actions/cart/updateItemPrice.ts": $$$$$$$$$14,
"vtex/actions/cart/updateItems.ts": $$$$$$$$$15,
"vtex/actions/cart/updateProfile.ts": $$$$$$$$$16,
"vtex/actions/cart/updateUser.ts": $$$$$$$$$17,
"vtex/actions/masterdata/createDocument.ts": $$$$$$$$$18,
"vtex/actions/newsletter/subscribe.ts": $$$$$$$$$19,
"vtex/actions/notifyme.ts": $$$$$$$$$20,
"vtex/actions/payments/delete.ts": $$$$$$$$$21,
"vtex/actions/profile/newsletterProfile.ts": $$$$$$$$$22,
"vtex/actions/profile/updateProfile.ts": $$$$$$$$$23,
"vtex/actions/review/submit.ts": $$$$$$$$$24,
"vtex/actions/sessions/delete.ts": $$$$$$$$$25,
"vtex/actions/trigger.ts": $$$$$$$$$26,
"vtex/actions/wishlist/addItem.ts": $$$$$$$$$27,
"vtex/actions/wishlist/removeItem.ts": $$$$$$$$$28,
"vtex/actions/address/createAddress.ts": $$$$$$$$$0,
"vtex/actions/address/updateAddress.ts": $$$$$$$$$1,
"vtex/actions/analytics/sendEvent.ts": $$$$$$$$$2,
"vtex/actions/cart/addItems.ts": $$$$$$$$$3,
"vtex/actions/cart/addOfferings.ts": $$$$$$$$$4,
"vtex/actions/cart/clearOrderformMessages.ts": $$$$$$$$$5,
"vtex/actions/cart/getInstallment.ts": $$$$$$$$$6,
"vtex/actions/cart/removeItemAttachment.ts": $$$$$$$$$7,
"vtex/actions/cart/removeItems.ts": $$$$$$$$$8,
"vtex/actions/cart/removeOffering.ts": $$$$$$$$$9,
"vtex/actions/cart/simulation.ts": $$$$$$$$$10,
"vtex/actions/cart/updateAttachment.ts": $$$$$$$$$11,
"vtex/actions/cart/updateCoupons.ts": $$$$$$$$$12,
"vtex/actions/cart/updateGifts.ts": $$$$$$$$$13,
"vtex/actions/cart/updateItemAttachment.ts": $$$$$$$$$14,
"vtex/actions/cart/updateItemPrice.ts": $$$$$$$$$15,
"vtex/actions/cart/updateItems.ts": $$$$$$$$$16,
"vtex/actions/cart/updateProfile.ts": $$$$$$$$$17,
"vtex/actions/cart/updateUser.ts": $$$$$$$$$18,
"vtex/actions/masterdata/createDocument.ts": $$$$$$$$$19,
"vtex/actions/newsletter/subscribe.ts": $$$$$$$$$20,
"vtex/actions/notifyme.ts": $$$$$$$$$21,
"vtex/actions/payments/delete.ts": $$$$$$$$$22,
"vtex/actions/profile/newsletterProfile.ts": $$$$$$$$$23,
"vtex/actions/profile/updateProfile.ts": $$$$$$$$$24,
"vtex/actions/review/submit.ts": $$$$$$$$$25,
"vtex/actions/sessions/delete.ts": $$$$$$$$$26,
"vtex/actions/trigger.ts": $$$$$$$$$27,
"vtex/actions/wishlist/addItem.ts": $$$$$$$$$28,
"vtex/actions/wishlist/removeItem.ts": $$$$$$$$$29,
},
"workflows": {
"vtex/workflows/events.ts": $$$$$$$$$$0,
Expand Down

0 comments on commit d9c45c6

Please sign in to comment.