Skip to content

Commit

Permalink
Merge pull request #39 from Dataatti/SER-49-env-vars
Browse files Browse the repository at this point in the history
SER-49 Move API URL to env var
  • Loading branch information
PetroSilenius authored Feb 13, 2022
2 parents 6f468b2 + 4900711 commit 3108296
Show file tree
Hide file tree
Showing 8 changed files with 27 additions and 788 deletions.
4 changes: 1 addition & 3 deletions .env.example
Original file line number Diff line number Diff line change
@@ -1,3 +1 @@
# PostgreSQL connection url
DATABASE_CONNECTION_URL=
AUTH_TOKEN=
NEXT_PUBLIC_API_URL=https://sertifikaattilukija.herokuapp.com
2 changes: 1 addition & 1 deletion .github/workflows/quality_check.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
lint_files:
runs-on: 'ubuntu-latest'
env:
DATABASE_CONNECTION_URL: ${{ secrets.DATABASE_CONNECTION_URL }}
NEXT_PUBLIC_API_URL: ${{ secrets.NEXT_PUBLIC_API_URL }}
defaults:
run:
working-directory: ./
Expand Down
19 changes: 2 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,23 +66,8 @@ Open Cypress for developing E2E tests
npm run cypress:open
```

## Data scraping
## Data scraping and database

Data scraping functions are run by [a cron based Github action](/.github/workflows/sync_certificates.yml).
Data scraping and database are found in a separate repository in [sertifikaattilukija-api](https://github.com/Dataatti/sertifikaattilukija-api).

The list of scraped certificate websites is based on the certificates that are part of Business Finland's [Sustainable Travel Finland](https://www.businessfinland.fi/suomalaisille-asiakkaille/palvelut/matkailun-edistaminen/vastuullisuus/sertifioinnit--ohjelmat) program.

## Database connection

The project uses PostgreSQL as its database.

Copy .env.example as .env and change variables according to your own environment

```bash
cp .env.example .env
```

```
# PostgreSQL connection url
DATABASE_CONNECTION_URL=postgres://<USERNAME>:<PASSWORD>@<HOST>:<PORT>/<DATABASE>
```
28 changes: 18 additions & 10 deletions components/SearchForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ const SearchForm = ({
setOffset,
offset,
searchLimit,
resultTotal,
}: {
setCompanies: Dispatch<SetStateAction<Company[]>>;
setResultTotal: Dispatch<SetStateAction<number>>;
setLoading: Dispatch<SetStateAction<boolean>>;
setOffset: Dispatch<SetStateAction<number>>;
offset: number;
searchLimit: number;
resultTotal: number;
}) => {
const router = useRouter();
const [company, setCompany] = useState('');
Expand Down Expand Up @@ -70,24 +72,30 @@ const SearchForm = ({
query.push(`offset=${offset}`);
}

const request = new Request(`https://sertifikaattilukija.herokuapp.com/data?${query.join('&')}`, {
const request = new Request(`${process.env.NEXT_PUBLIC_API_URL}/data?${query.join('&')}`, {
method: 'GET',
headers: new Headers({
'Content-Type': 'application/json',
}),
});
const result = await fetch(request);
const { data, totalResults, resultsFrom } = await result.json();
try {
const result = await fetch(request);
const { data, totalResults, resultsFrom } = await result.json();

if (offset !== 0) {
setCompanies((prevCompanies) => [...prevCompanies, ...data]);
} else {
setCompanies(data);
}
if (resultTotal !== totalResults) setOffset(0);

setResultTotal(totalResults);
if (offset !== 0) {
setCompanies((prevCompanies) => [...prevCompanies, ...data]);
} else {
setCompanies(data);
}

setResultTotal(totalResults);
setOffset(resultsFrom);
} catch (error) {
console.log(error);
}
setLoading(false);
setOffset(resultsFrom);
};

return (
Expand Down
Loading

1 comment on commit 3108296

@vercel
Copy link

@vercel vercel bot commented on 3108296 Feb 13, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.