Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
IT2810 H21
Team 28
project-3
Commits
bc6bffcf
Commit
bc6bffcf
authored
Oct 31, 2021
by
DESKTOP-J7QTMBR\mikke
Browse files
filter
parent
85839fed
Changes
6
Hide whitespace changes
Inline
Side-by-side
project-3/backend/src/resolver.js
View file @
bc6bffcf
//import Country from './country';
const
Country
=
require
(
'
./country
'
);
function
buildFilters
({
OR
=
[],
continent_contains
,
name_contains
})
{
const
filter
=
(
continent_contains
||
name_contains
)
?
{}
:
null
;
if
(
continent_contains
)
{
filter
.
continent
=
{
$regex
:
`.*
${
continent_contains
}
.*`
};
}
if
(
name_contains
)
{
filter
.
name
=
{
$regex
:
`.*
${
name_contains
}
.*`
};
}
let
filters
=
filter
?
[
filter
]
:
[];
for
(
let
i
=
0
;
i
<
OR
.
length
;
i
++
)
{
filters
=
filters
.
concat
(
buildFilters
(
OR
[
i
]));
}
return
filters
;
}
const
resolvers
=
{
Query
:
{
country
(
parent
,
args
,
context
,
info
)
{
return
country
.
find
(
country
=>
country
.
name
===
args
.
name
);
}
}
}
module
.
exports
=
{
country
:
async
({
filter
})
=>
{
try
{
let
query
=
filter
?
{
$or
:
buildFilters
(
resolvers
)}
:
{};
const
countryFetched
=
await
Country
.
find
(
query
);
return
countryFetched
.
map
((
country
)
=>
{
return
{
...
country
.
_doc
,
_id
:
country
.
_id
,
};
});
}
catch
(
error
)
{
throw
error
;
}
}
}
module
.
exports
=
{
country
:
async
()
=>
{
country
:
async
(
{
filter
}
)
=>
{
try
{
const
countryFetched
=
await
Country
.
find
();
let
query
=
filter
?
{
$or
:
buildFilters
(
filter
)}
:
{};
const
countryFetched
=
await
Country
.
find
(
query
);
return
countryFetched
.
map
((
country
)
=>
{
return
{
...
country
.
_doc
,
...
...
project-3/backend/src/schema.js
View file @
bc6bffcf
...
...
@@ -6,7 +6,7 @@ module.exports = buildSchema(`
_id: ID!
name: String!
native: String
continent: String
!
continent: String
capital: String!
currency: String
languages: [String]
...
...
@@ -16,10 +16,15 @@ module.exports = buildSchema(`
navn: String!
capital: String!
}
type Query {
country
:
[Country!]
country
(filter: ContinentFilter):
[Country!]
}
input ContinentFilter {
OR: [ContinentFilter!]
continent_contains: String
name_contains: String
}
type Mutation {
createCountry(country:CountryInput): Country
...
...
project-3/git
0 → 100644
View file @
bc6bffcf
project-3/src/components/Layout.tsx
View file @
bc6bffcf
...
...
@@ -3,7 +3,6 @@ import { Header } from './header/Header';
import
"
./Layout.css
"
import
{
Searchbar
}
from
'
./searchbar/searchbar
'
;
import
{
Filter
}
from
'
./searchbar/Filter
'
;
import
{
getData
}
from
'
../utils/APIUtil
'
;
interface
IProps
{
title
:
string
...
...
@@ -12,7 +11,6 @@ const Layout: React.FC<IProps> = ({
title
,
children
})
=>
{
getData
()
return
(
<
div
className
=
"Layout"
>
<
React
.
Fragment
>
...
...
project-3/src/components/searchbar/searchbar.tsx
View file @
bc6bffcf
...
...
@@ -4,7 +4,7 @@ import { Country } from '../../interfaces/Country';
import
'
./SearchbarStore.tsx
'
import
{
searchbarStore
}
from
'
./SearchbarStore
'
;
import
{
Search
}
from
'
@navikt/ds-icons
'
;
import
{
getData
}
from
'
../../utils/APIUtil
'
;
import
{
get
All
Data
}
from
'
../../utils/APIUtil
'
;
interface
IProps
{
items
?:
string
[]
...
...
@@ -23,11 +23,16 @@ export class Searchbar extends React.Component<IProps, IState> {
};
}
onSearch
()
{
getData
().
then
((
data
)
=>
{
get
All
Data
().
then
((
data
)
=>
{
searchbarStore
.
dispatch
({
type
:
'
setState
'
,
NewCountry
:
data
});
})
}
componentDidMount
(){
getAllData
().
then
((
data
)
=>
{
searchbarStore
.
dispatch
({
type
:
'
setState
'
,
NewCountry
:
data
});
})
}
render
()
{
return
(
<
div
className
=
"searchbar"
>
...
...
project-3/src/utils/APIUtil.tsx
View file @
bc6bffcf
...
...
@@ -2,11 +2,17 @@ import React from 'react';
import
axios
,
{
AxiosResponse
}
from
'
axios
'
;
import
{
Country
}
from
'
../interfaces/Country
'
;
export
function
getData
():
Promise
<
Country
[]
>
{
return
axios
.
get
(
'
http://localhost:3001/graphql?query=
{country{name, native, continent, capital, currency, languages}}
'
)
function
getData
(
query
:
string
):
Promise
<
Country
[]
>
{
return
axios
.
get
(
'
http://localhost:3001/graphql?query=
'
+
query
)
.
then
((
response
:
AxiosResponse
)
=>
{
console
.
log
(
response
.
data
);
console
.
log
(
response
.
data
.
data
.
country
[
0
]);
console
.
log
(
response
)
if
(
response
.
data
.
data
.
country
===
null
){
return
[]
}
return
response
.
data
.
data
.
country
as
Country
[]
});
}
export
function
getAllData
():
Promise
<
Country
[]
>
{
let
order
:
string
=
'
(order: { capital: Sort }, first: 5)
'
return
getData
(
'
{country
'
+
order
+
'
{name, native, continent, capital, currency, languages}}
'
);
}
\ 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