-
Notifications
You must be signed in to change notification settings - Fork 309
React native bad jokes app #301
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,26 +1,23 @@ | ||
| import React from 'react'; | ||
| import styled from 'styled-components/native'; | ||
| import { View, ScrollView, StyleSheet, Text } from 'react-native' | ||
|
|
||
| const Container = styled.View` | ||
| flex: 1; | ||
| background-color: papayawhip; | ||
| justify-content: center; | ||
| align-items: center; | ||
| `; | ||
| import Start from './components/Start' | ||
| import Header from './components/Header'; | ||
|
|
||
| const Title = styled.Text` | ||
| font-size: 24px; | ||
| color: palevioletred; | ||
| `; | ||
|
|
||
| const App = () => { | ||
| return ( | ||
| <Container> | ||
| <Title>This is your cool app!</Title> | ||
| <Title>Go to App.js and start coding</Title> | ||
| <Title>💅💅💅</Title> | ||
| </Container> | ||
| <ScrollView style={styles.wrapper}> | ||
| <Header /> | ||
| <Start /> | ||
| </ScrollView> | ||
| ); | ||
| }; | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| wrapper: { | ||
| backgroundColor: 'yellow', | ||
| } | ||
| }) | ||
|
|
||
| export default App; |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,129 @@ | ||
| import React, { useEffect, useState } from "react"; | ||
| import styled from "styled-components/native"; | ||
| import { View, StyleSheet, Text, Image, TouchableOpacity, Button } from "react-native"; | ||
| import { Accelerometer } from "expo-sensors"; | ||
|
|
||
|
|
||
| import { MEME_API, JOKES_API } from "./url/urls"; | ||
|
|
||
| const ArtFetch = () => { | ||
| const [joke, setJoke] = useState({}); | ||
| const [subscription, setSubscription] = useState(null); | ||
|
|
||
| const getJoke = () => { | ||
| fetch(JOKES_API) | ||
| .then((res) => res.json()) | ||
| .then((data) => setJoke(data)); | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| getJoke(); | ||
| }, []); | ||
|
|
||
| const [data, setData] = useState({ | ||
| x: 0, | ||
| y: 0, | ||
| z: 0, | ||
| }); | ||
|
|
||
|
|
||
| const { x, y, z } = data; | ||
| const subscribe = () => { | ||
| setSubscription( | ||
| Accelerometer.addListener(accelerometerData => { | ||
| setData(accelerometerData); | ||
| }) | ||
| ); | ||
| }; | ||
|
|
||
| const unsubscribe = () => { | ||
| subscription && subscription.remove(); | ||
| setSubscription(null); | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| // component mounts execute the function below | ||
| subscribe(); | ||
|
|
||
| // component unmounts execute the function in the return statement | ||
| return () => unsubscribe(); | ||
| }, []); | ||
|
|
||
|
|
||
| const isShaking = (data) => { | ||
| const totalForce = Math.abs(data.x) + Math.abs(data.y) + Math.abs(data.z); | ||
| return totalForce > 2.2; | ||
| } | ||
|
|
||
|
|
||
| useEffect(()=> { | ||
| if (isShaking(data)) { | ||
| getJoke(); | ||
| } | ||
| }, [data]) | ||
|
|
||
| return ( | ||
| <View style={styles.container}> | ||
| <View style={styles.textContainer}> | ||
| <Text style={styles.jokeText}>{joke.setup}</Text> | ||
| <Text style={styles.deliveryText}>{joke.delivery}</Text> | ||
|
Comment on lines
+68
to
+69
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I tested your app on my phone and in the browser and I noticed a small bug. Sometimes there are joke.setup and joke.delivery that don't get displayed. I tested the api you were calling in postman and there seem to be times when the api doesn't return a joke or delivery. This might be breaking your code because sometime when I shake for a new joke it comes back empty. Maybe there is a way you could handle this to show the joke or delivery even if one or the other doesn't come back from the response. |
||
| </View> | ||
| <View style={styles.button}> | ||
| <TouchableOpacity onPress={getJoke}> | ||
| <Text style={styles.buttonText}>Press to laugh</Text> | ||
| </TouchableOpacity> | ||
| </View> | ||
| </View> | ||
| ); | ||
| }; | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| container: { | ||
| display: "flex", | ||
| flexDirection: "row", | ||
| flexWrap: 'wrap', | ||
| justifyContent: 'space-evenly', | ||
| width: 250, | ||
| minHeight: 100, | ||
|
|
||
| }, | ||
| textContainer: { | ||
| marginTop: 28, | ||
| borderLeftWidth: 2, | ||
| borderLeftColor: 'tomato', | ||
| minHeight: 80, | ||
| width: 250, | ||
| }, | ||
|
|
||
| jokeText: { | ||
| fontFamily: "GillSans-SemiBold", | ||
| fontSize: 18, | ||
| marginBottom: 10, | ||
| paddingLeft: 10, | ||
| paddingTop: 5, | ||
| }, | ||
| deliveryText: { | ||
| paddingLeft: 10, | ||
| fontFamily: "Optima-Italic", | ||
|
Comment on lines
+99
to
+107
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice job adding custom fonts! |
||
| fontSize: 18, | ||
| }, | ||
| image: { | ||
| width: 300, | ||
| height: "auto", | ||
| }, | ||
| button: { | ||
| borderWidth: 2, | ||
| padding: 8, | ||
| marginTop: 80, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This marginTop of 80px seems to push the button outside of the container sometimes when the text content is a bit longer. |
||
| borderColor: 'tomato', | ||
| borderRadius: 5, | ||
| marginBottom: 20, | ||
| }, | ||
| buttonText: { | ||
| fontFamily: "Optima-Italic", | ||
| textTransform: 'uppercase', | ||
| color: 'tomato', | ||
| } | ||
| }); | ||
|
|
||
| export default ArtFetch; | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import React, { useEffect } from 'react' | ||
| import styled from 'styled-components/native'; | ||
| import { View, StyleSheet, Text } from 'react-native' | ||
|
|
||
| const Header = () => { | ||
|
|
||
| return( | ||
| <View style={styles.container}> | ||
| <Text style={styles.header}> | ||
| <Text style={styles.headOne}> | ||
| Randomly Funny / | ||
| </Text> | ||
| <Text style={styles.headTwo}> | ||
| Try not to laugh | ||
| </Text> | ||
| </Text> | ||
| </View> | ||
| ) | ||
| } | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| container: { | ||
| width: 375, | ||
| height: 120, | ||
| marginTop: 50, | ||
|
|
||
| display: 'flex', | ||
| flexDirection: 'column', | ||
|
|
||
| backgroundColor: 'yellow', | ||
| width: 375, | ||
| }, | ||
| header: { | ||
| fontFamily: 'GillSans-SemiBold', | ||
| fontSize: 40, | ||
| paddingLeft: 20, | ||
| }, | ||
| headTwo: { | ||
| fontFamily: 'Optima-Italic', | ||
| fontSize: 37, | ||
| color: 'tomato', | ||
| } | ||
| }) | ||
|
|
||
| export default Header |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import React, { useEffect, useState } from 'react' | ||
| import styled from 'styled-components/native'; | ||
| import { View, StyleSheet, Text, Image } from 'react-native' | ||
| import ArtFetch from './ArtFetch'; | ||
|
|
||
|
|
||
|
|
||
| const Start = () => { | ||
|
|
||
| return( | ||
| <View style={styles.main}> | ||
| <View style={styles.container}> | ||
|
|
||
| <Text style={styles.header}>/ Shake for fun /</Text> | ||
| <ArtFetch /> | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It was nice that you created a component to take care of this logic and code. |
||
|
|
||
| </View> | ||
| </View> | ||
| ) | ||
| } | ||
|
|
||
| const styles = StyleSheet.create({ | ||
| main: { | ||
| display: 'flex', | ||
| justifyContent: 'center', | ||
| alignItems: 'center', | ||
| }, | ||
| container: { | ||
| display: 'flex', | ||
| flexDirection: 'column', | ||
| paddingTop: 40, | ||
| marginTop: 25, | ||
| alignItems: 'center', | ||
| width: 340, | ||
| height: 420, | ||
| borderWidth: 3, | ||
| borderColor: 'black', | ||
| borderRadius: 10, | ||
|
|
||
| backgroundColor: 'yellow', | ||
|
|
||
| }, | ||
|
|
||
| header: { | ||
| fontFamily: 'Optima-Italic', | ||
| fontSize: 20, | ||
| textAlign: 'center', | ||
| paddingBottom: 65, | ||
| color: 'tomato', | ||
|
|
||
| }, | ||
|
|
||
| }) | ||
|
|
||
| export default Start | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| export const API_FETCH = 'https://www.rijksmuseum.nl/api/nl/collection/SK-C-5/tiles?key=aI1ZHUVZ' | ||
|
|
||
| export const API_ARTFETCH = 'https://www.rijksmuseum.nl/api/nl/collection?key=aI1ZHUVZ&involvedMaker=Rembrandt+van+Rijn' | ||
|
|
||
| export const MEME_API = 'https://api.imgflip.com/get_memes' | ||
|
|
||
| export const TEST_API = 'https://api.quotable.io/random' | ||
|
|
||
| export const JOKES_API = 'https://v2.jokeapi.dev/joke/Any?safe-mode' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems to be destructing data.x y and z and it doesn't seem to be used.