Skip to content

Commit

Permalink
configured shared types
Browse files Browse the repository at this point in the history
  • Loading branch information
vishalmishraa committed Aug 22, 2024
1 parent f03d142 commit 4944e1e
Show file tree
Hide file tree
Showing 9 changed files with 56 additions and 139 deletions.
38 changes: 4 additions & 34 deletions apps/web/app/(contest)/contests-dashboard/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,53 +4,23 @@ import SignInFirst from '@/components/SignInFirst'
import { Button } from '@/components/ui/button'
import { Card, CardContent, CardDescription, CardFooter, CardHeader, CardTitle } from '@/components/ui/card'
import UserContestTable from '@/components/UserContestTable'
import { IContest, IProblems } from "@repo/common/types"
import axios from 'axios'
import { useSession } from 'next-auth/react'
import Link from 'next/link'
import { useEffect, useState } from 'react'

export interface Icontest {
id: string;
title: string;
description: string;
startTime: Date;
hidden: boolean;
endTime: Date;
createdAt: Date;
updatedAt: Date;
leaderboard: boolean;
authorId: string;
}

export interface IContestProblem {
contestId: string;
problemId: string;
}

export interface IProblems {
id: string;
title: string;
description: string;
hidden: boolean;
slug: string;
solved: number;
difficulty: string;
contests: IContestProblem[]
}



