Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
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: 15 additions & 0 deletions .vscode/tasks.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"version": "2.0.0",
"tasks": [
{
"type": "shell",
"label": "Run Next.js app",
"command": "npm run dev",
"group": "build",
"isBackground": true,
"problemMatcher": [
"$tsc"
]
}
]
}
42 changes: 42 additions & 0 deletions api-gateways/post.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { url } from "../config";

export const getProduct = async (id: string | null): Promise<any[]> => {
try {
const response = await fetch(`${url}/api/post?id=${id}`, {
method: 'GET',
cache: 'no-store',
});

const jsonData = await response.json();

if (response.status === 200) {
return jsonData.data;
} else {
console.error("API error:", jsonData);
return [];
}
} catch (err) {
console.error("Fetch failed:", err);
return [];
}
};


export const createBlogPost = async (body: any, handleSuccess: (data?: any) => void, handleError: (err?: any) => void) => {
try {
const response = await fetch(`${url}/api/post`, {
method: 'POST',
headers: {
'Authorization': `Bearer ${localStorage.getItem('accessToken')}`,
},
body,
})

const jsonData = await response.json();

if (response.status === 201) handleSuccess(jsonData);
else handleError(jsonData);
} catch (err) {
handleError(err);
}
}
74 changes: 74 additions & 0 deletions api-gateways/user.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
import { url } from "@/config";

export const loginUser = async (
body: Object,
handleSuccess: (data?: any) => void,
handleError: (err?: any) => void
) => {
try {
const response = await fetch(`${url}/api/user/login`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body)
});

const jsonData = await response.json();

if (response.status === 200) handleSuccess(jsonData);
else handleError(jsonData);

} catch (err) {
handleError(err);
}
}


export const getUser = async (handleSuccess: (data?: any) => void, handleError: (err?: any) => void) => {
await fetch(`${url}/api/user/get_user`, {
method: "GET",
headers: {
'Authorization': `Bearer ${localStorage.getItem('accessToken')}`,
},
})
.then((res) => {
if (res.status !== 200) {
throw new Error('Network request failed');
} else {
return res.json();
}
})
.then((data) => {
handleSuccess(data);
})
.catch((error) => {
handleError(error.message);
});
}


export const createAdminUser = async (
body: Object,
handleSuccess: (data?: any) => void,
handleError: (err?: any) => void
) => {
try {
const response = await fetch(`${url}/api/user/register`, {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${localStorage.getItem('accessToken')}`,
},
body: JSON.stringify(body)
});

const jsonData = await response.json();

if (response.status === 201) handleSuccess(jsonData);
else handleError(jsonData);

} catch (err) {
handleError(err);
}
}
114 changes: 114 additions & 0 deletions app/[lang]/admin/admincreate/page.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
'use client';

import { createAdminUser } from '@/api-gateways/user';
import { useState } from 'react';
import { toast } from 'react-toastify';
import { FiEye, FiEyeOff } from 'react-icons/fi';

export default function AdminCreateUser() {
const [form, setForm] = useState({
email: '',
password: '',
first_name: '',
last_name: '',
phone_number: '',
});

const [loading, setLoading] = useState(false);
const [showPassword, setShowPassword] = useState(false);

const handleChange = (e) => {
const { name, value } = e.target;
setForm((prev) => ({ ...prev, [name]: value }));
};

const handleSubmit = async (e) => {
e.preventDefault();
setLoading(true);

createAdminUser(
form,
() => {
toast.success('Admin user created successfully!');
setLoading(false);
},
(error) => {
toast.error(`Error: ${error}`);
setLoading(false);
}
);
};

return (
<div className="max-w-xl mx-auto mt-10 p-8 bg-white rounded-xl shadow-md">
<h2 className="text-2xl font-bold text-center mb-6">Create Admin User</h2>

<form onSubmit={handleSubmit} className="space-y-4">
<input
type="text"
name="first_name"
placeholder="First Name"
value={form.first_name}
onChange={handleChange}
required
className="w-full p-2 border rounded bg-white"
/>
<input
type="text"
name="last_name"
placeholder="Last Name"
value={form.last_name}
onChange={handleChange}
required
className="w-full p-2 border rounded bg-white"
/>
<input
type="email"
name="email"
placeholder="Email"
value={form.email}
onChange={handleChange}
required
className="w-full p-2 border rounded bg-white"
/>


<div className="relative">
<input
type={showPassword ? 'text' : 'password'}
name="password"
placeholder="Password"
value={form.password}
onChange={handleChange}
required
className="w-full p-2 border rounded bg-white pr-10"
/>
<span
className="absolute inset-y-0 right-3 flex items-center cursor-pointer text-gray-500"
onClick={() => setShowPassword((prev) => !prev)}
>
{showPassword ? <FiEyeOff size={18} /> : <FiEye size={18} />}
</span>
</div>

<input
type="text"
name="phone_number"
placeholder="Phone Number"
value={form.phone_number}
onChange={handleChange}
required
className="w-full p-2 border rounded bg-white"
/>

<button
type="submit"
disabled={loading}
className="w-full py-2 bg-blue-600 text-white font-semibold rounded hover:bg-blue-700 transition"
>
{loading ? 'Creating...' : 'Create Admin'}
</button>
</form>
</div>
);
}
Loading