1
1
import fetch from "cross-fetch" ;
2
2
import AwesomeDebouncePromise from "awesome-debounce-promise" ;
3
3
import { batch } from "react-redux" ;
4
+ import { parsePhoneNumberFromString } from "libphonenumber-js" ;
4
5
import getCsrf from "../components/csrf" ;
5
6
import { MIN_FETCH_INTERVAL } from "../constants/sync_constants" ;
6
7
import { PATH_REGISTRATION_SCHEDULE_NAME } from "../constants/constants" ;
@@ -42,6 +43,14 @@ export const ADD_CART_ITEM = "ADD_CART_ITEM";
42
43
export const REMOVE_CART_ITEM = "REMOVE_CART_ITEM" ;
43
44
export const CHANGE_SORT_TYPE = "CHANGE_SORT_TYPE" ;
44
45
46
+ export const REGISTER_ALERT_ITEM = "REGISTER_ALERT_ITEM" ;
47
+ export const REACTIVATE_ALERT_ITEM = "REACTIVATE_ALERT_ITEM" ;
48
+ export const DEACTIVATE_ALERT_ITEM = "DEACTIVATE_ALERT_ITEM" ;
49
+ export const DELETE_ALERT_ITEM = "DELETE_ALERT_ITEM" ;
50
+ export const UPDATE_CONTACT_INFO = "UPDATE_CONTACT_INFO" ;
51
+
52
+ export const MARK_ALERTS_SYNCED = "MARK_ALERTS_SYNCED" ;
53
+
45
54
export const TOGGLE_CHECK = "TOGGLE_CHECK" ;
46
55
export const REMOVE_SCHED_ITEM = "REMOVE_SCHED_ITEM" ;
47
56
@@ -493,6 +502,35 @@ export const removeCartItem = (sectionId) => ({
493
502
sectionId,
494
503
} ) ;
495
504
505
+ export const registerAlertFrontend = ( alert ) => ( {
506
+ type : REGISTER_ALERT_ITEM ,
507
+ alert,
508
+ } ) ;
509
+
510
+ export const reactivateAlertFrontend = ( sectionId ) => ( {
511
+ type : REACTIVATE_ALERT_ITEM ,
512
+ sectionId,
513
+ } ) ;
514
+
515
+ export const deactivateAlertFrontend = ( sectionId ) => ( {
516
+ type : DEACTIVATE_ALERT_ITEM ,
517
+ sectionId,
518
+ } ) ;
519
+
520
+ export const deleteAlertFrontend = ( sectionId ) => ( {
521
+ type : DELETE_ALERT_ITEM ,
522
+ sectionId,
523
+ } ) ;
524
+
525
+ export const updateContactInfoFrontend = ( contactInfo ) => ( {
526
+ type : UPDATE_CONTACT_INFO ,
527
+ contactInfo,
528
+ } ) ;
529
+
530
+ export const markAlertsSynced = ( ) => ( {
531
+ type : MARK_ALERTS_SYNCED ,
532
+ } ) ;
533
+
496
534
export const changeSortType = ( sortMode ) => ( {
497
535
type : CHANGE_SORT_TYPE ,
498
536
sortMode,
@@ -632,9 +670,7 @@ export const createScheduleOnBackend = (name, sections = []) => (dispatch) => {
632
670
. then ( ( { id } ) => {
633
671
dispatch ( createScheduleOnFrontend ( name , id , sections ) ) ;
634
672
} )
635
- . catch ( ( error ) => {
636
- console . log ( error ) ;
637
- } ) ;
673
+ . catch ( ( error ) => console . log ( error ) ) ;
638
674
} ;
639
675
640
676
export const deleteScheduleOnBackend = ( user , scheduleName , scheduleId ) => (
@@ -667,9 +703,7 @@ export const deleteScheduleOnBackend = (user, scheduleName, scheduleId) => (
667
703
} )
668
704
) ;
669
705
} )
670
- . catch ( ( error ) => {
671
- console . log ( error ) ;
672
- } ) ;
706
+ . catch ( ( error ) => console . log ( error ) ) ;
673
707
} ;
674
708
675
709
export const findOwnPrimarySchedule = ( user ) => ( dispatch ) => {
@@ -686,9 +720,7 @@ export const findOwnPrimarySchedule = (user) => (dispatch) => {
686
720
setPrimaryScheduleIdOnFrontend ( foundSched ?. schedule . id )
687
721
) ;
688
722
} )
689
- . catch ( ( error ) => {
690
- console . log ( error ) ;
691
- } )
723
+ . catch ( ( error ) => console . log ( error ) )
692
724
) ;
693
725
} ;
694
726
@@ -716,3 +748,181 @@ export const setCurrentUserPrimarySchedule = (user, scheduleId) => (
716
748
} )
717
749
. catch ( ( error ) => console . log ( error ) ) ;
718
750
} ;
751
+
752
+ export const registerAlertItem = ( sectionId ) => ( dispatch ) => {
753
+ const registrationObj = {
754
+ section : sectionId ,
755
+ auto_resubscribe : true ,
756
+ close_notification : false ,
757
+ } ;
758
+ const init = {
759
+ method : "POST" ,
760
+ credentials : "include" ,
761
+ mode : "same-origin" ,
762
+ headers : {
763
+ Accept : "application/json" ,
764
+ "Content-Type" : "application/json" ,
765
+ "X-CSRFToken" : getCsrf ( ) ,
766
+ } ,
767
+ body : JSON . stringify ( registrationObj ) ,
768
+ } ;
769
+ doAPIRequest ( "/alert/registrations/" , init )
770
+ . then ( ( res ) => res . json ( ) )
771
+ . then ( ( data ) => {
772
+ dispatch (
773
+ registerAlertFrontend ( {
774
+ ...registrationObj ,
775
+ id : data . id ,
776
+ cancelled : false ,
777
+ status : "C" ,
778
+ } )
779
+ ) ;
780
+ } ) ;
781
+ } ;
782
+
783
+ export const reactivateAlertItem = ( sectionId , alertId ) => ( dispatch ) => {
784
+ const updateObj = {
785
+ resubscribe : true ,
786
+ } ;
787
+ const init = {
788
+ method : "PUT" ,
789
+ credentials : "include" ,
790
+ mode : "same-origin" ,
791
+ headers : {
792
+ Accept : "application/json" ,
793
+ "Content-Type" : "application/json" ,
794
+ "X-CSRFToken" : getCsrf ( ) ,
795
+ } ,
796
+ body : JSON . stringify ( updateObj ) ,
797
+ } ;
798
+ doAPIRequest ( `/alert/registrations/${ alertId } /` , init ) . then ( ( res ) => {
799
+ if ( res . ok ) {
800
+ dispatch ( reactivateAlertFrontend ( sectionId ) ) ;
801
+ }
802
+ } ) ;
803
+ } ;
804
+
805
+ export const deactivateAlertItem = ( sectionId , alertId ) => ( dispatch ) => {
806
+ const updateObj = {
807
+ cancelled : true ,
808
+ } ;
809
+ const init = {
810
+ method : "PUT" ,
811
+ credentials : "include" ,
812
+ mode : "same-origin" ,
813
+ headers : {
814
+ Accept : "application/json" ,
815
+ "Content-Type" : "application/json" ,
816
+ "X-CSRFToken" : getCsrf ( ) ,
817
+ } ,
818
+ body : JSON . stringify ( updateObj ) ,
819
+ } ;
820
+ doAPIRequest ( `/alert/registrations/${ alertId } /` , init ) . then ( ( res ) => {
821
+ if ( res . ok ) {
822
+ dispatch ( deactivateAlertFrontend ( sectionId ) ) ;
823
+ }
824
+ } ) ;
825
+ } ;
826
+
827
+ export const deleteAlertItem = ( sectionId , alertId ) => ( dispatch ) => {
828
+ const updateObj = {
829
+ deleted : true ,
830
+ } ;
831
+ const init = {
832
+ method : "PUT" ,
833
+ credentials : "include" ,
834
+ mode : "same-origin" ,
835
+ headers : {
836
+ Accept : "application/json" ,
837
+ "Content-Type" : "application/json" ,
838
+ "X-CSRFToken" : getCsrf ( ) ,
839
+ } ,
840
+ body : JSON . stringify ( updateObj ) ,
841
+ } ;
842
+ doAPIRequest ( `/alert/registrations/${ alertId } /` , init ) . then ( ( res ) => {
843
+ if ( res . ok ) {
844
+ dispatch ( deleteAlertFrontend ( sectionId ) ) ;
845
+ }
846
+ } ) ;
847
+ } ;
848
+
849
+ export const fetchAlerts = ( ) => ( dispatch ) => {
850
+ const init = {
851
+ method : "GET" ,
852
+ credentials : "include" ,
853
+ mode : "same-origin" ,
854
+ headers : {
855
+ Accept : "application/json" ,
856
+ "Content-Type" : "application/json" ,
857
+ "X-CSRFToken" : getCsrf ( ) ,
858
+ } ,
859
+ } ;
860
+ doAPIRequest ( "/alert/registrations/" , init )
861
+ . then ( ( res ) => res . json ( ) )
862
+ . then ( ( alerts ) => {
863
+ alerts . forEach ( ( alert ) => {
864
+ dispatch (
865
+ registerAlertFrontend ( {
866
+ id : alert . id ,
867
+ section : alert . section ,
868
+ cancelled : alert . cancelled ,
869
+ auto_resubscribe : alert . auto_resubscribe ,
870
+ close_notification : alert . close_notification ,
871
+ status : alert . section_status ,
872
+ } )
873
+ ) ;
874
+ } ) ;
875
+ } )
876
+ . catch ( ( error ) => console . log ( error ) ) ;
877
+ } ;
878
+
879
+ export const fetchContactInfo = ( ) => ( dispatch ) => {
880
+ fetch ( "/accounts/me/" , {
881
+ method : "GET" ,
882
+ credentials : "include" ,
883
+ mode : "same-origin" ,
884
+ headers : {
885
+ Accept : "application/json" ,
886
+ "Content-Type" : "application/json" ,
887
+ "X-CSRFToken" : getCsrf ( ) ,
888
+ } ,
889
+ } )
890
+ . then ( ( res ) => res . json ( ) )
891
+ . then ( ( data ) => {
892
+ dispatch (
893
+ updateContactInfoFrontend ( {
894
+ email : data . profile . email ,
895
+ phone : data . profile . phone ,
896
+ } )
897
+ ) ;
898
+ } )
899
+ // eslint-disable-next-line no-console
900
+ . catch ( ( error ) => console . log ( error ) ) ;
901
+ } ;
902
+
903
+ export const updateContactInfo = ( contactInfo ) => ( dispatch ) => {
904
+ const profile = {
905
+ email : contactInfo . email ,
906
+ phone :
907
+ parsePhoneNumberFromString ( contactInfo . phone , "US" ) ?. number ?? "" ,
908
+ } ;
909
+ fetch ( "/accounts/me/" , {
910
+ method : "PATCH" ,
911
+ credentials : "include" ,
912
+ mode : "same-origin" ,
913
+ headers : {
914
+ Accept : "application/json" ,
915
+ "Content-Type" : "application/json" ,
916
+ "X-CSRFToken" : getCsrf ( ) ,
917
+ } ,
918
+ body : JSON . stringify ( {
919
+ profile,
920
+ } ) ,
921
+ } ) . then ( ( res ) => {
922
+ if ( ! res . ok ) {
923
+ throw new Error ( JSON . stringify ( res ) ) ;
924
+ } else {
925
+ dispatch ( updateContactInfoFrontend ( profile ) ) ;
926
+ }
927
+ } ) ;
928
+ } ;
0 commit comments