Skip to content

Commit

Permalink
Merge branch 'dev' into zac-sanitize-code
Browse files Browse the repository at this point in the history
  • Loading branch information
ES-Legacy authored Dec 3, 2023
2 parents dbb7c4e + 23e8661 commit 5c84023
Show file tree
Hide file tree
Showing 21 changed files with 313 additions and 16 deletions.
11 changes: 11 additions & 0 deletions .xata/migrations/.ledger
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,14 @@ mig_ckeabvjgnj3bm0ispvgg_1ce47743
mig_ckevkeengvghfvu4alkg_ddcffced
mig_ckevkhungvghfvu4alsg_742fbcbb
mig_ckevkpungvghfvu4am50_1511cbec
mig_clhe670vdfloffdfg9s0_4744dcb2
mig_clhe6b6r7je6m59gktsg_fd744927
mig_clhe6cur7je6m59gkttg_107f96be
mig_clhe6g8vdfloffdfg9t0_8f3eb990
mig_clhe6l6r7je6m59gktug_5ae5be9b
mig_clhe6s6r7je6m59gktvg_1c6c9cf0
mig_cll7guf3d17cd4sn3q50_f99b28e0
mig_cll7h3v3d17cd4sn3q60_deca122c
mig_cll7kiv3d17cd4sn3qdg_1d8d01c9
mig_cll7kp2ifsth3gbude0g_4d9ec4f1
mig_cll7ksaifsth3gbude2g_4f044676
12 changes: 12 additions & 0 deletions .xata/migrations/mig_clhe670vdfloffdfg9s0_4744dcb2.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"id": "mig_clhe670vdfloffdfg9s0",
"parentID": "mig_ckevkpungvghfvu4am50",
"checksum": "1:4744dcb2d75e9cba5183e24a6a49447ea0282ba342d7a4fe86afb0dafefc0f79",
"operations": [
{
"addTable": {
"table": "Backlog-Score"
}
}
]
}
16 changes: 16 additions & 0 deletions .xata/migrations/mig_clhe6b6r7je6m59gktsg_fd744927.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"id": "mig_clhe6b6r7je6m59gktsg",
"parentID": "mig_clhe670vdfloffdfg9s0",
"checksum": "1:fd7449272458d918d7cf0f07f08e98650176ae39f2847539e63cfb43b996ba24",
"operations": [
{
"addColumn": {
"column": {
"name": "code",
"type": "string"
},
"table": "Backlog-Score"
}
}
]
}
16 changes: 16 additions & 0 deletions .xata/migrations/mig_clhe6cur7je6m59gkttg_107f96be.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"id": "mig_clhe6cur7je6m59gkttg",
"parentID": "mig_clhe6b6r7je6m59gktsg",
"checksum": "1:107f96be15c59f116769877a3ede887da639354f69e3dfa5e3080aa4f35f1908",
"operations": [
{
"addColumn": {
"column": {
"name": "email",
"type": "string"
},
"table": "Backlog-Score"
}
}
]
}
17 changes: 17 additions & 0 deletions .xata/migrations/mig_clhe6g8vdfloffdfg9t0_8f3eb990.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"id": "mig_clhe6g8vdfloffdfg9t0",
"parentID": "mig_clhe6cur7je6m59gkttg",
"checksum": "1:8f3eb990da0dfd0ca576d59a6aaeba6f55e0d00709bd250e7f208f44dfbdabab",
"operations": [
{
"addColumn": {
"column": {
"name": "retry",
"type": "int",
"defaultValue": "0"
},
"table": "Backlog-Score"
}
}
]
}
14 changes: 14 additions & 0 deletions .xata/migrations/mig_clhe6l6r7je6m59gktug_5ae5be9b.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"id": "mig_clhe6l6r7je6m59gktug",
"parentID": "mig_clhe6g8vdfloffdfg9t0",
"checksum": "1:5ae5be9b8b8d79df03cd1c6d5b6f3dd47d5ae1926ed3f526dc10456a5f2c1cc9",
"operations": [
{
"renameColumn": {
"newName": "retry_count",
"oldName": "retry",
"table": "Backlog-Score"
}
}
]
}
16 changes: 16 additions & 0 deletions .xata/migrations/mig_clhe6s6r7je6m59gktvg_1c6c9cf0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"id": "mig_clhe6s6r7je6m59gktvg",
"parentID": "mig_clhe6l6r7je6m59gktug",
"checksum": "1:1c6c9cf0790fe6efe3454c3edc25cc228681f0520c0cc2a233ba7515adad5929",
"operations": [
{
"addColumn": {
"column": {
"name": "challenge",
"type": "string"
},
"table": "Backlog-Score"
}
}
]
}
12 changes: 12 additions & 0 deletions .xata/migrations/mig_cll7guf3d17cd4sn3q50_f99b28e0.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"id": "mig_cll7guf3d17cd4sn3q50",
"parentID": "mig_clhe6s6r7je6m59gktvg",
"checksum": "1:f99b28e07a41df81e9f84ed761484daf31c3820f0fb251d12c037af42107346a",
"operations": [
{
"addTable": {
"table": "EmailSubmitRateLimiting"
}
}
]
}
17 changes: 17 additions & 0 deletions .xata/migrations/mig_cll7h3v3d17cd4sn3q60_deca122c.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"id": "mig_cll7h3v3d17cd4sn3q60",
"parentID": "mig_cll7guf3d17cd4sn3q50",
"checksum": "1:deca122c44d63a03805dd49378cb9b9fc470d2826fab6f9f5c018191bd139023",
"operations": [
{
"addColumn": {
"column": {
"name": "email",
"type": "string",
"unique": true
},
"table": "EmailSubmitRateLimiting"
}
}
]
}
16 changes: 16 additions & 0 deletions .xata/migrations/mig_cll7kiv3d17cd4sn3qdg_1d8d01c9.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"id": "mig_cll7kiv3d17cd4sn3qdg",
"parentID": "mig_cll7h3v3d17cd4sn3q60",
"checksum": "1:1d8d01c975b06096e6ceba8841f904dbe4158868fdca69371272e619f5c6db09",
"operations": [
{
"addColumn": {
"column": {
"name": "lastSubmission",
"type": "datetime"
},
"table": "EmailSubmitRateLimiting"
}
}
]
}
13 changes: 13 additions & 0 deletions .xata/migrations/mig_cll7kp2ifsth3gbude0g_4d9ec4f1.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
{
"id": "mig_cll7kp2ifsth3gbude0g",
"parentID": "mig_cll7kiv3d17cd4sn3qdg",
"checksum": "1:4d9ec4f1bc9566499f066e2d50b4f4b2403d7fbd52b38c6a7cfbbdec56ca4e01",
"operations": [
{
"removeColumn": {
"column": "lastSubmission",
"table": "EmailSubmitRateLimiting"
}
}
]
}
17 changes: 17 additions & 0 deletions .xata/migrations/mig_cll7ksaifsth3gbude2g_4f044676.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"id": "mig_cll7ksaifsth3gbude2g",
"parentID": "mig_cll7kp2ifsth3gbude0g",
"checksum": "1:4f0446762ff26866fb83e1415120923426212e10433b786ea582050c42f8811c",
"operations": [
{
"addColumn": {
"column": {
"name": "lastSubmission",
"type": "datetime",
"defaultValue": "now"
},
"table": "EmailSubmitRateLimiting"
}
}
]
}
30 changes: 30 additions & 0 deletions app/api/code/submit/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ On a successful OpenAI call, call the email generator (api)
export async function POST(req: Request) {
try {
const body = await req.json();

const { code } = body;
if (!validateHTML(code)) {
return NextResponse.json(
Expand All @@ -44,6 +45,35 @@ export async function POST(req: Request) {
},
{ status: 400 }
);

const { email } = body;
const record = await xata.db.EmailSubmitRateLimiting.filter({
email: email,
}).getFirst();
if (record) {
if (record.lastSubmission && process.env.IS_PRODUCTION === "true") {
//Time between submissions has to be at least 1 minute
const lastSubmission = new Date(record.lastSubmission);
const currentTime = new Date();
if (
currentTime.getTime() - lastSubmission.getTime() <
60 * 1000 * 1
) {
return NextResponse.json(
{ message: "Time between submissions too small!" },
{ status: 400 }
);
} else {
await xata.db.EmailSubmitRateLimiting.update(record.id, {
lastSubmission: new Date().toISOString(),
});
}
}
} else {
await xata.db.EmailSubmitRateLimiting.create({
email: email,
lastSubmission: new Date().toISOString(),
});
}
// Extract the host and protocol from the incoming request
const url = new URL(req.url);
Expand Down
9 changes: 6 additions & 3 deletions client-side-queries/rq-queries/code-submit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,22 @@ export function getCodeSubmitCount() {
return resp;
}

interface UsePostSubmitCountProps {
interface UseCodeSubmissionProps {
code: string;
dateTime: string;
email: string;
challenge: string;
}

export async function postSubmitCount({
export async function codeSubmission({
code,
dateTime,
email,
challenge,
}: UsePostSubmitCountProps) {
}: UseCodeSubmissionProps) {
if (!code || !dateTime || !email || !challenge) {
throw new Error("Something wasn't passed into codeSubmission.");
}
const resp = await axios.post("/api/code/submit", {
code: code,
dateTime: dateTime,
Expand Down
19 changes: 15 additions & 4 deletions components/core/code-area-actions/submit-button.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ import {
AlertDialogTitle,
} from "@/components/ui/alert-dialog";
import { useResetFeState } from "@/lib/reset-fe-state";
import { postSubmitCount } from "@/client-side-queries/rq-queries/code-submit";
import { codeSubmission } from "@/client-side-queries/rq-queries/code-submit";
import useSessionStore from "@/data-store/session-store";
import { useStepperStore } from "@/data-store/stepper-store";
import { loadFromLocalStorage } from "@/lib/localStorage";

const smallProps: ConfettiProps = {
force: 0.6,
Expand Down Expand Up @@ -63,10 +64,20 @@ const SubmitButton = () => {
const handleSubmitButtonClick = async () => {
try {
const dateTime = getCurrentDateTime();
await postSubmitCount({ code, dateTime, email, challenge });

const submitEmail = email ? email : loadFromLocalStorage("email");
const submitChallenge = challenge
? challenge
: loadFromLocalStorage("challenge");
await codeSubmission({
code,
dateTime,
email: submitEmail,
challenge: submitChallenge,
});
setSubmitClicked(true);
} catch (error) {
alert("Something went wrong. Try submitting again!");
} catch (error: any) {
alert(error.response.data.message);
}
};

Expand Down
12 changes: 11 additions & 1 deletion components/core/editor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { Button } from "../ui/button";
import StaticPrompt from "./target-image";
import LandingPageChallengeCode from "../landing/test-challenges/challenge-code";
import { useStepperStore } from "@/data-store/stepper-store";
import { loadFromLocalStorage, saveToLocalStorage } from "@/lib/localStorage";

const Editor = () => {
const { code, setCode } = useSessionStore();
Expand Down Expand Up @@ -39,10 +40,19 @@ const Editor = () => {
};

loadAce();
const localStorageCode = loadFromLocalStorage("code");
if (code.length === 0 && localStorageCode.length > 0) {
setCode(localStorageCode);
}
}, []);

if (!AceEditor) return <GlobalLoadingUI />;

const onType = (newCode: string) => {
setCode(newCode);
saveToLocalStorage("code", code);
};

return (
<div className='flex h-screen flex-col'>
<div className='flex flex-row'>
Expand Down Expand Up @@ -71,7 +81,7 @@ const Editor = () => {
theme={aceEditorTheme}
name='editor'
height='100%'
onChange={(newCode: string) => setCode(newCode)}
onChange={(newCode: string) => onType(newCode)}
fontSize={fontSize}
showPrintMargin={true}
showGutter={true}
Expand Down
6 changes: 4 additions & 2 deletions components/ui/useCode/StepTwo.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { useRouter } from "next/navigation";
import { ChallengeFormField } from "./Challenge-FormField";
import { Loader2 } from "lucide-react";
import { challengeEnum } from "@/data-store/challenge-store";
import { saveToLocalStorage } from "@/lib/localStorage";

const formStepTwoSchema = z.object({
challenge: z.nativeEnum(challengeEnum, {
Expand All @@ -31,7 +32,7 @@ const formStepTwoSchema = z.object({
});

export function StepTwo() {
const { progress, setProgress, step, setStep, setChallenge } =
const { progress, setProgress, step, setStep, setChallenge, email } =
useStepperStore();
const { setCode } = useSessionStore();

Expand All @@ -47,7 +48,8 @@ export function StepTwo() {
setCode(LandingPageCode());
setChallenge(selection);
setProgress(progress + progressIncrements);

saveToLocalStorage("email", email);
saveToLocalStorage("challenge", selection);
router.push("/code-area");
}
}
Expand Down
26 changes: 26 additions & 0 deletions lib/localStorage.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
export const saveToLocalStorage = (key: string, value: string) => {
try {
localStorage.setItem(key, value);
} catch (error) {
console.error("Error saving to localStorage", error);
}
};

export const loadFromLocalStorage = (key: string) => {
try {
const value = localStorage.getItem(key);
if (value === null) {
return "";
}
return value;
} catch (error) {
console.error("Error reading from localStorage", error);
return "";
}
};

export const removeItemFromLocalStorage = (key: string) => {
if (typeof window !== "undefined") {
localStorage.removeItem(key);
}
};
Loading

0 comments on commit 5c84023

Please sign in to comment.