Skip to content

Commit

Permalink
Merge pull request #67 from wildlifeai/adding-react-hook-form
Browse files Browse the repository at this point in the history
Adding react hook form and some components
  • Loading branch information
Burzo authored Jan 11, 2025
2 parents 0540333 + a5b4274 commit 7901ec5
Show file tree
Hide file tree
Showing 16 changed files with 841 additions and 93 deletions.
1 change: 1 addition & 0 deletions .cursorrules
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
When anwsering, always provide the full updated answer again. Always use the existing codebase in your context as a reference and don't make up new code when possible. always use the latest ts best practices, prefer types to interfaces.
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package com.wildlife.wildlifewatcher; //Change this
import com.facebook.react.modules.network.OkHttpClientFactory;
import com.facebook.react.modules.network.OkHttpClientFactory;
import com.facebook.react.modules.network.OkHttpClientProvider;
import com.facebook.react.modules.network.ReactCookieJarContainer;
import java.security.cert.CertificateException;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;
import android.util.Log;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import okhttp3.CipherSuite;
import okhttp3.ConnectionSpec;
import okhttp3.OkHttpClient;
import okhttp3.TlsVersion;
import static android.content.ContentValues.TAG;
public class IgnoreSSLFactory implements OkHttpClientFactory {
private static final String TAG = "IgnoreSSLFactory";

@Override
public OkHttpClient createNewNetworkModuleClient() {
try {
final TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
@Override
public void checkClientTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public void checkServerTrusted(java.security.cert.X509Certificate[] chain, String authType) throws CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return new java.security.cert.X509Certificate[]{};
}
}
};
final SSLContext sslContext = SSLContext.getInstance("SSL");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
final SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
OkHttpClient.Builder builder = new OkHttpClient.Builder()
.connectTimeout(0, TimeUnit.MILLISECONDS).readTimeout(0, TimeUnit.MILLISECONDS)
.writeTimeout(0, TimeUnit.MILLISECONDS).cookieJar(new ReactCookieJarContainer());
builder.sslSocketFactory(sslSocketFactory, (X509TrustManager) trustAllCerts[0]);
builder.hostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
OkHttpClient okHttpClient = builder.build();
return okHttpClient;
} catch (Exception e) {
Log.e(TAG, e.getMessage());
throw new RuntimeException(e);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ class MainApplication : Application(), ReactApplication {
// If you opted-in for the New Architecture, we load the native entry point for this app.
load()
}

OkHttpClientProvider.setOkHttpClientFactory(IgnoreSSLFactory())
}
}
14 changes: 13 additions & 1 deletion ios/Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ PODS:
- hermes-engine (0.74.6):
- hermes-engine/Pre-built (= 0.74.6)
- hermes-engine/Pre-built (0.74.6)
- NVActivityIndicatorView (5.1.1):
- NVActivityIndicatorView/Base (= 5.1.1)
- NVActivityIndicatorView/Base (5.1.1)
- PromisesObjC (2.4.0)
- RCT-Folly (2024.01.01.00):
- boost
Expand Down Expand Up @@ -997,6 +1000,9 @@ PODS:
- react-native-config/App (= 1.5.3)
- react-native-config/App (1.5.3):
- React-Core
- react-native-loader-kit (2.0.8):
- NVActivityIndicatorView (= 5.1.1)
- React-Core
- react-native-safe-area-context (4.14.0):
- React-Core
- React-nativeconfig (0.74.6)
Expand Down Expand Up @@ -1433,6 +1439,7 @@ DEPENDENCIES:
- react-native-app-auth (from `../node_modules/react-native-app-auth`)
- react-native-ble-manager (from `../node_modules/react-native-ble-manager`)
- react-native-config (from `../node_modules/react-native-config`)
- react-native-loader-kit (from `../node_modules/react-native-loader-kit`)
- react-native-safe-area-context (from `../node_modules/react-native-safe-area-context`)
- React-nativeconfig (from `../node_modules/react-native/ReactCommon`)
- React-NativeModulesApple (from `../node_modules/react-native/ReactCommon/react/nativemodule/core/platform/ios`)
Expand Down Expand Up @@ -1478,6 +1485,7 @@ SPEC REPOS:
- FirebaseCoreInternal
- FirebaseInstallations
- GoogleUtilities
- NVActivityIndicatorView
- PromisesObjC
- SocketRocket

