Skip to content

Commit 14b2d30

Browse files
committed
refactor code #63
1 parent 5914e0e commit 14b2d30

File tree

3 files changed

+36
-26
lines changed

3 files changed

+36
-26
lines changed

src/web/app/components/SiteLayout.tsx

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ function SiteLayout({
1616
<AppHeader userInfo={userInfo} />
1717
<div className="remix-app__main">
1818
<div className="remix-app__main-content container mx-auto">
19+
<div className="relative bg-cover bg-center bg-no-repeat py-10">
20+
<div className="container">
21+
<h1 className="mb-4 text-4xl font-medium text-gray-800 md:text-5xl xl:text-6xl">
22+
CoolStore buits with modern technologies
23+
</h1>
24+
<p className="text-base leading-6 text-gray-600">
25+
Lorem ipsum dolor sit amet, consectetur adipisicing elit. Culpa
26+
assumenda aliquid inventore nihil laboriosam odio
27+
</p>
28+
</div>
29+
</div>
30+
1931
{children}
2032
</div>
2133
</div>

src/web/app/lib/auth.ts

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -91,9 +91,7 @@ export const getUserInfo = async (request: Request) => {
9191
const { data } = await axios.get<any>(
9292
`${API_URL}/userinfo`
9393
, {
94-
headers: {
95-
cookie: request.headers.get("Cookie")?.toString()
96-
} as any
94+
headers: getHeaders(request)
9795
});
9896
if (Object.keys(data).length === 0) {
9997
return null;
@@ -105,9 +103,7 @@ export async function searchProduct(request: Request, query: string, price: numb
105103
const response = await axios.get<any>(
106104
`${PRODUCT_SEARCH_URL}/${price}/${page}/${pageSize}`
107105
, {
108-
headers: {
109-
cookie: request.headers.get("Cookie")?.toString()
110-
} as any
106+
headers: getHeaders(request)
111107
});
112108

113109
const { data } = response;
@@ -127,9 +123,7 @@ export async function getProductById(request: Request, id: string) {
127123
const { data } = await axios.get<any>(
128124
`${PRODUCT_URL}/${id}`
129125
, {
130-
headers: {
131-
cookie: request.headers.get("Cookie")?.toString()
132-
} as any
126+
headers: getHeaders(request)
133127
});
134128
return data as ProductDetailModel;
135129
}
@@ -145,26 +139,21 @@ export async function createUserSession(userId: string, redirectTo: string) {
145139
}
146140

147141
export async function getCartForCurrentUser(request: Request) {
148-
const cookie = request.headers.get("Cookie")?.toString()!;
149142
const response = await axios.get<any>(
150143
CART_URL
151144
, {
152-
headers: {
153-
cookie: cookie
154-
} as any
145+
headers: getHeaders(request)
155146
});
156147

157148
const { data } = response;
158-
const xsrfToken = convertCookie(cookie)['XSRF-TOKEN'];
159149

160150
console.log(data as CartModel);
161-
return { cartData: data as CartModel, csrf: xsrfToken };
151+
return { cartData: data as CartModel };
162152
}
163153

164154
export async function updateCartForCurrentUser(request: Request, productId: string) {
165155
const userData = await getUserInfo(request);
166-
const { cartData, csrf } = await getCartForCurrentUser(request);
167-
const cookie = request.headers.get("Cookie")?.toString();
156+
const { cartData } = await getCartForCurrentUser(request);
168157

169158
if (cartData.id === null) {
170159
// create new cart
@@ -176,10 +165,7 @@ export async function updateCartForCurrentUser(request: Request, productId: stri
176165
quantity: 1,
177166
},
178167
{
179-
headers: {
180-
cookie: cookie,
181-
"X-XSRF-TOKEN": csrf,
182-
} as any
168+
headers: getHeaders(request, true)
183169
});
184170
return data as CartModel;
185171
} else {
@@ -191,15 +177,27 @@ export async function updateCartForCurrentUser(request: Request, productId: stri
191177
quantity: 1,
192178
},
193179
{
194-
headers: {
195-
cookie: cookie,
196-
"X-XSRF-TOKEN": csrf,
197-
} as any
180+
headers: getHeaders(request, true)
198181
});
199182
return data as CartModel;
200183
}
201184
}
202185

186+
function getHeaders(request: Request, isCsrf = false) {
187+
const cookie = request.headers.get("Cookie")?.toString()!;
188+
if (isCsrf) {
189+
const csrf = convertCookie(cookie)['XSRF-TOKEN'];
190+
return {
191+
cookie: cookie,
192+
"X-XSRF-TOKEN": csrf,
193+
} as any;
194+
} else {
195+
return {
196+
cookie: cookie
197+
} as any;
198+
}
199+
}
200+
203201
function convertCookie(cookie: string) {
204202
const str: string[] | any = cookie?.toString().split('; ');
205203
const result: any = {};

src/web/app/routes/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import {
44
LoaderFunction,
55
redirect,
66
} from "@remix-run/node";
7-
import { Form, Link, useLoaderData, useSubmit } from "@remix-run/react";
7+
import { Form, Link, useLoaderData } from "@remix-run/react";
88
import { SearchIcon } from "@heroicons/react/outline";
99
import Image from "remix-image";
1010

0 commit comments

Comments
 (0)