Skip to content

Commit

Permalink
reduce code duplication of photocarousel
Browse files Browse the repository at this point in the history
  • Loading branch information
kea-roy committed Oct 26, 2024
1 parent a8401da commit a84e11f
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 114 deletions.
39 changes: 39 additions & 0 deletions frontend/src/components/PhotoCarousel/usePhotoCarousel.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// src/hooks/usePhotoCarousel.ts
import { useState } from 'react';

const usePhotoCarousel = (defaultPhotos: readonly string[] = []) => {
const [carouselPhotos, setCarouselPhotos] = useState<readonly string[]>(defaultPhotos);
const [carouselStartIndex, setCarouselStartIndex] = useState<number>(0);
const [carouselOpen, setCarouselOpen] = useState<boolean>(false);

/**
* showPhotoCarousel – Opens the photo carousel modal with the provided photos and start index.
*
* @remarks
* This function sets the photos and start index for the photo carousel and then opens the carousel modal.
* If no photos are provided, it defaults to [defaultPhotos].
*
* @param {readonly string[]} [photos] – The array of photo URLs to display in the carousel.
* @param {number} [startIndex] – The index of the photo to start the carousel from.
* @return {void} – This function does not return anything.
*/
const showPhotoCarousel = (photos: readonly string[] = defaultPhotos, startIndex: number = 0) => {
setCarouselPhotos(photos);
setCarouselStartIndex(startIndex);
setCarouselOpen(true);
};

const closePhotoCarousel = () => {
setCarouselOpen(false);
};

return {
carouselPhotos,
carouselStartIndex,
carouselOpen,
showPhotoCarousel,
closePhotoCarousel,
};
};

export default usePhotoCarousel;
30 changes: 9 additions & 21 deletions frontend/src/pages/AdminPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import { useTitle } from '../utils';
import { Chart } from 'react-google-charts';
import { sortReviews } from '../utils/sortReviews';
import PhotoCarousel from '../components/PhotoCarousel/PhotoCarousel';
import usePhotoCarousel from '../components/PhotoCarousel/usePhotoCarousel';

