From d4101a48b3eee8b1fb177831aa02a4b4fbec2588 Mon Sep 17 00:00:00 2001 From: Markus Hatvan Date: Sat, 18 Sep 2021 11:15:25 +0200 Subject: chore: convert various files from JS to TS (#1959) --- src/features/workspaces/actions.js | 27 --------- src/features/workspaces/actions.ts | 30 ++++++++++ src/features/workspaces/api.js | 66 ---------------------- src/features/workspaces/api.ts | 86 +++++++++++++++++++++++++++++ src/features/workspaces/constants.js | 4 -- src/features/workspaces/constants.ts | 4 ++ src/features/workspaces/index.js | 30 ---------- src/features/workspaces/index.ts | 28 ++++++++++ src/features/workspaces/models/Workspace.js | 37 ------------- src/features/workspaces/models/Workspace.ts | 39 +++++++++++++ 10 files changed, 187 insertions(+), 164 deletions(-) delete mode 100644 src/features/workspaces/actions.js create mode 100644 src/features/workspaces/actions.ts delete mode 100644 src/features/workspaces/api.js create mode 100644 src/features/workspaces/api.ts delete mode 100644 src/features/workspaces/constants.js create mode 100644 src/features/workspaces/constants.ts delete mode 100644 src/features/workspaces/index.js create mode 100644 src/features/workspaces/index.ts delete mode 100644 src/features/workspaces/models/Workspace.js create mode 100644 src/features/workspaces/models/Workspace.ts (limited to 'src/features/workspaces') diff --git a/src/features/workspaces/actions.js b/src/features/workspaces/actions.js deleted file mode 100644 index 5b5db422e..000000000 --- a/src/features/workspaces/actions.js +++ /dev/null @@ -1,27 +0,0 @@ -import PropTypes from 'prop-types'; -import Workspace from './models/Workspace'; -import { createActionsFromDefinitions } from '../../actions/lib/actions'; - -export const workspaceActions = createActionsFromDefinitions({ - edit: { - workspace: PropTypes.instanceOf(Workspace).isRequired, - }, - create: { - name: PropTypes.string.isRequired, - }, - delete: { - workspace: PropTypes.instanceOf(Workspace).isRequired, - }, - update: { - workspace: PropTypes.instanceOf(Workspace).isRequired, - }, - activate: { - workspace: PropTypes.instanceOf(Workspace).isRequired, - }, - deactivate: {}, - toggleWorkspaceDrawer: {}, - openWorkspaceSettings: {}, - toggleKeepAllWorkspacesLoadedSetting: {}, -}, PropTypes.checkPropTypes); - -export default workspaceActions; diff --git a/src/features/workspaces/actions.ts b/src/features/workspaces/actions.ts new file mode 100644 index 000000000..5e7e6e721 --- /dev/null +++ b/src/features/workspaces/actions.ts @@ -0,0 +1,30 @@ +import PropTypes from 'prop-types'; +import Workspace from './models/Workspace'; +import { createActionsFromDefinitions } from '../../actions/lib/actions'; + +export const workspaceActions = createActionsFromDefinitions( + { + edit: { + workspace: PropTypes.instanceOf(Workspace).isRequired, + }, + create: { + name: PropTypes.string.isRequired, + }, + delete: { + workspace: PropTypes.instanceOf(Workspace).isRequired, + }, + update: { + workspace: PropTypes.instanceOf(Workspace).isRequired, + }, + activate: { + workspace: PropTypes.instanceOf(Workspace).isRequired, + }, + deactivate: {}, + toggleWorkspaceDrawer: {}, + openWorkspaceSettings: {}, + toggleKeepAllWorkspacesLoadedSetting: {}, + }, + PropTypes.checkPropTypes, +); + +export default workspaceActions; diff --git a/src/features/workspaces/api.js b/src/features/workspaces/api.js deleted file mode 100644 index 322695ed2..000000000 --- a/src/features/workspaces/api.js +++ /dev/null @@ -1,66 +0,0 @@ -import { pick } from 'lodash'; -import { sendAuthRequest } from '../../api/utils/auth'; -import Request from '../../stores/lib/Request'; -import Workspace from './models/Workspace'; -import apiBase from '../../api/apiBase'; - -const debug = require('debug')('Ferdi:feature:workspaces:api'); - -export const workspaceApi = { - getUserWorkspaces: async () => { - const url = `${apiBase()}/workspace`; - debug('getUserWorkspaces GET', url); - const result = await sendAuthRequest(url, { method: 'GET' }); - debug('getUserWorkspaces RESULT', result); - if (!result.ok) throw result; - const workspaces = await result.json(); - return workspaces.map((data) => new Workspace(data)); - }, - - createWorkspace: async (name) => { - const url = `${apiBase()}/workspace`; - const options = { - method: 'POST', - body: JSON.stringify({ name }), - }; - debug('createWorkspace POST', url, options); - const result = await sendAuthRequest(url, options); - debug('createWorkspace RESULT', result); - if (!result.ok) throw result; - return new Workspace(await result.json()); - }, - - deleteWorkspace: async (workspace) => { - const url = `${apiBase()}/workspace/${workspace.id}`; - debug('deleteWorkspace DELETE', url); - const result = await sendAuthRequest(url, { method: 'DELETE' }); - debug('deleteWorkspace RESULT', result); - if (!result.ok) throw result; - return true; - }, - - updateWorkspace: async (workspace) => { - const url = `${apiBase()}/workspace/${workspace.id}`; - const options = { - method: 'PUT', - body: JSON.stringify(pick(workspace, ['name', 'services'])), - }; - debug('updateWorkspace UPDATE', url, options); - const result = await sendAuthRequest(url, options); - debug('updateWorkspace RESULT', result); - if (!result.ok) throw result; - return new Workspace(await result.json()); - }, -}; - -export const getUserWorkspacesRequest = new Request(workspaceApi, 'getUserWorkspaces'); -export const createWorkspaceRequest = new Request(workspaceApi, 'createWorkspace'); -export const deleteWorkspaceRequest = new Request(workspaceApi, 'deleteWorkspace'); -export const updateWorkspaceRequest = new Request(workspaceApi, 'updateWorkspace'); - -export const resetApiRequests = () => { - getUserWorkspacesRequest.reset(); - createWorkspaceRequest.reset(); - deleteWorkspaceRequest.reset(); - updateWorkspaceRequest.reset(); -}; diff --git a/src/features/workspaces/api.ts b/src/features/workspaces/api.ts new file mode 100644 index 000000000..8447fc247 --- /dev/null +++ b/src/features/workspaces/api.ts @@ -0,0 +1,86 @@ +import { pick } from 'lodash'; +import { sendAuthRequest } from '../../api/utils/auth'; +import Request from '../../stores/lib/Request'; +import Workspace from './models/Workspace'; +import apiBase from '../../api/apiBase'; + +const debug = require('debug')('Ferdi:feature:workspaces:api'); + +export const workspaceApi = { + getUserWorkspaces: async () => { + const url = `${apiBase()}/workspace`; + debug('getUserWorkspaces GET', url); + const result = await sendAuthRequest(url, { method: 'GET' }); + debug('getUserWorkspaces RESULT', result); + if (!result.ok) { + throw new Error("Couldn't getUserWorkspaces"); + } + const workspaces = await result.json(); + return workspaces.map(data => new Workspace(data)); + }, + + createWorkspace: async name => { + const url = `${apiBase()}/workspace`; + const options = { + method: 'POST', + body: JSON.stringify({ name }), + }; + debug('createWorkspace POST', url, options); + const result = await sendAuthRequest(url, options); + debug('createWorkspace RESULT', result); + if (!result.ok) { + throw new Error("Couldn't createWorkspace"); + } + return new Workspace(await result.json()); + }, + + deleteWorkspace: async workspace => { + const url = `${apiBase()}/workspace/${workspace.id}`; + debug('deleteWorkspace DELETE', url); + const result = await sendAuthRequest(url, { method: 'DELETE' }); + debug('deleteWorkspace RESULT', result); + if (!result.ok) { + throw new Error("Couldn't deleteWorkspace"); + } + return true; + }, + + updateWorkspace: async workspace => { + const url = `${apiBase()}/workspace/${workspace.id}`; + const options = { + method: 'PUT', + body: JSON.stringify(pick(workspace, ['name', 'services'])), + }; + debug('updateWorkspace UPDATE', url, options); + const result = await sendAuthRequest(url, options); + debug('updateWorkspace RESULT', result); + if (!result.ok) { + throw new Error("Couldn't updateWorkspace"); + } + return new Workspace(await result.json()); + }, +}; + +export const getUserWorkspacesRequest = new Request( + workspaceApi, + 'getUserWorkspaces', +); +export const createWorkspaceRequest = new Request( + workspaceApi, + 'createWorkspace', +); +export const deleteWorkspaceRequest = new Request( + workspaceApi, + 'deleteWorkspace', +); +export const updateWorkspaceRequest = new Request( + workspaceApi, + 'updateWorkspace', +); + +export const resetApiRequests = () => { + getUserWorkspacesRequest.reset(); + createWorkspaceRequest.reset(); + deleteWorkspaceRequest.reset(); + updateWorkspaceRequest.reset(); +}; diff --git a/src/features/workspaces/constants.js b/src/features/workspaces/constants.js deleted file mode 100644 index 2d1416ee0..000000000 --- a/src/features/workspaces/constants.js +++ /dev/null @@ -1,4 +0,0 @@ -export const WORKSPACES_ROUTES = { - ROOT: '/settings/workspaces', - EDIT: '/settings/workspaces/:action/:id', -}; diff --git a/src/features/workspaces/constants.ts b/src/features/workspaces/constants.ts new file mode 100644 index 000000000..2d1416ee0 --- /dev/null +++ b/src/features/workspaces/constants.ts @@ -0,0 +1,4 @@ +export const WORKSPACES_ROUTES = { + ROOT: '/settings/workspaces', + EDIT: '/settings/workspaces/:action/:id', +}; diff --git a/src/features/workspaces/index.js b/src/features/workspaces/index.js deleted file mode 100644 index 83e4d9049..000000000 --- a/src/features/workspaces/index.js +++ /dev/null @@ -1,30 +0,0 @@ -import { reaction } from 'mobx'; -import WorkspacesStore from './store'; -import { resetApiRequests } from './api'; - -const debug = require('debug')('Ferdi:feature:workspaces'); - -export const workspaceStore = new WorkspacesStore(); - -export default function initWorkspaces(stores, actions) { - stores.workspaces = workspaceStore; - const { features } = stores; - - // Toggle workspace feature - reaction( - () => ( - features.features.isWorkspaceEnabled - ), - (isEnabled) => { - if (isEnabled && !workspaceStore.isFeatureActive) { - debug('Initializing `workspaces` feature'); - workspaceStore.start(stores, actions); - } else if (workspaceStore.isFeatureActive) { - debug('Disabling `workspaces` feature'); - workspaceStore.stop(); - resetApiRequests(); - } - }, - { fireImmediately: true }, - ); -} diff --git a/src/features/workspaces/index.ts b/src/features/workspaces/index.ts new file mode 100644 index 000000000..ecca64b41 --- /dev/null +++ b/src/features/workspaces/index.ts @@ -0,0 +1,28 @@ +import { reaction } from 'mobx'; +import WorkspacesStore from './store'; +import { resetApiRequests } from './api'; + +const debug = require('debug')('Ferdi:feature:workspaces'); + +export const workspaceStore = new WorkspacesStore(); + +export default function initWorkspaces(stores, actions) { + stores.workspaces = workspaceStore; + const { features } = stores; + + // Toggle workspace feature + reaction( + () => features.features.isWorkspaceEnabled, + isEnabled => { + if (isEnabled && !workspaceStore.isFeatureActive) { + debug('Initializing `workspaces` feature'); + workspaceStore.start(stores, actions); + } else if (workspaceStore.isFeatureActive) { + debug('Disabling `workspaces` feature'); + workspaceStore.stop(); + resetApiRequests(); + } + }, + { fireImmediately: true }, + ); +} diff --git a/src/features/workspaces/models/Workspace.js b/src/features/workspaces/models/Workspace.js deleted file mode 100644 index 14add9437..000000000 --- a/src/features/workspaces/models/Workspace.js +++ /dev/null @@ -1,37 +0,0 @@ -import { observable } from 'mobx'; - -import { KEEP_WS_LOADED_USID } from '../../../config'; - -export default class Workspace { - id = null; - - @observable name = null; - - @observable order = null; - - @observable services = []; - - @observable userId = null; - - constructor(data) { - if (!data.id) { - throw new Error('Workspace requires Id'); - } - - this.id = data.id; - this.name = data.name; - this.order = data.order; - - let { services } = data; - if (data.saving && Boolean(data.keepLoaded)) { - // Keep workspaces loaded - services.push(KEEP_WS_LOADED_USID); - } else if (data.saving && data.services.includes(KEEP_WS_LOADED_USID)) { - // Don't keep loaded - services = services.filter((e) => e !== KEEP_WS_LOADED_USID); - } - this.services.replace(services); - - this.userId = data.userId; - } -} diff --git a/src/features/workspaces/models/Workspace.ts b/src/features/workspaces/models/Workspace.ts new file mode 100644 index 000000000..cd3918fba --- /dev/null +++ b/src/features/workspaces/models/Workspace.ts @@ -0,0 +1,39 @@ +import { observable } from 'mobx'; + +import { KEEP_WS_LOADED_USID } from '../../../config'; + +export default class Workspace { + id = null; + + @observable name = null; + + @observable order = null; + + @observable services = []; + + @observable userId = null; + + constructor(data) { + if (!data.id) { + throw new Error('Workspace requires Id'); + } + + this.id = data.id; + this.name = data.name; + this.order = data.order; + + let { services } = data; + if (data.saving && Boolean(data.keepLoaded)) { + // Keep workspaces loaded + services.push(KEEP_WS_LOADED_USID); + } else if (data.saving && data.services.includes(KEEP_WS_LOADED_USID)) { + // Don't keep loaded + services = services.filter(e => e !== KEEP_WS_LOADED_USID); + } + + // @ts-expect-error Property 'replace' does not exist on type 'never[]'. + this.services.replace(services); + + this.userId = data.userId; + } +} -- cgit v1.2.3-54-g00ecf