Browse Source

fix(gui): add newly added sort top of the list

Signed-off-by: Pranav C <pranavxc@gmail.com>
pull/4504/head
Pranav C 2 years ago
parent
commit
a44ce593e6
  1. 2
      packages/nc-gui/components/smartsheet/Grid.vue
  2. 2
      packages/nc-gui/components/smartsheet/column/EditOrAdd.vue
  3. 2
      packages/nc-gui/components/smartsheet/column/EditOrAddProvider.vue
  4. 2
      packages/nc-gui/components/smartsheet/header/Cell.vue
  5. 12
      packages/nc-gui/components/smartsheet/header/Menu.vue
  6. 2
      packages/nc-gui/components/smartsheet/header/VirtualCell.vue
  7. 2
      packages/nc-gui/composables/useColumnCreateStore.ts
  8. 12
      packages/nocodb-sdk/src/lib/Api.ts
  9. 6
      packages/nocodb/src/lib/meta/api/sortApis.ts
  10. 5
      packages/nocodb/src/lib/models/Column.ts
  11. 71
      packages/nocodb/src/lib/models/Sort.ts
  12. 6
      packages/nocodb/src/lib/models/View.ts
  13. 18
      scripts/sdk/swagger.json

2
packages/nc-gui/components/smartsheet/Grid.vue

@ -545,7 +545,7 @@ watch(
{ immediate: true }, { immediate: true },
) )
const columnPosition = ref<Pick<ColumnReqType, 'columnOrder'> | null>(null) const columnPosition = ref<Pick<ColumnReqType, 'column_order'> | null>(null)
eventBus.on(async (event, payload) => { eventBus.on(async (event, payload) => {
if (event === SmartsheetStoreEvents.FIELD_ADD) { if (event === SmartsheetStoreEvents.FIELD_ADD) {

2
packages/nc-gui/components/smartsheet/column/EditOrAdd.vue

@ -26,7 +26,7 @@ import MdiMinusIcon from '~icons/mdi/minus-circle-outline'
import MdiIdentifierIcon from '~icons/mdi/identifier' import MdiIdentifierIcon from '~icons/mdi/identifier'
const props = defineProps<{ const props = defineProps<{
columnPosition?: Pick<ColumnReqType, 'columnOrder'> columnPosition?: Pick<ColumnReqType, 'column_order'>
}>() }>()
const emit = defineEmits(['submit', 'cancel']) const emit = defineEmits(['submit', 'cancel'])

2
packages/nc-gui/components/smartsheet/column/EditOrAddProvider.vue

@ -5,7 +5,7 @@ import { MetaInj, inject, ref, toRef, useProvideColumnCreateStore } from '#impor
interface Props { interface Props {
column?: ColumnType & { meta: any } column?: ColumnType & { meta: any }
columnPosition?: Pick<ColumnReqType, 'columnOrder'> columnPosition?: Pick<ColumnReqType, 'column_order'>
} }
const props = defineProps<Props>() const props = defineProps<Props>()

2
packages/nc-gui/components/smartsheet/header/Cell.vue

@ -18,7 +18,7 @@ provide(ColumnInj, column)
const editColumnDropdown = ref(false) const editColumnDropdown = ref(false)
const columnOrder = ref<Pick<ColumnReqType, 'columnOrder'> | null>(null) const columnOrder = ref<Pick<ColumnReqType, 'column_order'> | null>(null)
const addField = async (payload) => { const addField = async (payload) => {
columnOrder.value = payload columnOrder.value = payload

12
packages/nc-gui/components/smartsheet/header/Menu.vue

@ -85,7 +85,7 @@ const setAsPrimaryValue = async () => {
const sortByColumn = async (direction: 'asc' | 'desc') => { const sortByColumn = async (direction: 'asc' | 'desc') => {
try { try {
$e('a:sort:add', { from: 'column-menu' }) $e('a:sort:add', { from: 'column-menu' })
await $api.dbTableSort.create(view.value?.id as string, { fk_column_id: column!.value.id, direction }) await $api.dbTableSort.create(view.value?.id as string, { fk_column_id: column!.value.id, direction, push_to_top: true })
eventBus.emit(SmartsheetStoreEvents.SORT_RELOAD) eventBus.emit(SmartsheetStoreEvents.SORT_RELOAD)
reloadDataHook?.trigger() reloadDataHook?.trigger()
} catch (e: any) { } catch (e: any) {
@ -144,9 +144,9 @@ const duplicateColumn = async () => {
await $api.dbTableColumn.create(meta!.value!.id!, { await $api.dbTableColumn.create(meta!.value!.id!, {
...columnCreatePayload, ...columnCreatePayload,
columnOrder: { column_order: {
order: newColumnOrder, order: newColumnOrder,
viewId: view.value?.id as string, view_id: view.value?.id as string,
}, },
}) })
await getMeta(meta!.value!.id!, true) await getMeta(meta!.value!.id!, true)
@ -178,10 +178,10 @@ const addColumn = async (before = false) => {
// viewId: view.value?.id as string, // viewId: view.value?.id as string,
// }, // },
// }) // })
emit('addColumn', { emit('addColumn', {
columnOrder: { column_order: {
order: newColumnOrder, order: newColumnOrder,
viewId: view.value?.id as string, view_id: view.value?.id as string,
}, },
}) })
} }

2
packages/nc-gui/components/smartsheet/header/VirtualCell.vue

@ -100,7 +100,7 @@ const tooltipMsg = computed(() => {
return '' return ''
}) })
const columnOrder = ref<Pick<ColumnReqType, 'columnOrder'> | null>(null) const columnOrder = ref<Pick<ColumnReqType, 'column_order'> | null>(null)
const addField = async (payload) => { const addField = async (payload) => {
columnOrder.value = payload columnOrder.value = payload

2
packages/nc-gui/composables/useColumnCreateStore.ts

@ -191,7 +191,7 @@ const [useProvideColumnCreateStore, useColumnCreateStore] = createInjectionState
if (cdf) formState.value.cdf = formState.value.cdf || null if (cdf) formState.value.cdf = formState.value.cdf || null
} }
const addOrUpdate = async (onSuccess: () => void, columnPosition?: Pick<ColumnReqType, 'columnOrder'>) => { const addOrUpdate = async (onSuccess: () => void, columnPosition?: Pick<ColumnReqType, 'column_order'>) => {
try { try {
if (!(await validate())) return if (!(await validate())) return
} catch (e) { } catch (e) {

12
packages/nocodb-sdk/src/lib/Api.ts

@ -669,8 +669,8 @@ export type ColumnReqType = (
) & { ) & {
column_name?: string; column_name?: string;
title?: string; title?: string;
columnOrder?: { column_order?: {
viewId?: string; view_id?: string;
order?: number; order?: number;
}; };
}; };
@ -2618,7 +2618,13 @@ export class Api<
* @request POST:/api/v1/db/meta/views/{viewId}/sorts * @request POST:/api/v1/db/meta/views/{viewId}/sorts
* @response `200` `void` OK * @response `200` `void` OK
*/ */
create: (viewId: string, data: SortType, params: RequestParams = {}) => create: (
viewId: string,
data: SortType & {
push_to_top?: boolean;
},
params: RequestParams = {}
) =>
this.request<void, any>({ this.request<void, any>({
path: `/api/v1/db/meta/views/${viewId}/sorts`, path: `/api/v1/db/meta/views/${viewId}/sorts`,
method: 'POST', method: 'POST',

6
packages/nocodb/src/lib/meta/api/sortApis.ts

@ -4,7 +4,7 @@ import Model from '../../models/Model';
import { Tele } from 'nc-help'; import { Tele } from 'nc-help';
// @ts-ignore // @ts-ignore
import { PagedResponseImpl } from '../helpers/PagedResponse'; import { PagedResponseImpl } from '../helpers/PagedResponse';
import { SortListType, TableReqType, TableType } from 'nocodb-sdk'; import { SortListType, SortType, TableType } from 'nocodb-sdk';
// @ts-ignore // @ts-ignore
import ProjectMgrv2 from '../../db/sql-mgr/v2/ProjectMgrv2'; import ProjectMgrv2 from '../../db/sql-mgr/v2/ProjectMgrv2';
// @ts-ignore // @ts-ignore
@ -28,11 +28,11 @@ export async function sortList(
} }
// @ts-ignore // @ts-ignore
export async function sortCreate(req: Request<any, any, TableReqType>, res) { export async function sortCreate(req: Request<any, any, SortType>, res) {
const sort = await Sort.insert({ const sort = await Sort.insert({
...req.body, ...req.body,
fk_view_id: req.params.viewId, fk_view_id: req.params.viewId,
}); } as Sort);
Tele.emit('evt', { evt_type: 'sort:created' }); Tele.emit('evt', { evt_type: 'sort:created' });
res.json(sort); res.json(sort);
} }

5
packages/nocodb/src/lib/models/Column.ts

@ -74,7 +74,7 @@ export default class Column<T = any> implements ColumnType {
[key: string]: any; [key: string]: any;
fk_model_id: string; fk_model_id: string;
uidt: UITypes | string; uidt: UITypes | string;
}, } & Pick<ColumnReqType, 'column_order'>,
ncMeta = Noco.ncMeta ncMeta = Noco.ncMeta
) { ) {
if (!column.fk_model_id) NcError.badRequest('Missing model id'); if (!column.fk_model_id) NcError.badRequest('Missing model id');
@ -130,7 +130,6 @@ export default class Column<T = any> implements ColumnType {
} }
if (!column.uidt) throw new Error('UI Datatype not found'); if (!column.uidt) throw new Error('UI Datatype not found');
const order = 1;
const row = await ncMeta.metaInsert2( const row = await ncMeta.metaInsert2(
null, //column.project_id || column.base_id, null, //column.project_id || column.base_id,
null, //column.db_alias, null, //column.db_alias,
@ -152,8 +151,8 @@ export default class Column<T = any> implements ColumnType {
{ {
fk_column_id: row.id, fk_column_id: row.id,
fk_model_id: column.fk_model_id, fk_model_id: column.fk_model_id,
order,
show: true, show: true,
column_order: column.column_order,
}, },
ncMeta ncMeta
); );

71
packages/nocodb/src/lib/models/Sort.ts

@ -34,18 +34,22 @@ export default class Sort {
}); });
} }
public static async insert(sortObj: Partial<Sort>, ncMeta = Noco.ncMeta) { public static async insert(
sortObj: Partial<Sort> & { push_to_top?: boolean },
ncMeta = Noco.ncMeta
) {
// todo: implement a generic function // todo: implement a generic function
const order = const order = sortObj.push_to_top
(+( ? 1
await ncMeta : (+(
.knex(MetaTable.SORT) await ncMeta
.max('order', { as: 'order' }) .knex(MetaTable.SORT)
.where({ .max('order', { as: 'order' })
fk_view_id: sortObj.fk_view_id, .where({
}) fk_view_id: sortObj.fk_view_id,
.first() })
)?.order || 0) + 1; .first()
)?.order || 0) + 1;
const insertObj = { const insertObj = {
id: sortObj.id, id: sortObj.id,
@ -62,20 +66,39 @@ export default class Sort {
insertObj.base_id = model.base_id; insertObj.base_id = model.base_id;
} }
const row = await ncMeta.metaInsert2(null, null, MetaTable.SORT, insertObj); // increment existing order
if (sortObj.push_to_top) {
await NocoCache.appendToList( await ncMeta
CacheScope.SORT, .knex(MetaTable.SORT)
[sortObj.fk_view_id], .where({
`${CacheScope.SORT}:${row.id}` fk_view_id: sortObj.fk_view_id,
); })
.increment('order', 1);
await NocoCache.appendToList( }
CacheScope.SORT,
[sortObj.fk_column_id],
`${CacheScope.SORT}:${row.id}`
);
const row = await ncMeta.metaInsert2(null, null, MetaTable.SORT, insertObj);
if (sortObj.push_to_top) {
// todo: delete cache
const sortList = await ncMeta.metaList2(null, null, MetaTable.SORT, {
condition: { fk_view_id: sortObj.fk_view_id },
orderBy: {
order: 'asc',
},
});
await NocoCache.setList(CacheScope.SORT, [sortObj.fk_view_id], sortList);
} else {
await NocoCache.appendToList(
CacheScope.SORT,
[sortObj.fk_view_id],
`${CacheScope.SORT}:${row.id}`
);
await NocoCache.appendToList(
CacheScope.SORT,
[sortObj.fk_column_id],
`${CacheScope.SORT}:${row.id}`
);
}
return this.get(row.id, ncMeta); return this.get(row.id, ncMeta);
} }

6
packages/nocodb/src/lib/models/View.ts

@ -450,7 +450,7 @@ export default class View implements ViewType {
fk_model_id: any; fk_model_id: any;
order?: number; order?: number;
show; show;
} & Pick<ColumnReqType, 'columnOrder'>, } & Pick<ColumnReqType, 'column_order'>,
ncMeta = Noco.ncMeta ncMeta = Noco.ncMeta
) { ) {
const insertObj = { const insertObj = {
@ -464,8 +464,8 @@ export default class View implements ViewType {
for (const view of views) { for (const view of views) {
const modifiedInsertObj = { ...insertObj, fk_view_id: view.id }; const modifiedInsertObj = { ...insertObj, fk_view_id: view.id };
if (param.columnOrder?.viewId === view.id) { if (param.column_order?.view_id === view.id) {
modifiedInsertObj.order = param.columnOrder?.order; modifiedInsertObj.order = param.column_order?.order;
} }
switch (view.type) { switch (view.type) {

18
scripts/sdk/swagger.json

@ -2286,7 +2286,19 @@
"content": { "content": {
"application/json": { "application/json": {
"schema": { "schema": {
"$ref": "#/components/schemas/Sort" "allOf": [
{
"$ref": "#/components/schemas/Sort"
},
{
"type": "object",
"properties": {
"push_to_top": {
"type": "boolean"
}
}
}
]
} }
} }
} }
@ -9195,10 +9207,10 @@
"title": { "title": {
"type": "string" "type": "string"
}, },
"columnOrder": { "column_order": {
"type": "object", "type": "object",
"properties": { "properties": {
"viewId": { "view_id": {
"type": "string" "type": "string"
}, },
"order": { "order": {

Loading…
Cancel
Save