/
IncubatorSubscribeButton.tsx
149 lines (128 loc) · 4.15 KB
/
IncubatorSubscribeButton.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
import React, {useContext, useEffect, useState} from 'react';
import {styled} from '@mui/material/styles';
import {SCOPE_SC_UI} from '../../constants/Errors';
import {LoadingButton} from '@mui/lab';
import {FormattedMessage} from 'react-intl';
import classNames from 'classnames';
import {SCIncubatorType} from '@selfcommunity/types';
import {CacheStrategies, Logger} from '@selfcommunity/utils';
import {
SCContextType,
SCSubscribedIncubatorsManagerType,
SCUserContextType,
useSCContext,
useSCFetchIncubator,
useSCUser
} from '@selfcommunity/react-core';
import {useThemeProps} from '@mui/system';
const PREFIX = 'SCIncubatorSubscribeButton';
const classes = {
root: `${PREFIX}-root`
};
const Root = styled(LoadingButton, {
name: PREFIX,
slot: 'Root',
overridesResolver: (props, styles) => styles.root
})(({theme}) => ({}));
export interface IncubatorSubscribeButtonProps {
/**
* Overrides or extends the styles applied to the component.
* @default null
*/
className?: string;
/**
* Id of the incubator
* @default null
*/
incubatorId?: number;
/**
* Incubator
* @default null
*/
incubator?: SCIncubatorType;
/**
* onSubscribe callback
* @param incubator
* @param subscribed
*/
onSubscribe?: (incubator: SCIncubatorType, subscribed: boolean) => any;
/**
* Others properties
*/
[p: string]: any;
}
/**
* > API documentation for the Community-JS Incubator Subscribe Button component. Learn about the available props and the CSS API.
#### Import
```jsx
import {IncubatorSubscribeButton} from '@selfcommunity/react-ui';
```
#### Component Name
The name `SCIncubatorSubscribeButton` can be used when providing style overrides in the theme.
#### CSS
|Rule Name|Global class|Description|
|---|---|---|
|root|.SCIncubatorSubscribeButton-root|Styles applied to the root element.|
* @param inProps
*/
export default function IncubatorSubscribeButton(inProps: IncubatorSubscribeButtonProps): JSX.Element {
// PROPS
const props: IncubatorSubscribeButtonProps = useThemeProps({
props: inProps,
name: PREFIX
});
const {className, incubatorId, incubator, onSubscribe, ...rest} = props;
// CONTEXT
const scContext: SCContextType = useSCContext();
const scUserContext: SCUserContextType = useSCUser();
const scSubscribedIncubatorsManager: SCSubscribedIncubatorsManagerType = scUserContext.managers.incubators;
const authUserId = scUserContext.user ? scUserContext.user.id : null;
// STATE
const {scIncubator, setSCIncubator} = useSCFetchIncubator({
id: incubatorId,
incubator,
cacheStrategy: authUserId ? CacheStrategies.CACHE_FIRST : CacheStrategies.STALE_WHILE_REVALIDATE
});
const [subscribed, setSubscribed] = useState<boolean>(null);
useEffect(() => {
/**
* Call scFollowedIncubatorsManager.isFollowed inside an effect
* to avoid warning rendering child during update parent state
*/
if (authUserId) {
setSubscribed(scSubscribedIncubatorsManager.isSubscribed(scIncubator));
}
}, [authUserId, scSubscribedIncubatorsManager.isSubscribed]);
const subscribe = () => {
scSubscribedIncubatorsManager
.subscribe(scIncubator)
.then(() => {
onSubscribe && onSubscribe(scIncubator, !subscribed);
})
.catch((e) => {
Logger.error(SCOPE_SC_UI, e);
});
};
const handleSubscribeAction = () => {
if (!scUserContext.user) {
scContext.settings.handleAnonymousAction();
} else {
subscribe();
}
};
return (
<Root
size="small"
variant={subscribed ? 'contained' : 'outlined'}
onClick={handleSubscribeAction}
loading={scUserContext.user ? subscribed === null || scSubscribedIncubatorsManager.isLoading(scIncubator) : null}
className={classNames(classes.root, className)}
{...rest}>
{subscribed && scUserContext.user ? (
<FormattedMessage defaultMessage="ui.incubator.subscribeButton.button.subscribed" id="ui.incubator.subscribeButton.button.subscribed" />
) : (
<FormattedMessage defaultMessage="ui.incubator.subscribeButton.button.subscribe" id="ui.incubator.subscribeButton.button.subscribe" />
)}
</Root>
);
}