2022-01-10 14:17:52 -08:00
import { Map as ImmutableMap } from 'immutable' ;
2021-11-01 22:35:35 -07:00
import PropTypes from 'prop-types' ;
2022-01-10 14:17:52 -08:00
import React from 'react' ;
2021-11-02 09:11:33 -07:00
import ImmutablePropTypes from 'react-immutable-proptypes' ;
import ImmutablePureComponent from 'react-immutable-pure-component' ;
2021-11-01 22:35:35 -07:00
import { injectIntl , FormattedMessage , defineMessages } from 'react-intl' ;
2022-01-10 14:17:52 -08:00
import { connect } from 'react-redux' ;
import { createApp } from 'soapbox/actions/apps' ;
import { obtainOAuthToken } from 'soapbox/actions/oauth' ;
2021-11-02 09:11:33 -07:00
import {
SimpleForm ,
TextInput ,
SimpleTextarea ,
FieldsGroup ,
} from 'soapbox/features/forms' ;
2022-01-10 14:17:52 -08:00
import Accordion from 'soapbox/features/ui/components/accordion' ;
import Column from 'soapbox/features/ui/components/column' ;
2021-11-02 09:11:33 -07:00
import { getBaseURL } from 'soapbox/utils/accounts' ;
import { getFeatures } from 'soapbox/utils/features' ;
2021-11-01 22:35:35 -07:00
const messages = defineMessages ( {
heading : { id : 'column.app_create' , defaultMessage : 'Create app' } ,
namePlaceholder : { id : 'app_create.name_placeholder' , defaultMessage : 'e.g. \'Soapbox\'' } ,
2021-11-02 09:11:33 -07:00
scopesPlaceholder : { id : 'app_create.scopes_placeholder' , defaultMessage : 'e.g. \'read write follow\'' } ,
2021-11-01 22:35:35 -07:00
} ) ;
2021-11-02 09:11:33 -07:00
const mapStateToProps = state => {
const me = state . get ( 'me' ) ;
const account = state . getIn ( [ 'accounts' , me ] ) ;
const instance = state . get ( 'instance' ) ;
const features = getFeatures ( instance ) ;
return {
account ,
defaultScopes : features . scopes ,
} ;
} ;
export default @ connect ( mapStateToProps )
@ injectIntl
class CreateApp extends ImmutablePureComponent {
2021-11-01 22:35:35 -07:00
static propTypes = {
intl : PropTypes . object . isRequired ,
2021-11-02 09:11:33 -07:00
dispatch : PropTypes . func . isRequired ,
account : ImmutablePropTypes . map . isRequired ,
defaultScopes : PropTypes . string ,
2021-11-01 22:35:35 -07:00
}
2021-11-02 09:11:33 -07:00
initialState = ( ) => {
return {
params : ImmutableMap ( {
client _name : '' ,
redirect _uris : 'urn:ietf:wg:oauth:2.0:oob' ,
scopes : '' ,
website : '' ,
} ) ,
app : null ,
token : null ,
isLoading : false ,
explanationExpanded : true ,
} ;
}
state = this . initialState ( )
handleCreateApp = ( ) => {
const { dispatch , account } = this . props ;
const { params } = this . state ;
const baseURL = getBaseURL ( account ) ;
return dispatch ( createApp ( params . toJS ( ) , baseURL ) )
. then ( app => this . setState ( { app } ) ) ;
}
handleCreateToken = ( ) => {
const { dispatch , account } = this . props ;
const { app , params : appParams } = this . state ;
const baseURL = getBaseURL ( account ) ;
const tokenParams = {
client _id : app . client _id ,
client _secret : app . client _secret ,
redirect _uri : appParams . get ( 'redirect_uri' ) ,
grant _type : 'client_credentials' ,
scope : appParams . get ( 'scopes' ) ,
} ;
return dispatch ( obtainOAuthToken ( tokenParams , baseURL ) )
. then ( token => this . setState ( { token } ) ) ;
}
handleSubmit = e => {
this . setState ( { isLoading : true } ) ;
this . handleCreateApp ( )
. then ( this . handleCreateToken )
. then ( ( ) => {
this . scrollToTop ( ) ;
this . setState ( { isLoading : false } ) ;
} ) . catch ( error => {
console . error ( error ) ;
this . setState ( { isLoading : false } ) ;
} ) ;
}
setParam = ( key , value ) => {
const { params } = this . state ;
const newParams = params . set ( key , value ) ;
this . setState ( { params : newParams } ) ;
}
handleParamChange = key => {
return e => {
this . setParam ( key , e . target . value ) ;
} ;
}
resetState = ( ) => {
this . setState ( this . initialState ( ) ) ;
}
handleReset = e => {
this . resetState ( ) ;
this . scrollToTop ( ) ;
}
toggleExplanation = expanded => {
this . setState ( { explanationExpanded : expanded } ) ;
}
scrollToTop = ( ) => {
window . scrollTo ( { top : 0 , behavior : 'smooth' } ) ;
}
renderResults = ( ) => {
2021-11-01 22:35:35 -07:00
const { intl } = this . props ;
2021-11-02 09:11:33 -07:00
const { app , token , explanationExpanded } = this . state ;
2021-11-01 22:35:35 -07:00
return (
< Column heading = { intl . formatMessage ( messages . heading ) } >
< SimpleForm >
2021-11-02 09:11:33 -07:00
< FieldsGroup >
< Accordion
headline = { < FormattedMessage id = 'app_create.results.explanation_title' defaultMessage = 'App created successfully' / > }
expanded = { explanationExpanded }
onToggle = { this . toggleExplanation }
>
< FormattedMessage
id = 'app_create.results.explanation_text'
defaultMessage = 'You created a new app and token! Please copy the credentials somewhere; you will not see them again after navigating away from this page.'
/ >
< / A c c o r d i o n >
< / F i e l d s G r o u p >
< FieldsGroup >
< div className = 'code-editor' >
< SimpleTextarea
label = { < FormattedMessage id = 'app_create.results.app_label' defaultMessage = 'App' / > }
value = { JSON . stringify ( app , null , 2 ) }
rows = { 10 }
readOnly
/ >
< / d i v >
< / F i e l d s G r o u p >
< FieldsGroup >
< div className = 'code-editor' >
< SimpleTextarea
label = { < FormattedMessage id = 'app_create.results.token_label' defaultMessage = 'OAuth token' / > }
value = { JSON . stringify ( token , null , 2 ) }
rows = { 10 }
readOnly
/ >
< / d i v >
< / F i e l d s G r o u p >
< div className = 'actions' >
< button name = 'button' onClick = { this . handleReset } className = 'btn button button-primary' >
< FormattedMessage id = 'app_create.restart' defaultMessage = 'Create another' / >
< / b u t t o n >
< / d i v >
< / S i m p l e F o r m >
< / C o l u m n >
) ;
}
render ( ) {
const { intl } = this . props ;
const { params , app , token , isLoading } = this . state ;
if ( app && token ) {
return this . renderResults ( ) ;
}
return (
< Column heading = { intl . formatMessage ( messages . heading ) } >
< SimpleForm onSubmit = { this . handleSubmit } >
< fieldset disabled = { isLoading } >
< TextInput
label = { < FormattedMessage id = 'app_create.name_label' defaultMessage = 'App name' / > }
placeholder = { intl . formatMessage ( messages . namePlaceholder ) }
onChange = { this . handleParamChange ( 'client_name' ) }
value = { params . get ( 'client_name' ) }
required
/ >
< TextInput
label = { < FormattedMessage id = 'app_create.website_label' defaultMessage = 'Website' / > }
placeholder = 'https://soapbox.pub'
onChange = { this . handleParamChange ( 'website' ) }
value = { params . get ( 'website' ) }
/ >
< TextInput
label = { < FormattedMessage id = 'app_create.redirect_uri_label' defaultMessage = 'Redirect URIs' / > }
placeholder = 'https://example.com'
onChange = { this . handleParamChange ( 'redirect_uris' ) }
value = { params . get ( 'redirect_uris' ) }
required
/ >
< TextInput
label = { < FormattedMessage id = 'app_create.scopes_label' defaultMessage = 'Scopes' / > }
placeholder = { intl . formatMessage ( messages . scopesPlaceholder ) }
onChange = { this . handleParamChange ( 'scopes' ) }
value = { params . get ( 'scopes' ) }
required
/ >
< div className = 'actions' >
< button name = 'button' type = 'submit' className = 'btn button button-primary' >
< FormattedMessage id = 'app_create.submit' defaultMessage = 'Create app' / >
< / b u t t o n >
< / d i v >
< / f i e l d s e t >
2021-11-01 22:35:35 -07:00
< / S i m p l e F o r m >
< / C o l u m n >
) ;
}
}