generated from shampsdev/react-template
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3b0f704
commit 17e20b3
Showing
11 changed files
with
299 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
BACKEND_URL='api.shamps.dev' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,6 +3,7 @@ | |
# ... | ||
.next | ||
next-env.d.ts | ||
.env | ||
|
||
logs | ||
*.log | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { client } from '@/instances/ghql.instance'; | ||
import { Stat } from '@/types/stat.interface'; | ||
import { gql } from '@apollo/client'; | ||
|
||
interface StatQuery { | ||
stats: Stat[]; | ||
} | ||
|
||
const GET_INITIAL_STATS = gql` | ||
query { | ||
stats { | ||
stat_id | ||
name | ||
count | ||
timestamp | ||
} | ||
} | ||
`; | ||
|
||
export const getStats = async () => { | ||
try { | ||
const response = client.query<StatQuery>({ | ||
query: GET_INITIAL_STATS, | ||
}); | ||
|
||
return response; | ||
} catch (error) { | ||
console.error('Error fetching initial stats:', error); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Stat } from '@/types/stat.interface'; | ||
import { gql, useSubscription } from '@apollo/client'; | ||
|
||
export interface StatSubscription { | ||
statCreated: Stat[]; | ||
} | ||
|
||
const STAT_CREATED_SUBSCRIPTION = gql` | ||
subscription { | ||
statCreated { | ||
stat_id | ||
name | ||
count | ||
timestamp | ||
} | ||
} | ||
`; | ||
|
||
export const useStats = () => { | ||
const { data, loading } = useSubscription<StatSubscription>( | ||
STAT_CREATED_SUBSCRIPTION | ||
); | ||
|
||
return { data, loading }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { ApolloClient, InMemoryCache } from '@apollo/client'; | ||
import { WebSocketLink } from '@apollo/client/link/ws'; | ||
import { SubscriptionClient } from 'subscriptions-transport-ws'; | ||
|
||
const wsLink = new WebSocketLink( | ||
new SubscriptionClient(`ws://${process.env.BACKEND_URL}/graphql`, { | ||
reconnect: true, | ||
}) | ||
); | ||
|
||
export const client = new ApolloClient({ | ||
link: wsLink, | ||
cache: new InMemoryCache(), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
export interface Stat { | ||
count: number; | ||
name: string; | ||
timestamp: Date; | ||
stat_id: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.