-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathpresenter.js
40 lines (36 loc) · 1.07 KB
/
presenter.js
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
export const PresentOrder = (formatters) => ({loading, error, data, hasChanges}) => ({
backButton: {
label: 'Back',
disabled: loading,
},
saveButton: {
label: 'Save',
disabled: !hasChanges,
},
data: isEmpty(data)
? error
? presentError(error)
: loading
? presentLoader()
: presentError({message: 'Order is empty', code: 'should never happen, fire your server devs'})
: presentContent(data, formatters)
})
const presentLoader = () => ({
state: 'loading',
text: 'Loading...',
})
const presentError = (error) => ({
state: 'error',
title: 'Order loading failed with error',
description: `${error.message} (${error.code})`,
})
const presentContent = ({createdDate, updatedDate, sum, user, ...rest}, {formatTime, formatNumber}) => ({
...rest,
state: 'content',
user: user?.name,
billingAddress: user?.billingAddress,
createdDate: createdDate ? formatTime(createdDate) : '',
updatedDate: updatedDate ? formatTime(updatedDate) : '',
sum: formatNumber(sum),
})
const isEmpty = (data) => !Object.keys(data).length