1
1
import { createContext , useContext , useEffect , useState , ReactNode } from 'react' ;
2
2
import api , { clearConnectionId , setConnectionId } from '../api' ;
3
3
import React from 'react' ;
4
+ import { useModal } from '../hooks/useModal' ;
4
5
5
6
interface Connection {
6
7
name : string ;
@@ -51,8 +52,12 @@ export const ConnectionsProvider = ({ children }: { children: ReactNode }) => {
51
52
const [ keys , setKeys ] = useState < KeyData [ ] > ( [ ] ) ;
52
53
const [ error , setError ] = useState ( '' ) ;
53
54
55
+ const { showError, showLoading, dismissLoading } = useModal ( )
56
+
57
+
54
58
const handleConnect = async ( { host, port, name } : Omit < Connection , 'id' > ) => {
55
59
try {
60
+ showLoading ( ) ;
56
61
const response = await api . post ( '/connections' , { host, port } ) ;
57
62
const { connectionId } = response . data ;
58
63
setConnectionId ( connectionId ) ;
@@ -68,13 +73,16 @@ export const ConnectionsProvider = ({ children }: { children: ReactNode }) => {
68
73
69
74
setCurrentConnection ( newConnection ) ;
70
75
await handleLoadKeys ( ) ;
76
+ dismissLoading ( )
71
77
} catch ( err ) {
72
- setError ( 'Falha na conexão. Verifique os dados e tente novamente.' ) ;
78
+ dismissLoading ( )
79
+ showError ( 'Falha na conexão. Verifique os dados e tente novamente.' ) ;
73
80
}
74
81
} ;
75
82
76
83
const choseConnection = async ( { name, host, port } : Omit < Connection , 'id' > ) => {
77
84
try {
85
+ showLoading ( ) ;
78
86
const connection = savedConnections . find ( ( c ) => c . host === host && c . port === port ) ;
79
87
80
88
if ( ! connection ) {
@@ -88,12 +96,14 @@ export const ConnectionsProvider = ({ children }: { children: ReactNode }) => {
88
96
setIsConnected ( true ) ;
89
97
setCurrentConnection ( connection ) ;
90
98
await handleLoadKeys ( ) ;
99
+ dismissLoading ( )
91
100
} catch ( err ) {
101
+ dismissLoading ( )
92
102
if ( err . status === 404 ) {
93
103
await handleConnect ( { name, host, port } ) ;
94
104
return ;
95
105
}
96
- setError ( 'Erro ao escolher conexão.' ) ;
106
+ showError ( 'Erro ao escolher conexão.' ) ;
97
107
}
98
108
} ;
99
109
@@ -106,11 +116,14 @@ export const ConnectionsProvider = ({ children }: { children: ReactNode }) => {
106
116
107
117
const handleLoadKeys = async ( ) => {
108
118
try {
119
+ showLoading ( )
109
120
const response = await api . get ( '/keys' ) ;
110
121
const sortedKeys = [ ...response . data ] . sort ( ( a , b ) => a . key . localeCompare ( b . key ) ) ;
111
122
setKeys ( sortedKeys ) ;
123
+ dismissLoading ( )
112
124
} catch ( err ) {
113
- setError ( 'Erro ao carregar chaves.' ) ;
125
+ dismissLoading ( )
126
+ showError ( 'Erro ao carregar chaves.' ) ;
114
127
}
115
128
} ;
116
129
@@ -120,18 +133,19 @@ export const ConnectionsProvider = ({ children }: { children: ReactNode }) => {
120
133
setKeys ( newList ) ;
121
134
await api . post ( '/keys' , { key : newKey . key , value : newKey . value , expires : newKey . timeUntilExpiration } ) ;
122
135
} catch ( error ) {
123
- setError ( 'Erro ao criar chave.' ) ;
136
+ showError ( 'Erro ao criar chave.' ) ;
124
137
}
125
138
} ;
126
139
127
140
const handleEditKey = async ( updatedKey : KeyData ) => {
128
141
try {
142
+ showLoading ( ) ;
129
143
setKeys ( ( prevKeys ) =>
130
144
prevKeys . map ( ( k ) => ( k . key === updatedKey . key ? { ...updatedKey , size : new Blob ( [ updatedKey . value ] ) . size , timeUntilExpiration : updatedKey . timeUntilExpiration ?? 0 } : k ) )
131
145
) ;
132
146
await api . post ( '/keys' , { key : updatedKey . key , value : updatedKey . value , expires : updatedKey . timeUntilExpiration } ) ;
133
147
} catch ( error ) {
134
- setError ( 'Erro ao editar chave.' ) ;
148
+ showError ( 'Erro ao editar chave.' ) ;
135
149
}
136
150
} ;
137
151
@@ -140,7 +154,7 @@ export const ConnectionsProvider = ({ children }: { children: ReactNode }) => {
140
154
await api . delete ( `/keys/${ key } ` ) ;
141
155
setKeys ( ( prevKeys ) => prevKeys . filter ( ( k ) => k . key !== key ) ) ;
142
156
} catch ( error ) {
143
- setError ( 'Erro ao excluir chave.' ) ;
157
+ showError ( 'Erro ao excluir chave.' ) ;
144
158
}
145
159
} ;
146
160
0 commit comments