Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
C
classwork-55
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
Нұрасыл Қайратұлы
classwork-55
Commits
d4ec6f02
Commit
d4ec6f02
authored
May 21, 2024
by
Nurasyl
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
update async redux
parent
a01fa831
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
74 additions
and
11 deletions
+74
-11
ContactData.tsx
src/containers/ContactData/ContactData.tsx
+6
-7
order.ts
src/interfaces/order.ts
+11
-3
index.ts
src/store/index.ts
+3
-1
orders.slice.ts
src/store/orders.slice.ts
+54
-0
No files found.
src/containers/ContactData/ContactData.tsx
View file @
d4ec6f02
import
React
,
{
useState
}
from
'react'
;
import
{
useLocation
,
useNavigate
}
from
'react-router-dom'
;
import
axiosBurger
from
'@/config/axiosBurger'
;
import
{
Button
}
from
"@/components/UI/Button/Button"
;
import
{
TContactData
}
from
'@/interfaces/contactData'
;
import
{
Modal
}
from
'@/components/UI/Modal/Modal'
;
import
{
Spinner
}
from
'@/components/UI/Spinner/Spinner'
;
import
{
useAppSelector
}
from
'@/store'
;
import
{
useAppDispatch
}
from
'@/store'
;
import
{
postOrder
}
from
'@/store/orders.slice'
;
import
'./ContactData.css'
;
export
const
ContactData
=
()
=>
{
const
location
=
useLocation
();
const
navigate
=
useNavigate
();
const
{
ingredints
}
=
useAppSelector
(
state
=>
state
);
const
dispatch
=
useAppDispatch
();
const
{
ingredints
,
orders
:
{
isLoading
}}
=
useAppSelector
(
state
=>
state
);
const
[
isLoading
,
setIsLoading
]
=
useState
<
boolean
>
(
false
);
const
[
showModal
,
setShowModal
]
=
useState
<
boolean
>
(
false
);
const
[
contactData
,
setContactData
]
=
useState
<
TContactData
>
({
name
:
""
,
...
...
@@ -35,13 +36,11 @@ export const ContactData = () => {
const
order
=
{
ingredints
,
contactData
,
price
:
location
.
state
.
price
price
:
1950
};
if
(
allDataReq
)
{
setIsLoading
(
true
);
await
axiosBurger
.
post
(
"orders.json"
,
order
);
setIsLoading
(
false
);
dispatch
(
postOrder
(
order
))
setContactData
(
prevState
=>
({...
prevState
,
name
:
""
,
street
:
""
,
postal
:
""
,
email
:
""
}));
navigate
({
pathname
:
"/"
});
}
else
{
...
...
src/interfaces/order.ts
View file @
d4ec6f02
...
...
@@ -6,8 +6,16 @@ export interface IGetOrder<T> {
};
export
type
TOrder
=
{
id
:
string
,
id
?
:
string
,
contactData
:
TContactData
,
price
:
number
,
ingredi
ents
:
Ingredients
ingredi
nts
:
Ingredients
,
};
export
type
TOrderState
=
{
contactData
:
TContactData
|
null
,
price
:
number
|
null
,
ingredients
:
Ingredients
|
null
,
isLoading
:
boolean
,
error
:
null
|
Error
}
\ No newline at end of file
src/store/index.ts
View file @
d4ec6f02
import
{
configureStore
}
from
"@reduxjs/toolkit"
;
import
{
TypedUseSelectorHook
,
useDispatch
,
useSelector
}
from
"react-redux"
;
import
{
ingredientsSlice
}
from
"./ingredients.slice"
;
import
{
ordersSlice
}
from
"./orders.slice"
;
export
const
store
=
configureStore
({
reducer
:
{
ingredints
:
ingredientsSlice
.
reducer
ingredints
:
ingredientsSlice
.
reducer
,
orders
:
ordersSlice
.
reducer
}
});
...
...
src/store/orders.slice.ts
0 → 100644
View file @
d4ec6f02
import
{
createAsyncThunk
,
createSlice
}
from
"@reduxjs/toolkit"
;
import
{
TOrder
,
TOrderState
}
from
"@/interfaces/order"
;
import
axiosBurger
from
"@/config/axiosBurger"
;
const
initialState
:
TOrderState
=
{
ingredients
:
null
,
price
:
null
,
contactData
:
null
,
isLoading
:
false
,
error
:
null
};
export
const
postOrder
=
createAsyncThunk
(
"orders/postOrder"
,
async
(
order
:
TOrder
)
=>
{
try
{
const
data
=
await
axiosBurger
.
post
(
"orders.json"
,
order
)
return
data
}
catch
(
error
:
any
)
{
throw
new
Error
(
error
)
}
}
)
export
const
ordersSlice
=
createSlice
({
name
:
"orders"
,
initialState
,
reducers
:
{
},
extraReducers
:
builder
=>
{
builder
.
addCase
(
postOrder
.
pending
,
(
state
)
=>
{
state
.
isLoading
=
true
console
.
log
(
"PENDING"
);
})
.
addCase
(
postOrder
.
rejected
,
(
state
,
action
)
=>
{
state
.
isLoading
=
false
state
.
error
=
action
.
error
as
Error
console
.
log
(
"REJECT"
);
})
.
addCase
(
postOrder
.
fulfilled
,
(
state
,
action
)
=>
{
const
{
ingredients
,
price
,
contactData
}
=
action
.
payload
as
any
console
.
log
(
action
.
payload
,
"FULFILLED"
);
state
.
isLoading
=
false
state
.
ingredients
=
ingredients
state
.
contactData
=
contactData
state
.
price
=
price
})
}
});
\ No newline at end of file
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