@cp949/japanpost-react 1.0.3 → 1.0.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/client.d.ts +296 -1
- package/dist/client.es.js +493 -1
- package/dist/index.d.ts +291 -1
- package/dist/index.es.js +512 -281
- package/package.json +4 -7
- package/dist/src/client.d.ts +0 -7
- package/dist/src/client.d.ts.map +0 -1
- package/dist/src/components/AddressSearchInput.d.ts +0 -8
- package/dist/src/components/AddressSearchInput.d.ts.map +0 -1
- package/dist/src/components/PostalCodeInput.d.ts +0 -8
- package/dist/src/components/PostalCodeInput.d.ts.map +0 -1
- package/dist/src/core/errors.d.ts +0 -11
- package/dist/src/core/errors.d.ts.map +0 -1
- package/dist/src/core/formatters.d.ts +0 -12
- package/dist/src/core/formatters.d.ts.map +0 -1
- package/dist/src/core/normalizers.d.ts +0 -7
- package/dist/src/core/normalizers.d.ts.map +0 -1
- package/dist/src/core/types.d.ts +0 -261
- package/dist/src/core/types.d.ts.map +0 -1
- package/dist/src/core/validators.d.ts +0 -6
- package/dist/src/core/validators.d.ts.map +0 -1
- package/dist/src/index.d.ts +0 -11
- package/dist/src/index.d.ts.map +0 -1
- package/dist/src/react/toJapanAddressError.d.ts +0 -8
- package/dist/src/react/toJapanAddressError.d.ts.map +0 -1
- package/dist/src/react/useJapanAddress.d.ts +0 -8
- package/dist/src/react/useJapanAddress.d.ts.map +0 -1
- package/dist/src/react/useJapanAddressSearch.d.ts +0 -7
- package/dist/src/react/useJapanAddressSearch.d.ts.map +0 -1
- package/dist/src/react/useJapanPostalCode.d.ts +0 -7
- package/dist/src/react/useJapanPostalCode.d.ts.map +0 -1
- package/dist/src/react/useLatestRequestState.d.ts +0 -23
- package/dist/src/react/useLatestRequestState.d.ts.map +0 -1
package/dist/client.es.js
CHANGED
|
@@ -1,3 +1,495 @@
|
|
|
1
1
|
"use client";
|
|
2
|
+
import { useState, useMemo, useRef, useCallback, useEffect } from 'react';
|
|
3
|
+
import { jsxs, jsx } from 'react/jsx-runtime';
|
|
2
4
|
|
|
3
|
-
|
|
5
|
+
// src/components/AddressSearchInput.tsx
|
|
6
|
+
function AddressSearchInput({
|
|
7
|
+
defaultValue = "",
|
|
8
|
+
value,
|
|
9
|
+
disabled,
|
|
10
|
+
label = "Address keyword",
|
|
11
|
+
buttonLabel = "Search",
|
|
12
|
+
inputProps,
|
|
13
|
+
buttonProps,
|
|
14
|
+
onChange,
|
|
15
|
+
onSearch
|
|
16
|
+
}) {
|
|
17
|
+
const [internalValue, setInternalValue] = useState(defaultValue);
|
|
18
|
+
const currentValue = value ?? internalValue;
|
|
19
|
+
function handleSubmit(event) {
|
|
20
|
+
event.preventDefault();
|
|
21
|
+
onSearch(currentValue.trim());
|
|
22
|
+
}
|
|
23
|
+
function handleChange(nextValue) {
|
|
24
|
+
if (value === void 0) {
|
|
25
|
+
setInternalValue(nextValue);
|
|
26
|
+
}
|
|
27
|
+
onChange?.(nextValue);
|
|
28
|
+
}
|
|
29
|
+
return /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, children: [
|
|
30
|
+
/* @__PURE__ */ jsxs("label", { children: [
|
|
31
|
+
label,
|
|
32
|
+
/* @__PURE__ */ jsx(
|
|
33
|
+
"input",
|
|
34
|
+
{
|
|
35
|
+
...inputProps,
|
|
36
|
+
disabled,
|
|
37
|
+
value: currentValue,
|
|
38
|
+
onChange: (event) => handleChange(event.target.value)
|
|
39
|
+
}
|
|
40
|
+
)
|
|
41
|
+
] }),
|
|
42
|
+
/* @__PURE__ */ jsx("button", { ...buttonProps, disabled, type: "submit", children: buttonLabel })
|
|
43
|
+
] });
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
// src/core/formatters.ts
|
|
47
|
+
function normalizeJapanPostalCode(value) {
|
|
48
|
+
return value.replace(/[^\d]/g, "");
|
|
49
|
+
}
|
|
50
|
+
function PostalCodeInput({
|
|
51
|
+
defaultValue = "",
|
|
52
|
+
value,
|
|
53
|
+
disabled,
|
|
54
|
+
label = "Postal code",
|
|
55
|
+
buttonLabel = "Search",
|
|
56
|
+
inputProps,
|
|
57
|
+
buttonProps,
|
|
58
|
+
onChange,
|
|
59
|
+
onSearch
|
|
60
|
+
}) {
|
|
61
|
+
const [internalValue, setInternalValue] = useState(defaultValue);
|
|
62
|
+
const currentValue = value ?? internalValue;
|
|
63
|
+
function handleSubmit(event) {
|
|
64
|
+
event.preventDefault();
|
|
65
|
+
onSearch(normalizeJapanPostalCode(currentValue));
|
|
66
|
+
}
|
|
67
|
+
function handleChange(nextValue) {
|
|
68
|
+
if (value === void 0) {
|
|
69
|
+
setInternalValue(nextValue);
|
|
70
|
+
}
|
|
71
|
+
onChange?.(nextValue);
|
|
72
|
+
}
|
|
73
|
+
return /* @__PURE__ */ jsxs("form", { onSubmit: handleSubmit, children: [
|
|
74
|
+
/* @__PURE__ */ jsxs("label", { children: [
|
|
75
|
+
label,
|
|
76
|
+
/* @__PURE__ */ jsx(
|
|
77
|
+
"input",
|
|
78
|
+
{
|
|
79
|
+
...inputProps,
|
|
80
|
+
disabled,
|
|
81
|
+
inputMode: inputProps?.inputMode ?? "numeric",
|
|
82
|
+
value: currentValue,
|
|
83
|
+
onChange: (event) => handleChange(event.target.value)
|
|
84
|
+
}
|
|
85
|
+
)
|
|
86
|
+
] }),
|
|
87
|
+
/* @__PURE__ */ jsx("button", { ...buttonProps, disabled, type: "submit", children: buttonLabel })
|
|
88
|
+
] });
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
// src/core/errors.ts
|
|
92
|
+
function createJapanAddressError(code, message, options) {
|
|
93
|
+
const error = new Error(message);
|
|
94
|
+
error.name = "JapanAddressError";
|
|
95
|
+
error.code = code;
|
|
96
|
+
error.cause = options?.cause;
|
|
97
|
+
error.status = options?.status;
|
|
98
|
+
return error;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// src/react/toJapanAddressError.ts
|
|
102
|
+
function toJapanAddressError(error) {
|
|
103
|
+
if (typeof error === "object" && error !== null && "code" in error && typeof error.code === "string") {
|
|
104
|
+
return error;
|
|
105
|
+
}
|
|
106
|
+
return createJapanAddressError(
|
|
107
|
+
"data_source_error",
|
|
108
|
+
error instanceof Error ? error.message : "Unknown error",
|
|
109
|
+
{
|
|
110
|
+
cause: error
|
|
111
|
+
}
|
|
112
|
+
);
|
|
113
|
+
}
|
|
114
|
+
function useLatestRequestState() {
|
|
115
|
+
const requestIdRef = useRef(0);
|
|
116
|
+
const isMountedRef = useRef(true);
|
|
117
|
+
const abortControllerRef = useRef(null);
|
|
118
|
+
const [loading, setLoading] = useState(false);
|
|
119
|
+
const [data, setData] = useState(null);
|
|
120
|
+
const [error, setError] = useState(null);
|
|
121
|
+
const isCurrentRequest = useCallback((requestId) => {
|
|
122
|
+
return isMountedRef.current && requestId === requestIdRef.current;
|
|
123
|
+
}, []);
|
|
124
|
+
const invalidateCurrentRequest = useCallback(() => {
|
|
125
|
+
requestIdRef.current += 1;
|
|
126
|
+
abortControllerRef.current?.abort();
|
|
127
|
+
abortControllerRef.current = null;
|
|
128
|
+
}, []);
|
|
129
|
+
useEffect(() => {
|
|
130
|
+
isMountedRef.current = true;
|
|
131
|
+
return () => {
|
|
132
|
+
isMountedRef.current = false;
|
|
133
|
+
invalidateCurrentRequest();
|
|
134
|
+
};
|
|
135
|
+
}, [invalidateCurrentRequest]);
|
|
136
|
+
const beginRequest = useCallback(() => {
|
|
137
|
+
const requestId = requestIdRef.current + 1;
|
|
138
|
+
requestIdRef.current = requestId;
|
|
139
|
+
abortControllerRef.current?.abort();
|
|
140
|
+
const abortController = new AbortController();
|
|
141
|
+
abortControllerRef.current = abortController;
|
|
142
|
+
setLoading(true);
|
|
143
|
+
setError(null);
|
|
144
|
+
return {
|
|
145
|
+
requestId,
|
|
146
|
+
signal: abortController.signal
|
|
147
|
+
};
|
|
148
|
+
}, []);
|
|
149
|
+
const setSuccess = useCallback((requestId, result) => {
|
|
150
|
+
if (isCurrentRequest(requestId)) {
|
|
151
|
+
setData(result);
|
|
152
|
+
}
|
|
153
|
+
}, [isCurrentRequest]);
|
|
154
|
+
const setFailure = useCallback(
|
|
155
|
+
(requestId, nextError) => {
|
|
156
|
+
if (isCurrentRequest(requestId)) {
|
|
157
|
+
setError(nextError);
|
|
158
|
+
setData(null);
|
|
159
|
+
}
|
|
160
|
+
return null;
|
|
161
|
+
},
|
|
162
|
+
[isCurrentRequest]
|
|
163
|
+
);
|
|
164
|
+
const cancel = useCallback(() => {
|
|
165
|
+
invalidateCurrentRequest();
|
|
166
|
+
setLoading(false);
|
|
167
|
+
}, [invalidateCurrentRequest]);
|
|
168
|
+
const finishRequest = useCallback((requestId) => {
|
|
169
|
+
if (isCurrentRequest(requestId)) {
|
|
170
|
+
setLoading(false);
|
|
171
|
+
abortControllerRef.current = null;
|
|
172
|
+
}
|
|
173
|
+
}, [isCurrentRequest]);
|
|
174
|
+
const reset = useCallback(() => {
|
|
175
|
+
cancel();
|
|
176
|
+
setData(null);
|
|
177
|
+
setError(null);
|
|
178
|
+
}, [cancel]);
|
|
179
|
+
return {
|
|
180
|
+
loading,
|
|
181
|
+
data,
|
|
182
|
+
error,
|
|
183
|
+
beginRequest,
|
|
184
|
+
setSuccess,
|
|
185
|
+
setFailure,
|
|
186
|
+
finishRequest,
|
|
187
|
+
cancel,
|
|
188
|
+
reset
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// src/react/useJapanAddressSearch.ts
|
|
193
|
+
function resolveAddressSearchDataSource(dataSource) {
|
|
194
|
+
if (dataSource) {
|
|
195
|
+
return dataSource;
|
|
196
|
+
}
|
|
197
|
+
throw new Error("useJapanAddressSearch requires options.dataSource");
|
|
198
|
+
}
|
|
199
|
+
function normalizeSearchText(value) {
|
|
200
|
+
const normalized = value?.trim();
|
|
201
|
+
return normalized ? normalized : void 0;
|
|
202
|
+
}
|
|
203
|
+
function normalizeAddressSearchInput(input) {
|
|
204
|
+
const request = typeof input === "string" ? { addressQuery: input } : input;
|
|
205
|
+
const addressQuery = normalizeSearchText(request.addressQuery);
|
|
206
|
+
const prefCode = normalizeSearchText(request.prefCode);
|
|
207
|
+
const prefName = normalizeSearchText(request.prefName);
|
|
208
|
+
const prefKana = normalizeSearchText(request.prefKana);
|
|
209
|
+
const prefRoma = normalizeSearchText(request.prefRoma);
|
|
210
|
+
const cityCode = normalizeSearchText(request.cityCode);
|
|
211
|
+
const cityName = normalizeSearchText(request.cityName);
|
|
212
|
+
const cityKana = normalizeSearchText(request.cityKana);
|
|
213
|
+
const cityRoma = normalizeSearchText(request.cityRoma);
|
|
214
|
+
const townName = normalizeSearchText(request.townName);
|
|
215
|
+
const townKana = normalizeSearchText(request.townKana);
|
|
216
|
+
const townRoma = normalizeSearchText(request.townRoma);
|
|
217
|
+
if (addressQuery === void 0 && prefCode === void 0 && prefName === void 0 && prefKana === void 0 && prefRoma === void 0 && cityCode === void 0 && cityName === void 0 && cityKana === void 0 && cityRoma === void 0 && townName === void 0 && townKana === void 0 && townRoma === void 0) {
|
|
218
|
+
return null;
|
|
219
|
+
}
|
|
220
|
+
return {
|
|
221
|
+
...addressQuery === void 0 ? {} : { addressQuery },
|
|
222
|
+
...prefCode === void 0 ? {} : { prefCode },
|
|
223
|
+
...prefName === void 0 ? {} : { prefName },
|
|
224
|
+
...prefKana === void 0 ? {} : { prefKana },
|
|
225
|
+
...prefRoma === void 0 ? {} : { prefRoma },
|
|
226
|
+
...cityCode === void 0 ? {} : { cityCode },
|
|
227
|
+
...cityName === void 0 ? {} : { cityName },
|
|
228
|
+
...cityKana === void 0 ? {} : { cityKana },
|
|
229
|
+
...cityRoma === void 0 ? {} : { cityRoma },
|
|
230
|
+
...townName === void 0 ? {} : { townName },
|
|
231
|
+
...townKana === void 0 ? {} : { townKana },
|
|
232
|
+
...townRoma === void 0 ? {} : { townRoma },
|
|
233
|
+
pageNumber: request.pageNumber ?? 0,
|
|
234
|
+
rowsPerPage: request.rowsPerPage ?? 100,
|
|
235
|
+
...request.includeCityDetails === void 0 ? {} : {
|
|
236
|
+
includeCityDetails: request.includeCityDetails
|
|
237
|
+
},
|
|
238
|
+
...request.includePrefectureDetails === void 0 ? {} : {
|
|
239
|
+
includePrefectureDetails: request.includePrefectureDetails
|
|
240
|
+
}
|
|
241
|
+
};
|
|
242
|
+
}
|
|
243
|
+
function waitForAbort(signal) {
|
|
244
|
+
if (signal.aborted) {
|
|
245
|
+
return Promise.resolve(null);
|
|
246
|
+
}
|
|
247
|
+
return new Promise((resolve) => {
|
|
248
|
+
const onAbort = () => {
|
|
249
|
+
signal.removeEventListener("abort", onAbort);
|
|
250
|
+
resolve(null);
|
|
251
|
+
};
|
|
252
|
+
signal.addEventListener("abort", onAbort, { once: true });
|
|
253
|
+
});
|
|
254
|
+
}
|
|
255
|
+
function useJapanAddressSearch(options) {
|
|
256
|
+
const dataSource = useMemo(
|
|
257
|
+
() => resolveAddressSearchDataSource(options.dataSource),
|
|
258
|
+
[options.dataSource]
|
|
259
|
+
);
|
|
260
|
+
const debounceMs = options.debounceMs ?? 0;
|
|
261
|
+
const timeoutRef = useRef(
|
|
262
|
+
null
|
|
263
|
+
);
|
|
264
|
+
const pendingResolveRef = useRef(null);
|
|
265
|
+
const {
|
|
266
|
+
loading,
|
|
267
|
+
data,
|
|
268
|
+
error,
|
|
269
|
+
beginRequest,
|
|
270
|
+
setSuccess,
|
|
271
|
+
setFailure,
|
|
272
|
+
finishRequest,
|
|
273
|
+
cancel: cancelRequestState,
|
|
274
|
+
reset: resetRequestState
|
|
275
|
+
} = useLatestRequestState();
|
|
276
|
+
const clearPendingDebounce = useCallback(
|
|
277
|
+
(result) => {
|
|
278
|
+
if (timeoutRef.current !== null) {
|
|
279
|
+
globalThis.clearTimeout(timeoutRef.current);
|
|
280
|
+
timeoutRef.current = null;
|
|
281
|
+
}
|
|
282
|
+
pendingResolveRef.current?.(result);
|
|
283
|
+
pendingResolveRef.current = null;
|
|
284
|
+
},
|
|
285
|
+
[]
|
|
286
|
+
);
|
|
287
|
+
useEffect(() => {
|
|
288
|
+
return () => {
|
|
289
|
+
clearPendingDebounce(null);
|
|
290
|
+
};
|
|
291
|
+
}, [clearPendingDebounce]);
|
|
292
|
+
const reset = useCallback(() => {
|
|
293
|
+
clearPendingDebounce(null);
|
|
294
|
+
resetRequestState();
|
|
295
|
+
}, [clearPendingDebounce, resetRequestState]);
|
|
296
|
+
const cancel = useCallback(() => {
|
|
297
|
+
clearPendingDebounce(null);
|
|
298
|
+
cancelRequestState();
|
|
299
|
+
}, [clearPendingDebounce, cancelRequestState]);
|
|
300
|
+
const runSearch = useCallback(async (requestId, signal, request) => {
|
|
301
|
+
try {
|
|
302
|
+
const requestOptions = {
|
|
303
|
+
signal
|
|
304
|
+
};
|
|
305
|
+
const searchPromise = dataSource.searchAddress(
|
|
306
|
+
request,
|
|
307
|
+
requestOptions
|
|
308
|
+
);
|
|
309
|
+
const result = await Promise.race([
|
|
310
|
+
searchPromise,
|
|
311
|
+
waitForAbort(signal)
|
|
312
|
+
]);
|
|
313
|
+
if (signal.aborted || result === null) {
|
|
314
|
+
return null;
|
|
315
|
+
}
|
|
316
|
+
setSuccess(requestId, result);
|
|
317
|
+
return result;
|
|
318
|
+
} catch (caughtError) {
|
|
319
|
+
if (signal.aborted) {
|
|
320
|
+
return null;
|
|
321
|
+
}
|
|
322
|
+
return setFailure(requestId, toJapanAddressError(caughtError));
|
|
323
|
+
} finally {
|
|
324
|
+
finishRequest(requestId);
|
|
325
|
+
}
|
|
326
|
+
}, [dataSource, finishRequest, setFailure, setSuccess]);
|
|
327
|
+
const search = useCallback((input) => {
|
|
328
|
+
const request = normalizeAddressSearchInput(input);
|
|
329
|
+
const { requestId, signal } = beginRequest();
|
|
330
|
+
clearPendingDebounce(null);
|
|
331
|
+
if (request === null) {
|
|
332
|
+
const result = setFailure(
|
|
333
|
+
requestId,
|
|
334
|
+
createJapanAddressError(
|
|
335
|
+
"invalid_query",
|
|
336
|
+
"Address query is required"
|
|
337
|
+
)
|
|
338
|
+
);
|
|
339
|
+
finishRequest(requestId);
|
|
340
|
+
return Promise.resolve(result);
|
|
341
|
+
}
|
|
342
|
+
if (debounceMs <= 0) {
|
|
343
|
+
return new Promise((resolve) => {
|
|
344
|
+
pendingResolveRef.current = resolve;
|
|
345
|
+
void runSearch(requestId, signal, request).then((result) => {
|
|
346
|
+
resolve(result);
|
|
347
|
+
if (pendingResolveRef.current === resolve) {
|
|
348
|
+
pendingResolveRef.current = null;
|
|
349
|
+
}
|
|
350
|
+
});
|
|
351
|
+
});
|
|
352
|
+
}
|
|
353
|
+
return new Promise((resolve) => {
|
|
354
|
+
pendingResolveRef.current = resolve;
|
|
355
|
+
timeoutRef.current = globalThis.setTimeout(() => {
|
|
356
|
+
timeoutRef.current = null;
|
|
357
|
+
const pendingResolve = pendingResolveRef.current;
|
|
358
|
+
pendingResolveRef.current = null;
|
|
359
|
+
void runSearch(requestId, signal, request).then((result) => {
|
|
360
|
+
pendingResolve?.(result);
|
|
361
|
+
});
|
|
362
|
+
}, debounceMs);
|
|
363
|
+
});
|
|
364
|
+
}, [beginRequest, clearPendingDebounce, debounceMs, runSearch]);
|
|
365
|
+
return {
|
|
366
|
+
loading,
|
|
367
|
+
data,
|
|
368
|
+
error,
|
|
369
|
+
cancel,
|
|
370
|
+
reset,
|
|
371
|
+
search
|
|
372
|
+
};
|
|
373
|
+
}
|
|
374
|
+
function resolvePostalCodeDataSource(dataSource) {
|
|
375
|
+
if (dataSource) {
|
|
376
|
+
return dataSource;
|
|
377
|
+
}
|
|
378
|
+
throw new Error("useJapanPostalCode requires options.dataSource");
|
|
379
|
+
}
|
|
380
|
+
function useJapanPostalCode(options) {
|
|
381
|
+
const dataSource = useMemo(
|
|
382
|
+
() => resolvePostalCodeDataSource(options.dataSource),
|
|
383
|
+
[options.dataSource]
|
|
384
|
+
);
|
|
385
|
+
const {
|
|
386
|
+
loading,
|
|
387
|
+
data,
|
|
388
|
+
error,
|
|
389
|
+
beginRequest,
|
|
390
|
+
setSuccess,
|
|
391
|
+
setFailure,
|
|
392
|
+
finishRequest,
|
|
393
|
+
cancel,
|
|
394
|
+
reset
|
|
395
|
+
} = useLatestRequestState();
|
|
396
|
+
const search = useCallback(
|
|
397
|
+
async (input) => {
|
|
398
|
+
const { requestId, signal } = beginRequest();
|
|
399
|
+
try {
|
|
400
|
+
const requestInput = typeof input === "string" ? { postalCode: input } : input;
|
|
401
|
+
const postalCode = normalizeJapanPostalCode(requestInput.postalCode);
|
|
402
|
+
if (!/^\d{3,7}$/.test(postalCode)) {
|
|
403
|
+
throw createJapanAddressError(
|
|
404
|
+
"invalid_postal_code",
|
|
405
|
+
"Postal code must contain between 3 and 7 digits"
|
|
406
|
+
);
|
|
407
|
+
}
|
|
408
|
+
const requestOptions = {
|
|
409
|
+
signal
|
|
410
|
+
};
|
|
411
|
+
const request = {
|
|
412
|
+
postalCode,
|
|
413
|
+
pageNumber: requestInput.pageNumber ?? 0,
|
|
414
|
+
rowsPerPage: requestInput.rowsPerPage ?? 100,
|
|
415
|
+
...requestInput.includeParenthesesTown === void 0 ? {} : {
|
|
416
|
+
includeParenthesesTown: requestInput.includeParenthesesTown
|
|
417
|
+
}
|
|
418
|
+
};
|
|
419
|
+
const result = await dataSource.lookupPostalCode(
|
|
420
|
+
request,
|
|
421
|
+
requestOptions
|
|
422
|
+
);
|
|
423
|
+
if (signal.aborted) {
|
|
424
|
+
return null;
|
|
425
|
+
}
|
|
426
|
+
setSuccess(requestId, result);
|
|
427
|
+
return result;
|
|
428
|
+
} catch (caughtError) {
|
|
429
|
+
if (signal.aborted) {
|
|
430
|
+
return null;
|
|
431
|
+
}
|
|
432
|
+
return setFailure(requestId, toJapanAddressError(caughtError));
|
|
433
|
+
} finally {
|
|
434
|
+
finishRequest(requestId);
|
|
435
|
+
}
|
|
436
|
+
},
|
|
437
|
+
[beginRequest, dataSource, finishRequest, setFailure, setSuccess]
|
|
438
|
+
);
|
|
439
|
+
return {
|
|
440
|
+
loading,
|
|
441
|
+
data,
|
|
442
|
+
error,
|
|
443
|
+
cancel,
|
|
444
|
+
reset,
|
|
445
|
+
search
|
|
446
|
+
};
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// src/react/useJapanAddress.ts
|
|
450
|
+
function useJapanAddress(options) {
|
|
451
|
+
const dataSource = useMemo(() => {
|
|
452
|
+
if (options.dataSource) {
|
|
453
|
+
return options.dataSource;
|
|
454
|
+
}
|
|
455
|
+
throw new Error("useJapanAddress requires options.dataSource");
|
|
456
|
+
}, [options.dataSource]);
|
|
457
|
+
const postalCode = useJapanPostalCode({ dataSource });
|
|
458
|
+
const addressSearch = useJapanAddressSearch({
|
|
459
|
+
dataSource,
|
|
460
|
+
debounceMs: options.debounceMs
|
|
461
|
+
});
|
|
462
|
+
const resetPostalCode = postalCode.reset;
|
|
463
|
+
const searchPostalCode = postalCode.search;
|
|
464
|
+
const resetAddressSearch = addressSearch.reset;
|
|
465
|
+
const searchAddressKeyword = addressSearch.search;
|
|
466
|
+
const [activeSearch, setActiveSearch] = useState(null);
|
|
467
|
+
const searchByPostalCode = useCallback(async (input) => {
|
|
468
|
+
resetAddressSearch();
|
|
469
|
+
setActiveSearch("postalCode");
|
|
470
|
+
return searchPostalCode(input);
|
|
471
|
+
}, [resetAddressSearch, searchPostalCode]);
|
|
472
|
+
const searchByAddressQuery = useCallback(async (input) => {
|
|
473
|
+
resetPostalCode();
|
|
474
|
+
setActiveSearch("addressQuery");
|
|
475
|
+
return searchAddressKeyword(input);
|
|
476
|
+
}, [resetPostalCode, searchAddressKeyword]);
|
|
477
|
+
const reset = useCallback(() => {
|
|
478
|
+
resetPostalCode();
|
|
479
|
+
resetAddressSearch();
|
|
480
|
+
setActiveSearch(null);
|
|
481
|
+
}, [resetAddressSearch, resetPostalCode]);
|
|
482
|
+
const data = activeSearch === "postalCode" ? postalCode.data : activeSearch === "addressQuery" ? addressSearch.data : null;
|
|
483
|
+
const error = activeSearch === "postalCode" ? postalCode.error : activeSearch === "addressQuery" ? addressSearch.error : null;
|
|
484
|
+
return {
|
|
485
|
+
// 사용자는 현재 활성 모드만 보더라도, 내부에서는 두 훅 중 하나라도 정리 중이면 로딩으로 본다.
|
|
486
|
+
loading: postalCode.loading || addressSearch.loading,
|
|
487
|
+
data,
|
|
488
|
+
error,
|
|
489
|
+
reset,
|
|
490
|
+
searchByPostalCode,
|
|
491
|
+
searchByAddressQuery
|
|
492
|
+
};
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
export { AddressSearchInput, PostalCodeInput, useJapanAddress, useJapanAddressSearch, useJapanPostalCode };
|