@fajarmaulana/komerce-lp-helper 0.2.2 → 0.3.1
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/components/Form.js +7 -30
- package/dist/components/LazyBackground.js +2 -3
- package/dist/components/hooks/useLazyBackground.js +8 -26
- package/dist/constants/index.js +1 -1
- package/dist/constants/regex.js +10 -10
- package/dist/hooks/debounce.js +16 -47
- package/dist/hooks/form.js +26 -65
- package/dist/hooks/router.js +49 -89
- package/dist/hooks/sectionObserver.js +7 -25
- package/dist/hooks/slider.js +36 -55
- package/dist/utils/api.js +261 -434
- package/dist/utils/cookie.js +18 -19
- package/dist/utils/error-provider.js +30 -55
- package/dist/utils/file.js +27 -31
- package/dist/utils/general.d.ts +10 -0
- package/dist/utils/general.js +24 -11
- package/dist/utils/local.js +10 -10
- package/dist/utils/useApi.js +188 -315
- package/package.json +11 -1
package/dist/components/Form.js
CHANGED
|
@@ -1,25 +1,3 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
|
-
var __rest = (this && this.__rest) || function (s, e) {
|
|
13
|
-
var t = {};
|
|
14
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
|
|
15
|
-
t[p] = s[p];
|
|
16
|
-
if (s != null && typeof Object.getOwnPropertySymbols === "function")
|
|
17
|
-
for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
|
|
18
|
-
if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
|
|
19
|
-
t[p[i]] = s[p[i]];
|
|
20
|
-
}
|
|
21
|
-
return t;
|
|
22
|
-
};
|
|
23
1
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
24
2
|
import { useImperativeHandle, useRef } from 'react';
|
|
25
3
|
/**
|
|
@@ -37,19 +15,18 @@ import { useImperativeHandle, useRef } from 'react';
|
|
|
37
15
|
* @property {ComponentPropsWithRef<'form'>} - All attribute of form element.
|
|
38
16
|
* @property {React.ReactNode} [children] - The form’s inner content (inputs, buttons, etc.).
|
|
39
17
|
*/
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
var handleSubmit = function (e) {
|
|
18
|
+
const Form = ({ action, ref, children, ...props }) => {
|
|
19
|
+
const innerRef = useRef(null);
|
|
20
|
+
useImperativeHandle(ref, () => innerRef.current);
|
|
21
|
+
const handleSubmit = (e) => {
|
|
45
22
|
e.preventDefault();
|
|
46
|
-
|
|
23
|
+
const activeEl = document.activeElement;
|
|
47
24
|
activeEl.blur();
|
|
48
25
|
if (!innerRef.current)
|
|
49
26
|
return;
|
|
50
|
-
|
|
27
|
+
const formData = new FormData(innerRef.current);
|
|
51
28
|
action(formData);
|
|
52
29
|
};
|
|
53
|
-
return (_jsx("form",
|
|
30
|
+
return (_jsx("form", { ...props, ref: innerRef, onSubmit: handleSubmit, children: children }));
|
|
54
31
|
};
|
|
55
32
|
export default Form;
|
|
@@ -18,9 +18,8 @@ import { useLazyBackground } from './hooks';
|
|
|
18
18
|
* The component renders a `<div>` with a `ref` attached for the lazy observer.
|
|
19
19
|
* The `url` is passed to `useLazyBackground`, which handles loading behavior internally.
|
|
20
20
|
*/
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
var ref = useLazyBackground({ url: url }).ref;
|
|
21
|
+
const LazyBackground = ({ url, children, className }) => {
|
|
22
|
+
const { ref } = useLazyBackground({ url });
|
|
24
23
|
return (_jsx("div", { ref: ref, className: className || '', children: children }));
|
|
25
24
|
};
|
|
26
25
|
export default memo(LazyBackground);
|
|
@@ -1,19 +1,3 @@
|
|
|
1
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
2
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
3
|
-
if (!m) return o;
|
|
4
|
-
var i = m.call(o), r, ar = [], e;
|
|
5
|
-
try {
|
|
6
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
7
|
-
}
|
|
8
|
-
catch (error) { e = { error: error }; }
|
|
9
|
-
finally {
|
|
10
|
-
try {
|
|
11
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
12
|
-
}
|
|
13
|
-
finally { if (e) throw e.error; }
|
|
14
|
-
}
|
|
15
|
-
return ar;
|
|
16
|
-
};
|
|
17
1
|
import { useEffect, useRef } from 'react';
|
|
18
2
|
/**
|
|
19
3
|
* `useLazyBackground` is a custom React hook that applies a background image
|
|
@@ -21,24 +5,22 @@ import { useEffect, useRef } from 'react';
|
|
|
21
5
|
*
|
|
22
6
|
* @param {TLazyBackground} props - The props object containing the image URL.
|
|
23
7
|
*/
|
|
24
|
-
export
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
var el = ref.current;
|
|
8
|
+
export const useLazyBackground = ({ url }) => {
|
|
9
|
+
const ref = useRef(null);
|
|
10
|
+
useEffect(() => {
|
|
11
|
+
const el = ref.current;
|
|
29
12
|
if (!el)
|
|
30
13
|
return;
|
|
31
|
-
|
|
32
|
-
var _b = __read(_a, 1), entry = _b[0];
|
|
14
|
+
const observer = new IntersectionObserver(([entry], obs) => {
|
|
33
15
|
if (entry.isIntersecting) {
|
|
34
|
-
el.style.backgroundImage =
|
|
16
|
+
el.style.backgroundImage = `url(${url})`;
|
|
35
17
|
obs.unobserve(el);
|
|
36
18
|
}
|
|
37
19
|
});
|
|
38
20
|
observer.observe(el);
|
|
39
|
-
return
|
|
21
|
+
return () => observer.disconnect();
|
|
40
22
|
}, [url]);
|
|
41
23
|
return {
|
|
42
|
-
ref
|
|
24
|
+
ref,
|
|
43
25
|
};
|
|
44
26
|
};
|
package/dist/constants/index.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
export
|
|
1
|
+
export const MOBILE_BOUND = 640;
|
package/dist/constants/regex.js
CHANGED
|
@@ -1,40 +1,40 @@
|
|
|
1
|
-
export
|
|
1
|
+
export const VALID_EMAIL = {
|
|
2
2
|
re: /^(?![.])[A-Za-z0-9._-]+(?<![.])@[A-Za-z0-9-]+(\.[A-Za-z0-9-]+)*\.[A-Za-z]{2,4}$/,
|
|
3
3
|
text: 'masukkan email yang valid',
|
|
4
4
|
};
|
|
5
|
-
export
|
|
5
|
+
export const DOTALPHANUM = {
|
|
6
6
|
re: /^[a-zA-Z0-9._-]+$/,
|
|
7
7
|
text: " hanya boleh berisi huruf, angka, '.', '_' atau '-'",
|
|
8
8
|
};
|
|
9
|
-
export
|
|
9
|
+
export const REPEATED_DOT = {
|
|
10
10
|
re: /(\.\.|__|--)/,
|
|
11
11
|
text: ' tidak boleh berisi simbol berulang',
|
|
12
12
|
};
|
|
13
|
-
export
|
|
13
|
+
export const NUMBER_ONLY = {
|
|
14
14
|
re: /[^\d]/,
|
|
15
15
|
text: ' hanya boleh berisi angka',
|
|
16
16
|
};
|
|
17
|
-
export
|
|
17
|
+
export const ALPHASPACE = {
|
|
18
18
|
re: /^[A-Za-z]+(?: [A-Za-z]+)*$/,
|
|
19
19
|
text: ' hanya boleh berisi huruf dan spasi antar kata',
|
|
20
20
|
};
|
|
21
|
-
export
|
|
21
|
+
export const ANY_UPPERCASE = {
|
|
22
22
|
re: /[A-Z]/,
|
|
23
23
|
text: ' harus berisi minimal 1 huruf kapital',
|
|
24
24
|
};
|
|
25
|
-
export
|
|
25
|
+
export const ANY_LOWERCASE = {
|
|
26
26
|
re: /[a-z]/,
|
|
27
27
|
text: ' harus berisi minimal 1 huruf kecil',
|
|
28
28
|
};
|
|
29
|
-
export
|
|
29
|
+
export const ANY_NUMBER = {
|
|
30
30
|
re: /\d/,
|
|
31
31
|
text: ' harus berisi minimal 1 angka',
|
|
32
32
|
};
|
|
33
|
-
export
|
|
33
|
+
export const ANY_SYMBOL = {
|
|
34
34
|
re: /[\W+_]/,
|
|
35
35
|
text: ' harus berisi minimal 1 simbol',
|
|
36
36
|
};
|
|
37
|
-
export
|
|
37
|
+
export const NO_SPACE = {
|
|
38
38
|
re: /^\S+$/,
|
|
39
39
|
text: ' tidak boleh berisi spasi',
|
|
40
40
|
};
|
package/dist/hooks/debounce.js
CHANGED
|
@@ -1,28 +1,3 @@
|
|
|
1
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
2
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
3
|
-
if (!m) return o;
|
|
4
|
-
var i = m.call(o), r, ar = [], e;
|
|
5
|
-
try {
|
|
6
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
7
|
-
}
|
|
8
|
-
catch (error) { e = { error: error }; }
|
|
9
|
-
finally {
|
|
10
|
-
try {
|
|
11
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
12
|
-
}
|
|
13
|
-
finally { if (e) throw e.error; }
|
|
14
|
-
}
|
|
15
|
-
return ar;
|
|
16
|
-
};
|
|
17
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
18
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
19
|
-
if (ar || !(i in from)) {
|
|
20
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
21
|
-
ar[i] = from[i];
|
|
22
|
-
}
|
|
23
|
-
}
|
|
24
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
25
|
-
};
|
|
26
1
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
|
27
2
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
28
3
|
/**
|
|
@@ -40,14 +15,13 @@ import { useCallback, useEffect, useRef, useState } from 'react';
|
|
|
40
15
|
* @example
|
|
41
16
|
* const debouncedSearchTerm = useDebounce(searchTerm, 500);
|
|
42
17
|
*/
|
|
43
|
-
export function useDebounce(value, delay) {
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
var handler = setTimeout(function () {
|
|
18
|
+
export function useDebounce(value, delay = 500) {
|
|
19
|
+
const [debouncedValue, setDebouncedValue] = useState(value);
|
|
20
|
+
useEffect(() => {
|
|
21
|
+
const handler = setTimeout(() => {
|
|
48
22
|
setDebouncedValue(value);
|
|
49
23
|
}, delay);
|
|
50
|
-
return
|
|
24
|
+
return () => {
|
|
51
25
|
clearTimeout(handler);
|
|
52
26
|
};
|
|
53
27
|
}, [value, delay]);
|
|
@@ -68,21 +42,17 @@ export function useDebounce(value, delay) {
|
|
|
68
42
|
* debouncedSearch('hello'); // Will wait 300ms before calling fetchResults
|
|
69
43
|
*/
|
|
70
44
|
export function useDebounceFunc(callback, delay) {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
var args = [];
|
|
74
|
-
for (var _i = 0; _i < arguments.length; _i++) {
|
|
75
|
-
args[_i] = arguments[_i];
|
|
76
|
-
}
|
|
45
|
+
const timerRef = useRef(null);
|
|
46
|
+
const debouncedFunction = (...args) => {
|
|
77
47
|
if (timerRef.current) {
|
|
78
48
|
clearTimeout(timerRef.current);
|
|
79
49
|
}
|
|
80
|
-
timerRef.current = setTimeout(
|
|
81
|
-
callback
|
|
50
|
+
timerRef.current = setTimeout(() => {
|
|
51
|
+
callback(...args);
|
|
82
52
|
}, delay);
|
|
83
53
|
};
|
|
84
|
-
useEffect(
|
|
85
|
-
return
|
|
54
|
+
useEffect(() => {
|
|
55
|
+
return () => {
|
|
86
56
|
if (timerRef.current) {
|
|
87
57
|
clearTimeout(timerRef.current);
|
|
88
58
|
}
|
|
@@ -98,18 +68,17 @@ export function useDebounceFunc(callback, delay) {
|
|
|
98
68
|
*
|
|
99
69
|
* @param {number} [delay=500] - The debounce delay in milliseconds before executing the callback.
|
|
100
70
|
*/
|
|
101
|
-
export function useConditionalDebounce(delay) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
var run = useCallback(function (condition, callback) {
|
|
71
|
+
export function useConditionalDebounce(delay = 500) {
|
|
72
|
+
const timeoutRef = useRef(null);
|
|
73
|
+
const run = useCallback((condition, callback) => {
|
|
105
74
|
if (timeoutRef.current)
|
|
106
75
|
clearTimeout(timeoutRef.current);
|
|
107
76
|
if (!condition)
|
|
108
77
|
return;
|
|
109
78
|
timeoutRef.current = window.setTimeout(callback, delay);
|
|
110
79
|
}, [delay]);
|
|
111
|
-
useEffect(
|
|
112
|
-
return
|
|
80
|
+
useEffect(() => {
|
|
81
|
+
return () => {
|
|
113
82
|
if (timeoutRef.current)
|
|
114
83
|
clearTimeout(timeoutRef.current);
|
|
115
84
|
};
|
package/dist/hooks/form.js
CHANGED
|
@@ -1,30 +1,3 @@
|
|
|
1
|
-
var __values = (this && this.__values) || function(o) {
|
|
2
|
-
var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
|
|
3
|
-
if (m) return m.call(o);
|
|
4
|
-
if (o && typeof o.length === "number") return {
|
|
5
|
-
next: function () {
|
|
6
|
-
if (o && i >= o.length) o = void 0;
|
|
7
|
-
return { value: o && o[i++], done: !o };
|
|
8
|
-
}
|
|
9
|
-
};
|
|
10
|
-
throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
|
|
11
|
-
};
|
|
12
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
13
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
14
|
-
if (!m) return o;
|
|
15
|
-
var i = m.call(o), r, ar = [], e;
|
|
16
|
-
try {
|
|
17
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
18
|
-
}
|
|
19
|
-
catch (error) { e = { error: error }; }
|
|
20
|
-
finally {
|
|
21
|
-
try {
|
|
22
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
23
|
-
}
|
|
24
|
-
finally { if (e) throw e.error; }
|
|
25
|
-
}
|
|
26
|
-
return ar;
|
|
27
|
-
};
|
|
28
1
|
import { useRef } from 'react';
|
|
29
2
|
import { getById } from '../utils/general';
|
|
30
3
|
/**
|
|
@@ -41,54 +14,42 @@ import { getById } from '../utils/general';
|
|
|
41
14
|
*
|
|
42
15
|
* @param {string[]} [fieldsWithoutNameIds=[]] - A list of field IDs that exist outside the form element.
|
|
43
16
|
*/
|
|
44
|
-
export function useForm(fieldsWithoutNameIds) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
var e_1, _a;
|
|
49
|
-
var result = {};
|
|
17
|
+
export function useForm(fieldsWithoutNameIds = []) {
|
|
18
|
+
const form = useRef(null);
|
|
19
|
+
const fields = () => {
|
|
20
|
+
const result = {};
|
|
50
21
|
if (!form.current)
|
|
51
22
|
return result;
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
field_info: form.current.querySelector("#".concat(key, "_info")),
|
|
66
|
-
};
|
|
67
|
-
}
|
|
68
|
-
}
|
|
69
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
70
|
-
finally {
|
|
71
|
-
try {
|
|
72
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
73
|
-
}
|
|
74
|
-
finally { if (e_1) throw e_1.error; }
|
|
23
|
+
const formData = new FormData(form.current);
|
|
24
|
+
for (const [name, value] of formData.entries()) {
|
|
25
|
+
const key = name;
|
|
26
|
+
const input = form.current.querySelector(`[name="${key}"]`);
|
|
27
|
+
let fieldValue = value;
|
|
28
|
+
if (input?.type === 'checkbox')
|
|
29
|
+
fieldValue = input.checked;
|
|
30
|
+
result[key] = {
|
|
31
|
+
field_value: fieldValue,
|
|
32
|
+
field_id: input?.id || '',
|
|
33
|
+
field_error: form.current.querySelector(`#${key}_error`),
|
|
34
|
+
field_info: form.current.querySelector(`#${key}_info`),
|
|
35
|
+
};
|
|
75
36
|
}
|
|
76
37
|
return result;
|
|
77
38
|
};
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
fieldsWithoutNameIds.forEach(
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
39
|
+
const fieldsWithoutName = () => {
|
|
40
|
+
const result = {};
|
|
41
|
+
fieldsWithoutNameIds.forEach(id => {
|
|
42
|
+
const field = getById(id);
|
|
43
|
+
const key = id;
|
|
44
|
+
const value = field.type === 'radio' || field.type === 'checkbox' ? field.checked : field.value;
|
|
84
45
|
result[key] = {
|
|
85
46
|
field_value: value,
|
|
86
47
|
field_id: field.id,
|
|
87
|
-
field_error: getById(
|
|
88
|
-
field_info: getById(
|
|
48
|
+
field_error: getById(`${key}_error`),
|
|
49
|
+
field_info: getById(`${key}_info`),
|
|
89
50
|
};
|
|
90
51
|
});
|
|
91
52
|
return result;
|
|
92
53
|
};
|
|
93
|
-
return { form
|
|
54
|
+
return { form, fields, fieldsWithoutName };
|
|
94
55
|
}
|
package/dist/hooks/router.js
CHANGED
|
@@ -1,39 +1,3 @@
|
|
|
1
|
-
var __assign = (this && this.__assign) || function () {
|
|
2
|
-
__assign = Object.assign || function(t) {
|
|
3
|
-
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
4
|
-
s = arguments[i];
|
|
5
|
-
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
6
|
-
t[p] = s[p];
|
|
7
|
-
}
|
|
8
|
-
return t;
|
|
9
|
-
};
|
|
10
|
-
return __assign.apply(this, arguments);
|
|
11
|
-
};
|
|
12
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
13
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
14
|
-
if (!m) return o;
|
|
15
|
-
var i = m.call(o), r, ar = [], e;
|
|
16
|
-
try {
|
|
17
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
18
|
-
}
|
|
19
|
-
catch (error) { e = { error: error }; }
|
|
20
|
-
finally {
|
|
21
|
-
try {
|
|
22
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
23
|
-
}
|
|
24
|
-
finally { if (e) throw e.error; }
|
|
25
|
-
}
|
|
26
|
-
return ar;
|
|
27
|
-
};
|
|
28
|
-
var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
|
|
29
|
-
if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
|
|
30
|
-
if (ar || !(i in from)) {
|
|
31
|
-
if (!ar) ar = Array.prototype.slice.call(from, 0, i);
|
|
32
|
-
ar[i] = from[i];
|
|
33
|
-
}
|
|
34
|
-
}
|
|
35
|
-
return to.concat(ar || Array.prototype.slice.call(from));
|
|
36
|
-
};
|
|
37
1
|
import { useCallback, useMemo } from 'react';
|
|
38
2
|
import { useLocation, useNavigate, useParams, useSearchParams } from 'react-router-dom';
|
|
39
3
|
/**
|
|
@@ -42,16 +6,17 @@ import { useLocation, useNavigate, useParams, useSearchParams } from 'react-rout
|
|
|
42
6
|
* @returns An object implementing the {@link TUseRouter} interface
|
|
43
7
|
*/
|
|
44
8
|
export function useRouter() {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
searchParams.forEach(
|
|
9
|
+
const navigate = useNavigate();
|
|
10
|
+
const location = useLocation();
|
|
11
|
+
const params = useParams();
|
|
12
|
+
const [searchParams] = useSearchParams();
|
|
13
|
+
const query = useMemo(() => {
|
|
14
|
+
const queryObj = {};
|
|
15
|
+
searchParams.forEach((value, key) => {
|
|
52
16
|
if (queryObj[key]) {
|
|
53
17
|
queryObj[key] = Array.isArray(queryObj[key])
|
|
54
|
-
?
|
|
18
|
+
? [...queryObj[key], value]
|
|
19
|
+
: [queryObj[key], value];
|
|
55
20
|
}
|
|
56
21
|
else {
|
|
57
22
|
queryObj[key] = value;
|
|
@@ -59,81 +24,77 @@ export function useRouter() {
|
|
|
59
24
|
});
|
|
60
25
|
return queryObj;
|
|
61
26
|
}, [searchParams]);
|
|
62
|
-
|
|
27
|
+
const push = useCallback((url, options) => {
|
|
63
28
|
if (typeof url === 'string') {
|
|
64
29
|
navigate(url, options);
|
|
65
30
|
}
|
|
66
31
|
else {
|
|
67
|
-
|
|
32
|
+
const queryString = url.query
|
|
68
33
|
? '?' +
|
|
69
|
-
new URLSearchParams(Object.entries(url.query).map(
|
|
70
|
-
var _b = __read(_a, 2), k = _b[0], v = _b[1];
|
|
71
|
-
return [k, Array.isArray(v) ? v.join(',') : v];
|
|
72
|
-
})).toString()
|
|
34
|
+
new URLSearchParams(Object.entries(url.query).map(([k, v]) => [k, Array.isArray(v) ? v.join(',') : v])).toString()
|
|
73
35
|
: '';
|
|
74
|
-
|
|
75
|
-
navigate(
|
|
36
|
+
const hash = url.hash ? (url.hash.startsWith('#') ? url.hash : `#${url.hash}`) : '';
|
|
37
|
+
navigate(`${url.pathname}${queryString}${hash}`, options);
|
|
76
38
|
}
|
|
77
39
|
}, [navigate]);
|
|
78
|
-
|
|
79
|
-
|
|
40
|
+
const route = useCallback((pathname, query, options) => {
|
|
41
|
+
const searchParams = new URLSearchParams();
|
|
80
42
|
if (query) {
|
|
81
|
-
Object.entries(query).forEach(
|
|
82
|
-
var _b = __read(_a, 2), key = _b[0], value = _b[1];
|
|
43
|
+
Object.entries(query).forEach(([key, value]) => {
|
|
83
44
|
if (Array.isArray(value)) {
|
|
84
|
-
value.forEach(
|
|
45
|
+
value.forEach(v => searchParams.append(key, v));
|
|
85
46
|
}
|
|
86
47
|
else {
|
|
87
48
|
searchParams.set(key, value);
|
|
88
49
|
}
|
|
89
50
|
});
|
|
90
51
|
}
|
|
91
|
-
|
|
92
|
-
|
|
52
|
+
const search = searchParams.toString();
|
|
53
|
+
const fullPath = search ? `${pathname}?${search}` : pathname;
|
|
93
54
|
navigate(fullPath, options);
|
|
94
55
|
}, [navigate]);
|
|
95
|
-
|
|
96
|
-
|
|
56
|
+
const replace = useCallback((url, options) => {
|
|
57
|
+
const replaceOptions = { ...options, replace: true };
|
|
97
58
|
push(url, replaceOptions);
|
|
98
59
|
}, [push]);
|
|
99
|
-
|
|
60
|
+
const back = useCallback(() => {
|
|
100
61
|
navigate(-1);
|
|
101
62
|
}, [navigate]);
|
|
102
|
-
|
|
63
|
+
const next = useCallback(() => {
|
|
103
64
|
navigate(1);
|
|
104
65
|
}, [navigate]);
|
|
105
|
-
|
|
66
|
+
const go = useCallback((num) => {
|
|
106
67
|
navigate(num);
|
|
107
68
|
}, [navigate]);
|
|
108
|
-
|
|
69
|
+
const refresh = useCallback(() => {
|
|
109
70
|
window.location.reload();
|
|
110
71
|
}, []);
|
|
111
|
-
|
|
112
|
-
|
|
72
|
+
const beforePopState = useCallback((cb) => {
|
|
73
|
+
const handlePopState = (event) => {
|
|
113
74
|
if (!cb(event.state)) {
|
|
114
75
|
event.preventDefault();
|
|
115
76
|
window.history.pushState(location.state, '', location.pathname + location.search);
|
|
116
77
|
}
|
|
117
78
|
};
|
|
118
79
|
window.addEventListener('popstate', handlePopState);
|
|
119
|
-
return
|
|
80
|
+
return () => window.removeEventListener('popstate', handlePopState);
|
|
120
81
|
}, [location]);
|
|
121
82
|
return {
|
|
122
|
-
push
|
|
123
|
-
route
|
|
124
|
-
replace
|
|
125
|
-
back
|
|
126
|
-
next
|
|
127
|
-
go
|
|
128
|
-
refresh
|
|
83
|
+
push,
|
|
84
|
+
route,
|
|
85
|
+
replace,
|
|
86
|
+
back,
|
|
87
|
+
next,
|
|
88
|
+
go,
|
|
89
|
+
refresh,
|
|
129
90
|
path: location.pathname,
|
|
130
91
|
hash: location.hash,
|
|
131
|
-
fullpath:
|
|
92
|
+
fullpath: `${location.pathname}${location.search}${location.hash}`,
|
|
132
93
|
origin: window.location.origin,
|
|
133
94
|
href: window.location.href,
|
|
134
|
-
query
|
|
135
|
-
params
|
|
136
|
-
beforePopState
|
|
95
|
+
query,
|
|
96
|
+
params,
|
|
97
|
+
beforePopState,
|
|
137
98
|
};
|
|
138
99
|
}
|
|
139
100
|
/**
|
|
@@ -147,11 +108,11 @@ export function useRouter() {
|
|
|
147
108
|
* - `updateQuery`: A function to update query parameters
|
|
148
109
|
*/
|
|
149
110
|
export function useQueryParams(defaultValues) {
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
searchParams.forEach(
|
|
154
|
-
|
|
111
|
+
const [searchParams, setSearchParams] = useSearchParams();
|
|
112
|
+
const queryObj = useMemo(() => {
|
|
113
|
+
const result = { ...defaultValues };
|
|
114
|
+
searchParams.forEach((value, key) => {
|
|
115
|
+
const allValues = searchParams.getAll(key);
|
|
155
116
|
if (allValues.length > 1) {
|
|
156
117
|
result[key] = allValues;
|
|
157
118
|
}
|
|
@@ -161,16 +122,15 @@ export function useQueryParams(defaultValues) {
|
|
|
161
122
|
});
|
|
162
123
|
return result;
|
|
163
124
|
}, [searchParams, defaultValues]);
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
Object.entries(updates).forEach(
|
|
167
|
-
var _b = __read(_a, 2), key = _b[0], value = _b[1];
|
|
125
|
+
const updateQuery = useCallback((updates) => {
|
|
126
|
+
const newParams = new URLSearchParams(searchParams);
|
|
127
|
+
Object.entries(updates).forEach(([key, value]) => {
|
|
168
128
|
if (value === undefined || value === null) {
|
|
169
129
|
newParams.delete(key);
|
|
170
130
|
}
|
|
171
131
|
else if (Array.isArray(value)) {
|
|
172
132
|
newParams.delete(key);
|
|
173
|
-
value.forEach(
|
|
133
|
+
value.forEach(v => newParams.append(key, v));
|
|
174
134
|
}
|
|
175
135
|
else {
|
|
176
136
|
newParams.set(key, value);
|
|
@@ -1,30 +1,12 @@
|
|
|
1
|
-
var __read = (this && this.__read) || function (o, n) {
|
|
2
|
-
var m = typeof Symbol === "function" && o[Symbol.iterator];
|
|
3
|
-
if (!m) return o;
|
|
4
|
-
var i = m.call(o), r, ar = [], e;
|
|
5
|
-
try {
|
|
6
|
-
while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
|
|
7
|
-
}
|
|
8
|
-
catch (error) { e = { error: error }; }
|
|
9
|
-
finally {
|
|
10
|
-
try {
|
|
11
|
-
if (r && !r.done && (m = i["return"])) m.call(i);
|
|
12
|
-
}
|
|
13
|
-
finally { if (e) throw e.error; }
|
|
14
|
-
}
|
|
15
|
-
return ar;
|
|
16
|
-
};
|
|
17
1
|
import { useEffect } from 'react';
|
|
18
2
|
import { getById } from '../utils/general';
|
|
19
|
-
export
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
var trigger = triggerRef.current;
|
|
3
|
+
export const useSectionObserver = ({ triggerRef, targetId, threshold = 0.8 }) => {
|
|
4
|
+
useEffect(() => {
|
|
5
|
+
const container = getById(targetId);
|
|
6
|
+
const trigger = triggerRef.current;
|
|
24
7
|
if (!trigger || !container)
|
|
25
8
|
return;
|
|
26
|
-
|
|
27
|
-
var _b = __read(_a, 1), entry = _b[0];
|
|
9
|
+
const observer = new IntersectionObserver(([entry]) => {
|
|
28
10
|
if (entry.isIntersecting) {
|
|
29
11
|
container.setAttribute('data-visible', 'true');
|
|
30
12
|
return;
|
|
@@ -32,9 +14,9 @@ export var useSectionObserver = function (_a) {
|
|
|
32
14
|
if (entry.boundingClientRect.top > 0) {
|
|
33
15
|
container.setAttribute('data-visible', 'false');
|
|
34
16
|
}
|
|
35
|
-
}, { threshold
|
|
17
|
+
}, { threshold });
|
|
36
18
|
observer.observe(trigger);
|
|
37
|
-
return
|
|
19
|
+
return () => {
|
|
38
20
|
observer.disconnect();
|
|
39
21
|
};
|
|
40
22
|
}, [triggerRef, targetId, threshold]);
|