Skip to content

Commit

Permalink
feat: migrate context (#2701)
Browse files Browse the repository at this point in the history
Co-authored-by: Akshat Nema <[email protected]>
  • Loading branch information
vishvamsinh28 and akshatnema authored Mar 2, 2024
1 parent b19eab1 commit a760106
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 0 deletions.
4 changes: 4 additions & 0 deletions context/AppContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { createContext } from 'react';
import { AppContextType } from '@/types/context/AppContext';

export default createContext<AppContextType>({ path: "" });
4 changes: 4 additions & 0 deletions context/BlogContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { createContext } from 'react';
import { IDocs } from '@/types/post';

export default createContext<IDocs>([]);
9 changes: 9 additions & 0 deletions context/DocsContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { createContext } from 'react';
import { DocsContextType } from '@/types/context/DocsContext';

const initialDocsContext: DocsContextType = {
post: [],
navItems: {},
};

export default createContext<DocsContextType>(initialDocsContext);
4 changes: 4 additions & 0 deletions context/GenericPostContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { createContext } from 'react';
import { IDocs } from '@/types/post';

export default createContext<IDocs>([]);
50 changes: 50 additions & 0 deletions context/ToolFilterContext.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import { useRouter } from 'next/router';
import { createContext, useEffect, useState } from 'react';

interface ToolFilterContextProps {
isPaid: string;
isAsyncAPIOwner: boolean;
languages: string[];
technologies: string[];
categories: string[];
}

export const ToolFilterContext = createContext<ToolFilterContextProps>({
isPaid: "all",
isAsyncAPIOwner: false,
languages: [],
technologies: [],
categories: [],
});

function ToolFilter({ children }: { children: React.ReactNode }) {
const router = useRouter();
const [isPaid, setIsPaid] = useState<string>("all");
const [isAsyncAPIOwner, setIsAsyncAPIOwner] = useState<boolean>(false);
const [languages, setLanguages] = useState<string[]>([]);
const [technologies, setTechnologies] = useState<string[]>([]);
const [categories, setCategories] = useState<string[]>([]);

// useEffect has been used to apply filters to the tool on each change of router query
useEffect(() => {
if (!router || !router.isReady) return;

let { pricing, owned, langs, techs, categories } = router.query;

setIsPaid(pricing as string || "all");
setIsAsyncAPIOwner(owned === "true");
setLanguages((langs as string)?.split(",") || []);
setTechnologies((techs as string)?.split(",") || []);
setCategories((categories as string)?.split(",") || []);
}, [router?.query]);

return (
<ToolFilterContext.Provider
value={{ isPaid, isAsyncAPIOwner, languages, technologies, categories }}
>
{children}
</ToolFilterContext.Provider>
);
}

export default ToolFilter;
3 changes: 3 additions & 0 deletions types/context/AppContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export interface AppContextType {
path: string;
}
14 changes: 14 additions & 0 deletions types/context/DocsContext.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { IDocs } from "../post";

export interface NavigationItem {
title: string;
}

export interface NavigationItems {
[key: string]: NavigationItem;
}

export interface DocsContextType {
post: IDocs;
navItems: NavigationItems;
}

0 comments on commit a760106

Please sign in to comment.