forked from vtex-apps/wishlist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUpdateList.tsx
120 lines (109 loc) · 2.91 KB
/
UpdateList.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
118
119
120
import { map } from 'ramda'
import React, { Component } from 'react'
import { compose, withApollo, WithApolloClient } from 'react-apollo'
import { InjectedIntlProps, injectIntl, defineMessages } from 'react-intl'
import { withToast } from 'vtex.styleguide'
import { updateList } from '../../GraphqlClient'
import Header from '../Header'
import FormView from './FormView'
import ListForm from './ListForm'
import styles from '../../wishList.css'
interface UpdateListProps extends InjectedIntlProps, WithApolloClient<{}> {
list: List
onFinishUpdate: (list: List) => void
onClose: () => void
showToast?: (input: ToastInput) => void
}
interface UpdateListState {
isLoading?: boolean
}
const messages = defineMessages({
optionConfiguration: {
defaultMessage: '',
id: 'store/wishlist-option-configuration',
},
save: {
defaultMessage: '',
id: 'store/wishlist-save',
},
listUpdate: {
defaultMessage: '',
id: 'store/wishlist-list-updated',
},
})
class UpdateList extends Component<UpdateListProps, UpdateListState> {
public state: UpdateListState = {}
private isComponentMounted: boolean = false
public componentDidMount() {
this.isComponentMounted = true
}
public componentWillUnmount() {
this.isComponentMounted = false
}
public render() {
const { onClose, intl, list } = this.props
const { isLoading } = this.state
return (
<FormView onClose={onClose}>
<div className={`${styles.updateList}`}>
<Header
title={intl.formatMessage(messages.optionConfiguration)}
onClose={onClose}
showIconBack
/>
<ListForm
list={list}
buttonLabel={intl.formatMessage(messages.save)}
onSubmit={this.handleSubmit}
isLoading={isLoading}
/>
</div>
</FormView>
)
}
private itemsToItemsInput = (items: ListItem[] | undefined): ListItem[] =>
items
? map(
({ id, productId, skuId, quantity }) => ({
id,
productId,
skuId,
quantity,
}),
items
)
: []
private handleSubmit = ({ name, isPublic }: List): void => {
const {
client,
list: { id, items },
showToast,
intl,
} = this.props
this.setState({ isLoading: true })
updateList(client, id || '', {
isPublic,
items: this.itemsToItemsInput(items),
name,
})
.then((response: ResponseList) => {
if (this.isComponentMounted) {
this.setState({ isLoading: false })
}
if (showToast) {
showToast({
message: intl.formatMessage(messages.listUpdate),
})
}
this.props.onFinishUpdate({ ...response.data.updateList, items })
})
.catch((err: {}) => {
console.error(err)
})
}
}
export default compose(
withToast,
withApollo,
injectIntl
)(UpdateList)