diff --git a/pages/rental/index.tsx b/pages/rental/index.tsx index 088eebd..cf11169 100644 --- a/pages/rental/index.tsx +++ b/pages/rental/index.tsx @@ -31,13 +31,19 @@ interface RentalPropsType { books: Array; users: Array; rentals: Array; + extensionDays: number; } const prisma = new PrismaClient(); const fetcher = (url: string) => fetch(url).then((r) => r.json()); -export default function Rental({ books, users, rentals }: RentalPropsType) { +export default function Rental({ + books, + users, + rentals, + extensionDays, +}: RentalPropsType) { const router = useRouter(); const [snackbarOpen, setSnackbarOpen] = useState(false); @@ -98,13 +104,10 @@ export default function Rental({ books, users, rentals }: RentalPropsType) { const handleExtendBookButton = (bookid: number, book: BookType) => { console.log("Extending book ", bookid, book); const newbook = replaceBookStringDate(book) as any; + + console.log("Extension days: ", extensionDays); //extend logic - const newDueDate = extendDays( - book.dueDate as Date, - process.env.EXTENSION_DURATION_DAYS - ? parseInt(process.env.EXTENSION_DURATION_DAYS) - : 14 - ); + const newDueDate = extendDays(new Date(), extensionDays); newbook.dueDate = newDueDate.toDate(); newbook.renewalCount = newbook.renewalCount + 1; @@ -255,6 +258,7 @@ export default function Rental({ books, users, rentals }: RentalPropsType) { } export async function getServerSideProps() { + const extensionDays = process.env.EXTENSION_DURATION_DAYS || 14; const allUsers = await getAllUsers(prisma); const users = allUsers.map((u) => { @@ -299,5 +303,5 @@ export async function getServerSideProps() { }); //console.log("Initial fetch of books", books[0]); - return { props: { books, users, rentals } }; + return { props: { books, users, rentals, extensionDays } }; } diff --git a/pages/user/[userid].tsx b/pages/user/[userid].tsx index 72031cc..dd46afd 100644 --- a/pages/user/[userid].tsx +++ b/pages/user/[userid].tsx @@ -28,6 +28,7 @@ import { GetServerSidePropsContext } from "next/types"; type UserDetailPropsType = { user: UserType; books: Array; + extensionDays: number; }; const theme = createTheme({ @@ -43,7 +44,11 @@ const Alert = forwardRef(function Alert( return ; }); -export default function UserDetail({ user, books }: UserDetailPropsType) { +export default function UserDetail({ + user, + books, + extensionDays, +}: UserDetailPropsType) { const router = useRouter(); const [userData, setUserData] = useState(user); @@ -129,12 +134,7 @@ export default function UserDetail({ user, books }: UserDetailPropsType) { const newbook = replaceBookStringDate(book) as any; //extend logic - const newDueDate = extendDays( - book.dueDate as Date, - process.env.EXTENSION_DURATION_DAYS - ? parseInt(process.env.EXTENSION_DURATION_DAYS) - : 14 - ); + const newDueDate = extendDays(new Date(), extensionDays); newbook.dueDate = newDueDate.toDate(); newbook.renewalCount = newbook.renewalCount + 1; @@ -216,6 +216,7 @@ export default function UserDetail({ user, books }: UserDetailPropsType) { } export async function getServerSideProps(context: GetServerSidePropsContext) { + const extensionDays = process.env.EXTENSION_DURATION_DAYS || 14; if (!context.query.userid) return { props: {} }; const prisma = new PrismaClient(); @@ -248,10 +249,10 @@ export async function getServerSideProps(context: GetServerSidePropsContext) { : ""; newBook.dueDate = b.dueDate ? convertDateToDayString(b.dueDate) : ""; //temp TODO - console.log("Book", newBook); + //console.log("Book", newBook); return newBook; }); // Pass data to the page via props - return { props: { user, books } }; + return { props: { user, books, extensionDays } }; } diff --git a/utils/dateutils.ts b/utils/dateutils.ts index 1929ea2..385ce31 100644 --- a/utils/dateutils.ts +++ b/utils/dateutils.ts @@ -38,6 +38,7 @@ export function extendWeeks(d: Date, weeks: number): Dayjs { export function extendDays(d: Date, days: number): Dayjs { //console.log("Converting string to dayjs", d, dayjs(d)); + console.log("Extending book by ", days, d); const newDate = dayjs(d).add(days, "day"); return newDate; }