Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Contribute to GitLab
Sign in
Toggle navigation
B
burger-builder-template
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
Нұрасыл Қайратұлы
burger-builder-template
Commits
03d7b0f3
Commit
03d7b0f3
authored
Feb 03, 2025
by
Нұрасыл Қайратұлы
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
classwork 69
parent
6545a21e
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
108 additions
and
23 deletions
+108
-23
ContactData.tsx
src/containers/Checkout/ContactData/ContactData.tsx
+23
-22
checkout.ts
src/interfaces/checkout.ts
+7
-0
index.ts
src/store/index.ts
+3
-1
contactData.slice.ts
src/store/slice/contactData.slice.ts
+75
-0
No files found.
src/containers/Checkout/ContactData/ContactData.tsx
View file @
03d7b0f3
import
Button
from
"@/components/UI/Button/Button"
;
import
axiosBase
from
"@/config/axiosBase"
;
import
{
useState
}
from
"react"
;
import
{
ICustomer
}
from
"@/interfaces/checkout"
;
import
{
useLocation
}
from
"react-router-dom"
;
import
Spinner
from
"@/components/UI/Spinner/Spinner"
;
import
{
useAppDispatch
,
useAppSelector
}
from
"@/store/hooks"
;
import
{
postContactData
,
setCustomer
}
from
"@/store/slice/contactData.slice"
;
import
{
useEffect
}
from
"react"
;
import
"./ContactData.css"
;
const
ContactData
=
()
=>
{
const
location
=
useLocation
()
const
dispatch
=
useAppDispatch
()
const
[
loading
,
setLoading
]
=
useState
<
boolean
>
(
false
)
const
[
customer
,
setCustomer
]
=
useState
<
ICustomer
>
({
name
:
''
,
email
:
''
,
street
:
''
,
postal
:
''
})
const
{
burger
:
{
ingredients
},
contactData
:
{
loading
,
customer
,
error
}
}
=
useAppSelector
(
state
=>
state
)
useEffect
(()
=>
{
if
(
error
)
{
console
.
log
(
error
)
}
},
[
error
])
const
onChangeCustomer
=
(
e
:
React
.
FormEvent
<
HTMLInputElement
>
)
=>
{
const
{
value
,
name
}
=
e
.
currentTarget
setCustomer
(
prevState
=>
({...
prevState
,
[
name
]:
valu
e
}))
dispatch
(
setCustomer
({
value
,
nam
e
}))
}
const
onOrder
=
async
(
e
:
React
.
SyntheticEvent
<
HTMLFormElement
>
)
=>
{
e
.
preventDefault
()
setLoading
(
true
)
const
data
=
{
contact
:
customer
,
ingredients
:
location
.
state
?.
ingredients
}
try
{
await
axiosBase
.
post
(
'orders.json'
,
data
)
}
catch
(
error
)
{
console
.
error
(
error
)
ingredients
:
ingredients
}
setLoading
(
false
)
dispatch
(
postContactData
(
data
)
)
}
if
(
loading
)
return
<
Spinner
/>
...
...
src/interfaces/checkout.ts
View file @
03d7b0f3
import
{
Ingredients
}
from
"./Ingredients"
export
interface
ICustomer
{
name
:
string
email
:
string
street
:
string
postal
:
string
}
export
interface
IConactData
{
contact
:
ICustomer
,
ingredients
:
Ingredients
}
\ No newline at end of file
src/store/index.ts
View file @
03d7b0f3
import
{
configureStore
}
from
"@reduxjs/toolkit"
;
import
{
burgerSlice
}
from
"./slice/burger.slice"
;
import
{
contactDataSlice
}
from
"./slice/contactData.slice"
;
export
const
store
=
configureStore
({
reducer
:
{
burger
:
burgerSlice
.
reducer
burger
:
burgerSlice
.
reducer
,
contactData
:
contactDataSlice
.
reducer
}
})
...
...
src/store/slice/contactData.slice.ts
0 → 100644
View file @
03d7b0f3
import
axiosBase
from
"@/config/axiosBase"
;
import
{
IConactData
,
ICustomer
}
from
"@/interfaces/checkout"
;
import
{
createAsyncThunk
,
createSlice
,
PayloadAction
}
from
"@reduxjs/toolkit"
;
interface
IState
{
loading
:
boolean
error
:
string
|
null
customer
:
ICustomer
}
const
initialState
:
IState
=
{
loading
:
false
,
error
:
null
,
customer
:
{
name
:
''
,
email
:
''
,
street
:
''
,
postal
:
''
}
}
export
const
postContactData
=
createAsyncThunk
(
'contact/postContactData'
,
async
(
data
:
IConactData
)
=>
{
try
{
await
axiosBase
.
post
(
'orders.json'
,
data
)
}
catch
{
throw
new
Error
(
'Unknown error'
)
}
}
)
export
const
contactDataSlice
=
createSlice
({
name
:
'contact'
,
initialState
,
reducers
:
{
setCustomer
:
(
state
,
action
:
PayloadAction
<
{
name
:
string
,
value
:
string
}
>
):
void
=>
{
const
{
name
,
value
}
=
action
.
payload
;
switch
(
name
)
{
case
'name'
:
state
.
customer
.
name
=
value
break
case
'email'
:
state
.
customer
.
email
=
value
break
case
'postal'
:
state
.
customer
.
postal
=
value
break
case
'street'
:
state
.
customer
.
street
=
value
break
default
:
break
;
}
}
},
extraReducers
(
builder
)
{
builder
.
addCase
(
postContactData
.
pending
,
(
state
)
=>
{
state
.
loading
=
true
state
.
error
=
null
})
.
addCase
(
postContactData
.
rejected
,
(
state
,
action
)
=>
{
state
.
loading
=
false
state
.
error
=
action
.
error
.
message
||
''
})
.
addCase
(
postContactData
.
fulfilled
,
(
state
)
=>
{
state
.
loading
=
false
state
.
error
=
null
})
},
})
export
const
{
setCustomer
}
=
contactDataSlice
.
actions
\ 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