Skip to content

Commit

Permalink
#
Browse files Browse the repository at this point in the history
  • Loading branch information
xvpc committed Mar 3, 2024
1 parent 477b88d commit 2a2e0e5
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 77 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
# testing
/coverage
/temp
/data

# next.js
/.next/
Expand Down
21 changes: 12 additions & 9 deletions components/Home.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@ import React, { useContext, useEffect, useState } from 'react'
// Components
import Layout from '@/components/Layout';
import Game from '@/components/Game';

// Mui
import { CircularProgress } from '@mui/material';

// Utils
import { userContext } from '@/utils/UserProvider';
import { siteInfo } from '@/utils/Assets';
import { textData } from '@/utils/Assets';


export default function Home(){
Expand All @@ -24,16 +28,15 @@ export default function Home(){
const [loading, setLoading] = useState(true);

useEffect(() => {
if(user && siteInfo?.url){
fetch(`${siteInfo.url}/api/data?t=${user?.time}&l=${user?.language}`,{ method: "POST" })
.then((res) => res.json())
.then((res) => {
setText(res.text);
})
.catch((err) => {
if(user && textData?.length > 0){
try{
setText(textData[Math.floor(Math.random() * textData.length)]);
}catch(err: any){
setError(err || "Something went wrong!");
console.log(err);
}).finally(() => setLoading(false))
}finally{
setLoading(false)
}
}
}, []);

Expand Down
2 changes: 1 addition & 1 deletion components/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ export default function Layout({ children, title, description, keywords, image }
<div className='text-gray-400 flex flex-col justify-between items-start gap-2 ps-1'>
<Link href="about" title='About typer' className="p-0 m-0 hover:underline">About typer</Link>
<Link href="https://xvpc.dev" target='_blank' title='Contact Us' className="p-0 m-0 hover:underline">Contact Us</Link>
<Link href="privecy" title='Privecy & Terms<' className="p-0 m-0 hover:underline">Privecy & Terms</Link>
<Link href="privecy" title='Privecy & Terms' className="p-0 m-0 hover:underline">Privecy & Terms</Link>
</div>
</div>
<div className='flex flex-col justify-between items-start gap-4'>
Expand Down
116 changes: 50 additions & 66 deletions pages/api/data/index.ts
Original file line number Diff line number Diff line change
@@ -1,75 +1,59 @@
// import type { NextApiRequest, NextApiResponse } from "next";
// import { readFileSync } from "fs";
// // import { readFileSync } from "fs";

type dataType = {
duraction: number,
text: string,
locale: string,
language: string
}
type errorType = {
error: string
}
// type dataType = {
// duraction: number,
// text: string,
// locale: string,
// language: string
// }
// type errorType = {
// error: string
// }

// const text = "data/text.json";

// export const config = {
// runtime: 'edge',
// }

// export default async function handler(req: NextApiRequest, res: NextApiResponse){
// if(req.method === "POST"){
// const time = req.query?.t;
// const language = req.query?.l;

// export default async function handler(request: Request){
// if(!await checkAuth(request)){
// return new Response(JSON.stringify({ error: "Unauthorized request!" }), {
// status: 401,
// headers: apiHeaders,
// if(time && language){
// try{
// // const readData = readFileSync(text, 'utf8');
// // const data = JSON.parse(readData);
// const data = {
// data:
// [
// "lorem ipsum dolor sit amet, consect lorem ipsum dolor sit amet, consect",
// "afkjh asoh aosh ofa oiash oha fm dolor sit amet, consect",
// "g3 2oig fadgfe fqw qw qw qwf qw fqw fct",
// "la fiaosjhf oisah fohnqwoih fqw fq"
// ]
// };
// const ranText: string = data?.data[Math.floor(Math.random() * data.data.length)];

// if(!ranText) return res.status(500).json({ error: "Something went wrong with parsing the text!" });
// return res.status(200).json({
// duraction: Number(time),
// text: ranText,
// locale: language as string,
// language: language === "en-us" ? "english" : "english" as string
// })
// }catch(err: any){
// return res.status(500).json({
// error: err?.message || "Something went wrong with parsing the text!"
// })
// }
// }else{
// return res.status(404).json({
// error: "Didn't provide data!"
// })
// }
// }else{
// return res.status(400).json({
// error: "Bad request"
// })
// }
// req: NextApiRequest, res: NextApiResponse
export default async function handler(request: Request){
return new Response(JSON.stringify({ error: "testing" }), {
status: 401,
})

// if(req.method === "POST"){
// const time = req.query?.t;
// const language = req.query?.l;

// if(time && language){
// try{
// // const readData = readFileSync(text, 'utf8');
// // const data = JSON.parse(readData);
// const data = {
// data:
// [
// "lorem ipsum dolor sit amet, consect lorem ipsum dolor sit amet, consect",
// "afkjh asoh aosh ofa oiash oha fm dolor sit amet, consect",
// "g3 2oig fadgfe fqw qw qw qwf qw fqw fct",
// "la fiaosjhf oisah fohnqwoih fqw fq"
// ]
// };
// const ranText: string = data?.data[Math.floor(Math.random() * data.data.length)];

// if(!ranText) return res.status(500).json({ error: "Something went wrong with parsing the text!" });
// return res.status(200).json({
// duraction: Number(time),
// text: ranText,
// locale: language as string,
// language: language === "en-us" ? "english" : "english" as string
// })
// }catch(err: any){
// return res.status(500).json({
// error: err?.message || "Something went wrong with parsing the text!"
// })
// }
// }else{
// return res.status(404).json({
// error: "Didn't provide data!"
// })
// }
// }else{
// return res.status(400).json({
// error: "Bad request"
// })
// }
}
// }
10 changes: 9 additions & 1 deletion utils/Assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,12 @@ export const siteInfo = {
image: "https://i.ibb.co/4RvMZfb/icon.png",
url: "https://xvpc.github.io/typer",
donationUrl: "https://paypal.me/xvp131"
}
}

export const textData =
[
"lorem ipsum dolor sit amet, consect lorem ipsum dolor sit amet, consect",
"afkjh asoh aosh ofa oiash oha fm dolor sit amet, consect",
"g3 2oig fadgfe fqw qw qw qwf qw fqw fct",
"la fiaosjhf oisah fohnqwoih fqw fq"
]

0 comments on commit 2a2e0e5

Please sign in to comment.