Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to add delete function in collection view #39

Open
iamrobreynolds opened this issue Apr 5, 2016 · 1 comment
Open

How to add delete function in collection view #39

iamrobreynolds opened this issue Apr 5, 2016 · 1 comment

Comments

@iamrobreynolds
Copy link

In client/modules/_colors/components/colors/_single.jsx there is a function deleteRecord

What is the appropriate way to use the same function or other functions in the collections view?

@iamrobreynolds
Copy link
Author

For those looking for how to write custom functions at the collection view, here is what I found to work.

/*
 * File:   /client/modules/module_name/composers/component_name/collection.jsx
*/
import {useDeps} from 'react-simple-di';
import {composeWithTracker, composeAll} from 'react-komposer';

export const collectionComposer = ({context}, onData) => {
  const {Meteor, Collections} = context();
  if (Meteor.subscribe('_collection_name.list').ready()) {
    const collection = Collections._collection_name.find().fetch();
    onData(null, {collection});
  }
};

export const depsMapper = (context, actions) => ({
  deleteAction: actions._collection_name.delete,
  clearErrors: actions._collection_name.clearErrors,
  context: () => context
});

export default (component) => composeAll(
    composeWithTracker(collectionComposer),
    useDeps(depsMapper)
  )(component);
/*
 * File:   /client/modules/module_name/components/component_name/_collection.jsx
*/
export default class extends React.Component {

  deleteRecord(_id) {
    //console.log('deleteRecord', _id);
    this.props.deleteAction(_id);
  };

  render() {
    const {collection} = this.props;
    return(
      <div>
        <h2>Collection Name</h2>
        <div className="table-responsive">
          <table className="table table-striped table-hover">
            <thead className="thead-inverse">
              <tr>
                <th>&nbsp;</th>
                <th>Title</th>
              </tr>
            </thead>
            <tbody>
              {collection.map(record => (
              <tr key={record._id}>
                <td><button type="button" className="btn btn-danger btn-sm" onClick={() => this.deleteRecord(`${record._id}`)}>x</button></td>
                <td scope="row">
                  <b>{record.title}</b><br />
                  <small><a href={`/_collection_name/${record._id}/edit`}>edit</a> | <a href={`/_collection_name/${record._id}`}>view</a></small>
                </td>
              </tr>
              ))}
            </tbody>
          </table>
        </div>
      </div>
    )
  }

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant