Gitlab - Argos ALM by PALO IT

Update blog example to use JSON server

parent 23aaf333
# Learn more https://docs.github.com/en/get-started/getting-started-with-git/ignoring-files
# dependencies
node_modules/
# Expo
.expo/
dist/
web-build/
# Native
*.orig.*
*.jks
*.p8
*.p12
*.key
*.mobileprovision
# Metro
.metro-health-check*
# debug
npm-debug.*
yarn-debug.*
yarn-error.*
# macOS
.DS_Store
*.pem
# local env files
.env*.local
# typescript
*.tsbuildinfo
...@@ -9,6 +9,7 @@ ...@@ -9,6 +9,7 @@
"version": "1.0.0", "version": "1.0.0",
"dependencies": { "dependencies": {
"@react-native-community/masked-view": "^0.1.11", "@react-native-community/masked-view": "^0.1.11",
"axios": "^1.5.1",
"expo": "~49.0.13", "expo": "~49.0.13",
"expo-status-bar": "~1.6.0", "expo-status-bar": "~1.6.0",
"react": "18.2.0", "react": "18.2.0",
...@@ -6194,6 +6195,29 @@ ...@@ -6194,6 +6195,29 @@
"node": ">= 4.0.0" "node": ">= 4.0.0"
} }
}, },
"node_modules/axios": {
"version": "1.5.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-1.5.1.tgz",
"integrity": "sha512-Q28iYCWzNHjAm+yEAot5QaAMxhMghWLFVf7rRdwhUI+c2jix2DUXjAHXVi+s1ibs3mjPO/cCgbA++3BjD0vP/A==",
"dependencies": {
"follow-redirects": "^1.15.0",
"form-data": "^4.0.0",
"proxy-from-env": "^1.1.0"
}
},
"node_modules/axios/node_modules/form-data": {
"version": "4.0.0",
"resolved": "https://registry.npmjs.org/form-data/-/form-data-4.0.0.tgz",
"integrity": "sha512-ETEklSGi5t0QMZuiXoA/Q6vcnxcLQP5vdugSpuAyi6SVGi2clPPp+xgEhuMaHC+zGgn31Kd235W35f7Hykkaww==",
"dependencies": {
"asynckit": "^0.4.0",
"combined-stream": "^1.0.8",
"mime-types": "^2.1.12"
},
"engines": {
"node": ">= 6"
}
},
"node_modules/babel-core": { "node_modules/babel-core": {
"version": "7.0.0-bridge.0", "version": "7.0.0-bridge.0",
"resolved": "https://registry.npmjs.org/babel-core/-/babel-core-7.0.0-bridge.0.tgz", "resolved": "https://registry.npmjs.org/babel-core/-/babel-core-7.0.0-bridge.0.tgz",
...@@ -7909,6 +7933,25 @@ ...@@ -7909,6 +7933,25 @@
"node": ">=0.4.0" "node": ">=0.4.0"
} }
}, },
"node_modules/follow-redirects": {
"version": "1.15.3",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.15.3.tgz",
"integrity": "sha512-1VzOtuEM8pC9SFU1E+8KfTjZyMztRsgEfwQl44z8A25uy13jSzTj6dyK2Df52iV0vgHCfBwLhDWevLn95w5v6Q==",
"funding": [
{
"type": "individual",
"url": "https://github.com/sponsors/RubenVerborgh"
}
],
"engines": {
"node": ">=4.0"
},
"peerDependenciesMeta": {
"debug": {
"optional": true
}
}
},
"node_modules/fontfaceobserver": { "node_modules/fontfaceobserver": {
"version": "2.3.0", "version": "2.3.0",
"resolved": "https://registry.npmjs.org/fontfaceobserver/-/fontfaceobserver-2.3.0.tgz", "resolved": "https://registry.npmjs.org/fontfaceobserver/-/fontfaceobserver-2.3.0.tgz",
...@@ -11698,6 +11741,11 @@ ...@@ -11698,6 +11741,11 @@
"resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz", "resolved": "https://registry.npmjs.org/react-is/-/react-is-16.13.1.tgz",
"integrity": "sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ==" "integrity": "sha512-24e6ynE2H+OKt4kqsOvNd8kBpV65zoxbA4BVsEOB3ARVWQki/DHzaUoC5KuON/BiccDaCCTZBuOcfZs70kR8bQ=="
}, },
"node_modules/proxy-from-env": {
"version": "1.1.0",
"resolved": "https://registry.npmjs.org/proxy-from-env/-/proxy-from-env-1.1.0.tgz",
"integrity": "sha512-D+zkORCbA9f1tdWRK0RaCR3GPv50cMxcrz4X8k5LTSUD1Dkw47mKJEZQNunItRTkWwgtaUSo1RVFRIG9ZXiFYg=="
},
"node_modules/pump": { "node_modules/pump": {
"version": "3.0.0", "version": "3.0.0",
"resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz", "resolved": "https://registry.npmjs.org/pump/-/pump-3.0.0.tgz",
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
}, },
"dependencies": { "dependencies": {
"@react-native-community/masked-view": "^0.1.11", "@react-native-community/masked-view": "^0.1.11",
"axios": "^1.5.1",
"expo": "~49.0.13", "expo": "~49.0.13",
"expo-status-bar": "~1.6.0", "expo-status-bar": "~1.6.0",
"react": "18.2.0", "react": "18.2.0",
......
import axios from "axios";
export default axios.create({
baseURL: 'https://faithful-bunny-adequately.ngrok-free.app'
});
\ No newline at end of file
import React, { useState } from "react";
import { View, Text, StyleSheet, TextInput, Button } from "react-native";
const BlogPostForm = ({ onSubmit, initialValues}) => {
const [title, setTitle] = useState(initialValues.title);
const [content, setContent] = useState(initialValues.content);
return (
<View>
<Text style={styles.labelStyle}>Enter Title:</Text>
<TextInput
style={styles.inputStyle}
value={title}
onChangeText={(text) => setTitle(text)}
/>
<Text style={styles.labelStyle}>Enter Content:</Text>
<TextInput
style={styles.inputStyle}
value={content}
onChangeText={(text) => setContent(text)}
/>
<Button
title="Save Blog Post"
onPress={ () => onSubmit(title, content)}
/>
</View>
);
};
BlogPostForm.defaultProps = {
initialValues: {
title: '',
content: ''
}
};
const styles = StyleSheet.create({
inputStyle: {
fontSize: 18,
borderColor: 'black',
borderWidth: 1,
paddingHorizontal: 16,
paddingVertical: 8,
marginHorizontal: 16,
borderRadius: 12
},
labelStyle: {
fontSize: 20,
marginHorizontal: 16,
marginVertical: 8
}
});
export default BlogPostForm;
\ No newline at end of file
import createDataContext from "./createDataContext"; import createDataContext from "./createDataContext";
import jsonServer from "../api/jsonServer";
const blogReducer = (state, action) => { const blogReducer = (state, action) => {
switch (action.type) { switch (action.type) {
...@@ -11,27 +12,61 @@ const blogReducer = (state, action) => { ...@@ -11,27 +12,61 @@ const blogReducer = (state, action) => {
} }
]; ];
case 'delete_blogpost': case 'delete_blogpost':
return state.filter( (blogPost) => blogPost.id !== action.payload ); return state.filter( blogPost => blogPost.id !== action.payload );
case 'edit_blogpost':
return state.map( blogPost => {
return blogPost.id == action.payload.id ? action.payload : blogPost;
});
case 'get_blogpost':
return action.payload;
default: default:
return state; return state;
} }
}; };
const getBlogPost = dispatch => {
return async () => {
const config = {
"ngrok-skip-browser-warning": true
}
const response = await jsonServer.get('/blogposts', config);
dispatch({ type: 'get_blogpost', payload: response.data});
};
};
const addBlogPost = dispatch => { const addBlogPost = dispatch => {
return (title, content, callback) => { return async (title, content, callback) => {
dispatch({ type: 'add_blogpost', payload: { title, content } });
callback(); await jsonServer.post('/blogposts', {title, content});
// dispatch({ type: 'add_blogpost', payload: { title, content } });
if (callback) {
callback();
}
}; };
}; };
const deleteBlogPost = dispatch => { const deleteBlogPost = dispatch => {
return (id) => { return async id => {
dispatch({ type: 'delete_blogpost', payload: id }) await jsonServer.delete(`/blogposts/${id}`);
dispatch({ type: 'delete_blogpost', payload: id });
};
};
const editBlogPost = dispatch => {
return async (id, title, content, callback) => {
await jsonServer.put(`/blogposts/${id}`, { title, content });
dispatch({
type: 'edit_blogpost',
payload: { id, title, content }
});
if (callback) {
callback();
}
}; };
}; };
export const { Context, Provider } = createDataContext( export const { Context, Provider } = createDataContext(
blogReducer, blogReducer,
{ addBlogPost, deleteBlogPost }, { addBlogPost, deleteBlogPost, editBlogPost, getBlogPost },
[{ title: "Test Title", content: "Test Content", id: 1}] []
); );
\ No newline at end of file
import React, { useContext, useState } from "react"; import React, { useContext } from "react";
import { View, Text, StyleSheet, TextInput, Button } from "react-native"; import { StyleSheet } from "react-native";
import { Context } from "../context/BlogContext"; import { Context } from "../context/BlogContext";
import BlogPostForm from "../components/BlogPostForm";
const CreateScreen = ({ navigation }) => { const CreateScreen = ({ navigation }) => {
const [title, setTitle] = useState('');
const [content, setContent] = useState('');
const { addBlogPost } = useContext(Context); const { addBlogPost } = useContext(Context);
return ( return (
<View> <BlogPostForm
<Text style={styles.labelStyle}>Enter Title:</Text> onSubmit={ (title, content) => {
<TextInput addBlogPost(title, content, () => navigation.navigate('Index'))
style={styles.inputStyle} }}
value={title} />
onChangeText={(text) => setTitle(text)}
/>
<Text style={styles.labelStyle}>Enter Content:</Text>
<TextInput
style={styles.inputStyle}
value={content}
onChangeText={(text) => setContent(text)}
/>
<Button
title="Add Blog Post"
onPress={() => {
addBlogPost(title, content, () => {
navigation.navigate('Index');
});
}}
/>
</View>
); );
}; };
const styles = StyleSheet.create({ const styles = StyleSheet.create({});
inputStyle: {
fontSize: 18,
borderColor: 'black',
borderWidth: 1,
paddingHorizontal: 16,
paddingVertical: 8,
marginHorizontal: 16,
borderRadius: 12
},
labelStyle: {
fontSize: 20,
marginHorizontal: 16,
marginVertical: 8
}
});
export default CreateScreen; export default CreateScreen;
\ No newline at end of file
import React, { useContext, useState } from "react"; import React, { useContext } from "react";
import { View, Text, StyleSheet, TextInput, Button } from "react-native"; import { StyleSheet } from "react-native";
import { Context } from "../context/BlogContext"; import { Context } from "../context/BlogContext";
import BlogPostForm from "../components/BlogPostForm";
const EditScreen = ({ navigation }) => { const EditScreen = ({ navigation }) => {
const [title, setTitle] = useState(''); const id = navigation.getParam('id');
const [content, setContent] = useState(''); const { state, editBlogPost } = useContext(Context);
const blogPost = state.find(
const { addBlogPost } = useContext(Context); blogPost => blogPost.id === id
)
return ( return (
<View> <BlogPostForm
<Text style={styles.labelStyle}>Enter Title:</Text> initialValues={{ title: blogPost.title, content: blogPost.content }}
<TextInput onSubmit={ (title, content) => {
style={styles.inputStyle} editBlogPost(id, title, content, () => navigation.pop());
value={title} }}
onChangeText={(text) => setTitle(text)} />
/>
<Text style={styles.labelStyle}>Enter Content:</Text>
<TextInput
style={styles.inputStyle}
value={content}
onChangeText={(text) => setContent(text)}
/>
<Button
title="Add Blog Post"
onPress={() => {
addBlogPost(title, content, () => {
navigation.navigate('Index');
});
}}
/>
</View>
); );
}; };
const styles = StyleSheet.create({ const styles = StyleSheet.create({});
inputStyle: {
fontSize: 18,
borderColor: 'black',
borderWidth: 1,
paddingHorizontal: 16,
paddingVertical: 8,
marginHorizontal: 16,
borderRadius: 12
},
labelStyle: {
fontSize: 20,
marginHorizontal: 16,
marginVertical: 8
}
});
export default EditScreen; export default EditScreen;
\ No newline at end of file
import React, { useContext } from "react"; import React, { useContext, useEffect } from "react";
import { View, Text, StyleSheet, FlatList, Button, TouchableOpacity } from "react-native"; import { View, Text, StyleSheet, FlatList, Button, TouchableOpacity } from "react-native";
import { Context } from "../context/BlogContext"; import { Context } from "../context/BlogContext";
import { FontAwesome } from '@expo/vector-icons'; import { FontAwesome } from '@expo/vector-icons';
const IndexScreen = ({ navigation }) => { const IndexScreen = ({ navigation }) => {
const { state, deleteBlogPost } = useContext(Context); const { state, deleteBlogPost, getBlogPost } = useContext(Context);
useEffect(() => {
getBlogPost();
navigation.addListener('didFocus', () => {
getBlogPost();
});
}, []);
return ( return (
<View> <View>
......
{
"blogposts": [
{
"id": 1,
"title": "API Post",
"content": "Content for my post"
},
{
"id": 2,
"title": "API Post 2",
"content": "Content 2 for my post"
},
{
"title": "Third post",
"content": "This is the content of the third post",
"id": 3
},
{
"title": "4th post (edited)",
"content": "Edited Content of 4th post",
"id": 4
}
]
}
\ No newline at end of file
This diff is collapsed.
{
"name": "jsonserver",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"db": "json-server -w db.json",
"tunnel": "ngrok http 3000"
},
"author": "",
"license": "ISC",
"dependencies": {
"json-server": "^0.17.4",
"ngrok": "^5.0.0-beta.2"
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment