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

[VNDA] - Feat: add user #893

Open
wants to merge 5 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
11 changes: 8 additions & 3 deletions vnda/hooks/context.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { IS_BROWSER } from "$fresh/runtime.ts";
import { signal } from "@preact/signals";
import { invoke } from "../runtime.ts";
import type { Person } from "../../commerce/types.ts";
import type { Cart } from "../loaders/cart.ts";

export interface Context {
user: Person | null;
cart: Cart;
}

const loading = signal<boolean>(true);
const context = {
cart: signal<Cart | null>(null),
user: IS_BROWSER && signal<Person | null>(null) || { value: null },
cart: IS_BROWSER && signal<Cart | null>(null) || { value: null },
IncognitaDev marked this conversation as resolved.
Show resolved Hide resolved
};

let queue = Promise.resolve();
Expand All @@ -24,13 +27,14 @@ const enqueue = (

queue = queue.then(async () => {
try {
const { cart } = await cb(controller.signal);
const { user, cart } = await cb(controller.signal);

if (controller.signal.aborted) {
throw { name: "AbortError" };
}

context.cart.value = { ...context.cart.value, ...cart };
context.cart.value = cart || context.cart.value;
context.user.value = user || context.user.value;

loading.value = false;
} catch (error) {
Expand All @@ -49,6 +53,7 @@ const enqueue = (
const load = (signal: AbortSignal) =>
invoke({
cart: invoke.vnda.loaders.cart(),
user: invoke.vnda.loaders.user(),
}, { signal });

if (IS_BROWSER) {
Expand Down
7 changes: 7 additions & 0 deletions vnda/hooks/useUser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { state as storeState } from "./context.ts";

const { user, loading } = storeState;

const state = { user, loading };

export const useUser = () => state;
37 changes: 37 additions & 0 deletions vnda/loaders/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Person } from "../../commerce/types.ts";
import type { AppContext } from "../mod.ts";
import { getUserCookie } from "../utils/user.ts";

/**
* @title VNDA Integration
* @description User loader
*/
const loader = async (
_props: unknown,
req: Request,
ctx: AppContext,
): Promise<Person | null> => {
const { api } = ctx;

const userAccessToken = getUserCookie(req.headers);

if (!userAccessToken) return null;

try {
const user = await api["GET /api/v2/clients/:id"]({ id: userAccessToken })
.then((res) => res.json());

if (!user) return null;

return {
"@id": String(user.id),
email: user.email ?? "",
givenName: user.first_name ?? "",
familyName: user.last_name ?? "",
};
} catch {
return null;
}
};

export default loader;
2 changes: 2 additions & 0 deletions vnda/manifest.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import * as $$$4 from "./loaders/productDetailsPageVideo.ts";
import * as $$$5 from "./loaders/productList.ts";
import * as $$$6 from "./loaders/productListingPage.ts";
import * as $$$7 from "./loaders/proxy.ts";
import * as $$$8 from "./loaders/user.ts";

const manifest = {
"loaders": {
Expand All @@ -28,6 +29,7 @@ const manifest = {
"vnda/loaders/productList.ts": $$$5,
"vnda/loaders/productListingPage.ts": $$$6,
"vnda/loaders/proxy.ts": $$$7,
"vnda/loaders/user.ts": $$$8,
},
"handlers": {
"vnda/handlers/sitemap.ts": $$$$0,
Expand Down
9 changes: 9 additions & 0 deletions vnda/utils/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { getCookies } from "std/http/cookie.ts";

const AUTH_COOKIE = "client_id";

export const getUserCookie = (headers: Headers): string | undefined => {
const cookies = getCookies(headers);

return cookies[AUTH_COOKIE];
};
Loading