Skip to content

Commit 427540c

Browse files
committed
Created a fresh reference nextjs app for phi hackathon
1 parent d91eb82 commit 427540c

26 files changed

+598
-0
lines changed

apps/hackathon-phi/.babelrc

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
{
2+
"presets": ["@nx/next/babel", "../../libs/shared/babel/web"],
3+
"plugins": []
4+
}

apps/hackathon-phi/.eslintrc.json

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
{
2+
"extends": ["plugin:@nx/react", "../../.eslintrc.json"],
3+
"ignorePatterns": ["!**/*"],
4+
"rules": {},
5+
"overrides": [
6+
{ "files": ["*.ts", "*.tsx", "*.js", "*.jsx"], "rules": {} },
7+
{ "files": ["*.ts", "*.tsx"], "rules": {} },
8+
{ "files": ["*.js", "*.jsx"], "rules": {} }
9+
]
10+
}

apps/hackathon-phi/README.md

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Hackathon
2+
3+
## About
4+
5+
The idea of this app is too tackle the possible hackathon solution.
6+
7+
## Urls
8+
9+
- Dev: N/A
10+
- Staging: N/A
11+
- Production: N/A
12+
13+
## Getting started
14+
15+
```bash
16+
yarn start hackathon-phi
17+
```
18+
19+
## Code owners and maintainers
20+
21+
- [Aranja](https://github.com/orgs/island-is/teams/aranja/members)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
const devConfig = {
2+
production: false,
3+
}
4+
5+
const prodConfig = {
6+
production: true,
7+
}
8+
9+
export default process.env.NODE_ENV === 'production' ? prodConfig : devConfig

apps/hackathon-phi/graphql/client.ts

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
import getConfig from 'next/config'
2+
import fetch from 'cross-fetch'
3+
import {
4+
ApolloClient,
5+
InMemoryCache,
6+
HttpLink,
7+
ApolloLink,
8+
} from '@apollo/client'
9+
import { onError } from '@apollo/client/link/error'
10+
import { RetryLink } from '@apollo/client/link/retry'
11+
import { setContext } from '@apollo/client/link/context'
12+
13+
const { publicRuntimeConfig, serverRuntimeConfig } = getConfig()
14+
const isBrowser: boolean = process.browser
15+
16+
let apolloClient = null
17+
18+
// Polyfill fetch() on the server (used by apollo-client)
19+
if (!isBrowser) {
20+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
21+
;(global as any).fetch = fetch
22+
}
23+
24+
function create(initialState?: any) {
25+
const { graphqlEndpoint: graphqlServerEndpoint } = serverRuntimeConfig
26+
const { graphqlEndpoint: graphqlClientEndpoint } = publicRuntimeConfig
27+
const graphqlEndpoint = graphqlServerEndpoint || graphqlClientEndpoint
28+
const httpLink = new HttpLink({
29+
uri: graphqlEndpoint,
30+
fetch,
31+
})
32+
33+
const retryLink = new RetryLink()
34+
35+
const errorLink = onError(({ graphQLErrors, networkError }) => {
36+
if (graphQLErrors)
37+
graphQLErrors.map(({ message, locations, path }) =>
38+
console.log(
39+
`[GraphQL error]: Message: ${message}, Location: ${locations}, Path: ${path}`,
40+
),
41+
)
42+
43+
if (networkError) console.log(`[Network error]: ${networkError}`)
44+
})
45+
46+
const authLink = setContext((_, { headers }) => {
47+
const token = 'mock_token'
48+
return {
49+
headers: {
50+
...headers,
51+
authorization: token ? `Bearer ${token}` : '',
52+
},
53+
}
54+
})
55+
56+
return new ApolloClient({
57+
link: ApolloLink.from([retryLink, errorLink, authLink, httpLink]),
58+
connectToDevTools: isBrowser,
59+
ssrMode: !isBrowser, // Disables forceFetch on the server (so queries are only run once)
60+
cache: new InMemoryCache().restore(initialState || {}),
61+
})
62+
}
63+
64+
export default function initApollo(initialState?: any) {
65+
// Make sure to create a new client for every server-side request so that data
66+
// isn't shared between connections (which would be bad)
67+
if (!isBrowser) {
68+
return create(initialState)
69+
}
70+
71+
// Reuse client on the client-side
72+
if (!apolloClient) {
73+
apolloClient = create(initialState)
74+
}
75+
76+
return apolloClient
77+
}

apps/hackathon-phi/graphql/index.ts

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { default as client } from './client'

apps/hackathon-phi/index.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
declare module '*.svg' {
2+
const content: any
3+
export const ReactComponent: any
4+
export default content
5+
}

apps/hackathon-phi/jest.config.ts

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
/* eslint-disable */
2+
export default {
3+
preset: './jest.preset.js',
4+
rootDir: '../..',
5+
roots: [__dirname],
6+
transform: {
7+
'^(?!.*\\.(js|jsx|ts|tsx|css|json)$)': '@nx/react/plugins/jest',
8+
'^.+\\.[tj]sx?$': [
9+
'ts-jest',
10+
{ tsconfig: `${__dirname}/tsconfig.spec.json` },
11+
],
12+
},
13+
moduleFileExtensions: ['ts', 'tsx', 'js', 'jsx', 'html'],
14+
coverageDirectory: '<rootDir>/coverage/apps/hackathon-phi',
15+
globals: {},
16+
displayName: 'hackathon-phi',
17+
}

apps/hackathon-phi/next-env.d.ts

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/// <reference types="next" />
2+
/// <reference types="next/image-types/global" />
3+
4+
// NOTE: This file should not be edited
5+
// see https://nextjs.org/docs/basic-features/typescript for more information.

apps/hackathon-phi/next.config.js

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
const { composePlugins, withNx } = require('@nx/next')
2+
const { createVanillaExtractPlugin } = require('@vanilla-extract/next-plugin')
3+
const withVanillaExtract = createVanillaExtractPlugin()
4+
5+
const { INTERNAL_API_URL = 'http://localhost:4444' } = process.env
6+
7+
const graphqlPath = '/api/graphql'
8+
9+
/**
10+
* @type {import('@nx/next/plugins/with-nx').WithNxOptions}
11+
**/
12+
const nextConfig = {
13+
webpack: (config, { isServer, dev }) => {
14+
if (!dev && isServer) {
15+
config.devtool = 'source-map'
16+
}
17+
return config
18+
},
19+
serverRuntimeConfig: {
20+
// Will only be available on the server side
21+
graphqlEndpoint: `${INTERNAL_API_URL}${graphqlPath}`,
22+
},
23+
publicRuntimeConfig: {
24+
// Will be available on both server and client
25+
graphqlEndpoint: graphqlPath,
26+
},
27+
env: {
28+
API_MOCKS: process.env.API_MOCKS ?? '',
29+
},
30+
}
31+
32+
const plugins = [
33+
// Add more Next.js plugins to this list if needed.
34+
withNx,
35+
withVanillaExtract,
36+
]
37+
38+
module.exports = composePlugins(...plugins)(nextConfig)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { About } from '../../screens'
2+
3+
export default About
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Home } from '../../screens'
2+
3+
export default Home

apps/hackathon-phi/pages/_app.tsx

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
import Head from 'next/head'
2+
import React, { FC } from 'react'
3+
import { ApolloProvider } from '@apollo/client/react'
4+
5+
import { appWithLocale } from '@island.is/localization'
6+
7+
import initApollo from '../graphql/client'
8+
9+
const Layout: FC<React.PropsWithChildren<unknown>> = ({ children }) => {
10+
return (
11+
<div>
12+
<Head>
13+
<title>Ísland.is</title>
14+
</Head>
15+
{children}
16+
</div>
17+
)
18+
}
19+
20+
const SupportApplication: any = ({ Component, pageProps }) => {
21+
return (
22+
<ApolloProvider client={initApollo(pageProps.apolloState)}>
23+
<Layout>
24+
<Component {...pageProps} />
25+
</Layout>
26+
</ApolloProvider>
27+
)
28+
}
29+
30+
SupportApplication.getInitialProps = async (appContext) => {
31+
const { Component, ctx } = appContext
32+
const apolloClient = initApollo({})
33+
const customContext = {
34+
...ctx,
35+
apolloClient,
36+
}
37+
const pageProps = (await Component.getInitialProps(customContext)) as any
38+
39+
const apolloState = apolloClient.cache.extract()
40+
41+
return {
42+
pageProps,
43+
apolloState,
44+
}
45+
}
46+
47+
export default appWithLocale(SupportApplication)
+31
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import React from 'react'
2+
import Document, { Html, Head, Main, NextScript } from 'next/document'
3+
import { defaultLanguage } from '@island.is/shared/constants'
4+
5+
interface Props {
6+
lang: string
7+
}
8+
9+
class MyDocument extends Document<Props> {
10+
static async getInitialProps(ctx) {
11+
const initialProps = await Document.getInitialProps(ctx)
12+
13+
return { ...initialProps, lang: ctx?.query?.lang ?? defaultLanguage }
14+
}
15+
16+
render() {
17+
const { lang } = this.props
18+
19+
return (
20+
<Html lang={lang}>
21+
<Head />
22+
<body>
23+
<Main />
24+
<NextScript />
25+
</body>
26+
</Html>
27+
)
28+
}
29+
}
30+
31+
export default MyDocument