Expand Down Expand Up @@ -1549,6 +1557,8 @@ EXTERNAL SOURCES:
:path: "../node_modules/react-native-ble-manager"
react-native-config:
:path: "../node_modules/react-native-config"
react-native-loader-kit:
:path: "../node_modules/react-native-loader-kit"
react-native-safe-area-context:
:path: "../node_modules/react-native-safe-area-context"
React-nativeconfig:
Expand Down Expand Up @@ -1634,6 +1644,7 @@ SPEC CHECKSUMS:
glog: fdfdfe5479092de0c4bdbebedd9056951f092c4f
GoogleUtilities: ea963c370a38a8069cc5f7ba4ca849a60b6d7d15
hermes-engine: 2102c92e54a031a270fd1fe84169ec8a0901b7bd
NVActivityIndicatorView: 1f6c5687f1171810aa27a3296814dc2d7dec3667
PromisesObjC: f5707f49cb48b9636751c5b2e7d227e43fba9f47
RCT-Folly: 02617c592a293bd6d418e0a88ff4ee1f88329b47
RCTDeprecation: 5f1d7e1f8ef6c53f0207e3ac0d0ca23575e8a6ab
Expand Down Expand Up @@ -1662,6 +1673,7 @@ SPEC CHECKSUMS:
react-native-app-auth: 79605228ffe689f63ad5b046319f5e154e3e96c2
react-native-ble-manager: 59a0c7178d6fa0272a1b86f976469b800ce08eba
react-native-config: 8f7283449bbb048902f4e764affbbf24504454af
react-native-loader-kit: ed18c8ce83b69083211912326897d8286f37d797
react-native-safe-area-context: 4532f1a0c5d34a46b9324ccaaedcb5582a302b7d
React-nativeconfig: bf6ea7351e761f3ab837b9fc406576c56492f57a
React-NativeModulesApple: d127c66e52964d33c7fb694ce98c078ea899dd16
Expand Down Expand Up @@ -1701,4 +1713,4 @@ SPEC CHECKSUMS:

PODFILE CHECKSUM: 89f1bbb0db36f947d13810e4b47cad167f6ba4fb

COCOAPODS: 1.14.3
COCOAPODS: 1.15.2
46 changes: 46 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
"eventemitter3": "^5.0.1",
"lodash.isempty": "^4.4.0",
"react": "18.2.0",
"react-hook-form": "^7.54.1",
"react-native": "0.74.6",
"react-native-app-auth": "^8.0.0",
"react-native-ble-manager": "^11.3.2",
Expand All @@ -33,7 +34,9 @@
"react-native-device-info": "^10.13.1",
"react-native-drawer-layout": "^3.3.0",
"react-native-gesture-handler": "^2.18.1",
"react-native-loader-kit": "^2.0.8",
"react-native-paper": "^5.12.3",
"react-native-paper-dropdown": "^2.3.1",
"react-native-reanimated": "^3.15.0",
"react-native-safe-area-context": "^4.9.0",
"react-native-screens": "^3.34.0",
Expand Down
140 changes: 140 additions & 0 deletions src/components/form/Field.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import { PropsWithChildren, ReactNode } from "react"
import { StyleSheet, View, ViewStyle } from "react-native"
import {
Control,
ControllerRenderProps,
FieldPath,
FieldValues,
useController,
UseControllerProps,
} from "react-hook-form"
import { WWText } from "../ui/WWText"

type Props<
TFieldValues extends FieldValues = FieldValues,
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
> = {
control: Control<TFieldValues>
style?: ViewStyle
label?: ReactNode
subText?: ReactNode
helpText?: ReactNode
required?: boolean
children: (
props: ControllerRenderProps<TFieldValues, TName> & {
hasError?: boolean
},
) => ReactNode
} & UseControllerProps<TFieldValues, TName>

export const Field = <
TFieldValues extends FieldValues = FieldValues,
TName extends FieldPath<TFieldValues> = FieldPath<TFieldValues>,
>({
children,
style,
name,
rules,
label,
helpText,
subText,
control,
required = false,
}: Props<TFieldValues, TName>) => {
const {
field,
fieldState: { error },
} = useController({
name,
rules,
control,
})

const hasError = error?.message ? true : undefined

return (
<View style={[style, styles.fieldWrapper]}>
{label && (
<View style={styles.labelContainer}>
<WWText style={styles.label}>
{label}
{required && <WWText style={styles.required}> *</WWText>}
</WWText>
{subText && <WWText style={styles.subText}>{subText}</WWText>}
</View>
)}
{children({
...field,
hasError,
})}
{helpText && !hasError && (
<WWText style={styles.helpText}>{helpText}</WWText>
)}
{hasError && <WWText style={styles.errorText}>{error?.message}</WWText>}
</View>
)
}

type UncontrolledFieldProps = {
style?: ViewStyle
label?: ReactNode
subText?: ReactNode
helpText?: ReactNode
errorText?: string
}

export const UncontrolledField = ({
children,
style,
label,
subText,
helpText,
errorText,
}: PropsWithChildren<UncontrolledFieldProps>) => {
return (
<View style={[style, styles.fieldWrapper]}>
{label && (
<View style={styles.labelContainer}>
<WWText style={styles.label}>{label}</WWText>
{subText && <WWText style={styles.subText}>{subText}</WWText>}
</View>
)}
{children}
{helpText && !errorText && (
<WWText style={styles.helpText}>{helpText}</WWText>
)}
{errorText && <WWText style={styles.errorText}>{errorText}</WWText>}
</View>
)
}

const styles = StyleSheet.create({
fieldWrapper: {
marginBottom: 16,
},
labelContainer: {
marginBottom: 8,
},
label: {
fontSize: 16,
fontWeight: "500",
},
required: {
color: "red",
},
subText: {
fontSize: 12,
color: "#666",
marginTop: 2,
},
helpText: {
fontSize: 12,
color: "#666",
marginTop: 4,
},
errorText: {
fontSize: 12,
color: "red",
marginTop: 4,
},
})
Loading

0 comments on commit 7901ec5

Please sign in to comment.