aboutsummaryrefslogtreecommitdiffstats
path: root/src/features
diff options
context:
space:
mode:
Diffstat (limited to 'src/features')
-rw-r--r--src/features/delayApp/Component.js2
-rw-r--r--src/features/workspaces/actions.js22
-rw-r--r--src/features/workspaces/api.js39
-rw-r--r--src/features/workspaces/components/CreateWorkspaceForm.js93
-rw-r--r--src/features/workspaces/components/EditWorkspaceForm.js192
-rw-r--r--src/features/workspaces/components/ServiceListItem.js48
-rw-r--r--src/features/workspaces/components/WorkspaceItem.js42
-rw-r--r--src/features/workspaces/components/WorkspacesDashboard.js85
-rw-r--r--src/features/workspaces/containers/EditWorkspaceScreen.js59
-rw-r--r--src/features/workspaces/containers/WorkspacesScreen.js33
-rw-r--r--src/features/workspaces/index.js67
-rw-r--r--src/features/workspaces/models/Workspace.js25
-rw-r--r--src/features/workspaces/state.js15
-rw-r--r--src/features/workspaces/store.js114
-rw-r--r--src/features/workspaces/styles/workspaces-table.scss53
15 files changed, 888 insertions, 1 deletions
diff --git a/src/features/delayApp/Component.js b/src/features/delayApp/Component.js
index ff84510e8..ff0f1f2f8 100644
--- a/src/features/delayApp/Component.js
+++ b/src/features/delayApp/Component.js
@@ -38,7 +38,7 @@ export default @inject('actions') @injectSheet(styles) @observer class DelayApp
38 38
39 state = { 39 state = {
40 countdown: config.delayDuration, 40 countdown: config.delayDuration,
41 } 41 };
42 42
43 countdownInterval = null; 43 countdownInterval = null;
44 44
diff --git a/src/features/workspaces/actions.js b/src/features/workspaces/actions.js
new file mode 100644
index 000000000..25246de09
--- /dev/null
+++ b/src/features/workspaces/actions.js
@@ -0,0 +1,22 @@
1import PropTypes from 'prop-types';
2import Workspace from './models/Workspace';
3import { createActionsFromDefinitions } from '../../actions/lib/actions';
4
5export default createActionsFromDefinitions({
6 edit: {
7 workspace: PropTypes.instanceOf(Workspace).isRequired,
8 },
9 create: {
10 name: PropTypes.string.isRequired,
11 },
12 delete: {
13 workspace: PropTypes.instanceOf(Workspace).isRequired,
14 },
15 update: {
16 workspace: PropTypes.instanceOf(Workspace).isRequired,
17 },
18 activate: {
19 workspace: PropTypes.instanceOf(Workspace).isRequired,
20 },
21 deactivate: {},
22}, PropTypes.checkPropTypes);
diff --git a/src/features/workspaces/api.js b/src/features/workspaces/api.js
new file mode 100644
index 000000000..733cb5593
--- /dev/null
+++ b/src/features/workspaces/api.js
@@ -0,0 +1,39 @@
1import { pick } from 'lodash';
2import { sendAuthRequest } from '../../api/utils/auth';
3import { API, API_VERSION } from '../../environment';
4
5export default {
6 getUserWorkspaces: async () => {
7 const url = `${API}/${API_VERSION}/workspace`;
8 const request = await sendAuthRequest(url, { method: 'GET' });
9 if (!request.ok) throw request;
10 return request.json();
11 },
12
13 createWorkspace: async (name) => {
14 const url = `${API}/${API_VERSION}/workspace`;
15 const request = await sendAuthRequest(url, {
16 method: 'POST',
17 body: JSON.stringify({ name }),
18 });
19 if (!request.ok) throw request;
20 return request.json();
21 },
22
23 deleteWorkspace: async (workspace) => {
24 const url = `${API}/${API_VERSION}/workspace/${workspace.id}`;
25 const request = await sendAuthRequest(url, { method: 'DELETE' });
26 if (!request.ok) throw request;
27 return request.json();
28 },
29
30 updateWorkspace: async (workspace) => {
31 const url = `${API}/${API_VERSION}/workspace/${workspace.id}`;
32 const request = await sendAuthRequest(url, {
33 method: 'PUT',
34 body: JSON.stringify(pick(workspace, ['name', 'services'])),
35 });
36 if (!request.ok) throw request;
37 return request.json();
38 },
39};
diff --git a/src/features/workspaces/components/CreateWorkspaceForm.js b/src/features/workspaces/components/CreateWorkspaceForm.js
new file mode 100644
index 000000000..83f6e07f7
--- /dev/null
+++ b/src/features/workspaces/components/CreateWorkspaceForm.js
@@ -0,0 +1,93 @@
1import React, { Component } from 'react';
2import PropTypes from 'prop-types';
3import { observer } from 'mobx-react';
4import { defineMessages, intlShape } from 'react-intl';
5import { Input, Button } from '@meetfranz/forms';
6import injectSheet from 'react-jss';
7import Form from '../../../lib/Form';
8import { required } from '../../../helpers/validation-helpers';
9
10const messages = defineMessages({
11 submitButton: {
12 id: 'settings.workspace.add.form.submitButton',
13 defaultMessage: '!!!Save workspace',
14 },
15 name: {
16 id: 'settings.workspace.add.form.name',
17 defaultMessage: '!!!Name',
18 },
19});
20
21const styles = () => ({
22 form: {
23 display: 'flex',
24 },
25 input: {
26 flexGrow: 1,
27 marginRight: '10px',
28 },
29 submitButton: {
30 height: 'inherit',
31 marginTop: '3px',
32 },
33});
34
35@injectSheet(styles) @observer
36class CreateWorkspaceForm extends Component {
37 static contextTypes = {
38 intl: intlShape,
39 };
40
41 static propTypes = {
42 classes: PropTypes.object.isRequired,
43 onSubmit: PropTypes.func.isRequired,
44 };
45
46 form = (() => {
47 const { intl } = this.context;
48 return new Form({
49 fields: {
50 name: {
51 label: intl.formatMessage(messages.name),
52 placeholder: intl.formatMessage(messages.name),
53 value: '',
54 validators: [required],
55 },
56 },
57 });
58 })();
59
60 submitForm() {
61 const { form } = this;
62 form.submit({
63 onSuccess: async (f) => {
64 const { onSubmit } = this.props;
65 onSubmit(f.values());
66 },
67 });
68 }
69
70 render() {
71 const { intl } = this.context;
72 const { classes } = this.props;
73 const { form } = this;
74 return (
75 <div className={classes.form}>
76 <Input
77 className={classes.input}
78 {...form.$('name').bind()}
79 showLabel={false}
80 onEnterKey={this.submitForm.bind(this, form)}
81 />
82 <Button
83 className={classes.submitButton}
84 type="submit"
85 label={intl.formatMessage(messages.submitButton)}
86 onClick={this.submitForm.bind(this, form)}
87 />
88 </div>
89 );
90 }
91}
92
93export default CreateWorkspaceForm;
diff --git a/src/features/workspaces/components/EditWorkspaceForm.js b/src/features/workspaces/components/EditWorkspaceForm.js
new file mode 100644
index 000000000..48090f608
--- /dev/null
+++ b/src/features/workspaces/components/EditWorkspaceForm.js
@@ -0,0 +1,192 @@
1import React, { Component } from 'react';
2import PropTypes from 'prop-types';
3import { observer } from 'mobx-react';
4import { defineMessages, intlShape } from 'react-intl';
5import { Link } from 'react-router';
6import { Input, Button } from '@meetfranz/forms';
7import injectSheet from 'react-jss';
8
9import Workspace from '../models/Workspace';
10import Service from '../../../models/Service';
11import Form from '../../../lib/Form';
12import { required } from '../../../helpers/validation-helpers';
13import ServiceListItem from './ServiceListItem';
14
15const messages = defineMessages({
16 buttonDelete: {
17 id: 'settings.workspace.form.buttonDelete',
18 defaultMessage: '!!!Delete workspace',
19 },
20 buttonSave: {
21 id: 'settings.workspace.form.buttonSave',
22 defaultMessage: '!!!Save workspace',
23 },
24 name: {
25 id: 'settings.workspace.form.name',
26 defaultMessage: '!!!Name',
27 },
28 yourWorkspaces: {
29 id: 'settings.workspace.form.yourWorkspaces',
30 defaultMessage: '!!!Your workspaces',
31 },
32 servicesInWorkspaceHeadline: {
33 id: 'settings.workspace.form.servicesInWorkspaceHeadline',
34 defaultMessage: '!!!Services in this Workspace',
35 },
36});
37
38const styles = () => ({
39 nameInput: {
40 height: 'auto',
41 },
42 serviceList: {
43 height: 'auto',
44 },
45});
46
47@injectSheet(styles) @observer
48class EditWorkspaceForm extends Component {
49 static contextTypes = {
50 intl: intlShape,
51 };
52
53 static propTypes = {
54 classes: PropTypes.object.isRequired,
55 isDeleting: PropTypes.bool.isRequired,
56 isSaving: PropTypes.bool.isRequired,
57 onDelete: PropTypes.func.isRequired,
58 onSave: PropTypes.func.isRequired,
59 services: PropTypes.arrayOf(PropTypes.instanceOf(Service)).isRequired,
60 workspace: PropTypes.instanceOf(Workspace).isRequired,
61 };
62
63 form = this.prepareWorkspaceForm(this.props.workspace);
64
65 componentWillReceiveProps(nextProps) {
66 const { workspace } = this.props;
67 if (workspace.id !== nextProps.workspace.id) {
68 this.form = this.prepareWorkspaceForm(nextProps.workspace);
69 }
70 }
71
72 prepareWorkspaceForm(workspace) {
73 const { intl } = this.context;
74 return new Form({
75 fields: {
76 name: {
77 label: intl.formatMessage(messages.name),
78 placeholder: intl.formatMessage(messages.name),
79 value: workspace.name,
80 validators: [required],
81 },
82 services: {
83 value: workspace.services.slice(),
84 },
85 },
86 });
87 }
88
89 submitForm(form) {
90 form.submit({
91 onSuccess: async (f) => {
92 const { onSave } = this.props;
93 const values = f.values();
94 onSave(values);
95 },
96 onError: async () => {},
97 });
98 }
99
100 toggleService(service) {
101 const servicesField = this.form.$('services');
102 const serviceIds = servicesField.value;
103 if (serviceIds.includes(service.id)) {
104 serviceIds.splice(serviceIds.indexOf(service.id), 1);
105 } else {
106 serviceIds.push(service.id);
107 }
108 servicesField.set(serviceIds);
109 }
110
111 render() {
112 const { intl } = this.context;
113 const {
114 classes,
115 isDeleting,
116 isSaving,
117 onDelete,
118 workspace,
119 services,
120 } = this.props;
121 const { form } = this;
122 const workspaceServices = form.$('services').value;
123 return (
124 <div className="settings__main">
125 <div className="settings__header">
126 <span className="settings__header-item">
127 <Link to="/settings/workspaces">
128 {intl.formatMessage(messages.yourWorkspaces)}
129 </Link>
130 </span>
131 <span className="separator" />
132 <span className="settings__header-item">
133 {workspace.name}
134 </span>
135 </div>
136 <div className="settings__body">
137 <div className={classes.nameInput}>
138 <Input {...form.$('name').bind()} />
139 </div>
140 <h2>{intl.formatMessage(messages.servicesInWorkspaceHeadline)}</h2>
141 <div className={classes.serviceList}>
142 {services.map(s => (
143 <ServiceListItem
144 key={s.id}
145 service={s}
146 isInWorkspace={workspaceServices.includes(s.id)}
147 onToggle={() => this.toggleService(s)}
148 />
149 ))}
150 </div>
151 </div>
152 <div className="settings__controls">
153 {/* ===== Delete Button ===== */}
154 {isDeleting ? (
155 <Button
156 label={intl.formatMessage(messages.buttonDelete)}
157 loaded={false}
158 buttonType="secondary"
159 className="settings__delete-button"
160 disabled
161 />
162 ) : (
163 <Button
164 buttonType="danger"
165 label={intl.formatMessage(messages.buttonDelete)}
166 className="settings__delete-button"
167 onClick={onDelete}
168 />
169 )}
170 {/* ===== Save Button ===== */}
171 {isSaving ? (
172 <Button
173 type="submit"
174 label={intl.formatMessage(messages.buttonSave)}
175 loaded={!isSaving}
176 buttonType="secondary"
177 disabled
178 />
179 ) : (
180 <Button
181 type="submit"
182 label={intl.formatMessage(messages.buttonSave)}
183 onClick={this.submitForm.bind(this, form)}
184 />
185 )}
186 </div>
187 </div>
188 );
189 }
190}
191
192export default EditWorkspaceForm;
diff --git a/src/features/workspaces/components/ServiceListItem.js b/src/features/workspaces/components/ServiceListItem.js
new file mode 100644
index 000000000..146cc5a36
--- /dev/null
+++ b/src/features/workspaces/components/ServiceListItem.js
@@ -0,0 +1,48 @@
1import React, { Component } from 'react';
2import PropTypes from 'prop-types';
3import { observer } from 'mobx-react';
4import injectSheet from 'react-jss';
5import { Toggle } from '@meetfranz/forms';
6
7import Service from '../../../models/Service';
8
9const styles = () => ({
10 service: {
11 height: 'auto',
12 display: 'flex',
13 },
14 name: {
15 marginTop: '4px',
16 },
17});
18
19@injectSheet(styles) @observer
20class ServiceListItem extends Component {
21 static propTypes = {
22 classes: PropTypes.object.isRequired,
23 isInWorkspace: PropTypes.bool.isRequired,
24 onToggle: PropTypes.func.isRequired,
25 service: PropTypes.instanceOf(Service).isRequired,
26 };
27
28 render() {
29 const {
30 classes,
31 isInWorkspace,
32 onToggle,
33 service,
34 } = this.props;
35
36 return (
37 <div className={classes.service}>
38 <Toggle
39 checked={isInWorkspace}
40 onChange={onToggle}
41 label={service.name}
42 />
43 </div>
44 );
45 }
46}
47
48export default ServiceListItem;
diff --git a/src/features/workspaces/components/WorkspaceItem.js b/src/features/workspaces/components/WorkspaceItem.js
new file mode 100644
index 000000000..b2c2a4830
--- /dev/null
+++ b/src/features/workspaces/components/WorkspaceItem.js
@@ -0,0 +1,42 @@
1import React, { Component } from 'react';
2import PropTypes from 'prop-types';
3import { intlShape } from 'react-intl';
4import { observer } from 'mobx-react';
5import classnames from 'classnames';
6import Workspace from '../models/Workspace';
7
8// const messages = defineMessages({});
9
10@observer
11class WorkspaceItem extends Component {
12 static propTypes = {
13 workspace: PropTypes.instanceOf(Workspace).isRequired,
14 onItemClick: PropTypes.func.isRequired,
15 };
16
17 static contextTypes = {
18 intl: intlShape,
19 };
20
21 render() {
22 const { workspace, onItemClick } = this.props;
23 // const { intl } = this.context;
24
25 return (
26 <tr
27 className={classnames({
28 'workspace-table__row': true,
29 })}
30 >
31 <td
32 className="workspace-table__column-name"
33 onClick={() => onItemClick(workspace)}
34 >
35 {workspace.name}
36 </td>
37 </tr>
38 );
39 }
40}
41
42export default WorkspaceItem;
diff --git a/src/features/workspaces/components/WorkspacesDashboard.js b/src/features/workspaces/components/WorkspacesDashboard.js
new file mode 100644
index 000000000..917807302
--- /dev/null
+++ b/src/features/workspaces/components/WorkspacesDashboard.js
@@ -0,0 +1,85 @@
1import React, { Component } from 'react';
2import PropTypes from 'prop-types';
3import { observer, PropTypes as MobxPropTypes } from 'mobx-react';
4import { defineMessages, intlShape } from 'react-intl';
5import injectSheet from 'react-jss';
6
7import Loader from '../../../components/ui/Loader';
8import WorkspaceItem from './WorkspaceItem';
9import CreateWorkspaceForm from './CreateWorkspaceForm';
10
11const messages = defineMessages({
12 headline: {
13 id: 'settings.workspaces.headline',
14 defaultMessage: '!!!Your workspaces',
15 },
16 noServicesAdded: {
17 id: 'settings.workspaces.noWorkspacesAdded',
18 defaultMessage: '!!!You haven\'t added any workspaces yet.',
19 },
20});
21
22const styles = () => ({
23 createForm: {
24 height: 'auto',
25 marginBottom: '20px',
26 },
27});
28
29@observer @injectSheet(styles)
30class WorkspacesDashboard extends Component {
31 static propTypes = {
32 classes: PropTypes.object.isRequired,
33 isLoading: PropTypes.bool.isRequired,
34 onCreateWorkspaceSubmit: PropTypes.func.isRequired,
35 onWorkspaceClick: PropTypes.func.isRequired,
36 workspaces: MobxPropTypes.arrayOrObservableArray.isRequired,
37 };
38
39 static contextTypes = {
40 intl: intlShape,
41 };
42
43 render() {
44 const {
45 workspaces,
46 isLoading,
47 onCreateWorkspaceSubmit,
48 onWorkspaceClick,
49 classes,
50 } = this.props;
51 const { intl } = this.context;
52
53 return (
54 <div className="settings__main">
55 <div className="settings__header">
56 <h1>{intl.formatMessage(messages.headline)}</h1>
57 </div>
58 <div className="settings__body">
59 <div className={classes.body}>
60 <div className={classes.createForm}>
61 <CreateWorkspaceForm onSubmit={onCreateWorkspaceSubmit} />
62 </div>
63 {isLoading ? (
64 <Loader />
65 ) : (
66 <table className="workspace-table">
67 <tbody>
68 {workspaces.map(workspace => (
69 <WorkspaceItem
70 key={workspace.id}
71 workspace={workspace}
72 onItemClick={w => onWorkspaceClick(w)}
73 />
74 ))}
75 </tbody>
76 </table>
77 )}
78 </div>
79 </div>
80 </div>
81 );
82 }
83}
84
85export default WorkspacesDashboard;
diff --git a/src/features/workspaces/containers/EditWorkspaceScreen.js b/src/features/workspaces/containers/EditWorkspaceScreen.js
new file mode 100644
index 000000000..1b13bc2d4
--- /dev/null
+++ b/src/features/workspaces/containers/EditWorkspaceScreen.js
@@ -0,0 +1,59 @@
1import React, { Component } from 'react';
2import { inject, observer } from 'mobx-react';
3import PropTypes from 'prop-types';
4
5import ErrorBoundary from '../../../components/util/ErrorBoundary';
6import EditWorkspaceForm from '../components/EditWorkspaceForm';
7import { workspacesState } from '../state';
8import ServicesStore from '../../../stores/ServicesStore';
9import Workspace from '../models/Workspace';
10
11@inject('stores', 'actions') @observer
12class EditWorkspaceScreen extends Component {
13 static propTypes = {
14 actions: PropTypes.shape({
15 workspace: PropTypes.shape({
16 delete: PropTypes.func.isRequired,
17 }),
18 }).isRequired,
19 stores: PropTypes.shape({
20 services: PropTypes.instanceOf(ServicesStore).isRequired,
21 }).isRequired,
22 };
23
24 onDelete = () => {
25 const { workspaceBeingEdited } = workspacesState;
26 const { actions } = this.props;
27 if (!workspaceBeingEdited) return null;
28 actions.workspaces.delete({ workspace: workspaceBeingEdited });
29 };
30
31 onSave = (values) => {
32 const { workspaceBeingEdited } = workspacesState;
33 const { actions } = this.props;
34 const workspace = new Workspace(
35 Object.assign({}, workspaceBeingEdited, values),
36 );
37 actions.workspaces.update({ workspace });
38 };
39
40 render() {
41 const { workspaceBeingEdited } = workspacesState;
42 const { stores } = this.props;
43 if (!workspaceBeingEdited) return null;
44 return (
45 <ErrorBoundary>
46 <EditWorkspaceForm
47 workspace={workspaceBeingEdited}
48 services={stores.services.all}
49 onDelete={this.onDelete}
50 onSave={this.onSave}
51 isDeleting={false}
52 isSaving={false}
53 />
54 </ErrorBoundary>
55 );
56 }
57}
58
59export default EditWorkspaceScreen;
diff --git a/src/features/workspaces/containers/WorkspacesScreen.js b/src/features/workspaces/containers/WorkspacesScreen.js
new file mode 100644
index 000000000..94e714255
--- /dev/null
+++ b/src/features/workspaces/containers/WorkspacesScreen.js
@@ -0,0 +1,33 @@
1import React, { Component } from 'react';
2import { inject, observer } from 'mobx-react';
3import PropTypes from 'prop-types';
4import { workspacesState } from '../state';
5import WorkspacesDashboard from '../components/WorkspacesDashboard';
6import ErrorBoundary from '../../../components/util/ErrorBoundary';
7
8@inject('actions') @observer
9class WorkspacesScreen extends Component {
10 static propTypes = {
11 actions: PropTypes.shape({
12 workspace: PropTypes.shape({
13 edit: PropTypes.func.isRequired,
14 }),
15 }).isRequired,
16 };
17
18 render() {
19 const { actions } = this.props;
20 return (
21 <ErrorBoundary>
22 <WorkspacesDashboard
23 workspaces={workspacesState.workspaces}
24 isLoading={workspacesState.isLoading}
25 onCreateWorkspaceSubmit={data => actions.workspaces.create(data)}
26 onWorkspaceClick={w => actions.workspaces.edit({ workspace: w })}
27 />
28 </ErrorBoundary>
29 );
30 }
31}
32
33export default WorkspacesScreen;
diff --git a/src/features/workspaces/index.js b/src/features/workspaces/index.js
new file mode 100644
index 000000000..26cadea64
--- /dev/null
+++ b/src/features/workspaces/index.js
@@ -0,0 +1,67 @@
1import { reaction, runInAction } from 'mobx';
2import WorkspacesStore from './store';
3import api from './api';
4import { workspacesState, resetState } from './state';
5
6const debug = require('debug')('Franz:feature:workspaces');
7
8let store = null;
9
10export const filterServicesByActiveWorkspace = (services) => {
11 const { isFeatureActive, activeWorkspace } = workspacesState;
12 if (isFeatureActive && activeWorkspace) {
13 return services.filter(s => activeWorkspace.services.includes(s.id));
14 }
15 return services;
16};
17
18export const getActiveWorkspaceServices = services => (
19 filterServicesByActiveWorkspace(services)
20);
21
22export default function initWorkspaces(stores, actions) {
23 const { features, user } = stores;
24
25 // Toggle workspace feature
26 reaction(
27 () => (
28 features.features.isWorkspaceEnabled && (
29 !features.features.isWorkspacePremiumFeature || user.data.isPremium
30 )
31 ),
32 (isEnabled) => {
33 if (isEnabled) {
34 debug('Initializing `workspaces` feature');
35 store = new WorkspacesStore(stores, api, actions, workspacesState);
36 store.initialize();
37 runInAction(() => { workspacesState.isFeatureActive = true; });
38 } else if (store) {
39 debug('Disabling `workspaces` feature');
40 runInAction(() => { workspacesState.isFeatureActive = false; });
41 store.teardown();
42 store = null;
43 resetState(); // Reset state to default
44 }
45 },
46 {
47 fireImmediately: true,
48 },
49 );
50
51 // Update active service on workspace switches
52 reaction(() => ({
53 isFeatureActive: workspacesState.isFeatureActive,
54 activeWorkspace: workspacesState.activeWorkspace,
55 }), ({ isFeatureActive, activeWorkspace }) => {
56 if (!isFeatureActive) return;
57 if (activeWorkspace) {
58 const services = stores.services.allDisplayed;
59 const activeService = services.find(s => s.isActive);
60 const workspaceServices = filterServicesByActiveWorkspace(services);
61 const isActiveServiceInWorkspace = workspaceServices.includes(activeService);
62 if (!isActiveServiceInWorkspace) {
63 actions.service.setActive({ serviceId: workspaceServices[0].id });
64 }
65 }
66 });
67}
diff --git a/src/features/workspaces/models/Workspace.js b/src/features/workspaces/models/Workspace.js
new file mode 100644
index 000000000..6c73d7095
--- /dev/null
+++ b/src/features/workspaces/models/Workspace.js
@@ -0,0 +1,25 @@
1import { observable } from 'mobx';
2
3export default class Workspace {
4 id = null;
5
6 @observable name = null;
7
8 @observable order = null;
9
10 @observable services = [];
11
12 @observable userId = null;
13
14 constructor(data) {
15 if (!data.id) {
16 throw Error('Workspace requires Id');
17 }
18
19 this.id = data.id;
20 this.name = data.name;
21 this.order = data.order;
22 this.services.replace(data.services);
23 this.userId = data.userId;
24 }
25}
diff --git a/src/features/workspaces/state.js b/src/features/workspaces/state.js
new file mode 100644
index 000000000..963b96f81
--- /dev/null
+++ b/src/features/workspaces/state.js
@@ -0,0 +1,15 @@
1import { observable } from 'mobx';
2
3const defaultState = {
4 activeWorkspace: null,
5 isLoading: false,
6 isFeatureActive: false,
7 workspaces: [],
8 workspaceBeingEdited: null,
9};
10
11export const workspacesState = observable(defaultState);
12
13export function resetState() {
14 Object.assign(workspacesState, defaultState);
15}
diff --git a/src/features/workspaces/store.js b/src/features/workspaces/store.js
new file mode 100644
index 000000000..a2997a0d2
--- /dev/null
+++ b/src/features/workspaces/store.js
@@ -0,0 +1,114 @@
1import { observable, reaction, action } from 'mobx';
2import Store from '../../stores/lib/Store';
3import CachedRequest from '../../stores/lib/CachedRequest';
4import Workspace from './models/Workspace';
5import { matchRoute } from '../../helpers/routing-helpers';
6import workspaceActions from './actions';
7
8const debug = require('debug')('Franz:feature:workspaces');
9
10export default class WorkspacesStore extends Store {
11 @observable allWorkspacesRequest = new CachedRequest(this.api, 'getUserWorkspaces');
12
13 constructor(stores, api, actions, state) {
14 super(stores, api, actions);
15 this.state = state;
16 }
17
18 setup() {
19 debug('fetching workspaces');
20 this.allWorkspacesRequest.execute();
21
22 /**
23 * Update the state workspaces array when workspaces request has results.
24 */
25 reaction(
26 () => this.allWorkspacesRequest.result,
27 workspaces => this._setWorkspaces(workspaces),
28 );
29 /**
30 * Update the loading state when workspace request is executing.
31 */
32 reaction(
33 () => this.allWorkspacesRequest.isExecuting,
34 isExecuting => this._setIsLoading(isExecuting),
35 );
36 /**
37 * Update the state with the workspace to be edited when route matches.
38 */
39 reaction(
40 () => ({
41 pathname: this.stores.router.location.pathname,
42 workspaces: this.state.workspaces,
43 }),
44 ({ pathname }) => {
45 const match = matchRoute('/settings/workspaces/edit/:id', pathname);
46 if (match) {
47 this.state.workspaceBeingEdited = this._getWorkspaceById(match.id);
48 }
49 },
50 );
51
52 workspaceActions.edit.listen(this._edit);
53 workspaceActions.create.listen(this._create);
54 workspaceActions.delete.listen(this._delete);
55 workspaceActions.update.listen(this._update);
56 workspaceActions.activate.listen(this._setActiveWorkspace);
57 workspaceActions.deactivate.listen(this._deactivateActiveWorkspace);
58 }
59
60 _getWorkspaceById = id => this.state.workspaces.find(w => w.id === id);
61
62 @action _setWorkspaces = (workspaces) => {
63 debug('setting user workspaces', workspaces.slice());
64 this.state.workspaces = workspaces.map(data => new Workspace(data));
65 };
66
67 @action _setIsLoading = (isLoading) => {
68 this.state.isLoading = isLoading;
69 };
70
71 @action _edit = ({ workspace }) => {
72 this.stores.router.push(`/settings/workspaces/edit/${workspace.id}`);
73 };
74
75 @action _create = async ({ name }) => {
76 try {
77 const result = await this.api.createWorkspace(name);
78 const workspace = new Workspace(result);
79 this.state.workspaces.push(workspace);
80 this._edit({ workspace });
81 } catch (error) {
82 throw error;
83 }
84 };
85
86 @action _delete = async ({ workspace }) => {
87 try {
88 await this.api.deleteWorkspace(workspace);
89 this.state.workspaces.remove(workspace);
90 this.stores.router.push('/settings/workspaces');
91 } catch (error) {
92 throw error;
93 }
94 };
95
96 @action _update = async ({ workspace }) => {
97 try {
98 await this.api.updateWorkspace(workspace);
99 const localWorkspace = this.state.workspaces.find(ws => ws.id === workspace.id);
100 Object.assign(localWorkspace, workspace);
101 this.stores.router.push('/settings/workspaces');
102 } catch (error) {
103 throw error;
104 }
105 };
106
107 @action _setActiveWorkspace = ({ workspace }) => {
108 this.state.activeWorkspace = workspace;
109 };
110
111 @action _deactivateActiveWorkspace = () => {
112 this.state.activeWorkspace = null;
113 };
114}
diff --git a/src/features/workspaces/styles/workspaces-table.scss b/src/features/workspaces/styles/workspaces-table.scss
new file mode 100644
index 000000000..6d0e7b4f5
--- /dev/null
+++ b/src/features/workspaces/styles/workspaces-table.scss
@@ -0,0 +1,53 @@
1@import '../../../styles/config';
2
3.theme__dark .workspace-table {
4 .workspace-table__column-info .mdi { color: $dark-theme-gray-lightest; }
5
6 .workspace-table__row {
7 border-bottom: 1px solid $dark-theme-gray-darker;
8
9 &:hover { background: $dark-theme-gray-darker; }
10 &.workspace-table__row--disabled { color: $dark-theme-gray; }
11 }
12}
13
14.workspace-table {
15 width: 100%;
16
17 .workspace-table__toggle {
18 width: 60px;
19
20 .franz-form__field {
21 margin-bottom: 0;
22 }
23 }
24
25 .workspace-table__column-action {
26 width: 40px
27 }
28
29 .workspace-table__column-info {
30 width: 40px;
31
32 .mdi {
33 color: $theme-gray-light;
34 display: block;
35 font-size: 18px;
36 }
37 }
38
39 .workspace-table__row {
40 border-bottom: 1px solid $theme-gray-lightest;
41
42 &:hover {
43 cursor: initial;
44 background: $theme-gray-lightest;
45 }
46
47 &.workspace-table__row--disabled {
48 color: $theme-gray-light;
49 }
50 }
51
52 td { padding: 10px; }
53}