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. 10
      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. 33
      packages/nocodb/src/lib/models/Sort.ts
  12. 6
      packages/nocodb/src/lib/models/View.ts
  13. 16
      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

10
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)
@ -179,9 +179,9 @@ const addColumn = async (before = false) => {
// }, // },
// }) // })
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
); );

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

@ -34,10 +34,14 @@ 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 await ncMeta
.knex(MetaTable.SORT) .knex(MetaTable.SORT)
.max('order', { as: 'order' }) .max('order', { as: 'order' })
@ -62,8 +66,27 @@ 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 ncMeta
.knex(MetaTable.SORT)
.where({
fk_view_id: sortObj.fk_view_id,
})
.increment('order', 1);
}
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( await NocoCache.appendToList(
CacheScope.SORT, CacheScope.SORT,
[sortObj.fk_view_id], [sortObj.fk_view_id],
@ -75,7 +98,7 @@ export default class Sort {
[sortObj.fk_column_id], [sortObj.fk_column_id],
`${CacheScope.SORT}:${row.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) {

16
scripts/sdk/swagger.json

@ -2286,7 +2286,19 @@
"content": { "content": {
"application/json": { "application/json": {
"schema": { "schema": {
"allOf": [
{
"$ref": "#/components/schemas/Sort" "$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