Commit 81415f4e by Madhankumar

code changes in header

parent bdf75829
"use client"; "use client";
import { useEffect } from "react";
import Header from "@components/base/header"; import Header from "@components/base/header";
import { useAppContext } from "app/theme-context"; import { useThemeContext } from "app/theme-context";
import { useRouter } from "next/navigation"; import { useRouter } from "next/navigation";
export default function PageHeader({ searchParams }) { export default function PageHeader({ searchParams }) {
const { theme, toggleTheme, handleSearchInput, text, isDisabled } = const { theme, toggleTheme } = useThemeContext();
useAppContext();
const router = useRouter(); const router = useRouter();
const currentPage = 1; // Assuming currentPage is constant for now const currentPage = 1; // Assuming currentPage is constant for now
const search = searchParams.search; const search = searchParams.search;
useEffect(() => {
if (search) handleSearchInput(search, currentPage);
}, [search]);
const handleSearch = (value) => { const handleSearch = (value) => {
handleSearchInput(value, currentPage); const query = value
? `?page=${currentPage}&search=${value}`
: `?page=${currentPage}`;
router.push(query);
}; };
const handleClear = () => { const handleClear = () => {
router.push(`/?page=${currentPage}`, undefined, { shallow: true }); router.push(`/?page=${currentPage}`);
}; };
return ( return (
...@@ -31,8 +29,7 @@ export default function PageHeader({ searchParams }) { ...@@ -31,8 +29,7 @@ export default function PageHeader({ searchParams }) {
onThemeChange={toggleTheme} onThemeChange={toggleTheme}
onSearch={handleSearch} onSearch={handleSearch}
onClear={handleClear} onClear={handleClear}
value={text} value={search}
isDisabled={isDisabled}
/> />
); );
} }
"use client"; "use client";
import { createContext, useContext, useState, useEffect } from "react"; import { createContext, useContext, useState, useEffect } from "react";
import { useRouter, useSearchParams, usePathname } from "next/navigation"; // import { useRouter, useSearchParams, usePathname } from "next/navigation";
export const ThemeContext = createContext(); export const ThemeContext = createContext();
export const useAppContext = () => useContext(ThemeContext); export const useThemeContext = () => useContext(ThemeContext);
export const ThemeProvider = ({ children }) => { export const ThemeProvider = ({ children }) => {
const [theme, setTheme] = useState("light"); const [theme, setTheme] = useState("light");
const [text, setText] = useState(""); // const [text, setText] = useState("");
const [isDisabled, setIsDisabled] = useState(false); // const [isDisabled, setIsDisabled] = useState(false);
const router = useRouter(); // const router = useRouter();
const pathName = usePathname(); // const pathName = usePathname();
const searchParams = useSearchParams(); // const searchParams = useSearchParams();
const search = searchParams.get("search"); // const search = searchParams.get("search");
useEffect(() => { useEffect(() => {
const storedTheme = localStorage.getItem("theme"); const storedTheme = localStorage.getItem("theme");
setTheme(storedTheme || "light"); setTheme(storedTheme || "light");
}, []); }, []);
useEffect(() => { // useEffect(() => {
setText(search || ""); // setText(search || "");
setIsDisabled(pathName.includes("singleblog")); // setIsDisabled(pathName.includes("singleblog"));
}, [pathName, search]); // }, [pathName, search]);
const toggleTheme = () => { const toggleTheme = () => {
const newTheme = theme === "light" ? "dark" : "light"; const newTheme = theme === "light" ? "dark" : "light";
...@@ -31,20 +31,17 @@ export const ThemeProvider = ({ children }) => { ...@@ -31,20 +31,17 @@ export const ThemeProvider = ({ children }) => {
setTheme(newTheme); setTheme(newTheme);
}; };
const handleSearchInput = (value, currentPage) => { // const handleSearchInput = (value, currentPage) => {
setText(value); // setText(value);
const query = value // const query = value
? `?page=${currentPage}&search=${value}` // ? `?page=${currentPage}&search=${value}`
: `?page=${currentPage}`; // : `?page=${currentPage}`;
router.push(query, undefined, { shallow: true }); // router.push(query, undefined, { shallow: true });
}; // };
const contextValue = { const contextValue = {
theme, theme,
toggleTheme, toggleTheme,
text,
handleSearchInput,
isDisabled,
}; };
return ( return (
......
...@@ -11,7 +11,6 @@ function Header({ ...@@ -11,7 +11,6 @@ function Header({
onClear, onClear,
onThemeChange, onThemeChange,
value, value,
isDisabled,
}) { }) {
const handleTheme = () => { const handleTheme = () => {
onThemeChange(currentTheme); onThemeChange(currentTheme);
...@@ -37,7 +36,6 @@ function Header({ ...@@ -37,7 +36,6 @@ function Header({
onSearch={handleSearch} onSearch={handleSearch}
onClear={handleClear} onClear={handleClear}
value={value} value={value}
disabled={isDisabled}
/> />
</div> </div>
......
...@@ -3,7 +3,7 @@ import PropTypes from "prop-types"; ...@@ -3,7 +3,7 @@ import PropTypes from "prop-types";
import Icons from "@components/base/icons"; import Icons from "@components/base/icons";
import styles from "./styles.module.css"; import styles from "./styles.module.css";
const Search = ({ onSearch, onClear, value, ...props }) => { const Search = ({ onSearch, onClear, value }) => {
const [inputValue, setInputValue] = useState(value); const [inputValue, setInputValue] = useState(value);
useEffect(() => { useEffect(() => {
...@@ -37,7 +37,6 @@ const Search = ({ onSearch, onClear, value, ...props }) => { ...@@ -37,7 +37,6 @@ const Search = ({ onSearch, onClear, value, ...props }) => {
onChange={handleChange} onChange={handleChange}
onKeyUp={handleKeyPress} onKeyUp={handleKeyPress}
placeholder="Discover news, articles and more" placeholder="Discover news, articles and more"
{...props}
/> />
{inputValue && ( {inputValue && (
<span className={`${styles["close-icon"]}`} onClick={handleCustomClear}> <span className={`${styles["close-icon"]}`} onClick={handleCustomClear}>
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
"@fortawesome/react-fontawesome": "^0.1.14", "@fortawesome/react-fontawesome": "^0.1.14",
"classnames": "^2.3.2", "classnames": "^2.3.2",
"gray-matter": "^4.0.3", "gray-matter": "^4.0.3",
"next": "^14.0.3", "next": "^14.0.4",
"path": "^0.12.7", "path": "^0.12.7",
"prettier": "^3.1.0", "prettier": "^3.1.0",
"react": "^18", "react": "^18",
......
...@@ -16,7 +16,7 @@ ...@@ -16,7 +16,7 @@
"@fortawesome/react-fontawesome": "^0.1.14", "@fortawesome/react-fontawesome": "^0.1.14",
"classnames": "^2.3.2", "classnames": "^2.3.2",
"gray-matter": "^4.0.3", "gray-matter": "^4.0.3",
"next": "^14.0.3", "next": "^14.0.4",
"path": "^0.12.7", "path": "^0.12.7",
"prettier": "^3.1.0", "prettier": "^3.1.0",
"react": "^18", "react": "^18",
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment