-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor(app): pass service through dependency injection (#288)
- Loading branch information
Kohei Asai
authored
Mar 28, 2021
1 parent
277c892
commit 2dc4587
Showing
9 changed files
with
172 additions
and
69 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,32 @@ | ||
import * as React from "react"; | ||
import { ServiceContainer } from "../core/service-container"; | ||
|
||
const ServiceContainerContext = React.createContext<ServiceContainer | null>( | ||
null | ||
); | ||
|
||
export function useService(): ServiceContainer { | ||
const service = React.useContext(ServiceContainerContext); | ||
|
||
if (!service) { | ||
throw new Error("useService() is called outside <ServiceProvider>."); | ||
} | ||
|
||
return service; | ||
} | ||
|
||
interface ServiceProviderProps { | ||
serviceContainer: ServiceContainer; | ||
} | ||
|
||
export const ServiceProvider: React.FC<ServiceProviderProps> = ({ | ||
serviceContainer, | ||
children, | ||
...props | ||
}) => { | ||
return ( | ||
<ServiceContainerContext.Provider value={serviceContainer} {...props}> | ||
{children} | ||
</ServiceContainerContext.Provider> | ||
); | ||
}; |
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,7 @@ | ||
import { I18nDictionaryService } from "../services/i18n-dictionary"; | ||
import { UserMonitoringService } from "../services/user-monitoring"; | ||
|
||
export interface ServiceContainer { | ||
i18nDictionary: I18nDictionaryService; | ||
userMonitoring: UserMonitoringService; | ||
} |
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
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,19 @@ | ||
export abstract class I18nDictionaryService { | ||
abstract fetch(locale: string): Promise<Record<string, any>>; | ||
} | ||
|
||
export class EmptyI18nDictionaryService implements I18nDictionaryService { | ||
async fetch(_locale: string): Promise<Record<string, any>> { | ||
return {}; | ||
} | ||
} | ||
|
||
export class IsomorphicI18nDictionaryService implements I18nDictionaryService { | ||
async fetch(locale: string): Promise<Record<string, any>> { | ||
const response = await fetch( | ||
`https://cdn.simplelocalize.io/${process.env.NEXT_PUBLIC_SIMPLE_LOCALIZE_TOKEN}/_latest/${locale}` | ||
); | ||
|
||
return await response.json(); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,49 @@ | ||
export abstract class UserMonitoringService { | ||
abstract trackPageView(): void; | ||
|
||
abstract trackUiEvent(actionName: string, value?: number): void; | ||
} | ||
|
||
export class EmptyUserMonitoringService implements UserMonitoringService { | ||
trackPageView(): void {} | ||
|
||
trackUiEvent(_actionName: string, _value?: number): void {} | ||
} | ||
|
||
export class BrowserUserMonitoringService implements UserMonitoringService { | ||
constructor() { | ||
this.gtag = (globalThis as any).gtag ? (globalThis as any).gtag : () => {}; | ||
} | ||
|
||
private gtag: (...args: any) => {}; | ||
|
||
trackPageView(): void { | ||
if (!globalThis.document) { | ||
console.warn( | ||
`gtag("event", "page_view") has been called in a non-browser context. It does nothing.` | ||
); | ||
|
||
return; | ||
} | ||
|
||
const url = new URL(globalThis.location.href); | ||
const title = globalThis.document.title; | ||
const href = url.href; | ||
const search = url.searchParams.has("hl") | ||
? `?hl=${url.searchParams.get("hl")}` | ||
: ""; | ||
|
||
this.gtag("event", "page_view", { | ||
page_title: title, | ||
page_location: href, | ||
page_path: url.pathname + search, | ||
}); | ||
} | ||
|
||
trackUiEvent(actionName: string, value?: number): void { | ||
this.gtag("event", actionName, { | ||
event_category: "ui_event", | ||
value: value, | ||
}); | ||
} | ||
} |
2dc4587
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Successfully deployed to the following URLs:
kohei-dev-storybook – ./
kohei-dev-storybook.vercel.app
kohei-dev-storybook-git-master-axross.vercel.app
kohei-dev-storybook-axross.vercel.app
storybook.kohei.dev