apps/hackathon-phi/pages/index.tsx

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { Home } from '../screens'
2+
3+
export default Home

apps/hackathon-phi/project.json

+105
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
{
2+
"name": "hackathon-phi",
3+
"$schema": "../../node_modules/nx/schemas/project-schema.json",
4+
"sourceRoot": "apps/hackathon-phi",
5+
"projectType": "application",
6+
"tags": ["scope:react-next"],
7+
"generators": {},
8+
"targets": {
9+
"build": {
10+
"executor": "@nx/next:build",
11+
"outputs": ["{options.outputPath}"],
12+
"defaultConfiguration": "production",
13+
"options": {
14+
"outputPath": "dist/apps/hackathon-phi"
15+
},
16+
"configurations": {
17+
"development": {
18+
"outputPath": "apps/hackathon-phi"
19+
},
20+
"production": {}
21+
},
22+
"dependsOn": ["build-custom-server"]
23+
},
24+
"build-custom-server": {
25+
"executor": "@nx/webpack:webpack",
26+
"defaultConfiguration": "production",
27+
"options": {
28+
"outputPath": "dist/apps/hackathon-phi",
29+
"main": "apps/hackathon-phi/server.ts",
30+
"tsConfig": "apps/hackathon-phi/tsconfig.server.json",
31+
"maxWorkers": 2,
32+
"assets": [],
33+
"compiler": "tsc",
34+
"target": "node",
35+
"webpackConfig": "apps/hackathon-phi/webpack.config.js"
36+
},
37+
"configurations": {
38+
"development": {},
39+
"production": {
40+
"optimization": true,
41+
"extractLicenses": true,
42+
"inspect": false
43+
}
44+
}
45+
},
46+
"serve": {
47+
"executor": "@nx/next:server",
48+
"defaultConfiguration": "development",
49+
"options": {
50+
"buildTarget": "hackathon-phi:build",
51+
"dev": true,
52+
"customServerTarget": "hackathon-phi:serve-custom-server"
53+
},
54+
"configurations": {
55+
"development": {
56+
"buildTarget": "hackathon-phi:build:development",
57+
"dev": true,
58+
"customServerTarget": "hackathon-phi:serve-custom-server:development"
59+
},
60+
"production": {
61+
"buildTarget": "hackathon-phi:build:production",
62+
"dev": false,
63+
"customServerTarget": "hackathon-phi:serve-custom-server:production"
64+
}
65+
}
66+
},
67+
"serve-custom-server": {
68+
"executor": "@nx/js:node",
69+
"defaultConfiguration": "development",
70+
"options": {
71+
"buildTarget": "hackathon-phi:build-custom-server"
72+
},
73+
"configurations": {
74+
"development": {
75+
"buildTarget": "hackathon-phi:build-custom-server:development"
76+
},
77+
"production": {
78+
"buildTarget": "hackathon-phi:build-custom-server:production"
79+
}
80+
}
81+
},
82+
"export": {
83+
"executor": "@nx/next:export",
84+
"options": {
85+
"buildTarget": "hackathon-phi:build:production"
86+
}
87+
},
88+
"lint": {
89+
"executor": "@nx/eslint:lint"
90+
},
91+
"test": {
92+
"executor": "@nx/jest:jest",
93+
"options": {
94+
"jestConfig": "apps/hackathon-phi/jest.config.ts"
95+
},
96+
"outputs": ["{workspaceRoot}/coverage/apps/hackathon-phi"]
97+
},
98+
"extract-strings": {
99+
"executor": "nx:run-commands",
100+
"options": {
101+
"command": "yarn ts-node -P libs/localization/tsconfig.lib.json libs/localization/scripts/extract 'apps/hackathon-phi/{screens,pages,components}/*.{js,ts,tsx}'"
102+
}
103+
}
104+
}
105+
}

apps/hackathon-phi/public/.gitkeep

Whitespace-only changes.

0 commit comments

Comments
 (0)