-
Notifications
You must be signed in to change notification settings - Fork 19
/
database.actions.ts
86 lines (77 loc) · 3.08 KB
/
database.actions.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
import { createAsyncThunk } from '@reduxjs/toolkit'
import { DatabaseActionsApi, DatabaseMainCallsApi } from 'qovery-typescript-axios'
import { ToastEnum, toast } from '@qovery/shared/toast'
import { fetchDatabasesStatus } from './databases.slice'
const databaseActionApi = new DatabaseActionsApi()
const databaseMainCallsApi = new DatabaseMainCallsApi()
export const postDatabaseActionsRestart = createAsyncThunk<any, { environmentId: string; databaseId: string }>(
'databaseActions/restart',
async (data, { dispatch }) => {
try {
const response = await databaseActionApi.restartDatabase(data.databaseId)
if (response.status === 202 || response.status === 200) {
// refetch status after update
await dispatch(fetchDatabasesStatus({ environmentId: data.environmentId }))
// success message
toast(ToastEnum.SUCCESS, 'Your database is redeploying')
}
return response
} catch (err) {
// error message
return toast(ToastEnum.ERROR, 'Redeploying error', (err as Error).message)
}
}
)
export const postDatabaseActionsDeploy = createAsyncThunk<any, { environmentId: string; databaseId: string }>(
'databaseActions/deploy',
async (data, { dispatch }) => {
try {
const response = await databaseActionApi.deployDatabase(data.databaseId)
if (response.status === 202 || response.status === 200) {
// refetch status after update
await dispatch(fetchDatabasesStatus({ environmentId: data.environmentId }))
}
// success message
toast(ToastEnum.SUCCESS, 'Your database is deploying')
return response
} catch (err) {
// error message
return toast(ToastEnum.ERROR, 'Deploying error', (err as Error).message)
}
}
)
export const postDatabaseActionsStop = createAsyncThunk<any, { environmentId: string; databaseId: string }>(
'databaseActions/stop',
async (data, { dispatch }) => {
try {
const response = await databaseActionApi.stopDatabase(data.databaseId)
if (response.status === 202 || response.status === 200) {
// refetch status after update
await dispatch(fetchDatabasesStatus({ environmentId: data.environmentId }))
// success message
toast(ToastEnum.SUCCESS, 'Your database is stopping')
}
return response
} catch (err) {
// error message
return toast(ToastEnum.ERROR, 'Stopping error', (err as Error).message)
}
}
)
export const deleteDatabaseAction = createAsyncThunk<any, { environmentId: string; databaseId: string }>(
'databaseActions/delete',
async (data, { dispatch }) => {
try {
const response = await databaseMainCallsApi.deleteDatabase(data.databaseId)
if (response.status === 204 || response.status === 200) {
// refetch status after update
await dispatch(fetchDatabasesStatus({ environmentId: data.environmentId }))
// success message
toast(ToastEnum.SUCCESS, 'Your database is being deleted')
}
return response
} catch (err) {
return toast(ToastEnum.ERROR, 'Deleting error', (err as Error).message)
}
}
)