const ContestDashboardPage = () => {
const { data, status } = useSession();
const [contests, setContests] = useState<Icontest[]>([]);
const [contests, setContests] = useState<IContest[]>([]);
const [problems, setProblems] = useState<IProblems[]>([]);
const [loading, setLoading] = useState(true);

useEffect(() => {
try {
const fetchData = async () => {
setLoading(true);
const contest: Icontest[] = (await axios.get(`api/contest`)).data;
const contest: IContest[] = (await axios.get(`api/contest`)).data;
setContests(contest);

const problems: IProblems[] = await (await axios.get(`api/problems`)).data
Expand Down Expand Up @@ -93,7 +63,7 @@ const ContestDashboardPage = () => {
<div>
{contests.length !== 0 ? (
<div>
<UserContestTable contests={contests} setContests={setContests}/>
<UserContestTable contests={contests} setContests={setContests} />
</div>
) : (
<Card className="w-full max-w-md mx-auto mt-8">
Expand Down
8 changes: 4 additions & 4 deletions apps/web/components/Contests.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
"use client"
import { useEffect, useState } from "react";
import { ContestCard } from "./ContestCard";
import { Icontest } from "./SelectProblemsTable";
import { IContest } from "@repo/common/types";
import axios from "axios";
import { toast } from "react-toastify";
import { Loader2 } from "lucide-react";

export function Contests() {
const [upcomingContests, setUpcomingContests] = useState<Icontest[]>([]);
const [pastContests, setPastContests] = useState<Icontest[]>([]);
const [upcomingContests, setUpcomingContests] = useState<IContest[]>([]);
const [pastContests, setPastContests] = useState<IContest[]>([]);
const [isLoading, setIsLoading] = useState(false);

useEffect(() => {
Expand Down Expand Up @@ -55,7 +55,7 @@ export function Contests() {
);
}

function ShowContest({ upcomingContests, pastContests }: { upcomingContests: Icontest[], pastContests: Icontest[] }) {
function ShowContest({ upcomingContests, pastContests }: { upcomingContests: IContest[], pastContests: IContest[] }) {
return (
<div className="min-h-screen bg-gradient-to-b from-indigo-50 to-white dark:from-gray-900 dark:to-gray-800">
<section className="py-12 md:py-16">
Expand Down
5 changes: 3 additions & 2 deletions apps/web/components/CreaatedContest.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@

import { Icontest } from "@/app/(contest)/contests-dashboard/page"
import { IContest } from "@repo/common/types"
import { Button } from "@/components/ui/button"
import { Card, CardContent, CardFooter, CardHeader, CardTitle } from "@/components/ui/card"
import moment from "moment"
import { useRouter } from "next/navigation"
import { toast } from "react-toastify"
import { formatDateTime, convertToIST} from "@/lib/time"

export function CreaatedContest({ contest, numberOfProblem }: { contest: Icontest, numberOfProblem: number }) {
export function CreaatedContest({ contest, numberOfProblem }: { contest: IContest, numberOfProblem: number }) {
const formattedDateTime = (date: Date) => {
return moment(date).utcOffset("+05:30").format('DD / MM / YYYY - hh:mm A');
}
Expand Down
31 changes: 1 addition & 30 deletions apps/web/components/SelectProblemsTable.tsx
Original file line number Diff line number Diff line change
@@ -1,40 +1,11 @@
"use client"
import { Button } from "@/components/ui/button"
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
import { IProblems } from "@repo/common/types"
import axios from 'axios'
import React, { useEffect, useState } from 'react'
import { toast } from "react-toastify"


export interface Icontest {
id: string;
title: string;
description: string;
startTime: Date;
hidden: boolean;
endTime: Date;
createdAt: Date;
updatedAt: Date;
leaderboard: boolean;
authorId: string;
}

export interface IContestProblem {
contestId: string;
problemId: string;
}

export interface IProblems {
id: string;
title: string;
description: string;
hidden: boolean;
slug: string;
solved: number;
difficulty: string;
contests: IContestProblem[]
}

export function SelectProblemsTable({ contestId, hight, setNumberOfProblems }: {
contestId: string,
hight: number,
Expand Down
31 changes: 2 additions & 29 deletions apps/web/components/UserContestTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,41 +12,14 @@ import {
import { Input } from "@/components/ui/input"
import { Table, TableBody, TableCell, TableHead, TableHeader, TableRow } from "@/components/ui/table"
import useConttestTable from "@/hooks/useUserContestTable"
import { IContest } from "@repo/common/types"
import { TrashIcon } from "lucide-react"
import Link from 'next/link'
import React from 'react'

export interface Icontest {
id: string;
title: string;
description: string;
startTime: Date;
hidden: boolean;
endTime: Date;
createdAt: Date;
updatedAt: Date;
leaderboard: boolean;
authorId: string;
}

export interface IContestProblem {
contestId: string;
problemId: string;
}

export interface IProblems {
id: string;
title: string;
description: string;
hidden: boolean;
slug: string;
solved: number;
difficulty: string;
contests: IContestProblem[]
}


export default function UserContestTable({ contests, setContests }: { contests: Icontest[], setContests: React.Dispatch<React.SetStateAction<Icontest[]>> }) {
export default function UserContestTable({ contests, setContests }: { contests: IContest[], setContests: React.Dispatch<React.SetStateAction<IContest[]>> }) {

const {
convertToIST,
Expand Down
35 changes: 3 additions & 32 deletions apps/web/hooks/useUserContestTable.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,13 @@
import { IContest } from "@repo/common/types"
import axios from "axios"
import moment from 'moment'
import React, { useState } from 'react'
import { toast } from "react-toastify"

export interface Icontest {
id: string;
title: string;
description: string;
startTime: Date;
hidden: boolean;
endTime: Date;
createdAt: Date;
updatedAt: Date;
leaderboard: boolean;
authorId: string;
}

export interface IContestProblem {
contestId: string;
problemId: string;
}

export interface IProblems {
id: string;
title: string;
description: string;
hidden: boolean;
slug: string;
solved: number;
difficulty: string;
contests: IContestProblem[]
}


export default function useConttestTable({ contests, setContests }: { contests: Icontest[], setContests: React.Dispatch<React.SetStateAction<Icontest[]>> }) {
export default function useConttestTable({ contests, setContests }: { contests: IContest[], setContests: React.Dispatch<React.SetStateAction<IContest[]>> }) {

const [editingContestId, setEditingContestId] = useState<string | null>(null);
const [editingContest, setEditingContest] = useState<Icontest | null>(null);
const [editingContest, setEditingContest] = useState<IContest | null>(null);
const [numberOfProblem, setNumberOfProblems] = useState<number>(0)
const [deletingContestId, setDeletingContestId] = useState<string | null>(null)

Expand Down
14 changes: 7 additions & 7 deletions apps/web/hooks/usecreateContest.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Icontest, IProblems } from "@/app/(contest)/contests-dashboard/page"
import { IContest, IProblems } from "@repo/common/types"
import axios from "axios"
import { useEffect, useState } from "react"
import { toast } from "react-toastify"

export function useCreateContest() {
const [newContest, setNewContest] = useState<Icontest>()
const [newContest, setNewContest] = useState<IContest>()
const [numberOfProblem, setNumberOfProblems] = useState<number>(0)
const [problems, setProblems] = useState<IProblems[]>([]);
const [loading, setLoading] = useState(true);
const [createContest, setCreateContest] = useState<Icontest>()
const [createContest, setCreateContest] = useState<IContest>()

useEffect(() => {
try {
Expand All @@ -34,15 +34,15 @@ export function useCreateContest() {
console.log({ name, value, type });
if ((name === 'startTime' || name === 'endTime') && value !== '') {
const formattedValue = new Date(value).toISOString();
setCreateContest((prevState: Icontest | undefined) => ({
setCreateContest((prevState: IContest | undefined) => ({
...prevState,
[name]: formattedValue
} as Icontest));
} as IContest));
} else {
setCreateContest((prevState: Icontest | undefined) => ({
setCreateContest((prevState: IContest | undefined) => ({
...prevState,
[name]: value
} as Icontest));
} as IContest));
}
}

Expand Down
3 changes: 2 additions & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
"name": "@repo/common",
"exports": {
"./language": "./language/index.ts",
"./zod": "./zod/index.ts"
"./zod": "./zod/index.ts",
"./types": "./types/index.ts"
},
"version": "1.0.0",
"description": "",
Expand Down
30 changes: 30 additions & 0 deletions packages/common/types/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
export interface IContest {
id: string;
title: string;
description: string;
startTime: Date;
hidden: boolean;
endTime: Date;
createdAt: Date;
updatedAt: Date;
leaderboard: boolean;
authorId: string;
}

export interface IContestProblem {
contestId: string;
problemId: string;
}

export interface IProblems {
id: string;
title: string;
description: string;
hidden: boolean;
slug: string;
solved: number;
difficulty: string;
contests: IContestProblem[]
}

// Add other shared interfaces here

0 comments on commit 4944e1e

Please sign in to comment.