Commit bc6bffcf authored by DESKTOP-J7QTMBR\mikke's avatar DESKTOP-J7QTMBR\mikke
Browse files

filter

parent 85839fed
//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,
......
......@@ -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
......
......@@ -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 >
......
......@@ -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 { getAllData } from '../../utils/APIUtil';
interface IProps {
items?: string[]
......@@ -23,11 +23,16 @@ export class Searchbar extends React.Component<IProps, IState> {
};
}
onSearch() {
getData().then((data) => {
getAllData().then((data) => {
searchbarStore.dispatch({ type: 'setState', NewCountry: data });
})
}
componentDidMount(){
getAllData().then((data) => {
searchbarStore.dispatch({ type: 'setState', NewCountry: data });
})
}
render() {
return (
<div className="searchbar">
......
......@@ -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
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment