Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
I
inventory-promotion-react
Project
Project
Details
Activity
Releases
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
Ascend
inventory-promotion-react
Commits
aedec205
Commit
aedec205
authored
May 10, 2021
by
Khai Yuan Liew
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
[AFP-12] Work on Search Component
parent
33c9bdb2
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
105 additions
and
10 deletions
+105
-10
apiRequests.js
src/actions/apiRequests.js
+10
-0
Header.jsx
src/component/Header.jsx
+2
-10
Main.jsx
src/component/Main.jsx
+2
-0
Search.jsx
src/component/Search.jsx
+58
-0
SearchResults.jsx
src/component/SearchResults.jsx
+33
-0
No files found.
src/actions/apiRequests.js
View file @
aedec205
...
...
@@ -5,4 +5,14 @@ export const getAllProducts = async data => {
const
res
=
await
axios
.
get
(
`
${
Config
.
inventoryUrl
}
`
);
// console.log(res.data);
return
res
.
data
;
}
export
const
getFilteredProducts
=
async
(
searchTerm
)
=>
{
const
res
=
await
axios
.
get
(
`
${
Config
.
inventoryUrl
}
`
);
return
res
.
data
.
filter
(
product
=>
{
const
productName
=
product
.
name
.
toLowerCase
();
console
.
log
(
"Search Term: "
+
searchTerm
);
console
.
log
(
"Found Product Name: "
+
productName
.
includes
(
searchTerm
));
return
productName
.
includes
(
searchTerm
);
});
}
\ No newline at end of file
src/component/Header.jsx
View file @
aedec205
import
React
,
{
useState
}
from
"react"
;
import
{
Link
}
from
"react-router-dom"
;
import
Search
from
"./Search"
;
const
Header
=
()
=>
{
const
[
show
,
setShow
]
=
useState
(
false
);
...
...
@@ -38,16 +39,7 @@ const Header = () => {
</
Link
>
</
li
>
</
ul
>
<
form
className=
"d-flex"
>
<
input
type=
"search"
className=
"form-control me-2"
placeholder=
"search"
/>
<
button
className=
"btn btn-light"
type=
"submit"
>
GO
</
button
>
</
form
>
<
Search
/>
</
div
>
</
div
>
</
nav
>
...
...
src/component/Main.jsx
View file @
aedec205
...
...
@@ -3,6 +3,7 @@ import { Redirect, Switch } from "react-router";
import
AuthRoute
from
"./AuthRoute"
;
import
ProductForm
from
"./ProductForm"
;
import
ProductGrid
from
"./ProductGrid"
;
import
SearchResults
from
"./SearchResults"
;
import
{
getAllProducts
}
from
"../actions/apiRequests.js"
export
default
function
Main
()
{
...
...
@@ -20,6 +21,7 @@ export default function Main() {
return
(
<
div
>
<
Switch
>
<
AuthRoute
path=
"/searchResults"
><
SearchResults
/></
AuthRoute
>
<
AuthRoute
exact
path=
"/products/new"
>
<
ProductForm
/>
</
AuthRoute
>
...
...
src/component/Search.jsx
View file @
aedec205
import
React
from
'react'
;
import
{
Redirect
,
withRouter
}
from
"react-router-dom"
;
import
{
getFilteredProducts
}
from
'../actions/apiRequests'
;
class
Search
extends
React
.
Component
{
constructor
(
props
){
super
(
props
);
this
.
state
=
{
searchTerm
:
''
,
results
:
[]
};
this
.
submit
=
this
.
submit
.
bind
(
this
);
this
.
changeTerm
=
this
.
changeTerm
.
bind
(
this
);
}
changeTerm
(
event
)
{
this
.
setState
({
searchTerm
:
event
.
target
.
value
});
}
async
submit
(
event
){
console
.
log
(
this
.
state
.
searchTerm
);
const
data
=
await
getFilteredProducts
(
this
.
state
.
searchTerm
)
.
then
(
res
=>
{
this
.
setState
({
result
:
res
.
data
},
()
=>
{
this
.
props
.
history
.
push
(
"/searchResults"
);
});
})
.
catch
(
err
=>
console
.
log
(
err
));
}
//Need to search by name or SKU
render
(){
return
(
<
div
>
<
form
className=
"d-flex"
onSubmit=
{
this
.
submit
}
>
<
input
type=
"search"
className=
"form-control me-2"
placeholder=
"Search for item..."
onChange=
{
this
.
changeTerm
}
/>
<
button
className=
"btn btn-light"
type=
"submit"
>
GO
</
button
>
</
form
>
{
/* {
<Redirect to={{
pathname: '/searchResults',
state: {results: this.state.results}
}} />
} */
}
</
div
>
);
}
}
export
default
withRouter
(
Search
);
\ No newline at end of file
src/component/SearchResults.jsx
0 → 100644
View file @
aedec205
import
React
from
"react"
;
import
Product
from
"./Product.jsx"
;
import
{
Col
,
Container
,
Row
}
from
"react-bootstrap"
;
import
"./../styles/ProductGrid.css"
;
export
default
class
SearchResults
extends
React
.
Component
{
constructor
(
props
){
super
(
props
);
this
.
state
=
{
results
:
[]
}
}
render
(){
return
(
<
div
>
<
h1
id=
"title"
className=
"text-center"
>
Search Results
</
h1
>
<
Container
id=
"prod-grid"
className=
"mt-3"
>
<
Row
xs=
{
1
}
sm=
{
2
}
md=
{
3
}
lg=
{
4
}
>
{
this
.
state
.
results
.
map
((
product
)
=>
{
return
(
<
Col
key=
{
product
.
sku
}
>
<
Product
product=
{
product
}
/>
</
Col
>
);
})
}
</
Row
>
</
Container
>
</
div
>
);
}
}
\ 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