diff --git a/packages/manager/modules/datagrid-preferences/package.json b/packages/manager/modules/datagrid-preferences/package.json new file mode 100644 index 000000000000..abe0b00b599c --- /dev/null +++ b/packages/manager/modules/datagrid-preferences/package.json @@ -0,0 +1,34 @@ +{ + "name": "@ovh-ux/datagrid-preferences", + "version": "0.0.0", + "private": true, + "description": "Store and retrieve datagrid configuration", + "homepage": "https://github.com/ovh/manager/tree/master/packages/manager/modules/datagrid-preferences#readme", + "bugs": { + "url": "https://github.com/ovh/manager/issues" + }, + "repository": { + "type": "git", + "url": "git+https://github.com/ovh/manager.git", + "directory": "packages/manager/modules/datagrid-preferences" + }, + "license": "BSD-3-Clause", + "sideEffects": false, + "main": "dist/index.js", + "module": "dist/index.js", + "files": [ + "dist" + ], + "scripts": { + "build": "tsc", + "dev": "tsc", + "dev:watch": "tsc --watch", + "prepare": "yarn run build", + "start": "lerna exec --stream --scope='@ovh-ux/datagrid-preferences' --include-dependencies -- yarn run build", + "start:dev": "lerna exec --stream --scope='@ovh-ux/datagrid-preferences' --include-dependencies -- yarn run dev", + "start:watch": "lerna exec --stream --parallel --scope='@ovh-ux/datagrid-preferences' --include-dependencies -- yarn run dev:watch" + }, + "dependencies": { + "lodash-es": "^4.17.15" + } +} diff --git a/packages/manager/modules/datagrid-preferences/src/index.ts b/packages/manager/modules/datagrid-preferences/src/index.ts new file mode 100644 index 000000000000..a9137578c8f1 --- /dev/null +++ b/packages/manager/modules/datagrid-preferences/src/index.ts @@ -0,0 +1,47 @@ +import { debounce, snakeCase } from 'lodash-es'; + +const PREFIX = 'DATAGRID_CONFIG_'; +const DEBOUNCE = 2000; + +export const retrieveColumnsConfig = async (datagridId: string) => { + const key = `${PREFIX}${snakeCase(datagridId).toUpperCase()}`; + const config = await fetch(`/engine/api/me/preferences/manager/${key}`, { + headers: { + 'Content-Type': 'application/json;charset=utf-8', + Accept: 'application/json', + }, + credentials: 'same-origin', + }); + + if (config.ok) { + const { value } = await config.json(); + return JSON.parse(value); + } + + return null; +}; + +export const saveColumnsConfig = debounce( + async (datagridId: string, columnsConfig: unknown) => { + const key = `${PREFIX}${snakeCase(datagridId).toUpperCase()}`; + const ret = await fetch(`/engine/api/me/preferences/manager`, { + method: 'POST', + headers: { + 'Content-Type': 'application/json;charset=utf-8', + Accept: 'application/json', + }, + credentials: 'same-origin', + body: JSON.stringify({ + key, + value: JSON.stringify(columnsConfig), + }), + }); + + if (ret.ok) { + return ret.json(); + } + + return null; + }, + DEBOUNCE, +); diff --git a/packages/manager/modules/datagrid-preferences/tsconfig.json b/packages/manager/modules/datagrid-preferences/tsconfig.json new file mode 100644 index 000000000000..6dc4649ccb4c --- /dev/null +++ b/packages/manager/modules/datagrid-preferences/tsconfig.json @@ -0,0 +1,20 @@ +{ + "compilerOptions": { + "target": "es2015", + "moduleResolution": "Node", + "lib": ["dom"], + "module": "esnext", + "removeComments": true, + "outDir": "dist", + "esModuleInterop": true, + "allowSyntheticDefaultImports": true, + "noImplicitAny": true, + "sourceMap": true, + "allowJs": true, + "declaration": true, + "skipLibCheck": true, + "declarationDir": "dist/types" + }, + "include": ["src/**/*"], + "exclude": ["node_modules", "dist"] +}