Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
H
Homework83_M11
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Ли Джен Сеп
Homework83_M11
Commits
23c5d165
Commit
23c5d165
authored
Dec 20, 2024
by
bekzat kapan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
#7
Создал файл requestSlice и написал в нем логику передачи данных в Api
parent
e04e5a9f
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
82 additions
and
0 deletions
+82
-0
requestSlice.ts
frontend/src/features/requestSlice.ts
+82
-0
No files found.
frontend/src/features/requestSlice.ts
0 → 100644
View file @
23c5d165
import
{
createSlice
,
createAsyncThunk
}
from
'@reduxjs/toolkit'
;
import
axiosClient
from
'@/helpers/axiosClient'
;
interface
RequestState
{
loading
:
boolean
;
error
?:
string
|
null
;
result
:
string
;
}
interface
IData
{
password
:
string
;
message
:
string
;
}
const
initialState
:
RequestState
=
{
loading
:
false
,
error
:
null
,
result
:
''
,
};
export
const
encodeMessage
=
createAsyncThunk
(
'request/encode'
,
async
(
data
:
IData
)
=>
{
try
{
const
response
=
await
axiosClient
.
post
(
'/encode'
,
data
);
return
response
.
data
.
encoded
;
}
catch
(
error
:
any
)
{
throw
new
Error
(
error
.
response
?.
data
?.
message
||
'Error encoding message'
);
}
}
);
export
const
decodeMessage
=
createAsyncThunk
(
'request/decode'
,
async
(
data
:
IData
)
=>
{
try
{
const
response
=
await
axiosClient
.
post
(
'/decode'
,
data
);
return
response
.
data
.
decoded
;
}
catch
(
error
:
any
)
{
throw
new
Error
(
error
.
response
?.
data
?.
message
||
'Error decoding message'
);
}
}
);
const
requestSlice
=
createSlice
({
name
:
'request'
,
initialState
,
reducers
:
{},
extraReducers
:
(
builder
)
=>
{
builder
.
addCase
(
encodeMessage
.
pending
,
(
state
)
=>
{
state
.
loading
=
true
;
state
.
error
=
null
;
})
.
addCase
(
encodeMessage
.
fulfilled
,
(
state
,
action
)
=>
{
state
.
loading
=
false
;
state
.
result
=
action
.
payload
;
})
.
addCase
(
encodeMessage
.
rejected
,
(
state
,
action
)
=>
{
state
.
loading
=
false
;
state
.
error
=
action
.
error
.
message
||
"Error occured"
;
})
.
addCase
(
decodeMessage
.
pending
,
(
state
)
=>
{
state
.
loading
=
true
;
state
.
error
=
null
;
})
.
addCase
(
encodeMessage
.
fulfilled
,
(
state
,
action
)
=>
{
state
.
loading
=
false
;
state
.
result
=
action
.
payload
;
})
.
addCase
(
encodeMessage
.
rejected
,
(
state
,
action
)
=>
{
state
.
loading
=
false
;
state
.
error
=
action
.
error
.
message
||
"Error occured"
;
});
},
});
export
default
requestSlice
.
reducer
;
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment