@towsila/expo-ui-library 4.1.1 → 4.3.0
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/package.json +1 -1
- package/src/components/screens/profileScreen.tsx +102 -0
- package/src/components/ui/phonePrefix.tsx +28 -0
- package/src/hooks/useHeading.ts +27 -0
- package/src/hooks/useRelativeTime.ts +24 -0
- package/src/hooks/useTrimmedRoute.ts +52 -0
- package/src/index.ts +10 -1
- package/src/types/route.ts +5 -0
- package/src/utils/formatPrice.ts +2 -0
- package/src/utils/geo.ts +21 -0
- package/src/utils/shortanName.ts +30 -0
package/package.json
CHANGED
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
import React from "react";
|
|
2
|
+
import { View, Text, ScrollView } from "react-native";
|
|
3
|
+
import { FieldConfig } from "../../types/validateForm";
|
|
4
|
+
import { useAppTheme } from "../../context/ThemeContext";
|
|
5
|
+
import { useLanguage } from "../../context/LanguageContext";
|
|
6
|
+
import Input from "../ui/input";
|
|
7
|
+
import ScreenHeader from "../ui/PageTopBar";
|
|
8
|
+
import { Fonts, textSize } from "../../tokens/typography";
|
|
9
|
+
import SharpButton from "../ui/sharpButton";
|
|
10
|
+
|
|
11
|
+
interface Props {
|
|
12
|
+
title: string;
|
|
13
|
+
email: string;
|
|
14
|
+
fields: FieldConfig[];
|
|
15
|
+
formState: Record<string, string>;
|
|
16
|
+
errors: Record<string, string>;
|
|
17
|
+
onChangeField: (key: string, value: string) => void;
|
|
18
|
+
clearError: (key: string) => void;
|
|
19
|
+
translateError?: (key: string) => string;
|
|
20
|
+
phonePrefix?: React.ReactNode; // app passes the flag + 213+
|
|
21
|
+
extraCard?: React.ReactNode; // provider only: basePrice, licence, authorisation
|
|
22
|
+
footer?: React.ReactNode; // client only: sits above the button
|
|
23
|
+
button: string;
|
|
24
|
+
showButton: boolean; // e.g. isChanged
|
|
25
|
+
loading?: boolean;
|
|
26
|
+
onSubmit: () => void;
|
|
27
|
+
onBack?: () => void;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
const cardStyle = (colors: any) => ({
|
|
31
|
+
borderWidth: 2,
|
|
32
|
+
borderColor: colors.text.primary,
|
|
33
|
+
borderRadius: 16,
|
|
34
|
+
padding: 15,
|
|
35
|
+
backgroundColor: colors.background.card,
|
|
36
|
+
gap: 15,
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
export default function ProfileLayout({
|
|
40
|
+
title, email, fields, formState, errors, onChangeField, clearError, translateError,
|
|
41
|
+
phonePrefix, extraCard, footer, button, showButton, loading, onSubmit, onBack,
|
|
42
|
+
}: Props) {
|
|
43
|
+
const { colors } = useAppTheme();
|
|
44
|
+
const { isRTL } = useLanguage();
|
|
45
|
+
const byName = (n: string) => fields.find((f) => f.name === n);
|
|
46
|
+
|
|
47
|
+
const renderInput = (field?: FieldConfig) => {
|
|
48
|
+
if (!field) return null;
|
|
49
|
+
return (
|
|
50
|
+
<Input
|
|
51
|
+
key={field.name}
|
|
52
|
+
label={field.label}
|
|
53
|
+
value={formState[field.name] ?? ""}
|
|
54
|
+
placeholder=""
|
|
55
|
+
onChangeText={(text) => onChangeField(field.name, text)}
|
|
56
|
+
autoCapitalize="words"
|
|
57
|
+
returnKeyType="next"
|
|
58
|
+
error={errors[field.name] ? (translateError?.(errors[field.name]) ?? errors[field.name]) : ""}
|
|
59
|
+
onClearError={() => clearError(field.name)}
|
|
60
|
+
keyboardType={field.type === "phone" ? "phone-pad" : field.type === "numeric" ? "numeric" : "default"}
|
|
61
|
+
prefix={field.type === "phone" ? phonePrefix : undefined}
|
|
62
|
+
borderColor={colors.semantic.border}
|
|
63
|
+
/>
|
|
64
|
+
);
|
|
65
|
+
};
|
|
66
|
+
|
|
67
|
+
return (
|
|
68
|
+
<View style={{ flex: 1, paddingHorizontal: 20, paddingTop: 60, backgroundColor: colors.background.main }}>
|
|
69
|
+
<ScreenHeader title={title} description="" onBack={onBack} />
|
|
70
|
+
<ScrollView
|
|
71
|
+
style={{ flex: 1 }}
|
|
72
|
+
contentContainerStyle={{ gap: 30, paddingBottom: 40 }}
|
|
73
|
+
showsVerticalScrollIndicator={false}
|
|
74
|
+
keyboardShouldPersistTaps="handled"
|
|
75
|
+
>
|
|
76
|
+
<View style={{ gap: 20 }}>
|
|
77
|
+
<Text style={{ fontSize: textSize.sm, fontFamily: Fonts.montserrat.regular, color: colors.text.placeholder, textAlign: isRTL ? "left" : "right" }}>
|
|
78
|
+
{email}
|
|
79
|
+
</Text>
|
|
80
|
+
|
|
81
|
+
<View style={cardStyle(colors)}>
|
|
82
|
+
<View style={{ flexDirection: isRTL ? "row" : "row-reverse", gap: 10 }}>
|
|
83
|
+
<View style={{ flex: 1 }}>{renderInput(byName("firstname"))}</View>
|
|
84
|
+
<View style={{ flex: 1 }}>{renderInput(byName("lastname"))}</View>
|
|
85
|
+
</View>
|
|
86
|
+
{renderInput(byName("phone"))}
|
|
87
|
+
</View>
|
|
88
|
+
|
|
89
|
+
{extraCard ? <View style={cardStyle(colors)}>{extraCard}</View> : null}
|
|
90
|
+
</View>
|
|
91
|
+
|
|
92
|
+
{footer}
|
|
93
|
+
|
|
94
|
+
{showButton && (
|
|
95
|
+
<View style={{ paddingHorizontal: 15 }}>
|
|
96
|
+
<SharpButton text={button} textColor={colors.text.primary} bgColor={colors.primary} onPress={onSubmit} loading={loading} />
|
|
97
|
+
</View>
|
|
98
|
+
)}
|
|
99
|
+
</ScrollView>
|
|
100
|
+
</View>
|
|
101
|
+
);
|
|
102
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { View, Text } from "react-native";
|
|
2
|
+
import DzFlag from "@/assets/images/icons/dz_flag.svg";
|
|
3
|
+
import { useAppTheme } from "../../context/ThemeContext";
|
|
4
|
+
import { useLanguage } from "../../context/LanguageContext";
|
|
5
|
+
import { Fonts, textSize } from "../../tokens/typography";
|
|
6
|
+
|
|
7
|
+
export default function PhonePrefix() {
|
|
8
|
+
const { colors } = useAppTheme();
|
|
9
|
+
const { isRTL } = useLanguage();
|
|
10
|
+
return (
|
|
11
|
+
<View
|
|
12
|
+
style={{
|
|
13
|
+
flexDirection: isRTL ? "row" : "row-reverse",
|
|
14
|
+
alignItems: "center",
|
|
15
|
+
gap: 4,
|
|
16
|
+
height: 50,
|
|
17
|
+
paddingHorizontal: 14,
|
|
18
|
+
[isRTL ? "borderRightWidth" : "borderLeftWidth"]: 1,
|
|
19
|
+
[isRTL ? "borderRightColor" : "borderLeftColor"]: colors.semantic.border,
|
|
20
|
+
}}
|
|
21
|
+
>
|
|
22
|
+
<DzFlag width={23} height={15} />
|
|
23
|
+
<Text style={{ fontSize: textSize.sm, fontFamily: Fonts.montserrat.semiBold, color: colors.text.primary }}>
|
|
24
|
+
213+
|
|
25
|
+
</Text>
|
|
26
|
+
</View>
|
|
27
|
+
);
|
|
28
|
+
}
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { useEffect, useRef, useState } from "react";
|
|
2
|
+
import { bearing, distanceM } from "../utils/geo";
|
|
3
|
+
|
|
4
|
+
export function useHeading(position: [number, number] | null, target: [number, number] | null) {
|
|
5
|
+
const last = useRef<[number, number] | null>(null);
|
|
6
|
+
const [heading, setHeading] = useState(0);
|
|
7
|
+
|
|
8
|
+
useEffect(() => {
|
|
9
|
+
if (!position) return;
|
|
10
|
+
if (!last.current) {
|
|
11
|
+
last.current = position;
|
|
12
|
+
if (target) setHeading(bearing(position, target));
|
|
13
|
+
return;
|
|
14
|
+
}
|
|
15
|
+
if (distanceM(last.current, position) >= 8) {
|
|
16
|
+
setHeading(bearing(last.current, position));
|
|
17
|
+
last.current = position;
|
|
18
|
+
}
|
|
19
|
+
}, [position?.[0], position?.[1]]);
|
|
20
|
+
|
|
21
|
+
// when the target changes (client -> destination), face it again
|
|
22
|
+
useEffect(() => {
|
|
23
|
+
if (position && target) setHeading(bearing(position, target));
|
|
24
|
+
}, [target?.[0], target?.[1]]);
|
|
25
|
+
|
|
26
|
+
return heading;
|
|
27
|
+
}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { useEffect, useState } from "react";
|
|
2
|
+
|
|
3
|
+
export function useRelativeTime(t: (key: string, opts?: any) => string) {
|
|
4
|
+
const [, forceTick] = useState(0);
|
|
5
|
+
|
|
6
|
+
useEffect(() => {
|
|
7
|
+
const id = setInterval(() => forceTick((n) => n + 1), 60_000);
|
|
8
|
+
return () => clearInterval(id);
|
|
9
|
+
}, []);
|
|
10
|
+
|
|
11
|
+
return (iso: string): string => {
|
|
12
|
+
const now = Date.now();
|
|
13
|
+
const then = new Date(iso).getTime();
|
|
14
|
+
const diffSec = Math.max(0, Math.floor((now - then) / 1000));
|
|
15
|
+
|
|
16
|
+
if (diffSec < 60) return t("request.time.second", { count: diffSec });
|
|
17
|
+
if (diffSec < 3600) return t("request.time.minute", { count: Math.floor(diffSec / 60) });
|
|
18
|
+
if (diffSec < 86400) return t("request.time.hour", { count: Math.floor(diffSec / 3600) });
|
|
19
|
+
if (diffSec < 604800) return t("request.time.day", { count: Math.floor(diffSec / 86400) });
|
|
20
|
+
if (diffSec < 2592000) return t("request.time.week", { count: Math.floor(diffSec / 604800) });
|
|
21
|
+
if (diffSec < 31536000) return t("request.time.month", { count: Math.floor(diffSec / 2592000) });
|
|
22
|
+
return t("request.time.year", { count: Math.floor(diffSec / 31536000) });
|
|
23
|
+
};
|
|
24
|
+
}
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
import { useMemo, useRef } from "react";
|
|
2
|
+
import { distanceM } from "../utils/geo";
|
|
3
|
+
import { RouteInfo } from "../types/route";
|
|
4
|
+
|
|
5
|
+
type LngLat = [number, number];
|
|
6
|
+
|
|
7
|
+
// closest point to p on segment a-b (local flat approximation, fine for short distances)
|
|
8
|
+
function project(p: LngLat, a: LngLat, b: LngLat) {
|
|
9
|
+
const k = Math.cos((p[1] * Math.PI) / 180);
|
|
10
|
+
const ax = a[0] * k, ay = a[1];
|
|
11
|
+
const dx = b[0] * k - ax, dy = b[1] - ay;
|
|
12
|
+
const len = dx * dx + dy * dy;
|
|
13
|
+
let t = len === 0 ? 0 : ((p[0] * k - ax) * dx + (p[1] - ay) * dy) / len;
|
|
14
|
+
t = Math.max(0, Math.min(1, t));
|
|
15
|
+
const pt: LngLat = [a[0] + (b[0] - a[0]) * t, a[1] + (b[1] - a[1]) * t];
|
|
16
|
+
return { pt, dist: distanceM(p, pt) };
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export function useTrimmedRoute(route: RouteInfo | null, position: LngLat | null) {
|
|
20
|
+
const lastIdx = useRef(0);
|
|
21
|
+
const lastRoute = useRef<RouteInfo | null>(null);
|
|
22
|
+
|
|
23
|
+
return useMemo(() => {
|
|
24
|
+
if (!route || !position) return null;
|
|
25
|
+
const coords = route.geometry.coordinates as LngLat[];
|
|
26
|
+
if (coords.length < 2) return null;
|
|
27
|
+
|
|
28
|
+
// new route: start again from the beginning
|
|
29
|
+
if (lastRoute.current !== route) {
|
|
30
|
+
lastRoute.current = route;
|
|
31
|
+
lastIdx.current = 0;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
// only look ahead of where we already are, so the line never grows back
|
|
35
|
+
let bestI = lastIdx.current;
|
|
36
|
+
let best = project(position, coords[bestI], coords[Math.min(bestI + 1, coords.length - 1)]);
|
|
37
|
+
const end = Math.min(lastIdx.current + 60, coords.length - 2);
|
|
38
|
+
|
|
39
|
+
for (let i = lastIdx.current + 1; i <= end; i++) {
|
|
40
|
+
const c = project(position, coords[i], coords[i + 1]);
|
|
41
|
+
if (c.dist < best.dist) {
|
|
42
|
+
best = c;
|
|
43
|
+
bestI = i;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
lastIdx.current = bestI;
|
|
47
|
+
|
|
48
|
+
const rest = [best.pt, ...coords.slice(bestI + 1)];
|
|
49
|
+
if (rest.length < 2) return null;
|
|
50
|
+
return { type: "LineString" as const, coordinates: rest };
|
|
51
|
+
}, [route, position?.[0], position?.[1]]);
|
|
52
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -69,4 +69,13 @@ export {default as BackIcon} from "./components/ui/backIcon";
|
|
|
69
69
|
export {default as SuccessScreen} from "./components/screens/sucessScreen";
|
|
70
70
|
export {default as AnimatedSuccessIcon} from "./components/ui/AnimatedSuccessIcon";
|
|
71
71
|
export {ScrollWithIndicator} from "./components/ui/scrollIndicator"
|
|
72
|
-
export {default as NavigationRow} from "./components/ui/navigationCard";
|
|
72
|
+
export {default as NavigationRow} from "./components/ui/navigationCard";
|
|
73
|
+
export {useHeading} from "./hooks/useHeading";
|
|
74
|
+
export {useTrimmedRoute} from "./hooks/useTrimmedRoute";
|
|
75
|
+
export {useRelativeTime} from "./hooks/useRelativeTime";
|
|
76
|
+
export { bearing, distanceM } from "./utils/geo";
|
|
77
|
+
export { formatPrice } from "./utils/formatPrice";
|
|
78
|
+
export { shortAddress, getWilaya } from "./utils/shortanName";
|
|
79
|
+
export {RouteInfo} from "./types/route";
|
|
80
|
+
export {default as PhonePrefix} from "./components/ui/phonePrefix";
|
|
81
|
+
export {default as ProfileScreen} from "./components/screens/profileScreen";
|
package/src/utils/geo.ts
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
const toRad = (d: number) => (d * Math.PI) / 180;
|
|
2
|
+
|
|
3
|
+
// points are [lng, lat]
|
|
4
|
+
export function bearing(from: [number, number], to: [number, number]) {
|
|
5
|
+
const dLng = toRad(to[0] - from[0]);
|
|
6
|
+
const y = Math.sin(dLng) * Math.cos(toRad(to[1]));
|
|
7
|
+
const x =
|
|
8
|
+
Math.cos(toRad(from[1])) * Math.sin(toRad(to[1])) -
|
|
9
|
+
Math.sin(toRad(from[1])) * Math.cos(toRad(to[1])) * Math.cos(dLng);
|
|
10
|
+
return ((Math.atan2(y, x) * 180) / Math.PI + 360) % 360;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export function distanceM(a: [number, number], b: [number, number]) {
|
|
14
|
+
const R = 6371000;
|
|
15
|
+
const dLat = toRad(b[1] - a[1]);
|
|
16
|
+
const dLng = toRad(b[0] - a[0]);
|
|
17
|
+
const h =
|
|
18
|
+
Math.sin(dLat / 2) ** 2 +
|
|
19
|
+
Math.cos(toRad(a[1])) * Math.cos(toRad(b[1])) * Math.sin(dLng / 2) ** 2;
|
|
20
|
+
return 2 * R * Math.asin(Math.sqrt(h));
|
|
21
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
export const shortAddress = (full?: string, parts = 2) => {
|
|
2
|
+
if (!full) return "";
|
|
3
|
+
// raw coordinates (failure fallback): don't show them as a name
|
|
4
|
+
if (/^-?\d+\.\d+,\s*-?\d+\.\d+$/.test(full)) return "";
|
|
5
|
+
return full
|
|
6
|
+
.split(",")
|
|
7
|
+
.map((p) => p.trim())
|
|
8
|
+
.filter((p) => p && !/^\d+$/.test(p))
|
|
9
|
+
.slice(0, parts)
|
|
10
|
+
.join(", ");
|
|
11
|
+
};
|
|
12
|
+
|
|
13
|
+
// utils/shortAddress.ts
|
|
14
|
+
|
|
15
|
+
export const getWilaya = (full?: string) => {
|
|
16
|
+
if (!full) return "";
|
|
17
|
+
// raw coordinates (failure fallback): no name available
|
|
18
|
+
if (/^-?\d+\.\d+,\s*-?\d+\.\d+$/.test(full)) return "";
|
|
19
|
+
|
|
20
|
+
const parts = full
|
|
21
|
+
.split(",")
|
|
22
|
+
.map((p) => p.trim())
|
|
23
|
+
.filter(Boolean)
|
|
24
|
+
.filter((p) => !/^\d{4,5}$/.test(p)) // postal codes
|
|
25
|
+
|
|
26
|
+
const last = parts[parts.length - 1] ?? "";
|
|
27
|
+
|
|
28
|
+
// some results say "Wilaya d'Alger" or "ولاية الجزائر"
|
|
29
|
+
return last.replace(/^(wilaya\s+(d'|de\s+)?|ولاية\s+)/i, "").trim();
|
|
30
|
+
};
|