Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add submodules to InitParams #47

Merged
merged 2 commits into from
Aug 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/hooks/useNaverMapInit/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import type { InitResult, UseNaverMapInit } from "./types";
*/
export const useNaverMapInit: UseNaverMapInit = ({
ncpClientId,
submodules,
onLoad,
onError,
}) => {
Expand All @@ -35,7 +36,7 @@ export const useNaverMapInit: UseNaverMapInit = ({

const initNaverMapScript = async () => {
const scriptInitResult = new Promise<InitResult>((resolve, reject) => {
const script = createNaverMapScriptByClientId(ncpClientId);
const script = createNaverMapScriptByClientId({ ncpClientId, submodules });
insertNaverMapScriptIntoHead(script);

script.addEventListener("load", function () {
Expand Down
7 changes: 7 additions & 0 deletions src/hooks/useNaverMapInit/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
export interface InitParams {
ncpClientId: string;
submodules?: Submodule[]
onLoad?: VoidFunction;
onError?: VoidFunction;
}
Expand All @@ -10,3 +11,9 @@ export interface InitResult {
}

export type UseNaverMapInit = (params: InitParams) => InitResult;

/**
* naver map submodule
* @link https://navermaps.github.io/maps.js.ncp/docs/tutorial-4-Submodules.html
*/
export type Submodule = "panorama" | "geocoder" | "drawing" | "visualization"
13 changes: 10 additions & 3 deletions src/hooks/useNaverMapInit/utils/createNaverMapScriptByClientId.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { SCRIPT_ID } from "../constants";
import type { InitParams } from "../types";

export const createNaverMapScriptByClientId = (
ncpClientId: InitParams["ncpClientId"],
) => {
export const createNaverMapScriptByClientId = ({
ncpClientId,
submodules,
}: Pick<InitParams, "ncpClientId" | "submodules">) => {
const script = document.createElement("script");
let paramsString = `ncpClientId=${ncpClientId}`;

if (submodules?.length) {
paramsString = paramsString.concat(`&submodules=${submodules.join(",")}`);

Check warning on line 12 in src/hooks/useNaverMapInit/utils/createNaverMapScriptByClientId.ts

View workflow job for this annotation

GitHub Actions / ci (16.x)

'paramsString' is assigned a value but never used

Check warning on line 12 in src/hooks/useNaverMapInit/utils/createNaverMapScriptByClientId.ts

View workflow job for this annotation

GitHub Actions / ci (16.x)

'paramsString' is assigned a value but never used
}

script.id = SCRIPT_ID;
script.type = "text/javascript";
script.src = `https://oapi.map.naver.com/openapi/v3/maps.js?ncpClientId=${ncpClientId}`;
Expand Down
Loading