Skip to content

Commit 36a3ad2

Browse files
chore: tidy up config
1 parent 8484f9a commit 36a3ad2

File tree

11 files changed

+154
-88
lines changed

11 files changed

+154
-88
lines changed
File renamed without changes.

config/postcss.config.cjs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/* eslint-disable no-undef */
2+
module.exports = {
3+
plugins: {
4+
tailwindcss: { config: './config/tailwind.config.cjs' },
5+
autoprefixer: {},
6+
},
7+
}

config/tailwind.config.cjs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/** @type {import('tailwindcss').Config} */
2+
export default {
3+
content: ['./src/**/*.{js,jsx,ts,tsx,mdx}'],
4+
darkMode: ['class', '[data-mode="dark"]'],
5+
theme: {
6+
extend: {
7+
colors: {
8+
modal: {
9+
bg: 'rgba(0, 0, 0, 0.3)'
10+
},
11+
code: {
12+
950: '#1E1E3F',
13+
750: '#2D2B55',
14+
600: '#a03fc0',
15+
500: '#A599E9'
16+
}
17+
}
18+
}
19+
},
20+
plugins: []
21+
};
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import { defineConfig } from 'vite';
22
import react from '@vitejs/plugin-react';
3+
import { resolve } from 'path';
34

45
// https://vitejs.dev/config/
56
export default defineConfig({
6-
plugins: [react()]
7+
plugins: [react()],
8+
root: resolve(__dirname, '..')
79
});

package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
"version": "0.0.0",
55
"type": "module",
66
"scripts": {
7-
"dev": "vite",
8-
"build": "tsc -b && vite build",
9-
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives",
10-
"preview": "vite preview",
7+
"dev": "vite --config config/vite.config.ts",
8+
"build": "tsc && vite build --config config/vite.config.ts",
9+
"lint": "eslint . --ext ts,tsx --report-unused-disable-directives --config config/.eslintrc.cjs",
10+
"preview": "vite preview --config config/vite.config.ts",
1111
"storybook": "storybook dev -p 6006",
1212
"build-storybook": "npm run build && storybook build -o dist/storybook",
1313
"test": "test-storybook",

postcss.config.cjs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1 @@
1-
/* eslint-disable no-undef */
2-
module.exports = {
3-
plugins: {
4-
tailwindcss: {},
5-
autoprefixer: {},
6-
},
7-
}
1+
module.exports = require('./config/postcss.config.cjs');

src/shared/hooks/usePokedex.ts

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
// define the api types like a generic
2+
3+
import { useEffect, useReducer } from 'react';
4+
5+
export type TPokemonCardsApiResponse = {
6+
id: string;
7+
name: string;
8+
images: {
9+
small: string;
10+
};
11+
};
12+
13+
export type TPokemonTypesApiResponse = string;
14+
15+
type TTypesApi = {
16+
path: 'types';
17+
skip?: boolean;
18+
queryParams?: string;
19+
fail?: boolean;
20+
};
21+
22+
type TCardsApi = {
23+
path: 'cards';
24+
queryParams?: string;
25+
skip?: boolean;
26+
fail?: boolean;
27+
};
28+
29+
interface IUsePokedexState<TResponse> {
30+
data?: TResponse;
31+
isError?: boolean;
32+
isLoading?: boolean;
33+
}
34+
35+
const usePokedexReducer = <TResponse>(
36+
state: IUsePokedexState<TResponse>,
37+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
38+
action: { type: string; payload?: any }
39+
): IUsePokedexState<TResponse> => {
40+
switch (action.type) {
41+
case 'SUCCESS':
42+
return { ...state, isLoading: false, data: action.payload };
43+
case 'LOADING':
44+
return { ...state, isLoading: true };
45+
case 'ERROR':
46+
return { ...state, isLoading: false };
47+
default:
48+
return state;
49+
}
50+
};
51+
52+
export const usePokedex = <TResponse>({
53+
path,
54+
queryParams = '',
55+
skip = false,
56+
fail = false
57+
}: TCardsApi | TTypesApi): IUsePokedexState<TResponse> => {
58+
const [state, dispatch] = useReducer(usePokedexReducer<TResponse>, {
59+
isError: false,
60+
isLoading: false,
61+
data: undefined
62+
});
63+
64+
useEffect(() => {
65+
if (skip) return;
66+
67+
const getData = async () => {
68+
try {
69+
dispatch({ type: 'LOADING' });
70+
const response = await fetch(
71+
`https://api.pokemontcg.io/v2/${path}?${queryParams}`
72+
);
73+
74+
if (!response.ok) {
75+
throw new Error(`HTTP error! status: ${response.status}`);
76+
}
77+
78+
const json = await response.json();
79+
80+
if (fail) {
81+
throw new Error('Error');
82+
}
83+
84+
dispatch({ type: 'SUCCESS', payload: json.data });
85+
} catch (e) {
86+
dispatch({ type: 'ERROR' });
87+
console.error('Failed to fetch Pokemon data:', e instanceof Error ? e.message : 'Unknown error');
88+
}
89+
};
90+
91+
getData();
92+
}, [skip, path, queryParams]);
93+
94+
return state;
95+
};

tailwind.config.cjs

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1 @@
1-
/** @type {import('tailwindcss').Config} */
2-
export default {
3-
content: ['./src/**/*.{js,jsx,ts,tsx,mdx}'],
4-
darkMode: ['class', '[data-mode="dark"]'],
5-
theme: {
6-
extend: {
7-
colors: {
8-
modal: {
9-
bg: 'rgba(0, 0, 0, 0.3)'
10-
},
11-
code: {
12-
950: '#1E1E3F',
13-
750: '#2D2B55',
14-
600: '#a03fc0',
15-
500: '#A599E9'
16-
}
17-
}
18-
}
19-
},
20-
plugins: []
21-
};
1+
module.exports = require('./config/tailwind.config.cjs');

tsconfig.app.json

Lines changed: 0 additions & 31 deletions
This file was deleted.

tsconfig.json

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,25 @@
11
{
2-
"files": [],
3-
"references": [
4-
{
5-
"path": "./tsconfig.app.json"
6-
},
7-
{
8-
"path": "./tsconfig.node.json"
2+
"compilerOptions": {
3+
"target": "ES2020",
4+
"useDefineForClassFields": true,
5+
"lib": ["ES2020", "DOM", "DOM.Iterable"],
6+
"module": "ESNext",
7+
"skipLibCheck": true,
8+
"moduleResolution": "bundler",
9+
"allowImportingTsExtensions": true,
10+
"resolveJsonModule": true,
11+
"isolatedModules": true,
12+
"moduleDetection": "force",
13+
"noEmit": true,
14+
"jsx": "react-jsx",
15+
"strict": true,
16+
"noUnusedLocals": true,
17+
"noUnusedParameters": true,
18+
"noFallthroughCasesInSwitch": true,
19+
"paths": {
20+
"@shared/*": ["./src/shared/*"]
921
}
10-
]
22+
},
23+
"include": ["src", "config/vite.config.ts"],
24+
"exclude": ["src/**/*.mdx"]
1125
}

0 commit comments

Comments
 (0)