Skip to content

Commit

Permalink
Merge pull request #211 from Bashamega/html-to-jsx
Browse files Browse the repository at this point in the history
Html to jsx
  • Loading branch information
annuk123 authored Jul 23, 2024
2 parents 800573c + 4a6a63d commit a0736f8
Show file tree
Hide file tree
Showing 11 changed files with 496 additions and 395 deletions.
303 changes: 146 additions & 157 deletions package-lock.json

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"file-saver": "^2.0.5",
"framer-motion": "^11.2.10",
"fuse.js": "^7.0.0",
"html-to-jsx-transform": "^1.1.0",
"jszip": "^3.10.1",
"next": "^14.2.3",
"next-navigation": "^1.0.6",
Expand Down
31 changes: 15 additions & 16 deletions src/app/api/posts/route.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import posts from '@/db/posts.json'
import { NextResponse } from 'next/server';

import posts from "@/db/posts.json";
import { NextResponse } from "next/server";

export async function GET(req) {
const { searchParams } = new URL(req.url);
let page = parseInt(searchParams.get('page'));
if(isNaN(page) || page < 1) {
page = 1;
}
const postsPerPage = 10;
const startIndex = (page - 1) * postsPerPage;
const { searchParams } = new URL(req.url);
let page = parseInt(searchParams.get("page"));
if (isNaN(page) || page < 1) {
page = 1;
}
const postsPerPage = 10;
const startIndex = (page - 1) * postsPerPage;

if (startIndex < 0 || startIndex >= posts.length) {
return NextResponse.json({ error: "Page not found" }, { status: 404 });
}
if (startIndex < 0 || startIndex >= posts.length) {
return NextResponse.json({ error: "Page not found" }, { status: 404 });
}

const paginatedPosts = posts.slice(startIndex, startIndex + postsPerPage);
return NextResponse.json(paginatedPosts, { status: 200 });
}
const paginatedPosts = posts.slice(startIndex, startIndex + postsPerPage);
return NextResponse.json(paginatedPosts, { status: 200 });
}
21 changes: 10 additions & 11 deletions src/app/api/users/[userId]/route.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import users from '@/db/users.json'
import { NextResponse } from 'next/server';
import users from "@/db/users.json";
import { NextResponse } from "next/server";


export async function GET(req, {params}) {
const userId = parseInt(params.userId);
const user = users.find(user => user.userId === userId);
if (!user) {
return NextResponse.json({error: 'user not found'}, { status: 404 });
}
return NextResponse.json(user, { status: 200 });
}
export async function GET(req, { params }) {
const userId = parseInt(params.userId);
const user = users.find((user) => user.userId === userId);
if (!user) {
return NextResponse.json({ error: "user not found" }, { status: 404 });
}
return NextResponse.json(user, { status: 200 });
}
31 changes: 15 additions & 16 deletions src/app/api/users/route.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,19 @@
import users from '@/db/users.json'
import { NextResponse } from 'next/server';

import users from "@/db/users.json";
import { NextResponse } from "next/server";

export async function GET(req) {
const { searchParams } = new URL(req.url);
let page = parseInt(searchParams.get('page'));
if(isNaN(page) || page < 1) {
page = 1;
}
const usersPerPage = 10;
const startIndex = (page - 1) * usersPerPage;
const { searchParams } = new URL(req.url);
let page = parseInt(searchParams.get("page"));
if (isNaN(page) || page < 1) {
page = 1;
}
const usersPerPage = 10;
const startIndex = (page - 1) * usersPerPage;

if (startIndex < 0 || startIndex >= users.length) {
return NextResponse.json({ error: "Page not found" }, { status: 404 });
}
if (startIndex < 0 || startIndex >= users.length) {
return NextResponse.json({ error: "Page not found" }, { status: 404 });
}

const paginatedUsers = users.slice(startIndex, startIndex + usersPerPage);
return NextResponse.json(paginatedUsers, { status: 200 });
}
const paginatedUsers = users.slice(startIndex, startIndex + usersPerPage);
return NextResponse.json(paginatedUsers, { status: 200 });
}
15 changes: 15 additions & 0 deletions src/app/convert/html-jsx/components/editor.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { Editor } from "@monaco-editor/react";

export default function CodeEditor({ language, theme, value, onChange }) {
return (
<Editor
height="100vh"
width="50vw"
language={language}
theme={theme}
value={value}
onChange={onChange}
className=" sticky"
/>
);
}
58 changes: 58 additions & 0 deletions src/app/convert/html-jsx/page.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
"use client";

import { useState } from "react";
import { NavBar } from "@/app/components/navbar";
import CodeEditor from "./components/editor";
import { htmlToJsx } from "html-to-jsx-transform";

export default function HTML_JSX() {
const [isDarkMode, setIsDarkMode] = useState(false);
const [value, setValue] = useState("");
const handleChange = (val) => {
setValue(val);
};
const toggleTheme = () => {
const newTheme = !isDarkMode;
setIsDarkMode(newTheme);
localStorage.setItem("theme", JSON.stringify(newTheme));
};
const generateJSX = () => {
const jsx = htmlToJsx(value);
return `function component(){
return(${jsx})
}
`;
};
return (
<main
className={`h-screen overflow-auto ${isDarkMode ? "bg-gray-900 text-white" : "bg-white text-black"}`}
>
<NavBar
title={"HTML to JSX"}
toggleTheme={toggleTheme}
isDarkMode={isDarkMode}
/>
<div className="flex h-full">
<div>
<h1 className="text-center p-2">HTML</h1>
<CodeEditor
theme={isDarkMode ? "vs-dark" : "vs-light"}
value={value}
language={"html"}
onChange={handleChange}
/>
</div>
<div>
<h1 className="text-center p-2">JSX</h1>
<CodeEditor
theme={isDarkMode ? "vs-dark" : "vs-light"}
language={"javascript"}
value={generateJSX()}
onChange={() => {}}
/>
</div>
</div>
</main>
);
}
58 changes: 45 additions & 13 deletions src/app/endpoints/page.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,25 @@ import Footer from "../components/Footer";
import { NavBar } from "../components/navbar";
import React, { useState } from "react";


