Skip to content

Commit 54e70c6

Browse files
authored
Merge pull request #60 from jzakotnik/59-prolongation-date
59 prolongation date
2 parents 9f29aa5 + 9101703 commit 54e70c6

File tree

3 files changed

+23
-17
lines changed

3 files changed

+23
-17
lines changed

pages/rental/index.tsx

+12-8
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,19 @@ interface RentalPropsType {
3131
books: Array<BookType>;
3232
users: Array<UserType>;
3333
rentals: Array<RentalsUserType>;
34+
extensionDays: number;
3435
}
3536

3637
const prisma = new PrismaClient();
3738

3839
const fetcher = (url: string) => fetch(url).then((r) => r.json());
3940

40-
export default function Rental({ books, users, rentals }: RentalPropsType) {
41+
export default function Rental({
42+
books,
43+
users,
44+
rentals,
45+
extensionDays,
46+
}: RentalPropsType) {
4147
const router = useRouter();
4248
const [snackbarOpen, setSnackbarOpen] = useState(false);
4349

@@ -98,13 +104,10 @@ export default function Rental({ books, users, rentals }: RentalPropsType) {
98104
const handleExtendBookButton = (bookid: number, book: BookType) => {
99105
console.log("Extending book ", bookid, book);
100106
const newbook = replaceBookStringDate(book) as any;
107+
108+
console.log("Extension days: ", extensionDays);
101109
//extend logic
102-
const newDueDate = extendDays(
103-
book.dueDate as Date,
104-
process.env.EXTENSION_DURATION_DAYS
105-
? parseInt(process.env.EXTENSION_DURATION_DAYS)
106-
: 14
107-
);
110+
const newDueDate = extendDays(new Date(), extensionDays);
108111
newbook.dueDate = newDueDate.toDate();
109112
newbook.renewalCount = newbook.renewalCount + 1;
110113

@@ -255,6 +258,7 @@ export default function Rental({ books, users, rentals }: RentalPropsType) {
255258
}
256259

257260
export async function getServerSideProps() {
261+
const extensionDays = process.env.EXTENSION_DURATION_DAYS || 14;
258262
const allUsers = await getAllUsers(prisma);
259263

260264
const users = allUsers.map((u) => {
@@ -299,5 +303,5 @@ export async function getServerSideProps() {
299303
});
300304
//console.log("Initial fetch of books", books[0]);
301305

302-
return { props: { books, users, rentals } };
306+
return { props: { books, users, rentals, extensionDays } };
303307
}

pages/user/[userid].tsx

+10-9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import { GetServerSidePropsContext } from "next/types";
2828
type UserDetailPropsType = {
2929
user: UserType;
3030
books: Array<BookType>;
31+
extensionDays: number;
3132
};
3233

3334
const theme = createTheme({
@@ -43,7 +44,11 @@ const Alert = forwardRef<HTMLDivElement, AlertProps>(function Alert(
4344
return <MuiAlert elevation={6} ref={ref} variant="filled" {...props} />;
4445
});
4546

46-
export default function UserDetail({ user, books }: UserDetailPropsType) {
47+
export default function UserDetail({
48+
user,
49+
books,
50+
extensionDays,
51+
}: UserDetailPropsType) {
4752
const router = useRouter();
4853

4954
const [userData, setUserData] = useState(user);
@@ -129,12 +134,7 @@ export default function UserDetail({ user, books }: UserDetailPropsType) {
129134
const newbook = replaceBookStringDate(book) as any;
130135
//extend logic
131136

132-
const newDueDate = extendDays(
133-
book.dueDate as Date,
134-
process.env.EXTENSION_DURATION_DAYS
135-
? parseInt(process.env.EXTENSION_DURATION_DAYS)
136-
: 14
137-
);
137+
const newDueDate = extendDays(new Date(), extensionDays);
138138
newbook.dueDate = newDueDate.toDate();
139139
newbook.renewalCount = newbook.renewalCount + 1;
140140

@@ -216,6 +216,7 @@ export default function UserDetail({ user, books }: UserDetailPropsType) {
216216
}
217217

218218
export async function getServerSideProps(context: GetServerSidePropsContext) {
219+
const extensionDays = process.env.EXTENSION_DURATION_DAYS || 14;
219220
if (!context.query.userid) return { props: {} };
220221
const prisma = new PrismaClient();
221222

@@ -248,10 +249,10 @@ export async function getServerSideProps(context: GetServerSidePropsContext) {
248249
: "";
249250
newBook.dueDate = b.dueDate ? convertDateToDayString(b.dueDate) : "";
250251
//temp TODO
251-
console.log("Book", newBook);
252+
//console.log("Book", newBook);
252253
return newBook;
253254
});
254255

255256
// Pass data to the page via props
256-
return { props: { user, books } };
257+
return { props: { user, books, extensionDays } };
257258
}

utils/dateutils.ts

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export function extendWeeks(d: Date, weeks: number): Dayjs {
3838

3939
export function extendDays(d: Date, days: number): Dayjs {
4040
//console.log("Converting string to dayjs", d, dayjs(d));
41+
console.log("Extending book by ", days, d);
4142
const newDate = dayjs(d).add(days, "day");
4243
return newDate;
4344
}

0 commit comments

Comments
 (0)