11import React from "react" ;
2+ import { useState } from "react" ;
23import { Button , Typography } from "@mui/material" ;
34
45import "../styles/JobPost.css" ;
56
7+
68function JobPost ( props ) {
9+
10+ const [ apply , setApply ] = useState ( false )
11+
12+
13+
14+
15+
716 const formatCurrency = ( num ) => {
817 return num . toLocaleString ( "en-US" , {
918 style : "currency" ,
@@ -12,6 +21,40 @@ function JobPost(props) {
1221 } ) ;
1322 } ;
1423
24+ const applyHandler = async ( ) => {
25+ setApply ( true )
26+ try {
27+
28+
29+ const response = await fetch ( `https://chapi.techstartucalgary.com/applications/${ props . job . id } ` , {
30+ method :"POST" ,
31+ mode :"cors" ,
32+ headers : {
33+ "Content-Type" : "application/json" ,
34+ Authorization : `Bearer ${ localStorage . getItem ( "access_token" ) } ` ,
35+ } ,
36+
37+ } )
38+ if ( response . ok ) {
39+ alert ( "Job was succesful" )
40+ setApply ( false )
41+ window . location . reload ( ) ;
42+ } else {
43+ throw new Error ( "Job application failed. Please try again later" )
44+ }
45+ } catch ( error ) {
46+ console . error ( error )
47+ alert ( error . message )
48+ setApply ( false )
49+ }
50+
51+
52+
53+ }
54+
55+
56+
57+
1558 return (
1659 < div className = "jobContainer" >
1760 < Typography className = "jobTitle" > { props . job . title } </ Typography >
@@ -37,8 +80,15 @@ function JobPost(props) {
3780 </ Typography >
3881 ) ;
3982 } ) }
40- < Button variant = "contained" className = "applyButton" >
41- < Typography > Apply</ Typography >
83+ < Button
84+ variant = "contained"
85+ href = "" className = "applyButton"
86+ disabled = { apply }
87+ onClick = { applyHandler }
88+
89+ >
90+ { apply ? < Typography > Applying...</ Typography > : < Typography > Apply</ Typography > }
91+
4292 </ Button >
4393 </ div >
4494 ) ;
0 commit comments