forked from vtex-apps/wishlist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCreateList.tsx
117 lines (103 loc) · 2.93 KB
/
CreateList.tsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import React, { Component } from 'react'
import { isMobile } from 'react-device-detect'
import { compose, withApollo, WithApolloClient, graphql } from 'react-apollo'
import { InjectedIntlProps, injectIntl, defineMessages } from 'react-intl'
import { withRuntimeContext, withSession } from 'vtex.render-runtime'
import { session } from 'vtex.store-resources/Queries'
import { getProfile } from '../../utils/profile'
import { createList } from '../../GraphqlClient'
import Header from '../Header'
import FormView from './FormView'
import ListForm from './ListForm'
import styles from '../../wishList.css'
interface CreateListProps extends InjectedIntlProps, WithApolloClient<{}> {
onFinishAdding: (list: List) => void
onClose: () => void
runtime: Runtime
session: Session
}
interface CreateListState {
isLoading?: boolean
}
const messages = defineMessages({
new: {
defaultMessage: '',
id: 'store/wishlist-new',
},
addButton: {
defaultMessage: '',
id: 'store/wishlist-add-button',
},
})
class CreateList extends Component<CreateListProps, CreateListState> {
public state: CreateListState = {}
private isComponentMounted: boolean = false
public componentDidMount() {
this.isComponentMounted = true
}
public componentWillUnmount() {
this.isComponentMounted = false
}
public render() {
const { onClose, intl } = this.props
const { isLoading } = this.state
return (
<FormView onClose={onClose}>
<div className={`${styles.createList} bg-base h-100`}>
<Header
title={intl.formatMessage(messages.new)}
onClose={onClose}
showIconBack
/>
<ListForm
buttonLabel={intl.formatMessage(messages.addButton)}
onSubmit={this.handleSubmit}
isLoading={isLoading}
/>
</div>
</FormView>
)
}
private handleSubmit = (listData: List): void => {
const { client, session } = this.props
const profile = getProfile(session)
this.setState({ isLoading: true })
createList(client, {
...listData,
items: [],
isEditable: true,
owner: profile ? profile.email : '',
})
.then((response: ResponseList) => {
const list = response.data.createList
if (list) {
!isMobile && this.redirectToList(list.id)
this.props.onFinishAdding(list)
}
if (this.isComponentMounted) {
this.setState({ isLoading: false })
}
})
.catch(error => {
console.error(error)
})
}
private redirectToList = (id: string | undefined): void => {
const {
runtime: { setQuery },
} = this.props
setQuery({ listId: id }, { merge: false, replace: true })
}
}
const options = {
name: 'session',
options: () => ({ ssr: false }),
}
export default withSession()(
compose(
injectIntl,
withApollo,
withRuntimeContext,
graphql(session, options)
)(CreateList)
)