Commit dcd1b297 authored by / /\ / /\/'s avatar / /\ / /\/
Browse files

wip, handle errors in community creation

parent 969e8cfd
......@@ -5,203 +5,238 @@ import * as communityActions from 'app/redux/CommunityReducer';
import tt from 'counterpart';
import { key_utils } from '@steemit/steem-js/lib/auth/ecc';
const CreateCommunity = ({
accountName,
communityCreateError,
communityCreatePending,
communityCreateSuccess,
createCommunity,
communityDescription,
communityNSFW,
communityOwnerWifPassword,
communityOwnerName,
communityTitle,
updateCommunityTitle,
updateCommunityDescription,
updateCommunityNSFW,
updateCommunityOwnerAccountName,
updateCommunityOwnerWifPassword,
communityHivemindOperationPending,
communityHivemindOperationError,
}) => {
const handleCommunityTitleInput = e => {
if (e.target.value.length > 32) {
return;
}
updateCommunityTitle(e.target.value);
};
const handleCommunityDescriptionInput = e => {
if (e.target.value.length > 120) {
return;
}
updateCommunityDescription(e.target.value);
};
const handleCommunityNSFWInput = e => {
updateCommunityNSFW(e.target.checked);
};
class CreateCommunity extends React.Component {
constructor() {
super();
this.state = { error: false };
}
render() {
const errorCB = () => {
debugger;
// Can we dispatch from within here.
this.setState({ error: true });
};
const handleCommunitySubmit = e => {
e.preventDefault();
const createCommunitypayload = {
const {
accountName,
communityTitle,
communityCreateError,
communityCreatePending,
communityCreateSuccess,
createCommunity,
communityDescription,
communityNSFW,
communityOwnerName,
communityOwnerWifPassword,
communityOwnerName,
communityTitle,
updateCommunityTitle,
updateCommunityDescription,
updateCommunityNSFW,
updateCommunityOwnerAccountName,
updateCommunityOwnerWifPassword,
communityHivemindOperationPending,
communityHivemindOperationError,
loginError,
transactionState,
hasTransactionError,
} = this.props;
console.log(this.state.error);
const handleCommunityTitleInput = e => {
if (e.target.value.length > 32) {
return;
}
updateCommunityTitle(e.target.value);
};
const handleCommunityDescriptionInput = e => {
if (e.target.value.length > 120) {
return;
}
updateCommunityDescription(e.target.value);
};
const handleCommunityNSFWInput = e => {
updateCommunityNSFW(e.target.checked);
};
createCommunity(createCommunitypayload);
};
const generateCommunityOwnerName = () => {
return `hive-${Math.floor(Math.random() * 100000) + 100000}`;
};
const handleCommunitySubmit = e => {
e.preventDefault();
const createCommunitypayload = {
accountName,
communityTitle,
communityDescription,
communityNSFW,
communityOwnerName,
communityOwnerWifPassword,
};
createCommunity(createCommunitypayload, errorCB);
};
const generateCreatorWifPassword = () => {
return 'P' + key_utils.get_random_key().toWif();
};
const generateCommunityOwnerName = () => {
return `hive-${Math.floor(Math.random() * 100000) + 100000}`;
};
const generateWif = () => {
const wif = generateCreatorWifPassword();
updateCommunityOwnerWifPassword(wif);
};
const generateCreatorWifPassword = () => {
return 'P' + key_utils.get_random_key().toWif();
};
const generateUsername = () => {
const ownerUsername = generateCommunityOwnerName();
updateCommunityOwnerAccountName(ownerUsername);
};
const generateWif = () => {
const wif = generateCreatorWifPassword();
updateCommunityOwnerWifPassword(wif);
};
const generateCommunityCredentials = () => {
generateWif();
generateUsername();
};
const generateUsername = () => {
const ownerUsername = generateCommunityOwnerName();
updateCommunityOwnerAccountName(ownerUsername);
};
const generateCommunityCredentialsButton = (
<button
type="button"
className="button hollow"
onClick={generateCommunityCredentials}
>
{tt('g.click_to_generate_password')}
</button>
);
const generateCommunityCredentials = () => {
generateWif();
generateUsername();
};
const rememberCredentialsPrompt = (
<div>
<div>{`${tt(
'g.community_owner_name_is'
)}: ${communityOwnerName}`}</div>
<div>{`${tt(
'g.community_password_is'
)}: ${communityOwnerWifPassword}`}</div>
</div>
);
const generateCommunityCredentialsButton = (
<button
type="button"
className="button hollow"
onClick={generateCommunityCredentials}
>
{tt('g.click_to_generate_password')}
</button>
);
const rememberCredentialsCheckbox1 = (
<label htmlFor="box1">
<input type="checkbox" name="box1" required />
{tt('g.understand_that_APP_NAME_cannot_recover_password', {
APP_NAME,
})}.
</label>
);
const rememberCredentialsPrompt = (
<div>
<div>{`${tt(
'g.community_owner_name_is'
)}: ${communityOwnerName}`}</div>
<div>{`${tt(
'g.community_password_is'
)}: ${communityOwnerWifPassword}`}</div>
</div>
);
const rememberCredentialsCheckbox2 = (
<label htmlFor="box2">
<input type="checkbox" name="box2" required />
{tt('g.i_saved_password')}.
</label>
);
const rememberCredentialsCheckbox1 = (
<label htmlFor="box1">
<input type="checkbox" name="box1" required />
{tt('g.understand_that_APP_NAME_cannot_recover_password', {
APP_NAME,
})}.
</label>
);
const submitCreateCommunityFormButton = (
<input type="submit" value="Submit" />
);
const rememberCredentialsCheckbox2 = (
<label htmlFor="box2">
<input type="checkbox" name="box2" required />
{tt('g.i_saved_password')}.
</label>
);
const createCommunitySuccessMessage = (
<div>
<p>Your community was created!</p>
<a href={`https://steemitdev.com/trending/${communityOwnerName}`}>
{tt('g.community_visit')}
</a>
</div>
);
const submitCreateCommunityFormButton = (
<input type="submit" value="Submit" />
);
const createCommunityErrorMessage = <div>{tt('g.community_error')}</div>;
const createCommunityLoadingMessage = (
<div>{tt('g.community_creating')}</div>
);
const createCommunityCustomOpsPendingMessage = (
<div>{`${tt('g.community_broadcasting_custom_ops')} ${accountName} ${tt(
'community_broadcasting_custom_ops_1'
)} ${communityTitle}} ${tt(
'community_broadcasting_custom_ops_2'
)}`}</div>
);
const createCommunitySuccessMessage = (
<div>
<p>Your community was created!</p>
<a
href={`https://steemitdev.com/trending/${communityOwnerName}`}
>
{tt('g.community_visit')}
</a>
</div>
);
const createCommunityForm = (
<form onSubmit={handleCommunitySubmit}>
<div>{tt('g.community_create')}</div>
<label htmlFor="community_title">
Title
<input
id="community_title"
name="community_title"
type="text"
minLength="4"
maxLength="30"
onChange={handleCommunityTitleInput}
value={communityTitle}
required
/>
</label>
<label htmlFor="community_description">
{tt('g.community_description')}
<input
id="community_description"
name="community_description"
type="text"
minLength="10"
maxLength="140"
onChange={handleCommunityDescriptionInput}
value={communityDescription}
required
/>
</label>
<label id="is_nsfw" htmlFor="is_nsfw">
{tt('g.community_nsfw')}
<input
type="checkbox"
name="is_nsfw"
checked={communityNSFW}
onChange={handleCommunityNSFWInput}
/>
</label>
{communityOwnerWifPassword.length <= 0 &&
generateCommunityCredentialsButton}
{communityOwnerWifPassword.length > 0 && rememberCredentialsPrompt}
{communityOwnerWifPassword.length > 0 &&
rememberCredentialsCheckbox1}
{communityOwnerWifPassword.length > 0 &&
rememberCredentialsCheckbox2}
{communityOwnerWifPassword.length > 0 &&
submitCreateCommunityFormButton}
</form>
);
return (
<div className="row">
<div className="column large-6 small-12">
{!communityCreatePending &&
!communityCreateSuccess &&
createCommunityForm}
{communityCreatePending && createCommunityLoadingMessage}
{communityHivemindOperationPending &&
createCommunityCustomOpsPendingMessage}
{communityCreateSuccess && createCommunitySuccessMessage}
{communityCreateError && createCommunityErrorMessage}
const createCommunityErrorMessage = (
<div>{tt('g.community_error')}</div>
);
const createCommunityLoadingMessage = (
<div>{tt('g.community_creating')}</div>
);
const createCommunityPasswordError = (
<div>
<div>{tt('g.community_create_password_error')}</div>
<div>{loginError}</div>
</div>
);
const createCommunityCustomOpsPendingMessage = (
<div>{`${tt(
'g.community_broadcasting_custom_ops'
)} ${accountName} ${tt(
'community_broadcasting_custom_ops_1'
)} ${communityTitle}} ${tt(
'community_broadcasting_custom_ops_2'
)}`}</div>
);
const createCommunityForm = (
<form onSubmit={handleCommunitySubmit}>
<div>{tt('g.community_create')}</div>
<label htmlFor="community_title">
Title
<input
id="community_title"
name="community_title"
type="text"
minLength="4"
maxLength="30"
onChange={handleCommunityTitleInput}
value={communityTitle}
required
/>
</label>
<label htmlFor="community_description">
{tt('g.community_description')}
<input
id="community_description"
name="community_description"
type="text"
minLength="10"
maxLength="140"
onChange={handleCommunityDescriptionInput}
value={communityDescription}
required
/>
</label>
<label id="is_nsfw" htmlFor="is_nsfw">
{tt('g.community_nsfw')}
<input
type="checkbox"
name="is_nsfw"
checked={communityNSFW}
onChange={handleCommunityNSFWInput}
/>
</label>
{communityOwnerWifPassword.length <= 0 &&
generateCommunityCredentialsButton}
{communityOwnerWifPassword.length > 0 &&
rememberCredentialsPrompt}
{communityOwnerWifPassword.length > 0 &&
rememberCredentialsCheckbox1}
{communityOwnerWifPassword.length > 0 &&
rememberCredentialsCheckbox2}
{communityOwnerWifPassword.length > 0 &&
submitCreateCommunityFormButton}
</form>
);
return (
<div className="row">
<div className="column large-6 small-12">
{loginError && createCommunityPasswordError}
{hasTransactionError && <div>THERE WAS AN ERROR!</div>}
{!communityCreatePending &&
!communityCreateSuccess &&
createCommunityForm}
{communityCreatePending && createCommunityLoadingMessage}
{communityHivemindOperationPending &&
createCommunityCustomOpsPendingMessage}
{communityCreateSuccess && createCommunitySuccessMessage}
{communityCreateError && createCommunityErrorMessage}
</div>
</div>
</div>
);
};
);
}
}
export default connect(
// mapStateToProps
......@@ -209,6 +244,15 @@ export default connect(
const { account } = ownProps;
const accountName = account.get('name');
const current = state.user.get('current');
// get userLogin error
console.log('userState', state.user.toJS());
console.log('transactionState', state.transaction.toJS());
const loginError = state.user.toJS().login_error;
const transactionState = state.transaction.toJS();
const hasTransactionError =
Object.values(state.transaction.toJS().errors).filter(v => !!v)
.length > 0;
console.log('There was a transaction error:', hasTransactionError);
const username = current && current.get('username');
const isMyAccount = username === accountName;
return {
......@@ -216,6 +260,9 @@ export default connect(
...state.community.toJS(),
isMyAccount,
accountName,
loginError,
transactionState,
hasTransactionError,
};
},
// mapDispatchToProps
......@@ -240,17 +287,16 @@ export default connect(
communityActions.setCommunityOwnerWifPassword(password)
);
},
createCommunity: createCommunityPayload => {
createCommunity: (createCommunityPayload, errorCB) => {
const successCallback = () =>
dispatch(
communityActions.communityHivemindOperation(
createCommunityPayload
)
);
const errorCallback = () =>
dispatch(
communityActions.createCommunityAccountError(true)
);
const errorCallback = () => {
errorCB();
};
const payload = {
successCallback: successCallback,
errorCallback: errorCallback,
......
......@@ -25,6 +25,8 @@
"community_owner_name_is": "Your community owner name is",
"community_password_is": "Your password is",
"community_creating": "Your community is being created...",
"community_create_password_error":
"There was a problem with your credentials:",
"community_broadcasting_custom_ops": "Making",
"community_broadcasting_custom_ops_1": "administrator of the",
"community_broadcasting_custom_ops_2": "community.",
......
......@@ -75,6 +75,7 @@ export default function reducer(state = defaultState, action) {
}
case CREATE_COMMUNITY_ERROR: {
const err = fromJS(payload);
debugger;
return state.merge({ communityCreateError: err });
}
case CREATE_COMMUNITY_SUCCESS: {
......@@ -136,10 +137,13 @@ export const createCommunityAccountPending = payload => ({
payload,
});
export const createCommunityAccountError = payload => ({
type: CREATE_COMMUNITY_ACCOUNT_ERROR,
payload,
});
export const createCommunityAccountError = payload => {
debugger;
return {
type: CREATE_COMMUNITY_ACCOUNT_ERROR,
payload,
};
};
export const createCommunitySuccess = payload => ({
type: CREATE_COMMUNITY_SUCCESS,
......
......@@ -35,7 +35,7 @@ export const communityWatches = [
),
takeLatest(communityActions.COMMUNITY_HIVEMIND_OPERATION, customOps),
takeLatest(
communityActions.COMMUNITY_CREATE_ACCOUNT_ERROR,
communityActions.CREATE_COMMUNITY_ACCOUNT_ERROR,
createAccountError
),
];
......@@ -189,7 +189,7 @@ export function* createCommunityAccount(createCommunityAction) {
errorCallback: res => {
debugger;
console.log('error', res);
errorCallback(error);
errorCallback(res);
},
})
);
......
......@@ -28,6 +28,7 @@ export default function reducer(state = defaultState, action) {
const operation = fromJS(payload.operation);
const confirm = payload.confirm;
const warning = payload.warning;
debugger;
return state.merge({
show_confirm_modal: true,
confirmBroadcastOperation: operation,
......
......@@ -132,6 +132,7 @@ export function* broadcastOperation({
const warning = tt('g.post_key_warning.warning');
const checkbox = tt('g.post_key_warning.checkbox');
operationParam.allowPostUnsafe = true;
debugger;
yield put(
transactionActions.confirmOperation({
confirm,
......@@ -182,6 +183,7 @@ export function* broadcastOperation({
serverApiRecordEvent(eventType, page);
} catch (error) {
console.error('TransactionSage', error);
debugger;
if (errorCallback) errorCallback(error.toString());
}
}
......
......@@ -49,6 +49,7 @@ const defaultState = fromJS({
locale: DEFAULT_LANGUAGE,
show_side_panel: false,
maybeLoggedIn: false,
login_error: undefined,
});
export default function reducer(state = defaultState, action) {
......@@ -202,6 +203,7 @@ export default function reducer(state = defaultState, action) {
});
case LOGIN_ERROR:
debugger;
return state.merge({
login_error: payload.error,
logged_out: undefined,
......
......@@ -219,6 +219,7 @@ function* usernamePasswordLogin({
Set()
);
if (!fullAuths.size) {
debugger;
console.log('No full auths');
localStorage.removeItem('autopost2');
const generated_type = password[0] === 'P' && password.length > 40;
......@@ -418,12 +419,8 @@ function* logout(action) {
yield serverApiLogout();
}
function* loginError({
payload: {
/*error*/
},
}) {
serverApiLogout();
function* loginError() {
yield serverApiLogout();
}
/**
......
......@@ -23,6 +23,7 @@ export function serverApiLogout() {
const request = Object.assign({}, request_base, {
body: JSON.stringify({ csrf: $STM_csrf }),
});
debugger;
return fetch('/api/v1/logout_account', request);
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment