Demo entry 6646074

mobx-state-tree store

   

Submitted by anonymous on Oct 13, 2017 at 11:23
Language: JavaScript. Code size: 1.4 kB.

import {types, clone} from 'mobx-state-tree';
import CompanyPerson from '../model/CompanyPerson';
import CompanyPersonService from '../service/CompanyPersonService';

const CompanyPersonStore = types.model({
    companyId: types.number,
    persons: types.array(CompanyPerson),
    createMode: false,
    editPerson: types.maybe(CompanyPerson),
    removePersonId: types.maybe(types.number)
}).actions((self) => ({
    startEditNewPerson: function () {
        self.createMode = true;
        self.editPerson = CompanyPerson.create({
            companyId: self.companyId
        });
    },
    startEditPerson: function (person) {
        self.createMode = false;
        self.editPerson = clone(person);
    },
    cancelEdit: function () {
        self.editPerson = null;
    },
    modalRemove: function (personId) {
        self.removePersonId = personId;
    },
    abortRemove: function () {
        self.removePersonId = null;
    },
    removePerson: function () {
        self.persons.splice(self.persons.findIndex(person => person.personId === self.removePersonId), 1);
        CompanyPersonService.delete(self.companyId, self.removePersonId);
        self.removePersonId = null;
    },
    submitPerson: function () {
        console.log(self.editPerson);
    }
}));
export default CompanyPersonStore;

This snippet took 0.01 seconds to highlight.

Back to the Entry List or Home.

Delete this entry (admin only).