forked from vtex-apps/wishlist
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContent.tsx
259 lines (237 loc) · 7.21 KB
/
Content.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
import React, { Component, ReactNode } from 'react'
import classNames from 'classnames'
import { append, filter, indexOf, map, path, remove, update } from 'ramda'
import { compose, withApollo, WithApolloClient } from 'react-apollo'
import { isMobile } from 'react-device-detect'
import { InjectedIntlProps, injectIntl, defineMessages } from 'react-intl'
import { getListsFromLocaleStorage, updateList } from '../../GraphqlClient'
import CreateList from '../Form/CreateList'
import Header from '../Header'
import ListDetails from '../ListDetails'
import ListItem from '../ListItem'
import renderLoading from '../Loading'
import Footer from './Footer'
import styles from '../../wishList.css'
const DEFAULT_LIST_INDEX = 0
const QUANTITY_WITH_ONLY_DEFAULT_LIST = 1
const messages = defineMessages({
addToList: {
defaultMessage: '',
id: 'store/wishlist-add-to-list',
},
})
interface AddToListContentProps
extends InjectedIntlProps,
WithApolloClient<{}> {
product: ListItem
onClose: () => void
onAddToListsSuccess: () => void
onAddToListsFail: () => void
}
interface AddToListContentState {
isLoading: boolean
isAdding?: boolean
showCreateList?: boolean
lists: List[]
changedLists: number[]
showListDetails?: boolean
selectedListId: string
}
class AddToListContent extends Component<
AddToListContentProps,
AddToListContentState
> {
public state: AddToListContentState = {
changedLists: [],
isLoading: true,
lists: [],
selectedListId: '',
}
public componentDidMount(): void {
const { client } = this.props
getListsFromLocaleStorage(client)
.then((response: ResponseList[]) => {
const lists = map(item => item.data.list || {}, response)
this.setState({ isLoading: false, lists })
})
.catch(() => this.setState({ isLoading: false }))
}
public render(): ReactNode {
const { onClose, intl } = this.props
const {
showCreateList,
showListDetails,
selectedListId,
changedLists,
isAdding,
lists,
} = this.state
const className = classNames(styles.contentContainer, 'overflow-y-auto', {
[styles.contentContainerMobile]: isMobile,
[styles.contentContainerDesktop]: !isMobile,
})
return (
<div className={`${styles.addToListContent} z-4 bg-base`}>
<Header
title={intl.formatMessage(messages.addToList)}
onClose={onClose}
action={() => this.setState({ showCreateList: true })}
/>
<div className={className}>{this.renderMainContent()}</div>
{lists && lists.length > QUANTITY_WITH_ONLY_DEFAULT_LIST && (
<Footer
changedLists={changedLists}
isLoading={isAdding}
onClick={this.handleAddProductTochangedLists}
/>
)}
{showCreateList && (
<CreateList
onFinishAdding={this.handleListCreated}
onClose={() => this.setState({ showCreateList: false })}
/>
)}
{showListDetails && (
<ListDetails
listId={selectedListId}
onClose={this.handleOnCloseListDetails}
onDeleted={this.handleOnDeleted}
/>
)}
</div>
)
}
private handleListCreated = (list: List): void => {
const { lists } = this.state
this.setState({ showCreateList: false, lists: append(list, lists) })
}
private handleAddProductTochangedLists = (): void => {
const {
client,
onClose,
onAddToListsSuccess,
onAddToListsFail,
} = this.props
const { lists, changedLists } = this.state
this.setState({ isAdding: true })
Promise.all(
map(index => {
const { id } = lists[index]
return updateList(client, id || '', { ...lists[index] })
}, changedLists)
)
.then(() => {
onClose()
onAddToListsSuccess()
})
.catch(error => {
console.error(error)
onClose()
onAddToListsFail()
})
}
private containsProduct = (list: List): boolean => {
const { product } = this.props
return (
filter(
(item: ListItem) =>
item.productId === product.productId && item.skuId === product.skuId,
list.items || []
).length > 0
)
}
private handleUpdateChangedLists = (
listIndex: number,
isSelected?: boolean
): void => {
const { changedLists } = this.state
if (listIndex !== DEFAULT_LIST_INDEX) {
const index = indexOf(listIndex, changedLists)
const listsUpdated = !isSelected
? this.addProductToList(listIndex)
: this.removeProductFromList(listIndex)
let changedListsUpdated: number[] = []
if (index !== -1) {
changedListsUpdated = remove(index, 1, changedLists)
} else {
changedListsUpdated = append(listIndex, changedLists)
}
this.setState({ changedLists: changedListsUpdated, lists: listsUpdated })
}
}
private handleShowListDetails = (index: number): void => {
const { lists } = this.state
const listId: string = path(['id'], lists[index]) || ''
this.setState({
selectedListId: listId,
showListDetails: true,
})
}
private handleOnDeleted = (listId: string): void => {
const { lists } = this.state
const listsWithDeletedList = filter(
list => path(['id'], list) !== listId,
lists
)
this.setState({ lists: listsWithDeletedList })
}
private addProductToList = (index: number): List[] => {
const { product } = this.props
const { lists } = this.state
const list = lists[index]
const listUpdated = { ...list, items: append(product, list.items || []) }
return update(index, listUpdated, lists)
}
private removeProductFromList = (index: number): List[] => {
const {
product: { productId, skuId },
} = this.props
const { lists } = this.state
const list = lists[index]
const items = filter(
(item: ListItem) => item.productId !== productId || item.skuId !== skuId,
list.items || []
)
return update(index, { ...list, items }, lists)
}
private handleOnCloseListDetails = (listUpdated?: List): void => {
const { lists } = this.state
const listsUpdated = listUpdated
? map(list => {
if (list.id === listUpdated.id) {
return listUpdated
}
return list
}, lists)
: lists
this.setState({ showListDetails: false, lists: listsUpdated })
}
private renderSwitchLists = (): ReactNode => {
const { lists } = this.state
return (
<div className={`${styles.addToListListsToSwitch} flex flex-column`}>
{lists.map((list: List, index: number) => (
<ListItem
key={index}
id={index}
list={list}
isDefault={index === DEFAULT_LIST_INDEX}
isSelected={
index === DEFAULT_LIST_INDEX || this.containsProduct(list)
}
onClick={this.handleShowListDetails}
onSelected={this.handleUpdateChangedLists}
/>
))}
</div>
)
}
private renderMainContent = (): ReactNode => {
const { isLoading } = this.state
return isLoading ? renderLoading() : this.renderSwitchLists()
}
}
export default compose(
withApollo,
injectIntl
)(AddToListContent)