Skip to content

Commit

Permalink
rewrite all the code when you change the language
Browse files Browse the repository at this point in the history
  • Loading branch information
noahlt committed Aug 18, 2024
1 parent b485620 commit 15e9943
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 7 deletions.
11 changes: 5 additions & 6 deletions app/BlockWidget.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -82,8 +82,7 @@ export function BlockWidget({
if (e.key === "Enter" && !e.shiftKey) {
e.preventDefault();
dBlocks({ type: "finish-edit-prose", id: block.id });
const resp = await generate({ prose: block.prose, lang });
const data = await resp.json();
const data = await generate({ prose: block.prose, lang });
dBlocks({
type: "save-generated-code",
id: block.id,
Expand Down Expand Up @@ -113,8 +112,7 @@ export function BlockWidget({
if (e.key === "Enter" && e.shiftKey) {
e.preventDefault();
dBlocks({ type: "finish-edit-code", id: block.id });
const resp = await generate({ code: block.code, lang });
const data = await resp.json();
const data = await generate({ code: block.code, lang });
dBlocks({
type: "save-generated-prose",
id: block.id,
Expand Down Expand Up @@ -156,10 +154,11 @@ export function BlockWidget({
);
}

async function generate(data: unknown) {
return fetch("/i/generate", {
export async function generate(data: unknown) {
const resp = await fetch("/i/generate", {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify(data),
});
return resp.json();
}
8 changes: 7 additions & 1 deletion app/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import {
} from "./blocksReducer";
import { ChangeEventHandler, useState } from "react";
import { AllLanguages, Language, isLanguage } from "@/lib/lang";
import { BlockWidget } from "./BlockWidget";
import { BlockWidget, generate } from "./BlockWidget";
import { linkColor } from "./styles";
import Link from "next/link";
import { ExportSource } from "./ExportSource";
Expand Down Expand Up @@ -60,6 +60,12 @@ export default function Home() {
onChange={(e) => {
const lang = e.target.value;
if (isLanguage(lang)) dFile({ type: "switch-language", lang });
file.blocks.forEach(async (block) => {
const id = block.id;
dFile({ type: "finish-edit-prose", id });
const data = await generate({ prose: block.prose, lang });
dFile({ type: "save-generated-code", id, code: data.code });
});
}}
/>
<ToggleWidget
Expand Down

0 comments on commit 15e9943

Please sign in to comment.