UNPKG

@kiwicom/smart-faq

Version:

97 lines (81 loc) 2.26 kB
// @flow import * as React from 'react'; export type GuaranteeChatBookingInfo = {| bid: ?number, status: ?string, departureCity: ?string, departureAirport: ?string, arrivalCity: ?string, arrivalAirport: ?string, phone: ?string, email: ?string, firstName: ?string, lastName: ?string, |}; export type ChatConfig = { [key: string]: string, } | null; type Props = {| enableChat?: boolean, chatConfig: ChatConfig, children: React.Node, |}; export type State = {| showGuaranteeChat: boolean, guaranteeChatBookingInfo: ?GuaranteeChatBookingInfo, enableChat: boolean, chatConfig: ChatConfig, isChatActive: boolean, onSetBookingInfo: GuaranteeChatBookingInfo => void, toggleIsChatActive: (isChatActive: boolean) => void, toggleGuaranteeChat: (showGuaranteeChat: boolean) => void, |}; const initialState = { showGuaranteeChat: false, guaranteeChatBookingInfo: null, chatConfig: {}, isChatActive: false, }; export const GuaranteeChatContext: React.Context<State> = React.createContext({ ...initialState, onSetBookingInfo: () => {}, toggleGuaranteeChat: () => {}, toggleIsChatActive: () => {}, enableChat: true, }); class GuaranteeChatInfo extends React.Component<Props, State> { static getDerivedStateFromProps(nextProps: Props) { return { enableChat: nextProps.enableChat, chatConfig: nextProps.chatConfig, }; } constructor(props: Props) { super(props); this.state = { ...initialState, onSetBookingInfo: this.onSetBookingInfo, toggleGuaranteeChat: this.toggleGuaranteeChat, toggleIsChatActive: this.toggleIsChatActive, chatConfig: props.chatConfig, enableChat: props.enableChat, }; } onSetBookingInfo = (guaranteeChatBookingInfo: GuaranteeChatBookingInfo) => { this.setState({ guaranteeChatBookingInfo }); }; toggleIsChatActive = (isChatActive: boolean) => { this.setState({ isChatActive }); }; toggleGuaranteeChat = (showGuaranteeChat: boolean) => { this.setState({ showGuaranteeChat }); }; render() { return ( <GuaranteeChatContext.Provider value={this.state}> {this.props.children} </GuaranteeChatContext.Provider> ); } } export default GuaranteeChatInfo;