imwithye commited on
Commit
4a3842e
·
1 Parent(s): 4bea016

send solve request

Browse files
src/components/state-modal.tsx CHANGED
@@ -6,6 +6,7 @@ import { useDisclosure } from '@heroui/use-disclosure';
6
  import { forwardRef, useImperativeHandle, useState } from 'react';
7
 
8
  import { Index2Color } from './consts';
 
9
 
10
  export type StateModalRef = {
11
  open: (state: Array<Array<number>>) => void;
@@ -26,6 +27,26 @@ export const StateModal = forwardRef<StateModalRef, unknown>((_, ref) => {
26
  navigator.clipboard.writeText(JSON.stringify(state));
27
  };
28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
29
  return (
30
  <Modal isOpen={isOpen} onOpenChange={onOpenChange} placement="center">
31
  <ModalContent>
@@ -70,9 +91,12 @@ export const StateModal = forwardRef<StateModalRef, unknown>((_, ref) => {
70
  <Button color="danger" variant="light" size="sm" onPress={onClose}>
71
  Close
72
  </Button>
73
- <Button color="primary" size="sm" onPress={copy}>
74
  Copy
75
  </Button>
 
 
 
76
  </ModalFooter>
77
  </>
78
  )}
 
6
  import { forwardRef, useImperativeHandle, useState } from 'react';
7
 
8
  import { Index2Color } from './consts';
9
+ import { rotationController } from './rotation-controller';
10
 
11
  export type StateModalRef = {
12
  open: (state: Array<Array<number>>) => void;
 
27
  navigator.clipboard.writeText(JSON.stringify(state));
28
  };
29
 
30
+ const solve = async () => {
31
+ try {
32
+ const response = await fetch('/api/solve', {
33
+ method: 'POST',
34
+ headers: {
35
+ 'Content-Type': 'application/json',
36
+ },
37
+ body: JSON.stringify({ state }),
38
+ });
39
+ if (!response.ok) {
40
+ throw new Error('Server error', { cause: response });
41
+ }
42
+ const { steps } = await response.json();
43
+ rotationController.addRotationStepCode(...steps);
44
+ } catch (err) {
45
+ alert('An error occurred. Check the console for details.');
46
+ console.error(err);
47
+ }
48
+ };
49
+
50
  return (
51
  <Modal isOpen={isOpen} onOpenChange={onOpenChange} placement="center">
52
  <ModalContent>
 
91
  <Button color="danger" variant="light" size="sm" onPress={onClose}>
92
  Close
93
  </Button>
94
+ <Button color="primary" variant="light" size="sm" onPress={copy}>
95
  Copy
96
  </Button>
97
+ <Button color="success" size="sm" onPress={solve}>
98
+ Solve
99
+ </Button>
100
  </ModalFooter>
101
  </>
102
  )}
src/components/ui-controls.tsx CHANGED
@@ -40,10 +40,6 @@ export const UIControls = () => {
40
  stateModalRef.current?.open(state);
41
  };
42
 
43
- const solve = () => {
44
- alert('Working on it!');
45
- };
46
-
47
  const train = () => {
48
  alert('Working on it!');
49
  };
@@ -105,11 +101,7 @@ export const UIControls = () => {
105
  <Button onPress={reset}>Reset</Button>
106
  </ButtonGroup>
107
 
108
- <Button size="sm" variant="light" onPress={showState}>
109
- Show State
110
- </Button>
111
-
112
- <Button size="sm" className="ms-auto" color="success" onPress={solve}>
113
  Solve
114
  </Button>
115
  </div>
 
40
  stateModalRef.current?.open(state);
41
  };
42
 
 
 
 
 
43
  const train = () => {
44
  alert('Working on it!');
45
  };
 
101
  <Button onPress={reset}>Reset</Button>
102
  </ButtonGroup>
103
 
104
+ <Button size="sm" className="ms-auto" color="success" onPress={showState}>
 
 
 
 
105
  Solve
106
  </Button>
107
  </div>
src/middleware.ts ADDED
@@ -0,0 +1,13 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import { NextResponse } from 'next/server';
2
+
3
+ export const config = {
4
+ matcher: '/api/solve',
5
+ };
6
+
7
+ export function middleware(req: Request) {
8
+ const url = new URL(req.url);
9
+ if (url.pathname === '/api/solve') {
10
+ return NextResponse.rewrite('http://localhost:8000/solve');
11
+ }
12
+ return NextResponse.next();
13
+ }