This repository has been archived by the owner on Mar 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added subscription management buttons on homepage #360
Signed-off-by: RaenonX <[email protected]>
- Loading branch information
Showing
36 changed files
with
741 additions
and
70 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Submodule api-def
updated
9 files
+4 −0 | api.ts | |
+2 −0 | api/info/home/response.ts | |
+2 −22 | api/user/subscription/key.ts | |
+22 −0 | api/user/subscription/key/type.ts | |
+28 −0 | api/user/subscription/key/utils.test.ts | |
+8 −0 | api/user/subscription/key/utils.ts | |
+14 −0 | jest.config.ts | |
+363 −2 | package-lock.json | |
+4 −0 | package.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import React from 'react'; | ||
|
||
import Button from 'react-bootstrap/Button'; | ||
import Spinner from 'react-bootstrap/Spinner'; | ||
|
||
import {IconNotSubscribed, IconSubscribed} from '../../icons'; | ||
import styles from './main.module.css'; | ||
import {SubscribeButtonCommonProps} from './type'; | ||
|
||
|
||
type Props = SubscribeButtonCommonProps; | ||
|
||
export const SubscribeButtonIconOnly = ({onClick, state, disabled}: Props) => { | ||
const {subscribed, updating} = state; | ||
|
||
const className = | ||
`${styles['subscribe-button']} ` + | ||
`${subscribed ? styles['subscribe-button-as-icon-enabled'] : styles['subscribe-button-as-icon-disabled']}`; | ||
|
||
return ( | ||
<Button variant="outline-secondary" className={className} onClick={onClick} disabled={updating || disabled}> | ||
{updating ? | ||
<Spinner animation="border"/> : | ||
(subscribed ? <IconSubscribed/> : <IconNotSubscribed/>) | ||
} | ||
</Button> | ||
); | ||
}; |
19 changes: 19 additions & 0 deletions
19
src/components/elements/common/button/subscribe/main.module.css
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
1 change: 1 addition & 0 deletions
1
src/components/elements/common/button/subscribe/main.module.css.map
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
26 changes: 26 additions & 0 deletions
26
src/components/elements/common/button/subscribe/main.module.scss
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
@import "../../../../../../styles/colors"; | ||
|
||
button { | ||
&.subscribe-button { | ||
border: 0; | ||
border-radius: 50%; | ||
font-size: 1.2rem; | ||
padding: 0; | ||
|
||
// 2rem is the exact size of a normal spinner | ||
height: 2rem; | ||
width: 2rem; | ||
|
||
&:disabled { | ||
background-color: $color-dark-red; | ||
} | ||
|
||
&.subscribe-button-as-icon-enabled { | ||
color: white; | ||
} | ||
|
||
&.subscribe-button-as-icon-disabled { | ||
color: $color-bw-170; | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
src/components/elements/common/button/subscribe/main.test.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import React from 'react'; | ||
|
||
import {screen} from '@testing-library/react'; | ||
import userEvent from '@testing-library/user-event'; | ||
import {ObjectId} from 'mongodb'; | ||
|
||
import {renderReact} from '../../../../../../test/render/main'; | ||
import {ApiResponseCode, SubscriptionKey} from '../../../../../api-def/api'; | ||
import {translation as translationEN} from '../../../../../i18n/translations/en/translation'; | ||
import {ApiRequestSender} from '../../../../../utils/services/api/requestSender'; | ||
import {SubscribeButton} from './main'; | ||
|
||
|
||
describe('Subscribe button', () => { | ||
it('shows login required on click if un-authed', async () => { | ||
renderReact(() => ( | ||
<SubscribeButton defaultSubscribed={false} subscriptionKey={{type: 'const', name: 'ALL_MISC'}}/> | ||
)); | ||
|
||
const subButton = screen.getByText('', {selector: 'i.bi-bell-slash'}); | ||
|
||
userEvent.click(subButton); | ||
|
||
expect(await screen.findByText(translationEN.message.error.auth.loginRequired)).toBeInTheDocument(); | ||
expect(screen.queryByText('', {selector: 'div.spinner-border'})).not.toBeInTheDocument(); | ||
}); | ||
|
||
it('sends subscription update request', async () => { | ||
const id = new ObjectId().toHexString(); | ||
const subKey: SubscriptionKey = {type: 'const', name: 'ALL_MISC'}; | ||
const fnUpdateSubscription = jest.spyOn(ApiRequestSender, 'addSubscription').mockResolvedValue({ | ||
code: ApiResponseCode.SUCCESS, | ||
success: true, | ||
}); | ||
|
||
renderReact( | ||
() => <SubscribeButton defaultSubscribed={false} subscriptionKey={subKey}/>, | ||
{user: {id}}, | ||
); | ||
|
||
const subButton = screen.getByText('', {selector: 'i.bi-bell-slash'}); | ||
|
||
userEvent.click(subButton); | ||
|
||
expect(screen.getByText('', {selector: 'div.spinner-border'})).toBeInTheDocument(); | ||
expect(fnUpdateSubscription).toHaveBeenCalledWith(id, subKey); | ||
|
||
expect(await screen.findByText('', {selector: 'i.bi-bell'})).toBeInTheDocument(); | ||
}); | ||
|
||
it('shows as text', async () => { | ||
renderReact(() => ( | ||
<SubscribeButton defaultSubscribed={false} subscriptionKey={{type: 'const', name: 'ALL_MISC'}} asIcon={false}/> | ||
)); | ||
|
||
expect(screen.getByText(translationEN.misc.subscription.add)).toBeInTheDocument(); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import React from 'react'; | ||
|
||
import {useSession} from 'next-auth/react'; | ||
|
||
import {isFailedResponse, SubscriptionKey} from '../../../../../api-def/api'; | ||
import {useI18n} from '../../../../../i18n/hook'; | ||
import {ApiRequestSender} from '../../../../../utils/services/api/requestSender'; | ||
import {ModalFixedContent} from '../../modal/fix'; | ||
import {ModalStateFix} from '../../modal/types'; | ||
import {SubscribeButtonIconOnly} from './icon'; | ||
import {SubscribeButtonText} from './text'; | ||
import {SubscribeButtonState} from './type'; | ||
|
||
|
||
export type SubscribeButtonProps = { | ||
subscriptionKey: SubscriptionKey, | ||
defaultSubscribed: boolean, | ||
asIcon?: boolean, | ||
disabled?: boolean, | ||
onClick?: () => void, | ||
}; | ||
|
||
export const SubscribeButton = ({ | ||
subscriptionKey, | ||
defaultSubscribed, | ||
asIcon = true, | ||
disabled, | ||
onClick, | ||
}: SubscribeButtonProps) => { | ||
const {status, data} = useSession(); | ||
const {t} = useI18n(); | ||
|
||
const [state, setState] = React.useState<SubscribeButtonState>({ | ||
subscribed: defaultSubscribed, | ||
updating: false, | ||
}); | ||
const [modalState, setModalState] = React.useState<ModalStateFix>({ | ||
show: false, | ||
title: '', | ||
}); | ||
const {subscribed} = state; | ||
|
||
const fnUpdateSubscription = subscribed ? ApiRequestSender.removeSubscription : ApiRequestSender.addSubscription; | ||
|
||
const onClickInternal = () => { | ||
if (status === 'unauthenticated') { | ||
setModalState({...modalState, show: true}); | ||
return; | ||
} | ||
|
||
setState({...state, updating: true}); | ||
|
||
fnUpdateSubscription(data?.user.id.toString() || '', subscriptionKey) | ||
.then((response) => { | ||
if (isFailedResponse(response)) { | ||
setModalState({...modalState, show: true}); | ||
return; | ||
} | ||
|
||
setState({subscribed: !subscribed, updating: false}); | ||
|
||
if (onClick) { | ||
onClick(); | ||
} | ||
}); | ||
}; | ||
|
||
return ( | ||
<> | ||
<ModalFixedContent state={modalState} setState={setModalState}> | ||
{t((t) => t.message.error.auth.loginRequired)} | ||
</ModalFixedContent> | ||
{ | ||
asIcon ? | ||
<SubscribeButtonIconOnly onClick={onClickInternal} state={state} disabled={disabled}/> : | ||
<SubscribeButtonText onClick={onClickInternal} state={state} disabled={disabled}/> | ||
} | ||
</> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import React from 'react'; | ||
|
||
import Button from 'react-bootstrap/Button'; | ||
|
||
import {useI18n} from '../../../../../i18n/hook'; | ||
import {SubscribeButtonCommonProps} from './type'; | ||
|
||
|
||
type Props = SubscribeButtonCommonProps; | ||
|
||
export const SubscribeButtonText = ({onClick, state, disabled}: Props) => { | ||
const {t} = useI18n(); | ||
|
||
const {subscribed, updating} = state; | ||
|
||
return ( | ||
<Button | ||
variant={subscribed ? 'outline-danger' : 'outline-warning'} | ||
onClick={onClick} | ||
disabled={updating || disabled} | ||
> | ||
{subscribed ? | ||
t((t) => t.misc.subscription.remove) : | ||
t((t) => t.misc.subscription.add)} | ||
</Button> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
export type SubscribeButtonState = { | ||
subscribed: boolean, | ||
updating: boolean, | ||
}; | ||
|
||
export type SubscribeButtonCommonProps = { | ||
onClick: () => void, | ||
state: SubscribeButtonState, | ||
disabled?: boolean, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.