import { PaymentInput , TicketStatus , TicketTypeNode } from 'lib/apollo_graphql/__generated__/globalTypes' ; import { client } from 'lib/apollo_graphql/client' ; import { buyTicket } from 'lib/apollo_graphql/mutations/buyTicket' ; import { cancelTicket } from 'lib/apollo_graphql/mutations/cancelTicket' ; import { ChildCareProductType , getChildCareProducts } from 'lib/apollo_graphql/queries/getChildCareProducts' ; import { ConferenceProductType , getConferenceProducts } from 'lib/apollo_graphql/queries/getConferenceProducts' ; import { getMyTicket , MyTicketNode } from 'lib/apollo_graphql/queries/getMyTicket' ; import { getMyTickets , TicketNode } from 'lib/apollo_graphql/queries/getMyTickets' ; import { getSprintProducts , SprintProductType } from 'lib/apollo_graphql/queries/getSprintProducts' ; import { getTutorialProducts , TutorialProductType } from 'lib/apollo_graphql/queries/getTutorialProducts' ; import { getYoungCoderProducts , YoungCoderProductType } from 'lib/apollo_graphql/queries/getYoungCoderProducts' ; import * as _ from 'lodash' ; import { action , configure , observable , computed , toJS } from 'mobx' ; import { TicketStep } from './TicketStep' ; configure ( { enforceActions : 'observed' } ) ; export enum VALIDATION_ERROR_TYPE { NONE = 'NONE' , NOT_AGREED_TO_OPTIONS = 'NOT_AGREED_TO_OPTIONS' , NOT_AGREED_TO_TERMS = 'NOT_AGREED_TO_TERMS' , NO_OPTION_SELECTED = 'NO_OPTIONS_SELECTED' } export enum CREDITCARD_TYPE { DOMESTIC = 'DOMESTIC' , FOREIGN = 'FOREIGN' } const initialTicketInput = { isDomesticCard : true , cardNumber : '' , expiry : '' , birth : '' , pwd2digit : '' , amount : 0 , buyerEmail : '' , buyerName : '' , buyerTel : '' } ; export class TicketStore { @ observable isInitialized : boolean = false ; @ observable conferenceProducts : ConferenceProductType [ ] = [ ] ; @ observable tutorialProducts : TutorialProductType [ ] = [ ] ; @ observable sprintProducts : SprintProductType [ ] = [ ] ; @ observable childCareProducts : ChildCareProductType [ ] = [ ] ; @ observable youngCoderProducts : YoungCoderProductType [ ] = [ ] ; @ observable isPaying : boolean = false ; @ observable isSubmitPayment : boolean = false ; @ observable payingTicketTitle : string | null = '' ; @ observable price : number = 0 ; @ observable options : string | null = null ; @ observable productId : string = '' ; @ observable expiryMonth : string = '' ; @ observable expiryYear : string = '' ; @ observable ticketInput : PaymentInput = initialTicketInput ; @ observable myTickets : TicketNode [ ] = [ ] ; @ observable currentTicket : MyTicketNode | null = null ; @ observable ticketSteps : Map < string , TicketStep > = new Map ( [ ] ) ; @ action getMyConferenceTickets = ( ) => { return this . myTickets . filter ( myTicket => myTicket . status === TicketStatus . PAID && myTicket . product . type === TicketTypeNode . CONFERENCE ) ; } ; @ computed get myConferenceTicket ( ) { const tickets = this . myTickets . filter ( myTicket => myTicket . status === TicketStatus . PAID && myTicket . product . type === TicketTypeNode . CONFERENCE ) ; if ( _ . isEmpty ( tickets ) ) { return null ; } return tickets [ 0 ] ; } @ action setConferenceProducts = ( conferenceProducts ) => { this . conferenceProducts = conferenceProducts ; } ; @ action setTutorialProducts = ( tutorialProducts ) => { this . tutorialProducts = tutorialProducts ; } ; @ action setSprintProducts = ( sprintProducts ) => { this . sprintProducts = sprintProducts ; } ; @ action setChildCareProducts = ( childCareProducts ) => { this . childCareProducts = childCareProducts ; } ; @ action setYoungCoderProducts = ( youngCoderProducts ) => { this . youngCoderProducts = youngCoderProducts ; } ; @ action setMyTickets = ( myTickets ) => { this . myTickets = myTickets ; } ; @ action setCurrentTicket = ( ticket ) => { this . currentTicket = ticket ; } ; @ action setIsDomesticCard = ( isDomesticCard ) => { this . ticketInput . isDomesticCard = isDomesticCard ; } ; @ action setPrice = ( price ) => { this . price = price ; } ; @ action setCardNumber = ( cardNumber ) => { this . ticketInput . cardNumber = cardNumber ; } ; @ action setExpiry = ( ) => { this . ticketInput . expiry = `20${ this . expiryYear }-${ this . expiryMonth }` ; } ; @ action setExpiryMonth = ( expiryMonth ) => { this . expiryMonth = expiryMonth ; } ; @ action setExpiryYear = ( expiryYear ) => { this . expiryYear = expiryYear ; } ; @ action setBirth = ( birth ) => { this . ticketInput . birth = birth ; } ; @ action setPwd2Digit = ( pwd2Digit ) => { this . ticketInput . pwd2digit = pwd2Digit ; } ; @ action setIsSubmitPayment = ( isSubmitPayment ) => { this . isSubmitPayment = isSubmitPayment ; } ; @ action initialize = async ( ) => { if ( this . isInitialized ) return ; await this . retrieveConferenceProducts ( ) ; await this . retrieveMyTickets ( ) ; this . isInitialized = true ; } ; @ action cleanupPaymentInfo = ( ) => { this . expiryMonth = '' ; this . expiryYear = '' ; this . ticketInput = initialTicketInput ; } ; @ action retrieveConferenceProducts = async ( ) => { const { data } = await getConferenceProducts ( client ) ( { } ) ; if ( data ) this . setConferenceProducts ( data . conferenceProducts as ConferenceProductType [ ] ) ; } ; @ action retrieveTutorialProducts = async ( ) => { const { data } = await getTutorialProducts ( client ) ( { } ) ; if ( data ) this . setTutorialProducts ( data . tutorialProducts as TutorialProductType [ ] ) ; } ; @ action retrieveSprintProducts = async ( ) => { const { data } = await getSprintProducts ( client ) ( { } ) ; if ( data ) this . setSprintProducts ( data . sprintProducts as SprintProductType [ ] ) ; } ; @ action retrieveChildCareProducts = async ( ) => { const { data } = await getChildCareProducts ( client ) ( { } ) ; if ( data ) this . setChildCareProducts ( data . childCareProducts as ChildCareProductType [ ] ) ; } ; @ action retrieveYoungCoderProducts = async ( ) => { const { data } = await getYoungCoderProducts ( client ) ( { } ) ; if ( data ) this . setYoungCoderProducts ( data . youngCoderProducts as YoungCoderProductType [ ] ) ; } ; @ action retrieveMyTickets = async ( ) => { const { data } = await getMyTickets ( client ) ( { } ) ; this . setMyTickets ( data . myTickets as TicketNode [ ] ) ; } ; @ action async retrieveMyTicket ( id ) { const { data } = await getMyTicket ( client ) ( { id } ) ; this . setCurrentTicket ( data . myTicket as MyTicketNode ) ; } @ action setPayingTicket = ( ticketStep ) => { this . isPaying = true ; this . productId = ticketStep . ticketId ; this . payingTicketTitle = ticketStep . ticketTitle ; if ( ticketStep . ticketOption ) { this . options = JSON . stringify ( ticketStep . ticketOption ) ; } } ; @ action setTicketStep = ( id , title ) => { const newTicketStep = new TicketStep ( ) ; newTicketStep . setTicketId ( id ) ; newTicketStep . setTicketTitle ( title || '' ) ; this . ticketSteps . set ( newTicketStep . ticketId , newTicketStep ) ; } ; @ action getTicketStep = ( id ) => { return this . ticketSteps . get ( id ) ; } ; @ action clearTicketSteps = ( ) => { this . ticketSteps . clear ( ) ; } ; @ action clearTicketOptions = ( ) => { this . options = null ; } ; @ action getIsTicketStepExist = ( id ) => { return ! ! this . getTicketStep ( id ) ; } ; @ action payTicket = async ( ) => { try { this . setIsSubmitPayment ( true ) ; this . setExpiry ( ) ; this . ticketInput . amount = this . price ; return await buyTicket ( client ) ( { options : this . options , payment : toJS ( this . ticketInput ) , productId : this . productId } ) ; } catch ( err ) { this . setIsSubmitPayment ( false ) ; return err ; } } ; @ action cancelTicket = async ( ticketId ) => { try { const data = await cancelTicket ( client ) ( { ticketId } ) ; this . retrieveMyTickets ( ) ; return data ; } catch ( err ) { return err ; } } ; } export default new TicketStore ( ) ;