const useStyles = makeStyles((theme) => ({
container: {
Expand Down Expand Up @@ -52,9 +53,13 @@ const AdminPage = (): ReactElement => {
const [pendingApartment, setPendingApartmentData] = useState<CantFindApartmentForm[]>([]);
const [pendingContactQuestions, setPendingContactQuestions] = useState<QuestionForm[]>([]);

const [carouselOpen, setCarouselOpen] = useState(false);
const [carouselPhotos, setCarouselPhotos] = useState<readonly string[]>([]);
const [carouselStartIndex, setCarouselStartIndex] = useState<number>(0);
const {
carouselPhotos,
carouselStartIndex,
carouselOpen,
showPhotoCarousel,
closePhotoCarousel,
} = usePhotoCarousel([]);

const { container } = useStyles();

Expand Down Expand Up @@ -120,29 +125,12 @@ const AdminPage = (): ReactElement => {
});
}, [toggle]);

/**
* showPhotoCarousel – Opens the photo carousel modal with the provided photos and start index.
*
* @remarks
* This function sets the photos and start index for the photo carousel and then opens the carousel modal.
* If no photos are provided, it defaults to no photos.
*
* @param {readonly string[]} [photos] – The array of photo URLs to display in the carousel.
* @param {number} [startIndex] – The index of the photo to start the carousel from.
* @return {void} – This function does not return anything.
*/
const showPhotoCarousel = (photos: readonly string[] = [], startIndex: number = 0) => {
setCarouselPhotos(photos);
setCarouselStartIndex(startIndex);
setCarouselOpen(true);
};

const Modals = (
<>
<PhotoCarousel
photos={carouselPhotos}
open={carouselOpen}
onClose={() => setCarouselOpen(false)}
onClose={closePhotoCarousel}
startIndex={carouselStartIndex}
/>
</>
Expand Down
33 changes: 9 additions & 24 deletions frontend/src/pages/ApartmentPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import ExpandMoreIcon from '@material-ui/icons/ExpandMore';

import ReviewModal from '../components/LeaveReview/ReviewModal';
import PhotoCarousel from '../components/PhotoCarousel/PhotoCarousel';
import usePhotoCarousel from '../components/PhotoCarousel/usePhotoCarousel';
import ReviewComponent from '../components/Review/Review';
import ReviewHeader from '../components/Review/ReviewHeader';
import { useTitle } from '../utils';
Expand Down Expand Up @@ -130,14 +131,18 @@ const ApartmentPage = ({ user, setUser }: Props): ReactElement => {
const [likeStatuses, setLikeStatuses] = useState<Likes>({});
const [reviewOpen, setReviewOpen] = useState(false);
const [mapOpen, setMapOpen] = useState(false);
const [carouselOpen, setCarouselOpen] = useState(false);
const [carouselPhotos, setCarouselPhotos] = useState<readonly string[]>([]);
const [carouselStartIndex, setCarouselStartIndex] = useState<number>(0);
const [showConfirmation, setShowConfirmation] = useState(false);
const [showEditSuccessConfirmation, setShowEditSuccessConfirmation] = useState(false);
const [buildings, setBuildings] = useState<Apartment[]>([]);
const [aptData, setAptData] = useState<ApartmentWithId[]>([]);
const [apt, setApt] = useState<ApartmentWithId | undefined>(undefined);
const {
carouselPhotos,
carouselStartIndex,
carouselOpen,
showPhotoCarousel,
closePhotoCarousel,
} = usePhotoCarousel(apt ? apt.photos : []);
const [loaded, setLoaded] = useState(false);
const [showSignInError, setShowSignInError] = useState(false);
const [sortBy, setSortBy] = useState<Fields>('date');
Expand Down Expand Up @@ -374,26 +379,6 @@ const ApartmentPage = ({ user, setUser }: Props): ReactElement => {
setReviewOpen(true);
};

/**
* showPhotoCarousel – Opens the photo carousel modal with the provided photos and start index.
*
* @remarks
* This function sets the photos and start index for the photo carousel and then opens the carousel modal.
* If no photos are provided, it defaults to the apartment's photos.
*
* @param {readonly string[]} [photos] – The array of photo URLs to display in the carousel.
* @param {number} [startIndex] – The index of the photo to start the carousel from.
* @return {void} – This function does not return anything.
*/
const showPhotoCarousel = (
photos: readonly string[] = apt ? apt.photos : [],
startIndex: number = 0
) => {
setCarouselPhotos(photos);
setCarouselStartIndex(startIndex);
setCarouselOpen(true);
};

const Modals = landlordData && apt && (
<>
<MapModal
Expand Down Expand Up @@ -422,7 +407,7 @@ const ApartmentPage = ({ user, setUser }: Props): ReactElement => {
photos={carouselPhotos}
open={carouselOpen}
startIndex={carouselStartIndex}
onClose={() => setCarouselOpen(false)}
onClose={closePhotoCarousel}
/>
</>
);
Expand Down
30 changes: 9 additions & 21 deletions frontend/src/pages/BookmarksPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import DropDownWithLabel from '../components/utils/DropDownWithLabel';
import { AptSortFields, sortApartments } from '../utils/sortApartments';
import Toast from '../components/utils/Toast';
import PhotoCarousel from '../components/PhotoCarousel/PhotoCarousel';
import usePhotoCarousel from '../components/PhotoCarousel/usePhotoCarousel';

type Props = {
user: firebase.User | null;
Expand Down Expand Up @@ -109,9 +110,13 @@ const BookmarksPage = ({ user, setUser }: Props): ReactElement => {
const [showMoreLessState, setShowMoreLessState] = useState<string>('Show More');
const [isMobile, setIsMobile] = useState<boolean>(false);
const [showEditSuccessConfirmation, setShowEditSuccessConfirmation] = useState(false);
const [carouselOpen, setCarouselOpen] = useState(false);
const [carouselPhotos, setCarouselPhotos] = useState<readonly string[]>([]);
const [carouselStartIndex, setCarouselStartIndex] = useState<number>(0);
const {
carouselPhotos,
carouselStartIndex,
carouselOpen,
showPhotoCarousel,
closePhotoCarousel,
} = usePhotoCarousel([]);

useEffect(() => {
const handleResize = () => setIsMobile(window.innerWidth <= 600);
Expand Down Expand Up @@ -207,29 +212,12 @@ const BookmarksPage = ({ user, setUser }: Props): ReactElement => {
const addLike = likeHelper(false);
const removeLike = likeHelper(true);

/**
* showPhotoCarousel – Opens the photo carousel modal with the provided photos and start index.
*
* @remarks
* This function sets the photos and start index for the photo carousel and then opens the carousel modal.
* If no photos are provided, it defaults to showing no photos.
*
* @param {readonly string[]} [photos] – The array of photo URLs to display in the carousel.
* @param {number} [startIndex] – The index of the photo to start the carousel from.
* @return {void} – This function does not return anything.
*/
const showPhotoCarousel = (photos: readonly string[] = [], startIndex: number = 0) => {
setCarouselPhotos(photos);
setCarouselStartIndex(startIndex);
setCarouselOpen(true);
};

const Modals = (
<>
<PhotoCarousel
photos={carouselPhotos}
open={carouselOpen}
onClose={() => setCarouselOpen(false)}
onClose={closePhotoCarousel}
startIndex={carouselStartIndex}
/>
</>
Expand Down
39 changes: 12 additions & 27 deletions frontend/src/pages/LandlordPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import React, { ReactElement, useState, useEffect } from 'react';
import { useParams } from 'react-router-dom';
import ReviewModal from '../components/LeaveReview/ReviewModal';
import PhotoCarousel from '../components/PhotoCarousel/PhotoCarousel';
import usePhotoCarousel from '../components/PhotoCarousel/usePhotoCarousel';
import InfoFeatures from '../components/Review/InfoFeatures';
import ReviewComponent from '../components/Review/Review';
import ReviewHeader from '../components/Review/ReviewHeader';
Expand Down Expand Up @@ -92,9 +93,13 @@ const LandlordPage = ({ user, setUser }: Props): ReactElement => {
const [likedReviews, setLikedReviews] = useState<Likes>({});
const [likeStatuses, setLikeStatuses] = useState<Likes>({});
const [reviewOpen, setReviewOpen] = useState(false);
const [carouselOpen, setCarouselOpen] = useState(false);
const [carouselPhotos, setCarouselPhotos] = useState<readonly string[]>([]);
const [carouselStartIndex, setCarouselStartIndex] = useState<number>(0);
const {
carouselPhotos,
carouselStartIndex,
carouselOpen,
showPhotoCarousel,
closePhotoCarousel,
} = usePhotoCarousel(landlordData ? landlordData.photos : []);
const [showConfirmation, setShowConfirmation] = useState(false);
const [showEditSuccessConfirmation, setShowEditSuccessConfirmation] = useState(false);
const [buildings, setBuildings] = useState<CardData[]>([]);
Expand Down Expand Up @@ -289,26 +294,6 @@ const LandlordPage = ({ user, setUser }: Props): ReactElement => {
setReviewOpen(true);
};

/**
* showPhotoCarousel – Opens the photo carousel modal with the provided photos and start index.
*
* @remarks
* This function sets the photos and start index for the photo carousel and then opens the carousel modal.
* If no photos are provided, it defaults to the landlord's photos.
*
* @param {readonly string[]} [photos] – The array of photo URLs to display in the carousel.
* @param {number} [startIndex] – The index of the photo to start the carousel from.
* @return {void} – This function does not return anything.
*/
const showPhotoCarousel = (
photos: readonly string[] = landlordData ? landlordData.photos : [],
startIndex: number = 0
) => {
setCarouselPhotos(photos);
setCarouselStartIndex(startIndex);
setCarouselOpen(true);
};

// Define a component 'Modals' conditionally based on landlordData existence
const Modals = landlordData && (
<>
Expand All @@ -327,7 +312,7 @@ const LandlordPage = ({ user, setUser }: Props): ReactElement => {
photos={carouselPhotos}
open={carouselOpen}
startIndex={carouselStartIndex}
onClose={() => setCarouselOpen(false)}
onClose={closePhotoCarousel}
/>
</>
);
Expand Down Expand Up @@ -365,7 +350,7 @@ const LandlordPage = ({ user, setUser }: Props): ReactElement => {
color="secondary"
variant="contained"
disableElevation
onClick={() => setCarouselOpen(true)}
onClick={() => showPhotoCarousel()}
>
Show all photos
</Button>
Expand Down Expand Up @@ -463,7 +448,7 @@ const LandlordPage = ({ user, setUser }: Props): ReactElement => {
color="secondary"
variant="contained"
disableElevation
onClick={() => setCarouselOpen(true)}
onClick={() => showPhotoCarousel()}
>
Show all photos
</Button>
Expand Down Expand Up @@ -523,7 +508,7 @@ const LandlordPage = ({ user, setUser }: Props): ReactElement => {
<>
{landlordData && (
<Container>
<LandlordHeader landlord={landlordData} handleClick={() => setCarouselOpen(true)} />
<LandlordHeader landlord={landlordData} handleClick={() => showPhotoCarousel()} />
</Container>
)}

Expand Down
30 changes: 9 additions & 21 deletions frontend/src/pages/ProfilePage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import defaultProfilePic from '../assets/cuapts-bear.png';
import { useTitle } from '../utils';
import { sortReviews } from '../utils/sortReviews';
import PhotoCarousel from '../components/PhotoCarousel/PhotoCarousel';
import usePhotoCarousel from '../components/PhotoCarousel/usePhotoCarousel';

type Props = {
user: firebase.User | null;
Expand Down Expand Up @@ -167,9 +168,13 @@ const ProfilePage = ({ user, setUser }: Props): ReactElement => {
const [toggle, setToggle] = useState(false);
const [showEditSuccessConfirmation, setShowEditSuccessConfirmation] = useState(false);
const toastTime = 3500;
const [carouselOpen, setCarouselOpen] = useState(false);
const [carouselPhotos, setCarouselPhotos] = useState<readonly string[]>([]);
const [carouselStartIndex, setCarouselStartIndex] = useState<number>(0);
const {
carouselPhotos,
carouselStartIndex,
carouselOpen,
showPhotoCarousel,
closePhotoCarousel,
} = usePhotoCarousel([]);

useTitle('Profile');

Expand Down Expand Up @@ -260,29 +265,12 @@ const ProfilePage = ({ user, setUser }: Props): ReactElement => {
};
}, [isModalOpen, setIsModalOpen]);

/**
* showPhotoCarousel – Opens the photo carousel modal with the provided photos and start index.
*
* @remarks
* This function sets the photos and start index for the photo carousel and then opens the carousel modal.
* If no photos are provided, it defaults to showing no photos.
*
* @param {readonly string[]} [photos] – The array of photo URLs to display in the carousel.
* @param {number} [startIndex] – The index of the photo to start the carousel from.
* @return {void} – This function does not return anything.
*/
const showPhotoCarousel = (photos: readonly string[] = [], startIndex: number = 0) => {
setCarouselPhotos(photos);
setCarouselStartIndex(startIndex);
setCarouselOpen(true);
};

const Modals = (
<>
<PhotoCarousel
photos={carouselPhotos}
open={carouselOpen}
onClose={() => setCarouselOpen(false)}
onClose={closePhotoCarousel}
startIndex={carouselStartIndex}
/>
</>
Expand Down

0 comments on commit a84e11f

Please sign in to comment.