Skip to content

Commit f9ef7c5

Browse files
committed
refactor: enums to union of literals
Signed-off-by: Pedro Lamas <[email protected]>
1 parent 3bed914 commit f9ef7c5

File tree

11 files changed

+48
-69
lines changed

11 files changed

+48
-69
lines changed

src/components/widgets/history/JobHistoryItemStatus.vue

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020
<script lang="ts">
2121
import { Component, Mixins, Prop } from 'vue-property-decorator'
2222
import FilesMixin from '@/mixins/files'
23-
import { HistoryItem, HistoryItemStatus } from '@/store/history/types'
23+
import { HistoryItem } from '@/store/history/types'
2424
2525
@Component({})
2626
export default class JobHistoryItemStatus extends Mixins(FilesMixin) {
2727
@Prop({ type: Object, required: true })
2828
readonly job!: HistoryItem
2929
3030
// get status () {
31-
// if (this.job.status === HistoryItemStatus.Completed) return HistoryItemStatus.Completed
32-
// if (this.job.status === HistoryItemStatus.InProgress) return HistoryItemStatus.InProgress
31+
// if (this.job.status === 'completed') return 'completed'
32+
// if (this.job.status === 'in_progress') return 'in_progress'
3333
// if (this.job.status.indexOf('_')) {
3434
// return this.job.status.split('_').pop()
3535
// }
@@ -53,29 +53,29 @@ export default class JobHistoryItemStatus extends Mixins(FilesMixin) {
5353
5454
get state () {
5555
if (
56-
this.job.status === HistoryItemStatus.Cancelled ||
57-
this.job.status === HistoryItemStatus.Error ||
58-
this.job.status === HistoryItemStatus.Server_Exit
56+
this.job.status === 'cancelled' ||
57+
this.job.status === 'error' ||
58+
this.job.status === 'server_exit'
5959
) return 'error'
6060
6161
if (
62-
this.job.status === HistoryItemStatus.Printing ||
63-
this.job.status === HistoryItemStatus.Completed ||
64-
this.job.status === HistoryItemStatus.InProgress
62+
this.job.status === 'printing' ||
63+
this.job.status === 'completed' ||
64+
this.job.status === 'in_progress'
6565
) return 'success'
6666
6767
if (
68-
this.job.status === HistoryItemStatus.Klippy_Shutdown ||
69-
this.job.status === HistoryItemStatus.Klippy_Disconnect
68+
this.job.status === 'klippy_shutdown' ||
69+
this.job.status === 'klippy_disconnect'
7070
) return 'warning'
7171
7272
return 'success'
7373
}
7474
7575
get inError () {
7676
return (
77-
this.job.status !== HistoryItemStatus.Completed &&
78-
this.job.status !== HistoryItemStatus.InProgress
77+
this.job.status !== 'completed' &&
78+
this.job.status !== 'in_progress'
7979
)
8080
}
8181
}

src/eventBus.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import Vue from 'vue'
22

33
export const EventBus = {
44
bus: new Vue(),
5-
$emit: (text?: string, options = {}): void => {
5+
$emit: (text?: string, options: Partial<FlashMessage> = {}): void => {
66
const opts: FlashMessage = {
77
open: true,
88
timeout: -1,
@@ -23,10 +23,4 @@ export interface FlashMessage {
2323
timeout?: number;
2424
}
2525

26-
export enum FlashMessageTypes {
27-
success = 'success',
28-
error = 'error',
29-
warning = 'warning',
30-
primary = 'primary',
31-
secondary = 'secondary'
32-
}
26+
export type FlashMessageTypes = 'success' | 'error' | 'warning' | 'primary' | 'secondary'

src/plugins/httpClient.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import _Vue from 'vue'
2-
import { EventBus, FlashMessageTypes } from '@/eventBus'
2+
import { EventBus } from '@/eventBus'
33
import { consola } from 'consola'
44
import Axios, { AxiosError, AxiosRequestConfig, AxiosResponse } from 'axios'
55
import { Globals } from '@/globals'
@@ -103,13 +103,13 @@ const createHttpClient = (store: any) => {
103103
switch (error.response.status) {
104104
case 500:
105105
consola.debug(error.response.status, error.message, message)
106-
EventBus.$emit(message || 'Server error', { type: FlashMessageTypes.error })
106+
EventBus.$emit(message || 'Server error', { type: 'error' })
107107
break
108108
case 502:
109109
case 400:
110110
consola.debug(error.response.status, error.message, message)
111111
if (!handledErrorRequests[error.response.status].includes(url)) {
112-
EventBus.$emit(message || 'Server error', { type: FlashMessageTypes.error })
112+
EventBus.$emit(message || 'Server error', { type: 'error' })
113113
}
114114
break
115115
case 401:
@@ -120,11 +120,11 @@ const createHttpClient = (store: any) => {
120120
break
121121
case 404:
122122
consola.debug(error.response.status, error.message, message)
123-
// EventBus.$emit(message || 'Server error', { type: FlashMessageTypes.warning })
123+
// EventBus.$emit(message || 'Server error', { type: 'warning' })
124124
break
125125
default:
126126
consola.debug(error.response.status, error.message)
127-
EventBus.$emit(message || 'Server error', { type: FlashMessageTypes.error })
127+
EventBus.$emit(message || 'Server error', { type: 'error' })
128128
}
129129

130130
return Promise.reject(error)

src/store/gcodePreview/types.ts

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ export interface ArcMove extends LinearMove {
4141

4242
export type Move = LinearMove | ArcMove;
4343

44-
export enum Rotation {
45-
Clockwise = 'clockwise',
46-
CounterClockwise = 'counter-clockwise',
47-
}
44+
export type Rotation = 'clockwise' | 'counter-clockwise'
4845

4946
export interface LayerPaths {
5047
moves: string;
@@ -63,10 +60,7 @@ export interface Point3D extends Point {
6360
z: number;
6461
}
6562

66-
export enum PositioningMode {
67-
Relative = 'relative',
68-
Absolute = 'absolute'
69-
}
63+
export type PositioningMode = 'relative' | 'absolute'
7064

7165
export interface Layer {
7266
move: number;

src/store/history/types.ts

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,13 +29,4 @@ export interface HistoryRollUp {
2929
longest_print: number;
3030
}
3131

32-
export enum HistoryItemStatus {
33-
Completed = 'completed',
34-
Cancelled = 'cancelled',
35-
Error = 'error',
36-
Printing = 'printing',
37-
InProgress = 'in_progress',
38-
Server_Exit = 'server_exit',
39-
Klippy_Shutdown = 'klippy_shutdown',
40-
Klippy_Disconnect = 'klippy_disconnect'
41-
}
32+
export type HistoryItemStatus = 'completed' | 'cancelled' | 'error' | 'printing' | 'in_progress' | 'server_exit' | 'klippy_shutdown' | 'klippy_disconnect'

src/store/notifications/actions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { ActionTree } from 'vuex'
2-
import { EventBus, FlashMessageTypes } from '@/eventBus'
2+
import { EventBus } from '@/eventBus'
33
import { v4 as uuidv4 } from 'uuid'
44
import { NotificationsState, AppPushNotification, AppNotification } from './types'
55
import { RootState } from '../types'
@@ -46,7 +46,7 @@ export const actions: ActionTree<NotificationsState, RootState> = {
4646

4747
if (payload.snackbar) {
4848
// Emit if snackbar is true.
49-
EventBus.$emit(n.title, { type: FlashMessageTypes.error })
49+
EventBus.$emit(n.title, { type: 'error' })
5050
}
5151
}
5252
},

src/store/server/actions.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { RootState } from '../types'
55
import { SocketActions } from '@/api/socketActions'
66
import { Globals } from '@/globals'
77
import { AppPushNotification } from '../notifications/types'
8-
import { EventBus, FlashMessageTypes } from '@/eventBus'
8+
import { EventBus } from '@/eventBus'
99
import i18n from '@/plugins/i18n'
1010

1111
let retryTimeout: number
@@ -87,14 +87,14 @@ export const actions: ActionTree<ServerState, RootState> = {
8787
const message = Object.values(payload.failed)
8888
.join('\n')
8989

90-
EventBus.$emit(message, { type: FlashMessageTypes.error })
90+
EventBus.$emit(message, { type: 'error' })
9191
} else if (payload?.rolled_over && payload.rolled_over.length) {
9292
const applications = payload.rolled_over
9393
.map(Vue.$filters.startCase)
9494
.join(', ')
9595
const message = i18n.tc('app.general.msg.rolledover_logs', 0, { applications })
9696

97-
EventBus.$emit(message, { type: FlashMessageTypes.success })
97+
EventBus.$emit(message, { type: 'success' })
9898
}
9999
},
100100

src/store/socket/actions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { SocketState } from './types'
55
import { RootState } from '../types'
66
import { Globals } from '@/globals'
77
import { SocketActions } from '@/api/socketActions'
8-
import { EventBus, FlashMessageTypes } from '@/eventBus'
8+
import { EventBus } from '@/eventBus'
99
import { upperFirst, camelCase } from 'lodash-es'
1010
import IsKeyOf from '@/util/is-key-of'
1111

@@ -100,7 +100,7 @@ export const actions: ActionTree<SocketState, RootState> = {
100100
message = payload.message
101101
}
102102

103-
EventBus.$emit(message, { type: FlashMessageTypes.error })
103+
EventBus.$emit(message, { type: 'error' })
104104
}
105105
if (payload.code === 503) {
106106
// This indicates klippy is non-responsive, or there's a configuration error

src/store/timelapse/actions.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import { TimelapseState } from './types'
33
import { RootState } from '../types'
44
import { SocketActions } from '@/api/socketActions'
55
import { consola } from 'consola'
6-
import { EventBus, FlashMessageTypes } from '@/eventBus'
6+
import { EventBus } from '@/eventBus'
77
import i18n from '@/plugins/i18n'
88

99
export const actions: ActionTree<TimelapseState, RootState> = {
@@ -39,7 +39,7 @@ export const actions: ActionTree<TimelapseState, RootState> = {
3939
case 'newframe': {
4040
if (payload.status === 'error') {
4141
// open snackbar
42-
EventBus.$emit(i18n.tc('app.timelapse.error.newframe'), { type: FlashMessageTypes.error })
42+
EventBus.$emit(i18n.tc('app.timelapse.error.newframe'), { type: 'error' })
4343
} else {
4444
const count = parseInt(payload.frame)
4545
commit('setLastFrame', {

src/util/gcode-preview.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { ArcMove, Move, Point, Rotation } from '@/store/gcodePreview/types'
1+
import { ArcMove, Move, Point } from '@/store/gcodePreview/types'
22

33
type BinarySearchComparer<T> = (item: T, index: number, array: T[]) => number
44

@@ -60,11 +60,11 @@ function arcIJMoveToSVGPath (toolhead: Point, move: ArcMove): string {
6060
}
6161

6262
switch (move.direction) {
63-
case Rotation.Clockwise:
63+
case 'clockwise':
6464
return 'A' + [
6565
radius, radius, 0, Number(angle < 0), 0, destination.x, destination.y
6666
].join(',')
67-
case Rotation.CounterClockwise:
67+
case 'counter-clockwise':
6868
return '' +
6969
'M' + [destination.x, destination.y].join(',') +
7070
'A' + [radius, radius, 0, Number(angle > 0), 0, toolhead.x, toolhead.y].join(',') +

0 commit comments

Comments
 (0)