Skip to content

Commit

Permalink
fix: moved out ctf axios (lol) and finished last few others
Browse files Browse the repository at this point in the history
  • Loading branch information
ollibowers committed Jul 26, 2024
1 parent 4005ba5 commit a4c896c
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 48 deletions.
5 changes: 2 additions & 3 deletions frontend/src/config/migrations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-nocheck
import axios from 'axios';
import type { MigrationManifest } from 'redux-persist';
import { createMigrate } from 'redux-persist';
import { Course } from 'types/api';
import { getCourseInfo } from 'utils/api/courseApi';

/**
* IMPORTANT NOTE:
Expand Down Expand Up @@ -43,7 +42,7 @@ const persistMigrations: MigrationManifest = {
await Promise.all(
courses.map(async (course) => {
try {
const res = await axios.get<Course>(`/courses/getCourse/${course}`);
const res = await getCourseInfo(course);
if (res.status === 200) {
const courseData = res.data;
newState.planner.courses[courseData.code].isMultiterm = courseData.is_multiterm;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@ import React, { useRef, useState } from 'react';
import { LoadingOutlined } from '@ant-design/icons';
import { useQuery } from '@tanstack/react-query';
import { Spin } from 'antd';
import axios from 'axios';
import { JSONPlanner, Term, UnPlannedToTerm } from 'types/planner';
import { JSONPlanner, Term } from 'types/planner';
import { badPlanner } from 'types/userResponse';
import { withAuthorization } from 'utils/api/auth';
import { getCourseInfo } from 'utils/api/courseApi';
import { addToUnplanned, setUnplannedCourseToTerm } from 'utils/api/plannerApi';
import {
getUserPlanner,
toggleSummerTerm,
Expand All @@ -28,34 +27,10 @@ const ImportPlannerMenu = () => {
const inputRef = useRef<HTMLInputElement>(null);
const [loading, setLoading] = useState(false);

const handleSetUnplannedCourseToTerm = async (data: UnPlannedToTerm) => {
try {
await axios.post('planner/unPlannedToTerm', data, {
headers: withAuthorization(token)
});
} catch (err) {
// eslint-disable-next-line no-console
console.error(`Error at handleSetUnplannedCourseToTerm:`, err);
}
};

const upload = () => {
inputRef.current?.click();
};

const handleAddToUnplanned = async (code: string) => {
try {
await axios.post(
'planner/addToUnplanned',
{ courseCode: code },
{ headers: withAuthorization(token) }
);
} catch (err) {
// eslint-disable-next-line no-console
console.error('Error at handleAddToUnplanned: ', err);
}
};

const uploadedJSONFile = async (e: React.ChangeEvent<HTMLInputElement>) => {
if (e.target.files === null) {
return;
Expand Down Expand Up @@ -133,7 +108,7 @@ const ImportPlannerMenu = () => {
const course = await getCourseInfo(code);
if (plannedCourses.indexOf(course.code) === -1) {
plannedCourses.push(course.code);
handleAddToUnplanned(course.code);
addToUnplanned(token, course.code);
const destYear = Number(yearIndex) + Number(planner.startYear);
const destTerm = term as Term;
const destRow = destYear - planner.startYear;
Expand All @@ -144,7 +119,7 @@ const ImportPlannerMenu = () => {
destIndex,
courseCode: code
};
handleSetUnplannedCourseToTerm(data);
setUnplannedCourseToTerm(token, data);
}
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import React from 'react';
import { Typography } from 'antd';
import axios from 'axios';
import styled from 'styled-components';
import { withAuthorization } from 'utils/api/auth';
import { CtfResult, validateCtf as validateCtfRequest } from 'utils/api/ctfApi';
import useToken from 'hooks/useToken';
import CS from '../common/styles';
import S from './styles';

type CtfResult = {
valid: boolean;
failed: number;
passed: Array<string>;
message: string;
flags: Array<string>;
};

const { Text, Title } = Typography;

const TextBlock = styled(Text)`
Expand Down Expand Up @@ -44,12 +35,8 @@ const ValidateCtfButton = () => {

const validateCtf = async () => {
setOpen(true);
const res = await axios.post<CtfResult>(
'/ctf/validateCtf/',
{},
{ headers: withAuthorization(token) }
);
setResult(res.data);
const res = await validateCtfRequest(token);
setResult(res);
};

return (
Expand Down
21 changes: 21 additions & 0 deletions frontend/src/utils/api/ctfApi.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import axios from 'axios';
import { withAuthorization } from './auth';

export type CtfResult = {
valid: boolean;
failed: number;
passed: Array<string>;
message: string;
flags: Array<string>;
};

// lol
export const validateCtf = async (token: string): Promise<CtfResult> => {
const res = await axios.post<CtfResult>(
'/ctf/validateCtf/',
{},
{ headers: withAuthorization(token) }
);

return res.data;
};

0 comments on commit a4c896c

Please sign in to comment.