@aditokmo/react-setup-cli 0.1.0 → 0.1.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +90 -28
- package/dist/index.js +13 -6
- package/dist/installers.js +10 -3
- package/dist/mapper.js +2 -1
- package/dist/packages.js +3 -4
- package/dist/questions.js +26 -1
- package/dist/utils.js +8 -0
- package/package.json +1 -1
- package/templates/base/src/modules/auth/hooks/useAuth.ts +49 -37
- package/templates/base/src/modules/common/pages/NotFound.tsx +12 -7
- package/templates/base/src/styles/404.css +105 -0
- package/templates/state/react-query/src/hook/useAuth.ts +43 -0
- package/templates/state/react-query/src/{ReactQueryProvider.tsx → provider/ReactQueryProvider.tsx} +10 -3
- package/templates/base/pnpm-lock.yaml +0 -2105
- /package/templates/state/react-query/src/{index.ts → provider/index.ts} +0 -0
package/README.md
CHANGED
|
@@ -4,17 +4,11 @@
|
|
|
4
4
|
|
|
5
5
|
A React CLI built with Vite that helps you build and structure your projects in seconds. It eliminates manual setup by configuring your favorite tools into a **clean, modular architecture** automatically.
|
|
6
6
|
|
|
7
|
-
**Note:** This package is a CLI tool. Do not install it
|
|
8
|
-
|
|
9
|
-
## What it does
|
|
10
|
-
|
|
11
|
-
- **Automated Installation:** Installs all selected libraries (listed below) for you.
|
|
12
|
-
- **Structuring:** Automatically generates a scalable folder structure based on your choices.
|
|
13
|
-
- **Boilerplate Injection:** Pre-configures Providers, Router paths, etc., so you can start coding features immediately
|
|
7
|
+
**Note:** This package is a CLI tool. Do not install it with `npm i`. Instead check `Quick Start` down below.
|
|
14
8
|
|
|
15
9
|
---
|
|
16
10
|
|
|
17
|
-
##
|
|
11
|
+
## Quick Start
|
|
18
12
|
|
|
19
13
|
Run the following command in your terminal to start CLI
|
|
20
14
|
|
|
@@ -31,23 +25,57 @@ yarn dlx @aditokmo/react-setup-cli
|
|
|
31
25
|
|
|
32
26
|
---
|
|
33
27
|
|
|
34
|
-
##
|
|
28
|
+
## How it saves your time
|
|
35
29
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
30
|
+
- **Minimal Installation:** No more manual `npm install` for 10 different packages. The CLI detects your package manager and handles everything.
|
|
31
|
+
- **Architecture:** Instead of a messy folder structure, you get a **modular (feature-based)** structure that is ready for large-scale production.
|
|
32
|
+
- **Smart Boilerplate Injection:** It doesn't just create files it also wires them. It sets up Axios interceptors, TanStack Query providers, and Router wrappers so you can jump straight into the code that actually matters.
|
|
33
|
+
|
|
34
|
+
---
|
|
35
|
+
|
|
36
|
+
## Use Cases when this CLI is useful:
|
|
37
|
+
|
|
38
|
+
- **Starting a Professional Project:** When you need a project that follows "Clean Architecture" and industry standards from the very first commit.
|
|
39
|
+
- **Focusing on a Specific Feature:** When you want to test a new library or a specific piece of logic, but you need a proper environment to do it. This CLI lets you focus on what you are testing instead of wasting time on a setup.
|
|
40
|
+
- **Prototyping & MVP:** When you have a startup idea and want to to build actual features right away without sacrificing code quality.
|
|
41
|
+
- **Hackathons:** When every second counts. You can get all your configuration and setup ready before the competition start.
|
|
42
|
+
|
|
43
|
+
---
|
|
44
|
+
|
|
45
|
+
## Decisions About Arhitecture
|
|
46
|
+
|
|
47
|
+
This CLI isn't just a downloader it is architectural choice designed to enforce professional standards.
|
|
48
|
+
|
|
49
|
+
### Feature-Based Structure
|
|
50
|
+
|
|
51
|
+
This architecture uses a modular approach to help you build large-scale projects. Instead of mixing all components and hooks into global folders, everything is grouped by domain, such as Auth or Dashboard. This method makes it much easier to navigate the codebase and ensures your project remains maintainable as it grows.
|
|
52
|
+
|
|
53
|
+
### Global State Management (Zustand)
|
|
54
|
+
|
|
55
|
+
Zustand is currently the only option in CLI for global state management. It is the most popular and easiest-to-use library today. I believe Redux is overkill for most modern projects. Most "global state" is now handled as server-state by TanStack Query. Global state should be reserved for things like authentication and UI stuff, and Zustand handles this perfectly with zero boilerplate.
|
|
56
|
+
|
|
57
|
+
### Axios
|
|
58
|
+
|
|
59
|
+
The CLI generates a pre-configured Axios client that serves as your central API bridge. It includes ready-to-use interceptors for handling authorization tokens and global error responses, saving you from writing the same repetitive setup every time. Axios is set by default beacause it is better choice then fetch, Axios is more user-friendly, has better error handling out of the box, and is overall a safer and more robust choice for production apps.
|
|
60
|
+
|
|
61
|
+
### TanStack Query
|
|
62
|
+
|
|
63
|
+
TanStack Query is integrated to handle server-state management. It is optional, but if selected, the CLI automatically wires up the necessary providers and configurations so you can start fetching data immediately. If you select to not use react query, you still get a traditional boilerplate for manual data handling.
|
|
64
|
+
|
|
65
|
+
### Styling
|
|
66
|
+
|
|
67
|
+
You can choose between CSS, SCSS (soon), or Tailwind CSS. While I personally recommend Tailwind for modern and faster development, the CLI ensures that regardless of your choice, the project is configured with a global styles directory and a consistent entry point. If you select TailwindCSS you will also have option to use Shadcn/UI, and with that you will have option to choose components that you want to install instead of doing it manually.
|
|
68
|
+
|
|
69
|
+
### Routing
|
|
70
|
+
|
|
71
|
+
The CLI offers two powerful options: React Router and TanStack Router.
|
|
72
|
+
|
|
73
|
+
- **React Router** is the industry standard that most developers are familiar with.
|
|
74
|
+
- **TanStack Router** is included for those who want a fully type-safe routing experience with built-in data loading capabilities. Whichever you choose, the CLI doesn't just install the library it will generate a `routes/` directory system to help you easily separate your public pages from protected pages.
|
|
75
|
+
|
|
76
|
+
### Package Manager Detection
|
|
77
|
+
|
|
78
|
+
To make the workflow even smoother, the tool has an automatic package manager detector. It identifies whether you are using npm, pnpm, or yarn based on the command you used to execute the CLI, and it handles all installations using your preferred package manager to ensure consistency and avoid conflicts.
|
|
51
79
|
|
|
52
80
|
---
|
|
53
81
|
|
|
@@ -67,6 +95,9 @@ src/
|
|
|
67
95
|
│ ├── MainLayout.tsx
|
|
68
96
|
│ ├── AuthLayout.tsx
|
|
69
97
|
├── modules/ # Feature-based modules (The core of your app)
|
|
98
|
+
| ├── common/ # Shared components & pages (404 Page, Navbar, Sidebar)
|
|
99
|
+
│ │ ├── pages/
|
|
100
|
+
│ │ └── components/
|
|
70
101
|
│ └── auth/ # Example: Auth module
|
|
71
102
|
│ ├── components/
|
|
72
103
|
│ ├── hooks/
|
|
@@ -81,6 +112,41 @@ src/
|
|
|
81
112
|
|
|
82
113
|
---
|
|
83
114
|
|
|
115
|
+
## Features
|
|
116
|
+
|
|
117
|
+
| Category | Options |
|
|
118
|
+
| :------------------- | :-------------------------------------- |
|
|
119
|
+
| **Folder Structure** | Feature-based |
|
|
120
|
+
| **Modules** | Common, Auth |
|
|
121
|
+
| **Routing** | React Router, TanStack Router |
|
|
122
|
+
| **Data Fetching** | TanStack Query (React Query) & Axios |
|
|
123
|
+
| **State Management** | Zustand |
|
|
124
|
+
| **Form** | React Hook Form, TanStack Form |
|
|
125
|
+
| **Schema** | Zod, Yup |
|
|
126
|
+
| **Styling** | CSS, SCSS, Tailwind CSS |
|
|
127
|
+
| **UI Components** | Shadcn |
|
|
128
|
+
| **Icons** | React Icons, Font Awesome |
|
|
129
|
+
| **Toast** | React Toastify, React Hot Toast, Sonner |
|
|
130
|
+
| **Custom Hooks** | |
|
|
131
|
+
| **Helpers** | |
|
|
132
|
+
|
|
133
|
+
---
|
|
134
|
+
|
|
135
|
+
## Future of CLI
|
|
136
|
+
|
|
137
|
+
Some of my ideas
|
|
138
|
+
|
|
139
|
+
- Options to choose between React, Next.js and TanStack Start
|
|
140
|
+
- Testing tools
|
|
141
|
+
- i18next pre-setup.
|
|
142
|
+
- Supabase & Firebase integration templates
|
|
143
|
+
- Global custom hooks & helper functions
|
|
144
|
+
- Pre-commit linters
|
|
145
|
+
- Github Action workflow
|
|
146
|
+
- TanStack Table (if your app has some kind of tables)
|
|
147
|
+
|
|
148
|
+
---
|
|
149
|
+
|
|
84
150
|
## Local Setup
|
|
85
151
|
|
|
86
152
|
To do your own changes and use this CLI locally:
|
|
@@ -128,7 +194,3 @@ react-setup-cli
|
|
|
128
194
|
- `templates/` - Pre-defined boilerplates and configurations.
|
|
129
195
|
|
|
130
196
|
---
|
|
131
|
-
|
|
132
|
-
<p align="center">
|
|
133
|
-
Developed with ❤️ by <a href="https://github.com/aditokmo">aditokmo</a>
|
|
134
|
-
</p>
|
package/dist/index.js
CHANGED
|
@@ -3,7 +3,7 @@ import fs from 'fs-extra';
|
|
|
3
3
|
import path from 'path';
|
|
4
4
|
import { execSync } from 'child_process';
|
|
5
5
|
import { askQuestions } from './questions.js';
|
|
6
|
-
import { copyTemplate, patchViteConfig, finalizeViteConfig, patchAppFile, finalizeAppFile } from './utils.js';
|
|
6
|
+
import { copyTemplate, patchViteConfig, finalizeViteConfig, patchAppFile, finalizeAppFile, detectPackageManager } from './utils.js';
|
|
7
7
|
import { collectDependencies } from './installers.js';
|
|
8
8
|
import { fileURLToPath } from 'url';
|
|
9
9
|
const __filename = fileURLToPath(import.meta.url);
|
|
@@ -11,8 +11,11 @@ const __dirname = path.dirname(__filename);
|
|
|
11
11
|
async function main() {
|
|
12
12
|
console.log('⚛️ Welcome to React CLI Setup by github/aditokmo');
|
|
13
13
|
let projectDir = '';
|
|
14
|
+
const originalDirectory = process.cwd();
|
|
14
15
|
try {
|
|
15
16
|
const answers = await askQuestions();
|
|
17
|
+
const packageManager = detectPackageManager();
|
|
18
|
+
const installAction = packageManager === 'yarn' ? 'add' : 'install';
|
|
16
19
|
projectDir = path.join(process.cwd(), answers.projectName);
|
|
17
20
|
const templateRoot = path.join(__dirname, '../templates');
|
|
18
21
|
const appFilePath = path.join(projectDir, 'src', 'App.tsx');
|
|
@@ -29,7 +32,8 @@ async function main() {
|
|
|
29
32
|
}
|
|
30
33
|
// State Management
|
|
31
34
|
if (answers.reactQuery) {
|
|
32
|
-
copyTemplate(path.join(templateRoot, 'state', 'react-query', 'src'), path.join(projectDir, 'src/providers'));
|
|
35
|
+
copyTemplate(path.join(templateRoot, 'state', 'react-query', 'src', 'provider'), path.join(projectDir, 'src/providers'));
|
|
36
|
+
copyTemplate(path.join(templateRoot, 'state', 'react-query', 'src', 'hook'), path.join(projectDir, 'src/modules/auth/hooks'));
|
|
33
37
|
patchAppFile(appFilePath, "import { ReactQueryProvider } from './providers'", "<ReactQueryProvider>", "</ReactQueryProvider>");
|
|
34
38
|
}
|
|
35
39
|
if (answers.globalState === 'zustand') {
|
|
@@ -50,15 +54,17 @@ async function main() {
|
|
|
50
54
|
}
|
|
51
55
|
finalizeAppFile(appFilePath);
|
|
52
56
|
finalizeViteConfig(viteConfigPath);
|
|
53
|
-
const { dependency, devDependency, cmd } = collectDependencies(answers);
|
|
57
|
+
const { dependency, devDependency, cmd } = collectDependencies(answers, packageManager);
|
|
54
58
|
process.chdir(projectDir);
|
|
59
|
+
console.log(`📦 Initializing ${packageManager} project...`);
|
|
60
|
+
execSync(`${packageManager} install`, { stdio: 'inherit' });
|
|
55
61
|
if (dependency.length) {
|
|
56
62
|
console.log('📦 Installing dependencies...');
|
|
57
|
-
execSync(
|
|
63
|
+
execSync(`${packageManager} ${installAction} ${dependency.join(' ')}`, { stdio: 'inherit' });
|
|
58
64
|
}
|
|
59
65
|
if (devDependency.length) {
|
|
60
66
|
console.log('📦 Installing dev dependencies...');
|
|
61
|
-
execSync(
|
|
67
|
+
execSync(`${packageManager} ${installAction} -D ${devDependency.join(' ')}`, { stdio: 'inherit' });
|
|
62
68
|
}
|
|
63
69
|
// Extra cmds like shadcn
|
|
64
70
|
for (const command of cmd) {
|
|
@@ -66,11 +72,12 @@ async function main() {
|
|
|
66
72
|
execSync(command, { stdio: 'inherit' });
|
|
67
73
|
}
|
|
68
74
|
console.log('✅ Project setup completed');
|
|
69
|
-
console.log(`👉 cd ${answers.projectName} &&
|
|
75
|
+
console.log(`👉 cd ${answers.projectName} && ${packageManager} run dev`);
|
|
70
76
|
}
|
|
71
77
|
catch (error) {
|
|
72
78
|
console.error('\n❌ Error while creating a project:');
|
|
73
79
|
console.error(`👉 ${error.message}`);
|
|
80
|
+
process.chdir(originalDirectory);
|
|
74
81
|
if (projectDir && fs.existsSync(projectDir)) {
|
|
75
82
|
console.log('🧹 Cleaning... Deleting failed project installation.');
|
|
76
83
|
fs.removeSync(projectDir);
|
package/dist/installers.js
CHANGED
|
@@ -1,19 +1,26 @@
|
|
|
1
1
|
import { installers } from './mapper.js';
|
|
2
|
-
import { axiosInstaller
|
|
3
|
-
export function collectDependencies(answers) {
|
|
2
|
+
import { axiosInstaller } from './packages.js';
|
|
3
|
+
export function collectDependencies(answers, packageManager) {
|
|
4
4
|
const dependency = new Set();
|
|
5
5
|
const devDependency = new Set();
|
|
6
6
|
const cmd = [];
|
|
7
|
+
const dlx = packageManager === 'npm' ? 'npx --yes' : `${packageManager} dlx`;
|
|
7
8
|
// Default base packages
|
|
8
9
|
axiosInstaller.dependency?.forEach(d => dependency.add(d));
|
|
9
10
|
// Packages from answers
|
|
10
11
|
Object.entries(answers).forEach(([key, value]) => {
|
|
12
|
+
if (Array.isArray(value))
|
|
13
|
+
return;
|
|
11
14
|
const installer = installers[value === true ? key : value];
|
|
12
15
|
installer?.dependency?.forEach(d => dependency.add(d));
|
|
13
16
|
installer?.devDependency?.forEach(d => devDependency.add(d));
|
|
14
17
|
});
|
|
15
18
|
if (answers.shadcn && answers.style === 'tailwind') {
|
|
16
|
-
cmd.push(
|
|
19
|
+
cmd.push(`${dlx} shadcn@latest init -d`);
|
|
20
|
+
if (answers.shadcnComponents && answers.shadcnComponents.length > 0) {
|
|
21
|
+
const componentsStr = answers.shadcnComponents.join(' ');
|
|
22
|
+
cmd.push(`${dlx} shadcn@latest add ${componentsStr} -y -o`);
|
|
23
|
+
}
|
|
17
24
|
}
|
|
18
25
|
return {
|
|
19
26
|
dependency: Array.from(dependency),
|
package/dist/mapper.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { fontAwesomeIconsInstaller, reactFormHookInstaller, reactHotToastInstaller, reactIconsInstaller, reactQueryInstaller, reactRouterInstaller, reactToastifyInstaller, sonnerInstaller, tailwindInstaller, tanstackFormInstaller, tanstackRouterInstaller, yupInstaller, zodInstaller, zustandInstaller } from './packages.js';
|
|
1
|
+
import { fontAwesomeIconsInstaller, phosphorIconsInstaller, reactFormHookInstaller, reactHotToastInstaller, reactIconsInstaller, reactQueryInstaller, reactRouterInstaller, reactToastifyInstaller, sonnerInstaller, tailwindInstaller, tanstackFormInstaller, tanstackRouterInstaller, yupInstaller, zodInstaller, zustandInstaller } from './packages.js';
|
|
2
2
|
export const installers = {
|
|
3
3
|
'reactQuery': reactQueryInstaller,
|
|
4
4
|
'zustand': zustandInstaller,
|
|
@@ -7,6 +7,7 @@ export const installers = {
|
|
|
7
7
|
'tailwind': tailwindInstaller,
|
|
8
8
|
'react-icons': reactIconsInstaller,
|
|
9
9
|
'font-awesome': fontAwesomeIconsInstaller,
|
|
10
|
+
'phosphor-icons': phosphorIconsInstaller,
|
|
10
11
|
'react-hot-toast': reactHotToastInstaller,
|
|
11
12
|
'react-toastify': reactToastifyInstaller,
|
|
12
13
|
'sonner': sonnerInstaller,
|
package/dist/packages.js
CHANGED
|
@@ -18,6 +18,9 @@ export const reactIconsInstaller = {
|
|
|
18
18
|
export const fontAwesomeIconsInstaller = {
|
|
19
19
|
dependency: ['@fortawesome/fontawesome-svg-core', '@fortawesome/react-fontawesome', '@fortawesome/free-solid-svg-icons', '@fortawesome/free-regular-svg-icons', '@fortawesome/free-brands-svg-icons'],
|
|
20
20
|
};
|
|
21
|
+
export const phosphorIconsInstaller = {
|
|
22
|
+
dependency: ['phosphor-react'],
|
|
23
|
+
};
|
|
21
24
|
// Toast
|
|
22
25
|
export const reactHotToastInstaller = {
|
|
23
26
|
dependency: ['react-hot-toast'],
|
|
@@ -28,10 +31,6 @@ export const reactToastifyInstaller = {
|
|
|
28
31
|
export const sonnerInstaller = {
|
|
29
32
|
dependency: ['sonner'],
|
|
30
33
|
};
|
|
31
|
-
// UI Libs
|
|
32
|
-
export const shadcnInstaller = {
|
|
33
|
-
cmd: ['pnpm dlx shadcn@latest init']
|
|
34
|
-
};
|
|
35
34
|
// Routers
|
|
36
35
|
export const reactRouterInstaller = {
|
|
37
36
|
dependency: ['react-router', 'react-router-dom'],
|
package/dist/questions.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { group, text, select, confirm, isCancel, cancel } from '@clack/prompts';
|
|
1
|
+
import { group, text, select, confirm, isCancel, cancel, multiselect } from '@clack/prompts';
|
|
2
2
|
export async function askQuestions() {
|
|
3
3
|
const results = await group({
|
|
4
4
|
projectName: () => text({
|
|
@@ -18,11 +18,32 @@ export async function askQuestions() {
|
|
|
18
18
|
return Promise.resolve(false);
|
|
19
19
|
return confirm({ message: 'Include Shadcn UI?' });
|
|
20
20
|
},
|
|
21
|
+
shadcnComponents: ({ results }) => {
|
|
22
|
+
if (!results.shadcn)
|
|
23
|
+
return Promise.resolve([]);
|
|
24
|
+
return multiselect({
|
|
25
|
+
message: 'Use space to select shadcn/ui components to install:',
|
|
26
|
+
options: [
|
|
27
|
+
{ value: 'button', label: 'Button' },
|
|
28
|
+
{ value: 'input', label: 'Input' },
|
|
29
|
+
{ value: 'card', label: 'Card' },
|
|
30
|
+
{ value: 'dialog', label: 'Dialog' },
|
|
31
|
+
{ value: 'sheet', label: 'Sheet' },
|
|
32
|
+
{ value: 'dropdown-menu', label: 'Dropdown Menu' },
|
|
33
|
+
{ value: 'table', label: 'Table' },
|
|
34
|
+
{ value: 'checkbox', label: 'Checkbox' },
|
|
35
|
+
{ value: 'avatar', label: 'Avatar' },
|
|
36
|
+
{ value: 'badge', label: 'Badge' },
|
|
37
|
+
],
|
|
38
|
+
required: false,
|
|
39
|
+
});
|
|
40
|
+
},
|
|
21
41
|
icons: () => select({
|
|
22
42
|
message: 'Choose icon library:',
|
|
23
43
|
options: [
|
|
24
44
|
{ value: 'react-icons', label: 'React Icons' },
|
|
25
45
|
{ value: 'font-awesome', label: 'Font Awesome' },
|
|
46
|
+
{ value: 'phosphor-icons', label: 'Phosphor Icons' }
|
|
26
47
|
]
|
|
27
48
|
}),
|
|
28
49
|
toast: () => select({
|
|
@@ -71,6 +92,9 @@ export async function askQuestions() {
|
|
|
71
92
|
isCancel(results.style) ||
|
|
72
93
|
isCancel(results.router) ||
|
|
73
94
|
isCancel(results.shadcn) ||
|
|
95
|
+
isCancel(results.shadcnComponents) ||
|
|
96
|
+
isCancel(results.icons) ||
|
|
97
|
+
isCancel(results.toast) ||
|
|
74
98
|
isCancel(results.reactQuery)) {
|
|
75
99
|
cancel('Setup cancelled.');
|
|
76
100
|
process.exit(0);
|
|
@@ -78,5 +102,6 @@ export async function askQuestions() {
|
|
|
78
102
|
return {
|
|
79
103
|
...results,
|
|
80
104
|
shadcn: results.shadcn,
|
|
105
|
+
shadcnComponents: (results.shadcnComponents ?? []),
|
|
81
106
|
};
|
|
82
107
|
}
|
package/dist/utils.js
CHANGED
|
@@ -72,3 +72,11 @@ export function finalizeAppFile(filePath) {
|
|
|
72
72
|
.trim();
|
|
73
73
|
fs.writeFileSync(filePath, content + '\n');
|
|
74
74
|
}
|
|
75
|
+
export function detectPackageManager() {
|
|
76
|
+
const userAgent = process.env.npm_config_user_agent || '';
|
|
77
|
+
if (userAgent.includes('pnpm'))
|
|
78
|
+
return 'pnpm';
|
|
79
|
+
if (userAgent.includes('yarn'))
|
|
80
|
+
return 'yarn';
|
|
81
|
+
return 'npm';
|
|
82
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@aditokmo/react-setup-cli",
|
|
3
|
-
"version": "0.1.
|
|
3
|
+
"version": "0.1.2",
|
|
4
4
|
"description": "A fast React CLI to jumpstart your projects. It sets up your libraries and organizes a scalable folder structure so you can skip the configuration and go straight to coding.",
|
|
5
5
|
"main": "dist/index.js",
|
|
6
6
|
"type": "module",
|
|
@@ -1,43 +1,55 @@
|
|
|
1
|
+
import { useState } from "react";
|
|
1
2
|
import { getAPIErrorMessage } from "@/utils/api-error-handler";
|
|
2
|
-
import { AuthService } from "../services"
|
|
3
|
-
import { useMutation } from '@tanstack/react-query';
|
|
3
|
+
import { AuthService } from "../services";
|
|
4
4
|
|
|
5
5
|
export const useAuth = () => {
|
|
6
|
-
const
|
|
7
|
-
|
|
8
|
-
onSuccess: (res) => {
|
|
9
|
-
// Handle mutation success
|
|
10
|
-
},
|
|
11
|
-
onError: (error) => {
|
|
12
|
-
const errorMessage = getAPIErrorMessage(error);
|
|
13
|
-
console.error(errorMessage)
|
|
14
|
-
// Display error message to user
|
|
15
|
-
},
|
|
16
|
-
});
|
|
6
|
+
const [isLoading, setIsLoading] = useState(false);
|
|
7
|
+
const [error, setError] = useState<string | null>(null);
|
|
17
8
|
|
|
18
|
-
const
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
9
|
+
const login = async (data: any) => {
|
|
10
|
+
setIsLoading(true);
|
|
11
|
+
setError(null);
|
|
12
|
+
try {
|
|
13
|
+
const res = await AuthService.login(data);
|
|
14
|
+
return res;
|
|
15
|
+
} catch (err) {
|
|
16
|
+
const errorMessage = getAPIErrorMessage(err);
|
|
17
|
+
setError(errorMessage);
|
|
18
|
+
console.error(errorMessage);
|
|
19
|
+
throw err;
|
|
20
|
+
} finally {
|
|
21
|
+
setIsLoading(false);
|
|
22
|
+
}
|
|
23
|
+
};
|
|
29
24
|
|
|
30
|
-
const
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
25
|
+
const register = async (data: any) => {
|
|
26
|
+
setIsLoading(true);
|
|
27
|
+
setError(null);
|
|
28
|
+
try {
|
|
29
|
+
const res = await AuthService.register(data);
|
|
30
|
+
return res;
|
|
31
|
+
} catch (err) {
|
|
32
|
+
const errorMessage = getAPIErrorMessage(err);
|
|
33
|
+
setError(errorMessage);
|
|
34
|
+
throw err;
|
|
35
|
+
} finally {
|
|
36
|
+
setIsLoading(false);
|
|
37
|
+
}
|
|
38
|
+
};
|
|
41
39
|
|
|
42
|
-
|
|
43
|
-
|
|
40
|
+
const logout = async () => {
|
|
41
|
+
try {
|
|
42
|
+
await AuthService.logout();
|
|
43
|
+
} catch (err) {
|
|
44
|
+
console.error(getAPIErrorMessage(err));
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
|
|
48
|
+
return {
|
|
49
|
+
login,
|
|
50
|
+
register,
|
|
51
|
+
logout,
|
|
52
|
+
isLoading,
|
|
53
|
+
error
|
|
54
|
+
};
|
|
55
|
+
};
|
|
@@ -1,9 +1,14 @@
|
|
|
1
|
-
|
|
1
|
+
import '@/styles/404.css';
|
|
2
|
+
|
|
3
|
+
export const NotFound = () => {
|
|
2
4
|
return (
|
|
3
|
-
<div className="
|
|
4
|
-
<
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
<div className="not-found-wrapper">
|
|
6
|
+
<div className="not-found-content">
|
|
7
|
+
<span>404</span>
|
|
8
|
+
<h1>Oops! Page Not Found</h1>
|
|
9
|
+
<p>The page you are looking for doesn't exist. Click button below to go to the homepage</p>
|
|
10
|
+
<a href="/">Back to Homepage</a>
|
|
11
|
+
</div>
|
|
7
12
|
</div>
|
|
8
|
-
)
|
|
9
|
-
}
|
|
13
|
+
);
|
|
14
|
+
};
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
.not-found-wrapper {
|
|
2
|
+
display: flex;
|
|
3
|
+
flex-direction: column;
|
|
4
|
+
align-items: center;
|
|
5
|
+
justify-content: center;
|
|
6
|
+
min-height: 100vh;
|
|
7
|
+
padding: 20px;
|
|
8
|
+
text-align: center;
|
|
9
|
+
font-family: "Poppins", sans-serif;
|
|
10
|
+
background: #e6edfc;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
.not-found-content {
|
|
14
|
+
padding: 100px 70px;
|
|
15
|
+
background: #fff;
|
|
16
|
+
max-width: 700px;
|
|
17
|
+
width: 100%;
|
|
18
|
+
display: flex;
|
|
19
|
+
flex-direction: column;
|
|
20
|
+
justify-content: center;
|
|
21
|
+
align-items: center;
|
|
22
|
+
border-radius: 24px;
|
|
23
|
+
box-shadow: 0px 0px 14px -14px rgba(0, 0, 0, 0.75);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
.not-found-content span {
|
|
27
|
+
color: #666;
|
|
28
|
+
font-weight: 700;
|
|
29
|
+
font-size: 6rem;
|
|
30
|
+
line-height: 1;
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
.not-found-content h1 {
|
|
34
|
+
font-size: 2.5rem;
|
|
35
|
+
font-weight: 600;
|
|
36
|
+
margin: 0;
|
|
37
|
+
color: #111;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
.not-found-content p {
|
|
41
|
+
font-size: 1rem;
|
|
42
|
+
color: #666;
|
|
43
|
+
max-width: 360px;
|
|
44
|
+
width: 100%;
|
|
45
|
+
margin: 10px 0;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
.not-found-content a {
|
|
49
|
+
font-size: 0.9rem;
|
|
50
|
+
color: #fff;
|
|
51
|
+
background-color: #37538a;
|
|
52
|
+
padding: 10px 30px;
|
|
53
|
+
margin-top: 20px;
|
|
54
|
+
border-radius: 10px;
|
|
55
|
+
transition: 0.2s;
|
|
56
|
+
cursor: pointer;
|
|
57
|
+
text-decoration: none;
|
|
58
|
+
display: inline-block;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
.not-found-content a:hover {
|
|
62
|
+
background: #263c67;
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
@media (max-width: 768px) {
|
|
66
|
+
.not-found-content {
|
|
67
|
+
padding: 60px 40px;
|
|
68
|
+
max-width: 90%;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
.not-found-content span {
|
|
72
|
+
font-size: 4.5rem;
|
|
73
|
+
margin-bottom: 20px;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
.not-found-content h1 {
|
|
77
|
+
font-size: 2rem;
|
|
78
|
+
}
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
@media (max-width: 480px) {
|
|
82
|
+
.not-found-content {
|
|
83
|
+
padding: 40px 20px;
|
|
84
|
+
border-radius: 16px;
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
.not-found-content span {
|
|
88
|
+
font-size: 3.5rem;
|
|
89
|
+
margin-bottom: 20px;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
.not-found-content h1 {
|
|
93
|
+
font-size: 1.5rem;
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
.not-found-content p {
|
|
97
|
+
font-size: 0.9rem;
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
.not-found-content a {
|
|
101
|
+
width: 100%;
|
|
102
|
+
box-sizing: border-box;
|
|
103
|
+
text-align: center;
|
|
104
|
+
}
|
|
105
|
+
}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
import { getAPIErrorMessage } from "@/utils/api-error-handler";
|
|
2
|
+
import { AuthService } from "../services"
|
|
3
|
+
import { useMutation } from '@tanstack/react-query';
|
|
4
|
+
|
|
5
|
+
export const useAuth = () => {
|
|
6
|
+
const login = useMutation({
|
|
7
|
+
mutationFn: AuthService.login,
|
|
8
|
+
onSuccess: (res) => {
|
|
9
|
+
// Handle mutation success
|
|
10
|
+
},
|
|
11
|
+
onError: (error) => {
|
|
12
|
+
const errorMessage = getAPIErrorMessage(error);
|
|
13
|
+
console.error(errorMessage)
|
|
14
|
+
// Display error message to user
|
|
15
|
+
},
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
const register = useMutation({
|
|
19
|
+
mutationFn: AuthService.register,
|
|
20
|
+
onSuccess: (res) => {
|
|
21
|
+
// Handle mutation success
|
|
22
|
+
},
|
|
23
|
+
onError: (error) => {
|
|
24
|
+
const errorMessage = getAPIErrorMessage(error);
|
|
25
|
+
console.error(errorMessage)
|
|
26
|
+
// Display error message to user
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const logout = useMutation({
|
|
31
|
+
mutationFn: AuthService.logout,
|
|
32
|
+
onSuccess: (res) => {
|
|
33
|
+
// Handle mutation success
|
|
34
|
+
},
|
|
35
|
+
onError: (error) => {
|
|
36
|
+
const errorMessage = getAPIErrorMessage(error);
|
|
37
|
+
console.error(errorMessage)
|
|
38
|
+
// Display error message to user
|
|
39
|
+
},
|
|
40
|
+
});
|
|
41
|
+
|
|
42
|
+
return { login, register, logout }
|
|
43
|
+
}
|
package/templates/state/react-query/src/{ReactQueryProvider.tsx → provider/ReactQueryProvider.tsx}
RENAMED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
import { useState } from "react";
|
|
2
|
-
import { QueryClientProvider } from '@tanstack/react-query';
|
|
3
|
-
import { QueryClient } from '@tanstack/react-query';
|
|
2
|
+
import { QueryClientProvider, QueryClient } from '@tanstack/react-query';
|
|
4
3
|
import { ReactQueryDevtools } from '@tanstack/react-query-devtools';
|
|
5
4
|
|
|
6
5
|
export function ReactQueryProvider({ children }: { children: React.ReactNode }) {
|
|
7
|
-
const [queryClient] = useState(() => new QueryClient(
|
|
6
|
+
const [queryClient] = useState(() => new QueryClient({
|
|
7
|
+
defaultOptions: {
|
|
8
|
+
queries: {
|
|
9
|
+
refetchOnWindowFocus: false,
|
|
10
|
+
retry: 1,
|
|
11
|
+
staleTime: 5 * 1000,
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
}));
|
|
8
15
|
|
|
9
16
|
return (
|
|
10
17
|
<QueryClientProvider client={queryClient}>
|