Skip to content

Commit 5b76c93

Browse files
committed
feat: add compile and execute code mutation
1 parent f42df3a commit 5b76c93

File tree

8 files changed

+143
-8
lines changed

8 files changed

+143
-8
lines changed

.env.example

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
RAPIDAPI_HOST = 'your-rapidapi-host'
2+
RAPIDAPI_KEY = 'your-rapidapi-key'
3+
SUBMISSIONS_URL = 'your-rapidapi-submissions-url'

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
"@trpc/client": "^10.45.0",
2323
"@trpc/react-query": "^10.45.0",
2424
"@trpc/server": "^10.45.0",
25+
"axios": "^1.6.5",
2526
"class-variance-authority": "^0.7.0",
2627
"clsx": "^2.1.0",
2728
"cmdk": "^0.2.0",

pnpm-lock.yaml

+64
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/app/page.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import Header from '@/components/global/header';
2-
import { CodeEditor, CodeOutput, CustomInput } from '@/components/pages/home/client';
2+
import { CodeEditor, CodeOutput, CompileCodeBtn, CustomInput } from '@/components/pages/home/client';
33
import { Fragment } from 'react';
44

55
export default function Home() {
@@ -18,6 +18,7 @@ export default function Home() {
1818
<h3 className='scroll-m-20 text-xl font-semibold tracking-tight'>Output</h3>
1919
<CodeOutput />
2020
<CustomInput />
21+
<CompileCodeBtn />
2122
</div>
2223
</div>
2324
</section>

src/client/store/editor.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { create } from 'zustand';
22

33
type State = {
44
code: string;
5-
customInput: string | undefined;
5+
customInput: string;
66
backgroundColor: string;
77
};
88

@@ -14,7 +14,7 @@ type Action = {
1414

1515
export const useEditor = create<State & Action>((set) => ({
1616
code: `console.log('Hello world')`,
17-
customInput: undefined,
17+
customInput: '',
1818
backgroundColor: '#000000',
1919
setcode: (code) => set((state) => ({ code })),
2020
setcustominput: (input) => set((state) => ({ customInput: input })),

src/components/pages/home/client.tsx

+26
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import { useEditor } from '@/client/store/editor';
44
import { useEditorOptions } from '@/client/store/editor-options';
5+
import { api } from '@/client/trpc';
6+
import { Button } from '@/components/ui/button';
57
import { Textarea } from '@/components/ui/textarea';
68
import { languageOptions } from '@/constants/app';
79
import Editor from '@monaco-editor/react';
@@ -47,3 +49,27 @@ export function CustomInput() {
4749
</div>
4850
);
4951
}
52+
53+
export function CompileCodeBtn() {
54+
const code = useEditor((state) => state.code);
55+
const input = useEditor((state) => state.customInput);
56+
const language = useEditorOptions((state) => state.language);
57+
const mutation = api.code.compileCode.useMutation({
58+
onSuccess: () => {
59+
console.log('hello world');
60+
},
61+
onError: () => {
62+
console.log('error');
63+
},
64+
});
65+
66+
function handleClick() {
67+
mutation.mutate({ language, code, input });
68+
}
69+
70+
return (
71+
<Button type='button' className='mt-3 ml-auto w-auto' onClick={handleClick}>
72+
Compile and Execute
73+
</Button>
74+
);
75+
}

src/lib/types/index.ts

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import { z } from 'zod';
2+
3+
const processEnvSchema = z.object({
4+
RAPIDAPI_HOST: z.string(),
5+
RAPIDAPI_KEY: z.string(),
6+
SUBMISSIONS_URL: z.string(),
7+
});
8+
processEnvSchema.parse(process.env);
9+
10+
// global
11+
declare global {
12+
namespace NodeJS {
13+
interface ProcessEnv extends z.infer<typeof processEnvSchema> {}
14+
}
15+
}

src/server/api/routers/code.ts

+30-5
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,42 @@
11
import { createTRPCRouter, publicProcedure } from '@/server/api/trpc';
2+
import axios from 'axios';
23
import { z } from 'zod';
34

45
export const codeRouter = createTRPCRouter({
56
compileCode: publicProcedure
67
.input(
78
z.object({
8-
subjectName: z.string(),
9-
totalClasses: z.number(),
10-
attendedClasses: z.number(),
11-
color: z.string(),
9+
language: z.number(),
10+
code: z.string(),
11+
input: z.string(),
1212
}),
1313
)
14-
.mutation(async ({ input, ctx }) => {
14+
.mutation(async ({ input }) => {
15+
const { language, code, input: customInput } = input;
16+
const options = {
17+
method: 'POST',
18+
url: process.env.SUBMISSIONS_URL,
19+
params: { base64_encoded: 'true', fields: '*' },
20+
headers: {
21+
'content-type': 'application/json',
22+
'Content-Type': 'application/json',
23+
'X-RapidAPI-Key': process.env.RAPIDAPI_KEY,
24+
'X-RapidAPI-Host': process.env.RAPIDAPI_HOST,
25+
},
26+
data: {
27+
language_id: language,
28+
source_code: btoa(code),
29+
stdin: btoa(customInput),
30+
},
31+
};
32+
33+
try {
34+
const response = await axios.request(options);
35+
console.log(response.data);
36+
} catch (error) {
37+
console.error(error);
38+
}
39+
1540
return 'code compiled';
1641
}),
1742
});

0 commit comments

Comments
 (0)