1
0
mirror of https://github.com/ascribe/onion.git synced 2024-12-22 17:33:14 +01:00

unshare and delete functionality + store updates for all views

This commit is contained in:
Tim Daubenschütz 2015-07-14 16:29:01 +02:00
parent adf4630efd
commit 5a0663ea14
9 changed files with 201 additions and 35 deletions

View File

@ -6,7 +6,11 @@ import Router from 'react-router';
import Button from 'react-bootstrap/lib/Button'; import Button from 'react-bootstrap/lib/Button';
import EditionDeleteForm from '../ascribe_forms/form_delete_edition'; import EditionDeleteForm from '../ascribe_forms/form_delete_edition';
import PieceDeleteForm from '../ascribe_forms/form_delete_piece';
import EditionRemoveFromCollectionForm from '../ascribe_forms/form_remove_editions_from_collection'; import EditionRemoveFromCollectionForm from '../ascribe_forms/form_remove_editions_from_collection';
import PieceRemoveFromCollectionForm from '../ascribe_forms/form_remove_piece_from_collection';
import ModalWrapper from '../ascribe_modal/modal_wrapper'; import ModalWrapper from '../ascribe_modal/modal_wrapper';
import { getAvailableAcls } from '../../utils/acl_utils'; import { getAvailableAcls } from '../../utils/acl_utils';
@ -15,23 +19,47 @@ import { getLangText } from '../../utils/lang_utils.js';
let DeleteButton = React.createClass({ let DeleteButton = React.createClass({
propTypes: { propTypes: {
editions: React.PropTypes.array.isRequired, editions: React.PropTypes.array,
piece: React.PropTypes.object,
handleSuccess: React.PropTypes.func handleSuccess: React.PropTypes.func
}, },
mixins: [Router.Navigation], mixins: [Router.Navigation],
render: function () { render: function () {
let availableAcls = getAvailableAcls(this.props.editions); let availableAcls;
let btnDelete = null; let btnDelete;
let content = null; let content;
let title;
if (availableAcls.acl_delete) { if(this.props.piece && !this.props.editions) {
content = <EditionDeleteForm editions={ this.props.editions }/>; availableAcls = getAvailableAcls([this.props.piece]);
btnDelete = <Button bsStyle="danger" className="btn-delete" bsSize="small">{getLangText('DELETE')}</Button>; } else {
availableAcls = getAvailableAcls(this.props.editions);
} }
else if (availableAcls.acl_unshare || (this.props.editions.constructor !== Array && this.props.editions.acl.acl_unshare)){
content = <EditionRemoveFromCollectionForm editions={ this.props.editions }/>; if(availableAcls.acl_delete) {
if(this.props.piece && !this.props.editions) {
content = <PieceDeleteForm pieceId={this.props.piece.id}/>;
title = getLangText('Remove Piece');
} else {
content = <EditionDeleteForm editions={this.props.editions}/>;
title = getLangText('Remove Edition');
}
btnDelete = <Button bsStyle="danger" className="btn-delete" bsSize="small">{getLangText('DELETE')}</Button>;
} else if(availableAcls.acl_unshare){
if(this.props.editions && this.props.editions.constructor !== Array && this.props.editions.acl.acl_unshare) {
content = <EditionRemoveFromCollectionForm editions={this.props.editions}/>;
title = getLangText('Remove Edition from Collection');
} else {
content = <PieceRemoveFromCollectionForm pieceId={this.props.piece.id}/>;
title = getLangText('Remove Piece from Collection');
}
btnDelete = <Button bsStyle="danger" className="btn-delete" bsSize="small">{getLangText('REMOVE FROM COLLECTION')}</Button>; btnDelete = <Button bsStyle="danger" className="btn-delete" bsSize="small">{getLangText('REMOVE FROM COLLECTION')}</Button>;
} }
else { else {
@ -41,9 +69,8 @@ let DeleteButton = React.createClass({
<ModalWrapper <ModalWrapper
button={btnDelete} button={btnDelete}
handleSuccess={this.props.handleSuccess} handleSuccess={this.props.handleSuccess}
title={getLangText('Remove Edition')} title={title}>
tooltip={getLangText('Click to remove edition')}> {content}
{ content }
</ModalWrapper> </ModalWrapper>
); );
} }

View File

@ -63,11 +63,13 @@ let Edition = React.createClass({
componentDidMount() { componentDidMount() {
UserStore.listen(this.onChange); UserStore.listen(this.onChange);
PieceListStore.listen(this.onChange);
UserActions.fetchCurrentUser(); UserActions.fetchCurrentUser();
}, },
componentWillUnmount() { componentWillUnmount() {
UserStore.unlisten(this.onChange); UserStore.unlisten(this.onChange);
PieceListStore.unlisten(this.onChange);
}, },
onChange(state) { onChange(state) {

View File

@ -1,6 +1,7 @@
'use strict'; 'use strict';
import React from 'react'; import React from 'react';
import Router from 'react-router';
import Row from 'react-bootstrap/lib/Row'; import Row from 'react-bootstrap/lib/Row';
import Col from 'react-bootstrap/lib/Col'; import Col from 'react-bootstrap/lib/Col';
@ -14,6 +15,11 @@ import FurtherDetails from './further_details';
import UserActions from '../../actions/user_actions'; import UserActions from '../../actions/user_actions';
import UserStore from '../../stores/user_store'; import UserStore from '../../stores/user_store';
import PieceListActions from '../../actions/piece_list_actions';
import PieceListStore from '../../stores/piece_list_store';
import EditionListActions from '../../actions/edition_list_actions';
import PieceActions from '../../actions/piece_actions'; import PieceActions from '../../actions/piece_actions';
import MediaContainer from './media_container'; import MediaContainer from './media_container';
@ -23,6 +29,7 @@ import EditionDetailProperty from './detail_property';
import AclButtonList from './../ascribe_buttons/acl_button_list'; import AclButtonList from './../ascribe_buttons/acl_button_list';
import CreateEditionsForm from '../ascribe_forms/create_editions_form'; import CreateEditionsForm from '../ascribe_forms/create_editions_form';
import CreateEditionsButton from '../ascribe_buttons/create_editions_button'; import CreateEditionsButton from '../ascribe_buttons/create_editions_button';
import DeleteButton from '../ascribe_buttons/delete_button';
import GlobalNotificationModel from '../../models/global_notification_model'; import GlobalNotificationModel from '../../models/global_notification_model';
import GlobalNotificationActions from '../../actions/global_notification_actions'; import GlobalNotificationActions from '../../actions/global_notification_actions';
@ -39,9 +46,12 @@ let Piece = React.createClass({
loadPiece: React.PropTypes.func loadPiece: React.PropTypes.func
}, },
mixins: [Router.Navigation],
getInitialState() { getInitialState() {
return mergeOptions( return mergeOptions(
UserStore.getState(), UserStore.getState(),
PieceListStore.getState(),
{ {
showCreateEditionsDialog: false showCreateEditionsDialog: false
} }
@ -50,11 +60,13 @@ let Piece = React.createClass({
componentDidMount() { componentDidMount() {
UserStore.listen(this.onChange); UserStore.listen(this.onChange);
PieceListStore.listen(this.onChange);
UserActions.fetchCurrentUser(); UserActions.fetchCurrentUser();
}, },
componentWillUnmount() { componentWillUnmount() {
UserStore.unlisten(this.onChange); UserStore.unlisten(this.onChange);
PieceListStore.unlisten(this.onChange);
}, },
onChange(state) { onChange(state) {
@ -72,6 +84,20 @@ let Piece = React.createClass({
this.toggleCreateEditionsDialog(); this.toggleCreateEditionsDialog();
}, },
handleDeleteSuccess(response) {
PieceListActions.fetchPieceList(this.state.page, this.state.pageSize, this.state.search, this.state.orderBy, this.state.orderAsc);
// since we're deleting a piece, we just need to close
// all editions dialogs and not reload them
EditionListActions.closeAllEditionLists();
EditionListActions.clearAllEditionSelections();
let notification = new GlobalNotificationModel(response.notification, 'success');
GlobalNotificationActions.appendGlobalNotification(notification);
this.transitionTo('pieces');
},
getCreateEditionsDialog() { getCreateEditionsDialog() {
if(this.props.piece.num_editions < 1 && this.state.showCreateEditionsDialog) { if(this.props.piece.num_editions < 1 && this.state.showCreateEditionsDialog) {
return ( return (
@ -127,6 +153,9 @@ let Piece = React.createClass({
piece={this.props.piece} piece={this.props.piece}
toggleCreateEditionsDialog={this.toggleCreateEditionsDialog} toggleCreateEditionsDialog={this.toggleCreateEditionsDialog}
onPollingSuccess={this.handlePollingSuccess}/> onPollingSuccess={this.handlePollingSuccess}/>
<DeleteButton
handleSuccess={this.handleDeleteSuccess}
piece={this.props.piece}/>
</AclButtonList> </AclButtonList>
{this.getCreateEditionsDialog()} {this.getCreateEditionsDialog()}

View File

@ -0,0 +1,41 @@
'use strict';
import React from 'react';
import requests from '../../utils/requests';
import ApiUrls from '../../constants/api_urls';
import FormMixin from '../../mixins/form_mixin';
import { getLangText } from '../../utils/lang_utils';
let PieceDeleteForm = React.createClass({
propTypes: {
pieceId: React.PropTypes.number
},
mixins: [FormMixin],
url() {
return requests.prepareUrl(ApiUrls.piece, {piece_id: this.props.pieceId});
},
httpVerb() {
return 'delete';
},
renderForm () {
return (
<div className="modal-body">
<p>{getLangText('Are you sure you would like to permanently delete this piece')}&#63;</p>
<p>{getLangText('This is an irrevocable action%s', '.')}</p>
<div className="modal-footer">
<button type="submit" className="btn btn-danger btn-delete btn-sm ascribe-margin-1px" onClick={this.submit}>{getLangText('YES, DELETE')}</button>
<button className="btn btn-default btn-sm ascribe-margin-1px" style={{marginLeft: '0'}}
onClick={this.props.onRequestHide}>{getLangText('CLOSE')}</button>
</div>
</div>
);
}
});
export default PieceDeleteForm;

View File

@ -12,13 +12,9 @@ let EditionRemoveFromCollectionForm = React.createClass({
mixins: [FormMixin], mixins: [FormMixin],
url() { url() {
if (this.props.editions.constructor === Array) { return requests.prepareUrl(apiUrls.edition_remove_from_collection, {edition_id: this.getBitcoinIds().join()});
return requests.prepareUrl(apiUrls.edition_remove_from_collection, {edition_id: this.getBitcoinIds().join()});
}
else {
return requests.prepareUrl(apiUrls.piece_remove_from_collection, {piece_id: this.props.editions.id});
}
}, },
httpVerb(){ httpVerb(){
return 'delete'; return 'delete';
}, },

View File

@ -0,0 +1,42 @@
'use strict';
import React from 'react';
import { getLangText } from '../../utils/lang_utils.js';
import requests from '../../utils/requests';
import apiUrls from '../../constants/api_urls';
import FormMixin from '../../mixins/form_mixin';
let PieceRemoveFromCollectionForm = React.createClass({
propTypes: {
pieceId: React.PropTypes.number
},
mixins: [FormMixin],
url() {
return requests.prepareUrl(apiUrls.piece_remove_from_collection, {piece_id: this.props.pieceId});
},
httpVerb(){
return 'delete';
},
renderForm () {
return (
<div className="modal-body">
<p>{getLangText('Are you sure you would like to remove this piece from your collection')}&#63;</p>
<p>{getLangText('This is an irrevocable action%s', '.')}</p>
<div className="modal-footer">
<button type="submit" className="btn btn-danger btn-delete btn-sm ascribe-margin-1px" onClick={this.submit}>{getLangText('YES, REMOVE')}</button>
<button className="btn btn-default btn-sm ascribe-margin-1px" style={{marginLeft: '0'}}
onClick={this.props.onRequestHide}>{getLangText('CLOSE')}</button>
</div>
</div>
);
}
});
export default PieceRemoveFromCollectionForm;

View File

@ -17,24 +17,41 @@ let ModalWrapper = React.createClass({
handleSuccess: React.PropTypes.func.isRequired, handleSuccess: React.PropTypes.func.isRequired,
button: React.PropTypes.object.isRequired, button: React.PropTypes.object.isRequired,
children: React.PropTypes.object, children: React.PropTypes.object,
tooltip: React.PropTypes.string.isRequired tooltip: React.PropTypes.string
},
getModalTrigger() {
return (
<ModalTrigger modal={
<ModalBody
title={this.props.title}
handleSuccess={this.props.handleSuccess}>
{this.props.children}
</ModalBody>
}>
{this.props.button}
</ModalTrigger>
);
}, },
render() { render() {
return ( if(this.props.tooltip) {
<OverlayTrigger delay={500} placement="left" return (
overlay={<Tooltip>{this.props.tooltip}</Tooltip>}> <OverlayTrigger
<ModalTrigger modal={ delay={500}
<ModalBody placement="left"
title={this.props.title} overlay={<Tooltip>{this.props.tooltip}</Tooltip>}>
handleSuccess={this.props.handleSuccess}> {this.getModalTrigger()}
{this.props.children} </OverlayTrigger>
</ModalBody> );
}> } else {
{this.props.button} return (
</ModalTrigger> <span>
</OverlayTrigger> {/* This needs to be some kind of inline-block */}
); {this.getModalTrigger()}
</span>
);
}
} }
}); });

View File

@ -8,7 +8,7 @@ function intersectAcls(a, b) {
export function getAvailableAcls(editions, filterFn) { export function getAvailableAcls(editions, filterFn) {
let availableAcls = []; let availableAcls = [];
if (editions.constructor !== Array){ if (!editions || editions.constructor !== Array){
return []; return [];
} }
// if you copy a javascript array of objects using slice, then // if you copy a javascript array of objects using slice, then

View File

@ -54,6 +54,11 @@ class Requests {
} }
getUrl(url) { getUrl(url) {
// Handle case, that the url string is not defined at all
if (!url) {
throw new Error('Url was not defined and could therefore not be mapped.');
}
let name = url; let name = url;
if (!url.match(/^http/)) { if (!url.match(/^http/)) {
url = this.urlMap[url]; url = this.urlMap[url];
@ -66,9 +71,16 @@ class Requests {
} }
prepareUrl(url, params, attachParamsToQuery) { prepareUrl(url, params, attachParamsToQuery) {
let newUrl = this.getUrl(url); let newUrl;
let re = /\${(\w+)}/g; let re = /\${(\w+)}/g;
// catch errors and throw them to react
try {
newUrl = this.getUrl(url);
} catch(err) {
throw err;
}
newUrl = newUrl.replace(re, (match, key) => { newUrl = newUrl.replace(re, (match, key) => {
let val = params[key]; let val = params[key];
if (!val) { if (!val) {