1+ /**
2+ * Copyright 2018 OpenStack Foundation
3+ * Licensed under the Apache License, Version 2.0 (the "License");
4+ * you may not use this file except in compliance with the License.
5+ * You may obtain a copy of the License at
6+ * http://www.apache.org/licenses/LICENSE-2.0
7+ * Unless required by applicable law or agreed to in writing, software
8+ * distributed under the License is distributed on an "AS IS" BASIS,
9+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10+ * See the License for the specific language governing permissions and
11+ * limitations under the License.
12+ * */
13+
14+ import {
15+ authErrorHandler ,
16+ createAction ,
17+ getRequest ,
18+ deleteRequest ,
19+ startLoading ,
20+ stopLoading
21+ } from "openstack-uicore-foundation/lib/utils/actions" ;
22+
23+ import T from "i18n-react" ;
24+ import { escapeFilterValue , getAccessTokenSafely } from "../utils/methods" ;
25+ import { snackbarErrorHandler , snackbarSuccessHandler } from "./base-actions" ;
26+ import { ERROR_CODE_404 } from "../utils/constants" ;
27+
28+ export const REQUEST_SPONSOR_CART = "REQUEST_SPONSOR_CART" ;
29+ export const RECEIVE_SPONSOR_CART = "RECEIVE_SPONSOR_CART" ;
30+ export const SPONSOR_CART_FORM_DELETED = "SPONSOR_CART_FORM_DELETED" ;
31+
32+ const customErrorHandler = ( err , res ) => ( dispatch , state ) => {
33+ const code = err . status ;
34+ dispatch ( stopLoading ( ) ) ;
35+ switch ( code ) {
36+ case ERROR_CODE_404 :
37+ break ;
38+ default :
39+ authErrorHandler ( err , res ) ( dispatch , state ) ;
40+ }
41+ } ;
42+
43+ export const getSponsorCart =
44+ ( term = "" ) =>
45+ async ( dispatch , getState ) => {
46+ const { currentSummitState, currentSponsorState } = getState ( ) ;
47+ const { currentSummit } = currentSummitState ;
48+ const {
49+ entity : { id : sponsorId }
50+ } = currentSponsorState ;
51+ const accessToken = await getAccessTokenSafely ( ) ;
52+ const summitTZ = currentSummit . time_zone . name ;
53+ const filter = [ ] ;
54+
55+ dispatch ( startLoading ( ) ) ;
56+
57+ if ( term ) {
58+ const escapedTerm = escapeFilterValue ( term ) ;
59+ filter . push ( `name=@${ escapedTerm } ,code=@${ escapedTerm } ` ) ;
60+ }
61+
62+ const params = {
63+ access_token : accessToken
64+ } ;
65+
66+ if ( filter . length > 0 ) {
67+ params [ "filter[]" ] = filter ;
68+ }
69+
70+ return getRequest (
71+ createAction ( REQUEST_SPONSOR_CART ) ,
72+ createAction ( RECEIVE_SPONSOR_CART ) ,
73+ `${ window . PURCHASES_API_URL } /api/v1/summits/${ currentSummit . id } /sponsors/${ sponsorId } /carts/current` ,
74+ customErrorHandler ,
75+ { term, summitTZ }
76+ ) ( params ) ( dispatch )
77+ . catch ( ( err ) => {
78+ console . error ( err ) ;
79+ } )
80+ . finally ( ( ) => {
81+ dispatch ( stopLoading ( ) ) ;
82+ } ) ;
83+ } ;
84+
85+
86+ export const deleteSponsorCartForm =
87+ ( formId ) => async ( dispatch , getState ) => {
88+ const { currentSummitState, currentSponsorState } = getState ( ) ;
89+ const { currentSummit } = currentSummitState ;
90+ const {
91+ entity : { id : sponsorId }
92+ } = currentSponsorState ;
93+ const accessToken = await getAccessTokenSafely ( ) ;
94+ const params = { access_token : accessToken } ;
95+
96+ dispatch ( startLoading ( ) ) ;
97+
98+ return deleteRequest (
99+ null ,
100+ createAction ( SPONSOR_CART_FORM_DELETED ) ( { formId } ) ,
101+ `${ window . PURCHASES_API_URL } /api/v1/summits/${ currentSummit . id } /sponsors/${ sponsorId } /sponsor-forms/${ formId } ` ,
102+ null ,
103+ snackbarErrorHandler
104+ ) ( params ) ( dispatch )
105+ . then ( ( ) => {
106+ dispatch (
107+ snackbarSuccessHandler ( {
108+ title : T . translate ( "general.success" ) ,
109+ html : T . translate ( "sponsor_forms.form_delete_success" )
110+ } )
111+ ) ;
112+ } )
113+ . finally ( ( ) => {
114+ dispatch ( stopLoading ( ) ) ;
115+ } ) ;
116+ } ;
0 commit comments