Skip to content

Commit

Permalink
[ADD] - Increased decoupling of API calls
Browse files Browse the repository at this point in the history
Msal
  • Loading branch information
nulzo authored Nov 18, 2023
2 parents 5b91d1d + 717834b commit 73555d9
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 57 deletions.
11 changes: 8 additions & 3 deletions backend/base/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@

ALLOWED_HOSTS: list[str] = ["*", "http://localhost", "localhost"]

CSRF_TRUSTED_ORIGINS = [
'http://localhost', 'http://localhost:80', 'http://localhost:4200', 'http://localhost:6969'
]

INSTALLED_APPS = [
# 'daphne',
"django.contrib.admin",
Expand All @@ -47,18 +51,19 @@
"django.middleware.security.SecurityMiddleware",
"django.contrib.sessions.middleware.SessionMiddleware",
"django.middleware.common.CommonMiddleware",
# "django.middleware.csrf.CsrfViewMiddleware",
"django.middleware.csrf.CsrfViewMiddleware",
"django.contrib.auth.middleware.AuthenticationMiddleware",
"django.contrib.messages.middleware.MessageMiddleware",
"django.middleware.clickjacking.XFrameOptionsMiddleware",
"corsheaders.middleware.CorsMiddleware",
]

CORS_ORIGIN_ALLOW_ALL = True
# CORS_ORIGIN_WHITELIST = ("http://localhost:5173",)
CORS_ALLOW_ALL_ORIGINS = True

ASGI_APPLICATION = "api.routing.application"
CHANNEL_LAYERS = {"default": {"BACKEND": "channels.layers.InMemoryChannelLayer"}}
CHANNEL_LAYERS = {"default": {
"BACKEND": "channels.layers.InMemoryChannelLayer"}}

MESSAGE_STORAGE = "django.contrib.messages.storage.cookie.CookieStorage"

Expand Down
26 changes: 13 additions & 13 deletions frontend/index.html
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image" href="/UNO.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<script src="./node_modules/preline/dist/preline.js"></script>
<!-- <script src="https://cdn.tailwindcss.com"></script> -->
<title>CSLC Tutoring Portal</title>
</head>

<body class="overscroll-none">
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>
</html>
<head>
<meta charset="UTF-8" />
<link rel="icon" type="image" href="/UNO.svg" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>CSLC Tutoring Portal</title>
</head>

<body class="overscroll-none">
<div id="root"></div>
<script type="module" src="/src/main.tsx"></script>
</body>

</html>
50 changes: 34 additions & 16 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"@radix-ui/themes": "^1.1.2",
"@tailwindcss/forms": "^0.5.6",
"@tanstack/react-query": "^5.0.5",
"@tanstack/react-query-devtools": "^5.8.4",
"@tanstack/react-table": "^8.10.7",
"@types/react-router-dom": "^5.3.3",
"axios": "^1.5.1",
Expand Down
Empty file.
23 changes: 23 additions & 0 deletions frontend/src/API/tickets/ticketRequests.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import axios from "axios"

Check failure on line 1 in frontend/src/API/tickets/ticketRequests.ts

View workflow job for this annotation

GitHub Actions / run-npm

Missing semicolon

Check failure on line 1 in frontend/src/API/tickets/ticketRequests.ts

View workflow job for this annotation

GitHub Actions / run-npm

Missing semicolon

export function createTicket(data: any) {

Check warning on line 3 in frontend/src/API/tickets/ticketRequests.ts

View workflow job for this annotation

GitHub Actions / run-npm

Unexpected any. Specify a different type

Check warning on line 3 in frontend/src/API/tickets/ticketRequests.ts

View workflow job for this annotation

GitHub Actions / run-npm

Unexpected any. Specify a different type
function getCookie(name) {
const value = `; ${document.cookie}`;
const parts = value.split(`; ${name}=`);
if (parts.length === 2) return parts.pop().split(';').shift();

Check failure on line 7 in frontend/src/API/tickets/ticketRequests.ts

View workflow job for this annotation

GitHub Actions / run-npm

Strings must use doublequote

Check failure on line 7 in frontend/src/API/tickets/ticketRequests.ts

View workflow job for this annotation

GitHub Actions / run-npm

Strings must use doublequote
}
const csrftoken = getCookie('csrftoken');

Check failure on line 9 in frontend/src/API/tickets/ticketRequests.ts

View workflow job for this annotation

GitHub Actions / run-npm

Strings must use doublequote

Check failure on line 9 in frontend/src/API/tickets/ticketRequests.ts

View workflow job for this annotation

GitHub Actions / run-npm

Strings must use doublequote
return axios.post("/api/tickets/", {
name: data.student_name,
description: data.body,
professor: data.professor,
course: data.section,
issue: data.issue,
},
{
headers: {
'X-CSRFToken': csrftoken

Check failure on line 19 in frontend/src/API/tickets/ticketRequests.ts

View workflow job for this annotation

GitHub Actions / run-npm

Strings must use doublequote

Check failure on line 19 in frontend/src/API/tickets/ticketRequests.ts

View workflow job for this annotation

GitHub Actions / run-npm

Strings must use doublequote
}
})
.then(res => res.data);
}
40 changes: 16 additions & 24 deletions frontend/src/components/forms/TicketForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ import useFetchProfessor from "@/API/professors/useFetchProfessor";
import useFetchCourse from "@/API/courses/useFetchCourse";
import useFetchIssue from "@/API/issues/useFetchIssue";
import { ScrollArea } from "../ui/scroll-area";
import axios from "axios";
import LoadingSelect from "../loading/loading_select";
import { useMutation } from "@tanstack/react-query";
import { useToast } from "@/components/ui/use-toast";
import { createTicket } from "@/API/tickets/ticketRequests";
// import useFetchSection from "@/API/sections/useFetchSection";

const max_ticket_length = 500;
Expand Down Expand Up @@ -94,19 +94,11 @@ export default function TicketForm() {
const { toast } = useToast();

const mutation = useMutation({
mutationFn: (data) => {
return axios.post("http://localhost:6969/api/tickets/", {
name: data.student_name,
description: data.body,
professor: data.professor,
course: data.section,
issue: data.issue,
});
},
mutationFn: createTicket,
});

async function onSubmit(data: z.infer<typeof FormSchema>) {
await mutation.mutateAsync(data);
function onSubmit(data: z.infer<typeof FormSchema>) {
mutation.mutate(data);
toast({
title: "Scheduled: Catch up",
description: "Friday, February 10, 2023 at 5:57 PM",
Expand Down Expand Up @@ -191,14 +183,14 @@ export default function TicketForm() {
className={cn(
"w-full md:w-[35vw] lg:w-[30vw] xl:w-[40vw] justify-between",
!field.value &&
"text-muted-foreground font-normal",
"text-muted-foreground font-normal",
)}
>
{field.value
? professors?.data.find(
(professor: any) =>
professor.professor_id === field.value,
)?.full_name
(professor: any) =>
professor.professor_id === field.value,
)?.full_name
: "select a professor"}
<CaretSortIcon className="ml-2 h-4 w-4 shrink-0" />
</Button>
Expand Down Expand Up @@ -272,13 +264,13 @@ export default function TicketForm() {
className={cn(
"w-full md:w-[35vw] lg:w-[30vw] xl:w-[40vw] justify-between",
!field.value &&
"text-muted-foreground font-normal",
"text-muted-foreground font-normal",
)}
>
{field.value
? courses?.data.find(
(course: any) => course.id === field.value,
)?.course_code
(course: any) => course.id === field.value,
)?.course_code
: "select a course"}
<CaretSortIcon
key="course_sort_icon"
Expand Down Expand Up @@ -362,14 +354,14 @@ export default function TicketForm() {
className={cn(
"w-full md:w-[35vw] lg:w-[30vw] xl:w-[40vw] justify-between",
!field.value &&
"text-muted-foreground font-normal",
"text-muted-foreground font-normal",
)}
>
{field.value
? issues?.data.find(
(issue: any) =>
issue.issue_id === field.value,
)?.problem_type
(issue: any) =>
issue.issue_id === field.value,
)?.problem_type
: "select an issue"}
<CaretSortIcon
key="issue_sort_icon"
Expand Down Expand Up @@ -466,7 +458,7 @@ export default function TicketForm() {
</FormItem>
)}
/>
<Button type="submit">Submit Ticket</Button>
<Button disabled={mutation.isL} type="submit">Submit Ticket</Button>
</form>
</Form>
</>
Expand Down
2 changes: 2 additions & 0 deletions frontend/src/main.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ import ForEthan from "./views/ethan.tsx";
import { PublicClientApplication } from "@azure/msal-browser";
import { MsalProvider } from "@azure/msal-react";
import { msalConfig } from "./authConfig.ts";
import { ReactQueryDevtools } from '@tanstack/react-query-devtools'

Check failure on line 33 in frontend/src/main.tsx

View workflow job for this annotation

GitHub Actions / run-npm

Strings must use doublequote

Check failure on line 33 in frontend/src/main.tsx

View workflow job for this annotation

GitHub Actions / run-npm

Missing semicolon

Check failure on line 33 in frontend/src/main.tsx

View workflow job for this annotation

GitHub Actions / run-npm

Strings must use doublequote

Check failure on line 33 in frontend/src/main.tsx

View workflow job for this annotation

GitHub Actions / run-npm

Missing semicolon

const queryClient = new QueryClient();

Expand Down Expand Up @@ -126,6 +127,7 @@ ReactDOM.createRoot(document.getElementById("root")!).render(
<React.StrictMode>
<MsalProvider instance={msalInstance}>
<QueryClientProvider client={queryClient}>
<ReactQueryDevtools />
<RouterProvider router={router} />
</QueryClientProvider>
</MsalProvider>
Expand Down
1 change: 0 additions & 1 deletion frontend/tailwind.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ module.exports = {
"./components/**/*.{ts,tsx}",
"./app/**/*.{ts,tsx}",
"./src/**/*.{ts,tsx}",
"node_modules/preline/dist/*.js",
],
theme: {
container: {
Expand Down
3 changes: 3 additions & 0 deletions nginx/nginx.conf
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ server {

# All frontend requests
location / {
proxy_set_header 'Access-Control-Allow-Credentials' 'true';
# proxy_set_header 'Access-Control-Allow-Headers' 'Authorization,Accept,Origin,DNT,X-CustomHeader,X-CSRFToken,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
proxy_set_header 'Access-Control-Allow-Methods' 'GET,POST,OPTIONS,PUT,DELETE,PATCH';
proxy_pass http://frontend:4200;
proxy_set_header Host $host;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
Expand Down

0 comments on commit 73555d9

Please sign in to comment.