Skip to content

Commit

Permalink
fixed bug in stripe checkout
Browse files Browse the repository at this point in the history
  • Loading branch information
devMukulSingh committed May 8, 2024
1 parent b285682 commit 77087c3
Show file tree
Hide file tree
Showing 12 changed files with 66 additions and 43 deletions.
2 changes: 1 addition & 1 deletion actions/get-formatted-orders.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export const getFormattedOrders = cache(async (storeId: string) => {
phone: item.phone,
address: item.address,
isPaid: item.isPaid,
customerName:item.customerName,
customerName: item.customerName,
products: item.orderItems
.map((orderItem: IorderItem) => orderItem.product.name)
.join(","),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,7 @@ const Header: FC<HeaderProps> = ({ billboard }) => {
</h1>
<p className="text-sm text-slate-500">Manage Billboards</p>
</div>
<SearchBar
tableData={billboard}
/>
<SearchBar tableData={billboard} />
<Link href={`/${storeId}/billboards/new`} prefetch={true}>
<Button className="flex gap-2">
<PlusCircle />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ const Header: FC<HeaderProps> = ({ brand }) => {
<h1 className="text-2xl font-bold">Brand({brand?.length})</h1>
<p className="text-sm text-slate-500">Manage brands</p>
</div>
<SearchBar tableData={brand}/>
<SearchBar tableData={brand} />
<Link href={`/${storeId}/brands/new`} prefetch={true}>
<Button className="flex gap-2">
<PlusCircle />
Expand Down
21 changes: 9 additions & 12 deletions app/(dashboard)/[storeId]/(routes)/orders/components/Header.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
import SearchBar from '@/components/commons/SearchBar';
import { OrdersColumn } from '@/components/ui/OrdersColumn';
import React, { FC } from 'react'
import SearchBar from "@/components/commons/SearchBar";
import { OrdersColumn } from "@/components/ui/OrdersColumn";
import React, { FC } from "react";

interface HeaderProps{
orders:OrdersColumn[]
interface HeaderProps {
orders: OrdersColumn[];
}

const Header:FC<HeaderProps> = ({
orders
}) => {
const Header: FC<HeaderProps> = ({ orders }) => {
return (
<>
<header className="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-20">
Expand All @@ -17,11 +15,10 @@ const Header:FC<HeaderProps> = ({
<p className="text-sm text-slate-500">Manage orders</p>
</div>

<SearchBar tableData={orders}/>

<SearchBar tableData={orders} />
</header>
</>
);
}
};

export default Header
export default Header;
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const OrderTable: FC<OrdersClientCompProps> = async ({ storeId }) => {
const orders = await getFormattedOrders(storeId);
return (
<>
<Header orders={orders}/>
<Header orders={orders} />
<DataTable columns={columns} data={orders} />
</>
);
Expand Down
27 changes: 16 additions & 11 deletions app/api/[storeId]/checkout/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ export async function POST(
{ params }: { params: { storeId: string } },
) {
try {
const { productIds, storeId } = await req.json();
const { storeId } = params;
const { productIds } = await req.json();

if (!productIds || productIds?.length < 0)
return NextResponse.json(
Expand All @@ -32,14 +33,15 @@ export async function POST(
{ status: 400 },
);

const products = await prisma.product.findMany({
where: {
id: {
in: [...productIds],
const products = await prisma.product.findMany({
where: {
id: {
in: [...productIds]
},
storeId
},
storeId,
},
});
});

const line_items: Stripe.Checkout.SessionCreateParams.LineItem[] = [];

products.forEach((product) => {
Expand Down Expand Up @@ -69,10 +71,13 @@ export async function POST(
})),
},
},
});
});
console.log(line_items,"line_items");
console.log(order,"order");


const session = await stripe.checkout.sessions.create({
line_items,
const session = await stripe.checkout.sessions.create({
line_items,
mode: "payment",
billing_address_collection: "required",

Expand Down
2 changes: 1 addition & 1 deletion app/api/webhook/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ export async function POST(req: Request, res: Response) {
data: {
isPaid: true,
address: addressString,
customerName:session.customer_details?.name || "",
customerName: session.customer_details?.name || "",
phone: session?.customer_details?.phone || "",
},
include: {
Expand Down
10 changes: 5 additions & 5 deletions components/commons/DataTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,18 +22,18 @@ import { useAppDispatch, useAppSelector } from "@/redux/hooks";

interface IdataTableProps<TData, TValue> {
columns: ColumnDef<TData, TValue>[];
data: TData[] ;
data: TData[];
}

export function DataTable<TData, TValue>({
data,
columns,
}: IdataTableProps<TData, TValue>) {
const dispatch = useAppDispatch();
useEffect( () => {
dispatch(setTableData(data))
},[]);
const tableData = useAppSelector( state => state.adminSlice.tableData);
useEffect(() => {
dispatch(setTableData(data));
}, []);
const tableData = useAppSelector((state) => state.adminSlice.tableData);
const table = useReactTable({
data: tableData,
columns,
Expand Down
4 changes: 2 additions & 2 deletions components/commons/SearchBar.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
'use client'
"use client";
import React, { FC, useState } from "react";
import { Input } from "../ui/input";
import { Search, X } from "lucide-react";
Expand All @@ -20,7 +20,7 @@ export function SearchBar({ tableData }: SearchBarProps) {
item?.customerName?.toLowerCase()?.includes(query),
);
dispatch(setTableData(filterdData));
} else dispatch(setTableData(tableData))
} else dispatch(setTableData(tableData));
};

const handleClearSearch = () => {
Expand Down
2 changes: 1 addition & 1 deletion components/ui/OrdersColumn.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export type OrdersColumn = {
isPaid: boolean;
totalPrice: number;
createdAt: string;
customerName:string
customerName: string;
};

export const columns: ColumnDef<OrdersColumn>[] = [
Expand Down
22 changes: 22 additions & 0 deletions lib/hooks/useLocalStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
"use client";

interface IsetInLocaStorage {
key: string;
item: any;
}

export const useLocalStorage = () => {
const setInLocalStorage = ({ key, item }: IsetInLocaStorage) => {
if (typeof window !== "undefined") {
localStorage.setItem(key, JSON.stringify(item));
}
return;
};

const getFromLocalStorage = (key: string) => {
if (typeof window !== "undefined") {
return JSON.parse(localStorage.getItem(key) || "{}");
}
};
return { setInLocalStorage, getFromLocalStorage };
};
11 changes: 6 additions & 5 deletions redux/slice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ interface IinitialState {
isOpen: boolean;
loading: boolean;
openSidebar: boolean;
tableData: any[]
tableData: any[];
}
const initialState: IinitialState = {
isOpen: false,
loading: false,
openSidebar: false,
tableData:[]
tableData: [],
};

export const adminSlice = createSlice({
Expand All @@ -26,11 +26,12 @@ export const adminSlice = createSlice({
setOpenSidebar: (state) => {
state.openSidebar = !state.openSidebar;
},
setTableData : (state,action) => {
setTableData: (state, action) => {
state.tableData = action.payload;
}
},
},
});

export default adminSlice.reducer;
export const { setDialog, setLoading, setOpenSidebar, setTableData } = adminSlice.actions;
export const { setDialog, setLoading, setOpenSidebar, setTableData } =
adminSlice.actions;

0 comments on commit 77087c3

Please sign in to comment.