Skip to content

Commit a8cb122

Browse files
committed
Convert some classes to TypeScript
1 parent 47e2f18 commit a8cb122

File tree

14 files changed

+90
-94
lines changed

14 files changed

+90
-94
lines changed

src/components/cylc/workspace/lumino-widget.js renamed to src/components/cylc/workspace/lumino-widget.ts

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
import { Widget } from '@lumino/widgets'
1919
import { eventBus } from '@/services/eventBus'
20+
import { type Message } from '@lumino/messaging'
2021

2122
/**
2223
* This is a valid Lumino widget, that contains only a dummy div
@@ -31,33 +32,33 @@ import { eventBus } from '@/services/eventBus'
3132
export default class LuminoWidget extends Widget {
3233
/**
3334
* Create a LuminoWidget object.
34-
* @param {string} id - unique ID of the widget
35-
* @param {string} name - text displayed in the widget tab
36-
* @param {boolean} closable - flag that controls whether the tab can be
37-
* closed or not
35+
* @param id - unique ID of the widget
36+
* @param name - text displayed in the widget tab
37+
* @param closable - flag that controls whether the tab can be closed or not
3838
*/
39-
constructor (id, name, closable = true) {
39+
constructor (
40+
id: string,
41+
public name: string,
42+
public readonly closable: boolean = true
43+
) {
4044
super({ node: LuminoWidget.createNode(id) })
4145
this.id = id
42-
this.name = name
43-
this.closable = closable
4446
// classes and flags
4547
this.setFlag(Widget.Flag.DisallowLayout)
4648
this.addClass('content')
4749
}
4850

4951
/**
5052
* Return a dummy div to be used as parent for the Vue component element.
51-
* @param {string} id - widget id
52-
* @return {HTMLElement}
53+
* @param id - widget id
5354
*/
54-
static createNode (id) {
55+
static createNode (id: string): HTMLElement {
5556
const div = document.createElement('div')
5657
div.setAttribute('id', id)
5758
return div
5859
}
5960

60-
onBeforeAttach (msg) {
61+
onBeforeAttach (msg: Message) {
6162
// Set tab title as this is not handled automatically for some reason.
6263
// NOTE: We do this in the onBeforeAttach hook rather than in the constructor
6364
// because the constructor does not get called when we restore layout to the
@@ -76,13 +77,13 @@ export default class LuminoWidget extends Widget {
7677
// super.onActivateRequest(msg)
7778
// }
7879

79-
onCloseRequest (msg) {
80+
onCloseRequest (msg: Message) {
8081
// Emit an event so that the Vue component knows that it needs to be removed too
8182
eventBus.emit('lumino:deleted', this.id)
8283
super.onCloseRequest(msg)
8384
}
8485

85-
onAfterShow (msg) {
86+
onAfterShow (msg: Message) {
8687
// Emit an event so that the Vue component knows that this widget is visible again
8788
eventBus.emit(`lumino:show:${this.id}`)
8889
super.onAfterShow(msg)

src/model/Alert.model.js renamed to src/model/Alert.model.ts

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,17 @@
1717

1818
/** Alert model */
1919
export class Alert {
20-
/**
21-
* @param {Error|string} err - original thrown error to log if possible, or an error message.
22-
* @param {string} color - color of the displayed alert.
23-
* @param {?string} msg - a custom message to display in the alert instead of err.
24-
*/
25-
constructor (err, color, msg = null) {
26-
this.err = err
20+
/** The text content of the displayed alert. */
21+
text: string | Error
22+
23+
constructor (
24+
/** Original thrown error to log if possible, or an error message. */
25+
public err: Error | string,
26+
/** Color of the displayed alert. */
27+
public color: string,
28+
/** A custom message to display in the alert instead of err. */
29+
msg?: string
30+
) {
2731
this.text = msg || err
28-
this.color = color
2932
}
3033
}

src/model/User.model.js

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/model/User.model.ts

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*
2+
* Copyright (C) NIWA & British Crown (Met Office) & Contributors.
3+
*
4+
* This program is free software: you can redistribute it and/or modify
5+
* it under the terms of the GNU General Public License as published by
6+
* the Free Software Foundation, either version 3 of the License, or
7+
* (at your option) any later version.
8+
*
9+
* This program is distributed in the hope that it will be useful,
10+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12+
* GNU General Public License for more details.
13+
*
14+
* You should have received a copy of the GNU General Public License
15+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
16+
*/
17+
18+
export interface User {
19+
/**
20+
* The authenticated user
21+
*/
22+
username: string
23+
/**
24+
* The UIS owner (i.e. the user who's workflows we are looking at)
25+
* (this might not be the authenticated user for multi-user setups)
26+
*/
27+
owner: string
28+
/**
29+
* List of permissions
30+
*/
31+
permissions: string[]
32+
/**
33+
* Single or multi user mode
34+
*/
35+
mode: 'single user' | 'multi user'
36+
/**
37+
* User initials
38+
*/
39+
initials: string
40+
/**
41+
* User avatar color if set
42+
*/
43+
color: string | null
44+
}
File renamed without changes.

src/services/plugin.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
import { createSubscriptionClient, createGraphQLUrls } from '@/graphql'
1919
import SubscriptionWorkflowService from '@/services/workflow.service'
20-
import UserService from '@/services/user.service'
20+
import { UserService } from '@/services/user.service'
2121

2222
/**
2323
* A plugin that loads the application services.

src/services/user.service.js renamed to src/services/user.service.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,18 @@
1616
*/
1717

1818
import axios from 'axios'
19-
import User from '@/model/User.model'
19+
import { type User } from '@/model/User.model'
2020
import { createUrl, getCylcHeaders } from '@/utils/urls'
2121

22-
export default class UserService {
22+
export class UserService {
2323
/**
2424
* Gets the user profile from the backend server.
25-
* @returns {Promise<User>} - a promise that dispatches Vuex action
2625
*/
27-
async getUserProfile () {
26+
async getUserProfile (): Promise<User> {
2827
const { data } = await axios.get(
2928
createUrl('userprofile'),
3029
{ headers: getCylcHeaders() }
3130
)
32-
return new User(data)
31+
return data
3332
}
3433
}

src/store/user.module.js renamed to src/store/user.module.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,18 +15,24 @@
1515
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1616
*/
1717

18-
export const state = () => ({
18+
import { type User } from '@/model/User.model'
19+
20+
type State = {
21+
user: User | null
22+
}
23+
24+
export const state = (): State => ({
1925
user: null,
2026
})
2127

2228
export const mutations = {
23-
SET_USER (state, user) {
29+
SET_USER (state: State, user: User) {
2430
state.user = user
2531
}
2632
}
2733

2834
export const actions = {
29-
setUser ({ commit }, user) {
35+
setUser ({ commit }, user: User) {
3036
commit('SET_USER', user)
3137
}
3238
}

src/utils/urls.js

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -79,12 +79,10 @@ function createUrl (path, websockets = false, baseOnly = false) {
7979
*/
8080
function getCylcHeaders () {
8181
const xsrfToken = document.cookie.match('\\b_xsrf=([^;]*)\\b')
82-
const cylcHeaders = {}
83-
if (Array.isArray(xsrfToken) && xsrfToken.length > 0) {
82+
return {
8483
// pick the last match
85-
cylcHeaders['X-XSRFToken'] = xsrfToken.splice(-1)
84+
'X-XSRFToken': xsrfToken?.pop()
8685
}
87-
return cylcHeaders
8886
}
8987

9088
export {

tests/unit/mixins/graphql.spec.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,14 +17,13 @@
1717

1818
import { shallowMount } from '@vue/test-utils'
1919
import { createStore } from 'vuex'
20-
import User from '@/model/User.model'
2120
import storeOptions from '@/store/options'
2221
import graphqlMixin from '@/mixins/graphql'
2322

2423
describe('GraphQL mixin', () => {
2524
const store = createStore(storeOptions)
2625
it('should create the GraphQL Query variables', () => {
27-
const user = new User({ username: 'cylc', permissions: [], owner: 'owner' })
26+
const user = { username: 'cylc', permissions: [], owner: 'owner' }
2827
store.commit('user/SET_USER', user)
2928
const workflowName = 'test'
3029
const Component = {

0 commit comments

Comments
 (0)