Skip to content

feat: add theme param url behaviour with react-router #69

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 6 additions & 9 deletions src/components/Themes/ThemeCard.tsx
Original file line number Diff line number Diff line change
@@ -1,29 +1,26 @@
import React, { useState } from "react";
import React from "react";

import Skeleton from "react-loading-skeleton";

import { Theme } from "../../interfaces/Theme";
import { useTranslation } from 'react-i18next';
import "../../styles/theme_card.css";
import ThemeModal from "./ThemeModal";
import { InfoIcon } from "lucide-react";

type Props = {
theme: Theme;
isPreviewed: boolean;
onPreview: (name: string) => void;
isLoading: boolean;
onViewDetails: (theme: Theme) => void
};

/**
* Theme card component to hold the details of each theme in the themes page.
*/
const ThemeCard: React.FC<Props> = ({ theme, isPreviewed, onPreview, isLoading }) => {
const [viewDetails, setViewDetails] = useState(false);
const ThemeCard: React.FC<Props> = ({ theme, isPreviewed, onViewDetails, onPreview, isLoading }) => {


const onViewDetails = () => {
setViewDetails(true);
};

const onClickPreview = () => {
onPreview(theme.name)
Expand Down Expand Up @@ -68,7 +65,7 @@ const ThemeCard: React.FC<Props> = ({ theme, isPreviewed, onPreview, isLoading }
</div>
<div className="flex-1 basis-1/6 md:basis-1/6 flex flex-col">
<div className="flex items-center text-blue-500">
<button onClick={onViewDetails} className="text-sm my-4 w-fit mr-[3px]">
<button onClick={()=>(onViewDetails(theme))} className="text-sm my-4 w-fit mr-[3px]">
More Info
</button>
<InfoIcon size={15}/>
Expand All @@ -79,7 +76,7 @@ const ThemeCard: React.FC<Props> = ({ theme, isPreviewed, onPreview, isLoading }
</label>
</div>
</div>
<ThemeModal isOpen={viewDetails} theme={theme} onClose={() => setViewDetails(false)} />

</>
);
};
Expand Down
54 changes: 53 additions & 1 deletion src/pages/Themes.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ import ThemePreview from '../components/Themes/ThemePreview';
import { InfoIcon } from 'lucide-react';

import { useTranslation } from 'react-i18next';
import ThemeModal from '@/components/Themes/ThemeModal';
import { useEffect } from 'react';
import { Theme } from '@/interfaces/Theme';

/**
* Displays themes for users to search, browse and rate.
Expand All @@ -26,6 +29,8 @@ const Themes: React.FC = () => {
() => searchParams.get('searchQuery') || ''
);

const [focusedTheme, setFocusedTheme] = useState(false)

// id of themes being selected to be preview (and applied to the interactive chatbot)
const [previewIds, setPreviewIds] = useState<string[]>([]);

Expand All @@ -41,7 +46,47 @@ const Themes: React.FC = () => {
searchQuery
);

console.log(themes);
useEffect(()=>{
//to update the current focused theme based on url and fetched themes
const focusedThemeId = searchParams.get('theme') || '';
if(focusedThemeId == ''){
return;
}
const focusedThemeObject = themes.find(theme => theme.id === focusedThemeId);
if(!focusedThemeObject){
return
}
setFocusedTheme(focusedThemeObject as unknown as boolean)

}, [themes])

/**
* Handles the closinf of modal and cleanup of url
*
*/
const modalCloseHandler = () => {
//first update the search params
setSearchParams(params => {
params.delete('theme')
return params;
})
return false;
}

/**
* Handles opening of the modal for a theme, and updates the url accordingly
*
* @param theme the theme to focus on
*/
const modalOpenHandler = (theme: Theme) => {
setSearchParams(params => {
params.set('theme', theme.id)
return params;
})
return theme as unknown as boolean
}


/**
* Handles setting of search query when user hits enter.
*
Expand Down Expand Up @@ -75,6 +120,7 @@ const Themes: React.FC = () => {
return <div>Error: {error.message}</div>;
}


return (
// 92vh comes from 100vh - 8vh (the height of the navbar)
<div className="bg-accent-950 flex h-[92vh] w-full">
Expand Down Expand Up @@ -107,11 +153,17 @@ const Themes: React.FC = () => {
isPreviewed={previewIds.includes(theme.id)}
onPreview={() => onPreview(theme.id)}
isLoading={loading}
onViewDetails={(theme) => (setFocusedTheme(modalOpenHandler(theme)))}
/>
);
})}
</div>
</div>
{focusedTheme && <ThemeModal
isOpen={focusedTheme}
onClose={()=>(setFocusedTheme(modalCloseHandler))}
theme={focusedTheme as unknown as Theme}
/>}
{/* Drawer Section */}
<ThemePreview
setPreviewIds={setPreviewIds}
Expand Down
Loading