Skip to content

Commit

Permalink
fix: allow single value child tables
Browse files Browse the repository at this point in the history
  • Loading branch information
18alantom committed Jul 13, 2023
1 parent 8d8d9ee commit 319466a
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 24 deletions.
20 changes: 17 additions & 3 deletions backend/database/core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,17 @@ export default class DatabaseCore extends DatabaseBase {
order: 'asc',
});

return getValueMapFromList(values, 'fieldname', 'value') as FieldValueMap;
const fieldValueMap = getValueMapFromList(
values,
'fieldname',
'value'
) as FieldValueMap;
const tableFields: TargetField[] = this.#getTableFields(schemaName);
if (tableFields.length) {
await this.#loadChildren(schemaName, fieldValueMap, tableFields);
}

return fieldValueMap;
}

#insertOne(schemaName: string, fieldValueMap: FieldValueMap) {
Expand All @@ -800,7 +810,7 @@ export default class DatabaseCore extends DatabaseBase {
fieldValueMap: FieldValueMap
) {
const fields = this.schemaMap[singleSchemaName]!.fields.filter(
(f) => !f.computed
(f) => !f.computed && f.fieldtype !== 'Table'
);
for (const field of fields) {
const value = fieldValueMap[field.fieldname] as RawValue | undefined;
Expand Down Expand Up @@ -935,7 +945,11 @@ export default class DatabaseCore extends DatabaseBase {
fieldValueMap: FieldValueMap,
isUpdate: boolean
) {
const parentName = fieldValueMap.name as string;
let parentName = fieldValueMap.name as string;
if (this.schemaMap[schemaName]?.isSingle) {
parentName = schemaName;
}

const tableFields = this.#getTableFields(schemaName);

for (const field of tableFields) {
Expand Down
2 changes: 1 addition & 1 deletion backend/database/manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ export class DatabaseManager extends DatabaseDemuxBase {
const backupFolder = path.join(path.dirname(dbPath), 'backups');
const date = new Date().toISOString().split('T')[0];
const version = await this.#getAppVersion();
const backupFile = `${fileName}-${version}-${date}.books.db`;
const backupFile = `${fileName}_${version}_${date}.books.db`;
fs.ensureDirSync(backupFolder);
return path.join(backupFolder, backupFile);
}
Expand Down
3 changes: 2 additions & 1 deletion fyo/model/doc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import {
TargetField,
} from 'schemas/types';
import { getIsNullOrUndef, getMapFromList, getRandomString } from 'utils';
import { markRaw } from 'vue';
import { markRaw, reactive } from 'vue';
import { isPesa } from '../utils/index';
import { getDbSyncError } from './errorHelpers';
import {
Expand Down Expand Up @@ -83,6 +83,7 @@ export class Doc extends Observable<DocValue | Doc[]> {

this._setDefaults();
this._setValuesWithoutChecks(data, convertToDocValue);
return reactive(this) as Doc;
}

get schemaName(): string {
Expand Down
16 changes: 5 additions & 11 deletions src/components/Controls/Table.vue
Original file line number Diff line number Diff line change
Expand Up @@ -155,17 +155,11 @@ export default {
},
methods: {
focus() {},
addRow() {
this.doc.append(this.df.fieldname, {}).then((s) => {
if (!s) {
return;
}
nextTick(() => {
this.scrollToRow(this.value.length - 1);
});
this.triggerChange(this.value);
});
async addRow() {
await this.doc.append(this.df.fieldname);
await nextTick();
this.scrollToRow(this.value.length - 1);
this.triggerChange(this.value);
},
removeRow(row) {
this.doc.remove(this.df.fieldname, row.idx).then((s) => {
Expand Down
17 changes: 9 additions & 8 deletions src/pages/Settings/Settings.vue
Original file line number Diff line number Diff line change
Expand Up @@ -101,17 +101,24 @@ export default defineComponent({
data() {
return {
errors: {},
canSave: false,
activeTab: ModelNameEnum.AccountingSettings,
groupedFields: null,
} as {
errors: Record<string, string>;
canSave: boolean;
activeTab: string;
groupedFields: null | UIGroupedFields;
};
},
computed: {
canSave() {
return [
ModelNameEnum.AccountingSettings,
ModelNameEnum.InventorySettings,
ModelNameEnum.Defaults,
ModelNameEnum.PrintSettings,
ModelNameEnum.SystemSettings,
].some((s) => this.fyo.singles[s]?.canSave);
},
doc(): Doc | null {
const doc = this.fyo.singles[this.activeTab];
if (!doc) {
Expand Down Expand Up @@ -256,14 +263,8 @@ export default defineComponent({
this.update();
},
update(): void {
this.updateCanSave();
this.updateGroupedFields();
},
updateCanSave(): void {
this.canSave = this.schemas
.map(({ name }) => this.fyo.singles[name]?.canSave)
.some(Boolean);
},
updateGroupedFields(): void {
const grouped: UIGroupedFields = new Map();
const fields: Field[] = this.schemas.map((s) => s.fields).flat();
Expand Down

0 comments on commit 319466a

Please sign in to comment.