Skip to content

Commit

Permalink
Merge pull request #676 from getguesstimate/delete-facts
Browse files Browse the repository at this point in the history
Adds fact deletion logic and buttons.
  • Loading branch information
OAGr authored Aug 2, 2016
2 parents d433aa0 + 603daca commit 8fc4b94
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 3 deletions.
7 changes: 4 additions & 3 deletions src/components/organizations/show/facts/factRow.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import Histogram from 'gComponents/simulations/histogram/index'

import './facts.css'

export const FactRow = ({fact, onEdit}) => (
export const FactRow = ({fact, onEdit, onDelete}) => (
<div className='Fact'>
<div className='row'>
<div className='col-md-3'>
Expand All @@ -16,15 +16,16 @@ export const FactRow = ({fact, onEdit}) => (
cutOffRatio={0.995}
/>
</div>
<div className='col-md-6'><span className='name'>{fact.name}</span></div>
<div className='col-md-5'><span className='name'>{fact.name}</span></div>
<div className='col-md-2'>
<div className='variableName'>
<span className='prefix'>#</span>
<span className='variable'>{fact.variable_name}</span>
</div>
</div>
<div className='col-md-1'>
<div className='col-md-2'>
<span className='ui button options' onClick={onEdit}>Edit</span>
<span className='ui button options' onClick={onDelete}>Delete</span>
</div>
</div>
</div>
Expand Down
1 change: 1 addition & 0 deletions src/components/organizations/show/facts/facts.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export class FactBookTab extends Component {
key={fact.id}
fact={fact}
onEdit={() => {this.setState({editingFactId: fact.id})}}
onDelete={() => {this.props.onDeleteFact(fact)}}
/>
)
}
Expand Down
1 change: 1 addition & 0 deletions src/components/organizations/show/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ export default class OrganizationShow extends Component{

{(openTab === FACT_BOOK_TAB) && meIsMember && !!facts &&
<FactBookTab
onDeleteFact={fact => this.props.dispatch(organizationActions.deleteFact(organization, fact))}
onAddFact={fact => this.props.dispatch(organizationActions.addFact(organization, fact))}
onEditFact={fact => this.props.dispatch(organizationActions.editFact(organization, fact))}
facts={facts}
Expand Down
7 changes: 7 additions & 0 deletions src/lib/guesstimate_api/resources/Organizations.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,13 @@ export default class Organizations extends AbstractResource {
this.guesstimateMethod({url, method, data})(callback)
}

deleteFact({id}, fact, callback) {
const url = `organizations/${id}/facts/${fact.id}`
const method = 'DELETE'

this.guesstimateMethod({url, method})(callback)
}

getInvitations({organizationId}, callback) {
const url = `organizations/${organizationId}/invitees`
const method = 'GET'
Expand Down
4 changes: 4 additions & 0 deletions src/modules/facts/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,7 @@ export function loadByOrg(facts) {
export function addToOrg(organizationVariableName, fact) {
return {type: 'ADD_FACT_TO_ORG', organizationVariableName, fact: withSortedValues(fact)}
}

export function deleteFromOrg(organizationVariableName, {id}) {
return {type: 'DELETE_FACT_FROM_ORG', organizationVariableName, id}
}
14 changes: 14 additions & 0 deletions src/modules/facts/reducer.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,20 @@ export function factsR(state = INITIAL_STATE, action) {
],
}
}
case 'DELETE_FACT_FROM_ORG': {
const organizationFact = state.organizationFacts.find(e => e.variable_name === action.organizationVariableName)
const children = !!organizationFact ? organizationFact.children.filter(c => c.id !== action.id) : []
return {
...state,
organizationFacts: [
{
variable_name: action.organizationVariableName,
children,
},
...state.organizationFacts.filter(e => e.variable_name !== action.organizationVariableName)
],
}
}
case 'SUGGEST_FACT':
return {
...state,
Expand Down
12 changes: 12 additions & 0 deletions src/modules/organizations/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -113,3 +113,15 @@ export function editFact(organization, rawFact) {
})
}
}

export function deleteFact(organization, fact) {
return (dispatch, getState) => {
api(getState()).organizations.deleteFact(organization, fact, (err, serverFact) => {
if (err) {
captureApiError('OrganizationsFactDestroy', err.jqXHR, err.textStatus, err, {url: 'destroyOrganizationMember'})
} else {
dispatch(factActions.deleteFromOrg(organizationReadableId(organization), fact))
}
})
}
}

0 comments on commit 8fc4b94

Please sign in to comment.