Skip to content

Commit 676f654

Browse files
committed
Save submission on Run Code
1 parent 96f0c52 commit 676f654

File tree

3 files changed

+46
-6
lines changed

3 files changed

+46
-6
lines changed

app/javascript/components/bootcamp/FrontendExercisePage/LHS/LHS.tsx

Lines changed: 44 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ import toast from 'react-hot-toast'
1212
import { wrapJSCode } from './wrapJSCode'
1313
import { validateHtml5 } from '../../common/validateHtml5/validateHtml5'
1414
import { normalizeHtmlText } from '../../common/validateHtml5/normalizeHtmlText'
15+
import { submitCode } from '../../JikiscriptExercisePage/hooks/useConstructRunCode/submitCode'
16+
import { getCodeMirrorFieldValue } from '../../JikiscriptExercisePage/CodeMirror/getCodeMirrorFieldValue'
17+
import { readOnlyRangesStateField } from '../../JikiscriptExercisePage/CodeMirror/extensions/read-only-ranges/readOnlyRanges'
1518

1619
export type TabIndex = 'html' | 'css' | 'javascript'
1720

@@ -32,6 +35,7 @@ export function LHS() {
3235
expectedReferenceIFrameRef,
3336
exercise,
3437
code,
38+
links,
3539
} = useContext(FrontendExercisePageContext)
3640

3741
const {
@@ -57,10 +61,10 @@ export function LHS() {
5761
const handleRunCode = useCallback(() => {
5862
if (!jsEditorRef.current || !htmlEditorRef.current) return
5963

60-
const htmlText = htmlEditorRef.current.state.doc.toString()
64+
const htmlCode = htmlEditorRef.current.state.doc.toString()
6165

62-
if (htmlText.length > 0) {
63-
const normalizedHtml = normalizeHtmlText(htmlText)
66+
if (htmlCode.length > 0) {
67+
const normalizedHtml = normalizeHtmlText(htmlCode)
6468
const isHTMLValid = validateHtml5(normalizedHtml)
6569

6670
if (!isHTMLValid.isValid) {
@@ -72,9 +76,44 @@ export function LHS() {
7276
}
7377
}
7478

79+
const cssCode = cssEditorRef.current?.state.doc.toString()
80+
7581
const jsView = jsEditorRef.current
7682
const jsCode = jsEditorRef.current.state.doc.toString()
7783

84+
const cssReadonlyRanges = getCodeMirrorFieldValue(
85+
cssEditorRef.current,
86+
readOnlyRangesStateField
87+
)
88+
const htmlReadonlyRanges = getCodeMirrorFieldValue(
89+
htmlEditorRef.current,
90+
readOnlyRangesStateField
91+
)
92+
const jsReadonlyRanges = getCodeMirrorFieldValue(
93+
jsEditorRef.current,
94+
readOnlyRangesStateField
95+
)
96+
97+
const submittedCode = JSON.stringify({
98+
html: htmlCode,
99+
css: cssCode,
100+
js: jsCode,
101+
})
102+
submitCode({
103+
postUrl: links.postSubmission,
104+
code: submittedCode,
105+
testResults: {
106+
status: 'unknown',
107+
tests: [],
108+
},
109+
customFunctions: [],
110+
readonlyRanges: {
111+
html: cssReadonlyRanges,
112+
css: cssReadonlyRanges,
113+
js: jsReadonlyRanges,
114+
},
115+
})
116+
78117
const result = parseJS(jsView.state.doc.toString())
79118
switch (result.status) {
80119
case 'success':
@@ -87,8 +126,8 @@ export function LHS() {
87126
actualIFrameRef,
88127
{
89128
script: fullScript,
90-
html: htmlEditorRef.current?.state.doc.toString(),
91-
css: cssEditorRef.current?.state.doc.toString(),
129+
html: htmlCode,
130+
css: cssCode,
92131
},
93132
code
94133
)

app/javascript/components/bootcamp/JikiscriptExercisePage/hooks/useConstructRunCode/submitCode.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ export async function submitCode({
1414
readonlyRanges:
1515
| ReadonlyRange[]
1616
| { html: ReadonlyRange[]; css: ReadonlyRange[] }
17+
| { html: ReadonlyRange[]; css: ReadonlyRange[]; js: ReadonlyRange[] }
1718
customFunctions: string[]
1819
}) {
1920
const response = await fetch(postUrl, {

app/models/bootcamp/submission.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ class Bootcamp::Submission < ApplicationRecord
44
serialize :test_results, JSONWithIndifferentAccess
55
serialize :readonly_ranges, JSONWithIndifferentAccess
66
serialize :custom_functions, JSONWithIndifferentAccess
7-
enum :status, { pass: 0, fail: 1, pass_bonus: 2 }
7+
enum :status, { pass: 0, fail: 1, pass_bonus: 2, unknown: 3 }
88

99
scope :passed, -> { where(status: :pass) }
1010
scope :passed_bonus, -> { where(status: :pass_bonus) }

0 commit comments

Comments
 (0)