export default function Endpoints() {
const [isDarkMode, setIsDarkMode] = useState(false);

const toggleTheme = () => {
setIsDarkMode(!isDarkMode);
};

const sectionStyle = "p-4 text-left w-full max-w-[26rem] md:max-w-[40rem] bg-white border border-gray-200 rounded-lg shadow sm:p-8 dark:bg-gray-800 dark:border-gray-700";
const sectionStyle =
"p-4 text-left w-full max-w-[26rem] md:max-w-[40rem] bg-white border border-gray-200 rounded-lg shadow sm:p-8 dark:bg-gray-800 dark:border-gray-700";

return (
<main
className={`${isDarkMode ? "bg-gray-900 text-gray-400" : "bg-gray-100 text-gray-800"} min-h-screen pb-10`}
>
<NavBar toggleTheme={toggleTheme} isDarkMode={isDarkMode} title="Endpoints" />
<NavBar
toggleTheme={toggleTheme}
isDarkMode={isDarkMode}
title="Endpoints"
/>
<div className="flex justify-center flex-col items-center w-full">
<div
id="about"
Expand All @@ -27,7 +31,10 @@ export default function Endpoints() {
API Endpoints for Dummy JSON Data
</h5>
<p className="font-normal text-gray-700 dark:text-gray-400">
There are three api endpoints available for fetching dummy JSON data: <code>/api/users</code>, <code>/api/users/:userId</code>, and <code>/api/posts</code>. The data is stored in JSON files in the <code>db</code> directory.
There are three api endpoints available for fetching dummy JSON
data: <code>/api/users</code>, <code>/api/users/:userId</code>, and{" "}
<code>/api/posts</code>. The data is stored in JSON files in the{" "}
<code>db</code> directory.
</p>
</div>

Expand All @@ -36,12 +43,19 @@ export default function Endpoints() {
Get All Users
</h5>
<p className="mb-5 text-base text-gray-500 sm:text-lg dark:text-gray-400">
<code className="bg-gray-200 dark:bg-gray-700 p-1 rounded">GET /api/users</code>
<code className="bg-gray-200 dark:bg-gray-700 p-1 rounded">
GET /api/users
</code>
<br />
Fetches a paginated list of users. Accepts an optional <code>page</code> query parameter to specify the page of results. Renders 10 users per page.
Fetches a paginated list of users. Accepts an optional{" "}
<code>page</code> query parameter to specify the page of results.
Renders 10 users per page.
</p>
<p className="mb-5 text-base text-gray-500 sm:text-lg dark:text-gray-400">
Example: <code className="bg-gray-200 dark:bg-gray-700 p-1 rounded">GET /api/users?page=1</code>
Example:{" "}
<code className="bg-gray-200 dark:bg-gray-700 p-1 rounded">
GET /api/users?page=1
</code>
</p>
<p className="mb-5 text-base text-gray-500 sm:text-lg dark:text-gray-400">
Response:
Expand Down Expand Up @@ -71,12 +85,17 @@ export default function Endpoints() {
Get Single User
</h5>
<p className="mb-5 text-base text-gray-500 sm:text-lg dark:text-gray-400">
<code className="bg-gray-200 dark:bg-gray-700 p-1 rounded">GET /api/users/:userId</code>
<code className="bg-gray-200 dark:bg-gray-700 p-1 rounded">
GET /api/users/:userId
</code>
<br />
Fetches the details of a single user by their userId.
</p>
<p className="mb-5 text-base text-gray-500 sm:text-lg dark:text-gray-400">
Example: <code className="bg-gray-200 dark:bg-gray-700 p-1 rounded">GET /api/users/1</code>
Example:{" "}
<code className="bg-gray-200 dark:bg-gray-700 p-1 rounded">
GET /api/users/1
</code>
</p>
<p className="mb-5 text-base text-gray-500 sm:text-lg dark:text-gray-400">
Response:
Expand All @@ -91,7 +110,11 @@ export default function Endpoints() {
</pre>
</p>
<p className="mb-5 text-base text-gray-500 sm:text-lg dark:text-gray-400">
If the user is not found, a <code className="bg-gray-200 dark:bg-gray-700 p-1 rounded">404 Not Found</code> status is returned:
If the user is not found, a{" "}
<code className="bg-gray-200 dark:bg-gray-700 p-1 rounded">
404 Not Found
</code>{" "}
status is returned:
<pre className="bg-gray-200 dark:bg-gray-700 p-2 rounded mt-2 text-gray-800 dark:text-gray-300">
{`{
"error": "User not found"
Expand All @@ -105,12 +128,21 @@ export default function Endpoints() {
Get All Posts
</h5>
<p className="mb-5 text-base text-gray-500 sm:text-lg dark:text-gray-400">
<code className="bg-gray-200 dark:bg-gray-700 p-1 rounded">GET /api/posts</code>
<code className="bg-gray-200 dark:bg-gray-700 p-1 rounded">
GET /api/posts
</code>
<br />
Fetches a list of all posts. Accepts an optional <code>page</code> query parameter to specify the page of results. Renders 10 posts per page. Default page is 1.
Fetches a list of all posts. Accepts an optional <code>
page
</code>{" "}
query parameter to specify the page of results. Renders 10 posts per
page. Default page is 1.
</p>
<p className="mb-5 text-base text-gray-500 sm:text-lg dark:text-gray-400">
Example: <code className="bg-gray-200 dark:bg-gray-700 p-1 rounded">GET /api/posts</code>
Example:{" "}
<code className="bg-gray-200 dark:bg-gray-700 p-1 rounded">
GET /api/posts
</code>
</p>
<p className="mb-5 text-base text-gray-500 sm:text-lg dark:text-gray-400">
Response:
Expand Down
5 changes: 5 additions & 0 deletions src/app/sitemap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -90,4 +90,9 @@
<lastmod>2024-07-23T04:23:21.178Z</lastmod>
<priority>0.80</priority>
</url>
<url>
<loc>https://wdt.adambashaahmednaji.com/convert/html-jsx</loc>
<lastmod>2024-07-23T08:03:08.323Z</lastmod>
<priority>0.80</priority>
</url>
</urlset>
Loading

0 comments on commit a0736f8

Please sign in to comment.