-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #67 from wildlifeai/adding-react-hook-form
Adding react hook form and some components
- Loading branch information
Showing
16 changed files
with
841 additions
and
93 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
62 changes: 62 additions & 0 deletions
62
android/app/src/main/java/com/wildlife/wildlifewatcher/IgnoreSSLFactory.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}) |
Oops, something went wrong.