Skip to content

Commit

Permalink
allow creating report as guest user #93 WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
tmfrnz committed Sep 12, 2017
1 parent 93f394d commit 4903171
Show file tree
Hide file tree
Showing 7 changed files with 69 additions and 18 deletions.
6 changes: 5 additions & 1 deletion app/containers/App/sagas.js
Original file line number Diff line number Diff line change
Expand Up @@ -445,7 +445,11 @@ export function* newEntitySaga({ data }) {
data.onSuccess();
}
if (data.redirect) {
yield put(push(`${data.redirect}/${entityCreated.data.id}`));
if (data.redirectWithoutCreatedId) {
yield put(push(`${data.redirect}`));
} else {
yield put(push(`${data.redirect}/${entityCreated.data.id}`));
}
}
} catch (err) {
err.response.json = yield err.response.json();
Expand Down
17 changes: 12 additions & 5 deletions app/containers/IndicatorView/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ export class IndicatorView extends React.PureComponent { // eslint-disable-line
getReportsField(
reports,
appMessages,
isContributor && {
{
type: 'add',
title: this.context.intl.formatMessage(messages.addReport),
onClick: this.props.handleNewReport,
Expand Down Expand Up @@ -159,10 +159,17 @@ export class IndicatorView extends React.PureComponent { // eslint-disable-line
onClick: this.props.handleClose,
},
]
: [{
type: 'close',
onClick: this.props.handleClose,
}];
: [
{
type: 'text',
title: this.context.intl.formatMessage(messages.addReport),
onClick: this.props.handleNewReport,
},
{
type: 'close',
onClick: this.props.handleClose,
},
];

return (
<div>
Expand Down
4 changes: 3 additions & 1 deletion app/containers/ReportNew/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import {
SAVE,
} from './constants';

export function save(data) {
export function save(data, redirect, redirectWithoutCreatedId) {
return {
type: SAVE,
data,
redirect,
redirectWithoutCreatedId,
};
}
48 changes: 40 additions & 8 deletions app/containers/ReportNew/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import React from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import Helmet from 'react-helmet';
import { FormattedMessage } from 'react-intl';
import { actions as formActions } from 'react-redux-form/immutable';

import {
Expand All @@ -19,6 +20,10 @@ import {
getUploadField,
} from 'utils/forms';

import { getStatusField as getStatusInfoField } from 'utils/fields';

import { attributesEqual } from 'utils/entities';

import { scrollToTop } from 'utils/scroll-to-component';
import { hasNewError } from 'utils/entity-form';

Expand All @@ -33,7 +38,12 @@ import {
saveErrorDismiss,
} from 'containers/App/actions';

import { selectReady } from 'containers/App/selectors';
import {
selectReady,
selectIsUserContributor,
selectIsUserManager,
selectSessionUserId,
} from 'containers/App/selectors';

import ErrorMessages from 'components/ErrorMessages';
import Loading from 'components/Loading';
Expand Down Expand Up @@ -74,8 +84,12 @@ export class ReportNew extends React.PureComponent { // eslint-disable-line reac
},
]);

getHeaderAsideFields = () => ([{
fields: [getStatusField(this.context.intl.formatMessage, appMessages)],
getHeaderAsideFields = (canUserPublish) => ([{
fields: [
canUserPublish
? getStatusField(this.context.intl.formatMessage, appMessages)
: getStatusInfoField(),
],
}]);

getBodyMainFields = () => ([
Expand Down Expand Up @@ -103,10 +117,19 @@ export class ReportNew extends React.PureComponent { // eslint-disable-line reac
},
]);

canUserPublish = (isUserContributor, isUserManager, userId, indicator) =>
isUserManager || (isUserContributor && attributesEqual(userId, indicator.getIn(['attributes', 'manager_id'])));

render() {
const { dataReady, indicator, viewDomain } = this.props;
const { dataReady, indicator, viewDomain, isUserContributor, isUserManager, userId } = this.props;
const { saveSending, saveError, submitValid } = viewDomain.page;
const indicatorReference = this.props.params.id;
const canUserPublish = dataReady && this.canUserPublish(
isUserContributor,
isUserManager,
userId,
indicator
);

let pageTitle = this.context.intl.formatMessage(messages.pageTitle);
pageTitle = `${pageTitle} for indicator ${indicatorReference}`;
Expand Down Expand Up @@ -139,6 +162,9 @@ export class ReportNew extends React.PureComponent { // eslint-disable-line reac
}] : null
}
/>
{ !canUserPublish &&
<FormattedMessage {...messages.guestNote} />
}
{!submitValid &&
<ErrorMessages
error={{ messages: [this.context.intl.formatMessage(appMessages.forms.multipleErrors)] }}
Expand Down Expand Up @@ -169,11 +195,11 @@ export class ReportNew extends React.PureComponent { // eslint-disable-line reac
fields={{
header: {
main: this.getHeaderMainFields(),
aside: this.getHeaderAsideFields(),
aside: this.getHeaderAsideFields(canUserPublish),
},
body: {
main: this.getBodyMainFields(),
aside: this.getBodyAsideFields(indicator),
aside: canUserPublish && this.getBodyAsideFields(indicator),
},
}}
/>
Expand Down Expand Up @@ -201,6 +227,9 @@ ReportNew.propTypes = {
initialiseForm: PropTypes.func,
onErrorDismiss: PropTypes.func.isRequired,
onServerErrorDismiss: PropTypes.func.isRequired,
isUserContributor: PropTypes.bool,
isUserManager: PropTypes.bool,
userId: PropTypes.string,
};

ReportNew.contextTypes = {
Expand All @@ -211,6 +240,9 @@ const mapStateToProps = (state, props) => ({
viewDomain: selectDomain(state),
dataReady: selectReady(state, { path: DEPENDENCIES }),
indicator: selectIndicator(state, props.params.id),
isUserContributor: selectIsUserContributor(state),
isUserManager: selectIsUserManager(state),
userId: selectSessionUserId(state),
});

function mapDispatchToProps(dispatch) {
Expand All @@ -234,7 +266,7 @@ function mapDispatchToProps(dispatch) {
handleSubmitRemote: (model) => {
dispatch(formActions.submit(model));
},
handleSubmit: (formData, indicatorReference) => {
handleSubmit: (formData, indicatorReference, canUserPublish) => {
let saveData = formData;

saveData = saveData.setIn(['attributes', 'indicator_id'], indicatorReference);
Expand All @@ -244,7 +276,7 @@ function mapDispatchToProps(dispatch) {
saveData = saveData.setIn(['attributes', 'due_date_id'], null);
}

dispatch(save(saveData.toJS()));
dispatch(save(saveData.toJS(), canUserPublish ? '/reports' : `/indicators/${indicatorReference}`, !canUserPublish));
},
handleCancel: (indicatorReference) => {
dispatch(updatePath(`/indicators/${indicatorReference}`));
Expand Down
4 changes: 4 additions & 0 deletions app/containers/ReportNew/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,8 @@ export default defineMessages({
},
},
},
guestNote: {
id: 'app.containers.ReportNew.guestNote',
defaultMessage: 'Please note: you are contributing as a guest user. Your report will only be publicly available once verified and published by an authorised user.',
},
});
5 changes: 3 additions & 2 deletions app/containers/ReportNew/sagas.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,12 @@ import { LOCATION_CHANGE } from 'react-router-redux';
import { newEntity, dueDateAssigned } from 'containers/App/actions';
import { SAVE } from './constants';

export function* save({ data }) {
export function* save({ data, redirect, redirectWithoutCreatedId = false }) {
yield put(newEntity({
path: 'progress_reports',
entity: data,
redirect: '/reports',
redirect,
redirectWithoutCreatedId,
}));
if (data.attributes.due_date_id) {
yield put(dueDateAssigned(data.attributes.due_date_id));
Expand Down
3 changes: 2 additions & 1 deletion app/utils/fields.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ export const getTitleField = (entity, isManager, attribute = 'title', label) =>
label,
});
export const getStatusField = (entity, attribute = 'draft', options, label) => ({
controlType: 'info',
type: 'status',
value: entity.getIn(['attributes', attribute]),
value: entity ? entity.getIn(['attributes', attribute]) : true,
options,
label,
});
Expand Down

0 comments on commit 4903171

Please sign in to comment.