1- import { createEffect , createSignal , For , onMount } from "solid-js" ;
1+ import { createEffect , createSignal , For , onMount , Show } from "solid-js" ;
22import { createStore } from "solid-js/store" ;
33import toast , { Toaster } from "solid-toast" ;
44
@@ -50,7 +50,9 @@ const addCourse = async () => {
5050
5151 const courseExists = await checkCourseExists ( newCourse ( ) ) ;
5252
53- if ( courseExists ) {
53+ console . log ( courseExists ) ;
54+
55+ if ( ! courseExists ) {
5456 toast . error ( "Course CRN doesn't exist" ) ;
5557 return ;
5658 }
@@ -72,6 +74,7 @@ const addCourse = async () => {
7274 courseNum : newCourse ( ) ,
7375 seatAvailable : false ,
7476 waitlistAvailable : false ,
77+ valid : true ,
7578 } ,
7679 ] ) ;
7780 return ;
@@ -83,6 +86,7 @@ const addCourse = async () => {
8386 courseNum : newCourse ( ) ,
8487 seatAvailable : false ,
8588 waitlistAvailable : false ,
89+ valid : true ,
8690 } ,
8791 ] ) ;
8892} ;
@@ -98,22 +102,51 @@ const App = () => {
98102 }
99103 } ) ;
100104
101- createEffect ( ( ) => {
105+ const saveCourses = ( ) => {
106+ checkCoursesValid ( ) ;
102107 localStorage . setItem ( "courses" , JSON . stringify ( courses ) ) ;
103- } , courses ) ;
108+ } ;
109+
110+ const checkCoursesValid = async ( ) => {
111+ let invalidCoursesIdx : number [ ] = [ ] ;
112+
113+ console . log ( courses ) ;
114+
115+ for ( let i = 0 ; i < courses . length ; i ++ ) {
116+ const courseExists = await checkCourseExists ( courses [ i ] . courseNum ) ;
117+ if ( ! courseExists ) {
118+ invalidCoursesIdx . push ( i ) ;
119+ console . log ( i ) ;
120+ }
121+ }
122+
123+ console . log ( "invalid!" ) ;
124+
125+ for ( let i = 0 ; i < invalidCoursesIdx . length ; i ++ ) {
126+ console . log ( invalidCoursesIdx [ i ] ) ;
127+ setCourses ( invalidCoursesIdx [ i ] , { valid : false } ) ;
128+ }
129+ } ;
104130
105131 const getData = async ( ) => {
106132 if ( courses . length == 0 ) {
107133 toast . error ( "Add a course before fetching data!" ) ;
108134 return ;
109135 }
136+
110137 setData ( "Loading" ) ;
111138 toast . loading ( "Fetching your data" ) ;
139+ checkCoursesValid ( ) ;
112140
113- const courseArr = courses . map ( ( el : Course ) => {
114- return el . courseNum ;
115- } ) ;
141+ const courseArr = courses
142+ . filter ( ( el : Course ) => {
143+ return el . valid == true ;
144+ } )
145+ . map ( ( el : Course ) => {
146+ return el . courseNum ;
147+ } ) ;
116148
149+ console . log ( courseArr ) ;
117150 setData ( await get_courses ( courseArr ) ) ;
118151
119152 toast . remove ( ) ;
@@ -123,7 +156,7 @@ const App = () => {
123156 return (
124157 < div class = "container" >
125158 < Toaster />
126- < h1 > Georgia Tech self-coursicle !</ h1 >
159+ < h1 > Georgia Tech self-course checker !</ h1 >
127160 < For each = { courses } >
128161 { ( course : Course ) => (
129162 < div >
@@ -133,23 +166,25 @@ const App = () => {
133166 max = { courseMAX }
134167 type = "number"
135168 onChange = { async ( e ) => {
136- const value : number = e . target . value as number ;
137-
138- if ( value < courseMAX && value > courseMIN ) {
139- const idx = courses . findIndex (
140- ( el : Course ) => el . id === course . id
141- ) ;
142- setCourses ( idx , { courseNum : e . target . value as number } ) ;
143- }
169+ const idx = courses . findIndex (
170+ ( el : Course ) => el . id === course . id
171+ ) ;
172+ setCourses ( idx , { courseNum : e . target . value as number } ) ;
144173 } }
145174 />
146175 < button
147176 onClick = { ( ) => {
148177 setCourses ( courses . filter ( ( el : any ) => el . id !== course . id ) ) ;
149178 } }
150179 >
151- & #10060
180+ & #10060 Remove
152181 </ button >
182+ < Show when = { course . valid } >
183+ < button > & #x2705 Course CRN is valid </ button >
184+ </ Show >
185+ < Show when = { ! course . valid } >
186+ < button > ! CRN Doesn't exist !</ button >
187+ </ Show >
153188 </ div >
154189 ) }
155190 </ For >
@@ -166,6 +201,10 @@ const App = () => {
166201 />
167202 < button onClick = { addCourse } > & #10133 Add Course </ button >
168203 </ div >
204+ < button type = "button" onClick = { saveCourses } >
205+ Save Courses
206+ </ button >
207+ < br />
169208 < button type = "button" onClick = { getData } >
170209 Get Course Data
171210 </ button >
0 commit comments