Skip to content

Commit

Permalink
fix(collection-field): always use new determine if creating (#483)
Browse files Browse the repository at this point in the history
* fix(collection-field): always use new determine if creating

* fix(collection-field): set id if blank
  • Loading branch information
redgeoff authored Oct 5, 2021
1 parent b47a6e1 commit 5fa352c
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 20 deletions.
8 changes: 4 additions & 4 deletions src/fields/collection-field.js
Original file line number Diff line number Diff line change
Expand Up @@ -1039,7 +1039,7 @@ export default class CollectionField extends Field {
}

_setMaxColumns(maxColumns) {
// 12 (the number of grids) must be divisble by maxColumns. And, we require a power of 2 so that
// 12 (the number of grids) must be divisible by maxColumns. And, we require a power of 2 so that
// we can shrink the screen and not have gaps in our grid, i.e. each layer is an even multiple
// of the previous.
const allowed = [1, 2, 4, 6, 12];
Expand Down Expand Up @@ -1168,7 +1168,7 @@ export default class CollectionField extends Field {
async _saveForm(form) {
const id = form.getField('id');
const store = this.get('store');
const creating = id.isBlank();
const creating = form.get('new');
let fieldForm = null;

if (store) {
Expand All @@ -1178,7 +1178,7 @@ export default class CollectionField extends Field {

let record = null;

if (form.get('new')) {
if (creating) {
record = await store.createDoc({ form, reorder });
} else {
record = await store.updateDoc({ form, reorder });
Expand All @@ -1191,7 +1191,7 @@ export default class CollectionField extends Field {
order: record.order,
userId: record.userId,
});
} else if (creating) {
} else if (id.isBlank()) {
// TODO: use the id from this._docs.set instead of this dummy id
id.setValue(utils.uuid());
}
Expand Down
44 changes: 28 additions & 16 deletions src/fields/collection-field.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -420,6 +420,20 @@ const updateDocMock = () => async (props) => {
};
};

const jack = {
firstName: 'Jack',
lastName: 'Johnson',
userId: 'myUserId',
};

const clickNew = (field) => {
// Simulate the user clicking the new button
field.set({
currentForm: null,
mode: CollectionField.MODES.CREATE,
});
};

it('should save', async () => {
const field = createField();

Expand All @@ -444,17 +458,7 @@ it('should save', async () => {
const createSpy = jest.spyOn(store, 'createDoc');
const updateSpy = jest.spyOn(store, 'updateDoc');

const jack = {
firstName: 'Jack',
lastName: 'Johnson',
userId: 'myUserId',
};

// Simulate the user clicking the new button
field.set({
currentForm: null,
mode: CollectionField.MODES.CREATE,
});
clickNew(field);

const form = field.get('form');

Expand All @@ -472,18 +476,26 @@ it('should save', async () => {
await field.save();
expect(updateSpy).toHaveBeenCalledWith({ form, reorder: false });
expect(field.getValue()).toHaveLength(1);
});

// Simulate the lack of a store
field.set({ store: null });
form.clearValues();
it('should save without store', async () => {
const field = createField();

clickNew(field);

const form = field.get('form');

// Save
form.setValues(jack);
await field.save();
expect(form.getValue('id')).not.toBeUndefined();
expect(field.getValue()).toHaveLength(2);
expect(field.getValue()).toHaveLength(1);

// Update
form.setValues({ lastName: 'Ryan' });
await field.save();
expect(form.getValue('lastName')).toEqual('Ryan');
expect(field.getValue()).toHaveLength(2);
expect(field.getValue()).toHaveLength(1);
});

it('should archive', async () => {
Expand Down

0 comments on commit 5fa352c

Please sign in to comment.