Expect following JSON(mention in image) as output using react-admin framework - react-admin

Below is the my code or react component:-
import { Box } from '#material-ui/core';
import React,{ useEffect, useState } from 'react'
import {
Create,
SimpleForm,
TextInput,
TextField,
ArrayInput,
SimpleShowLayout,
SimpleFormIterator,
RadioButtonGroupInput,TopToolbar
} from 'react-admin'
import "../index.css"
const JobCreate = (props) => {
const types = [
{id:'default'},
{id:'custom'}
];
const [checked,setChecked] = useState(true);
const onClickType = (id) => {
(id === "custom") ? setChecked(false) : setChecked(true)
}
return (
<Create title='Create an Job' {...props}>
<SimpleForm>
<TextInput source='source' />
<TextInput source='destination' />
<RadioButtonGroupInput label="Type"
source="custom"
choices={types}
optionText="id"
onChange={onClickType} />
<RadioButtonGroupInput source="category" choices={[
{ id: 'default', name: 'default' },
{ id: 'custom', name: 'custom' },
]} />
{!checked && (
<SimpleShowLayout source="video">
<TextInput label='voice' name="codec" required />
</SimpleShowLayout>
)}
{ !checked && (
<ArrayInput source="Profiles">
<SimpleFormIterator disableReordering>
<TextInput label='Name' source="Name" required />
<TextInput label='Profile' source="profile" required />
</SimpleFormIterator>
</ArrayInput>
)}
{!checked && (
<SimpleShowLayout source="audios">
<TextInput label='audio1' required />
<TextInput label='audio2' required />
</SimpleShowLayout></source>
)}
</SimpleForm>
</Create>
)
}
export default JobCreate
I want to create complex UI using the react-admin framework, I have created the UI but when hit the save button, not getting actual or expected JSON in the output while sending to the api.
I tried so many ways, but no luck, Hence posting this question.
Here is the documentation link : https://marmelab.com/react-admin/Readme.html

Related

How to add an Update + Delete feature in my Details page in React Native? (DjangoRestFramework + React Native)?

My app is a CRUD React Native that interacts with Django Rest Framework based off of a book called "Building Versatile Mobile Apps with Python and REST: RESTful Web Services with Django and React".
The project lets users enter in different data fields about pizza restaurants and display them.
I've been able to build the Crud web application in React and Django and a mobile app with Django and React native that only lets me add and read data.
I'm on the very last chapter and I'm about to deploy, but I don't know how to send update and delete requests in React Native to an API the book never covers this section. I added an update button but I don't know how to logically navigate to an edit section from the detail_view.js section.
Here is the open source code from the book on github
This is my app.js file
import React from 'react';
import { StyleSheet, Text, SafeAreaView, Image } from 'react-native';
import { NavigationContainer } from "#react-navigation/native";
import { createStackNavigator } from "#react-navigation/stack";
import { createDrawerNavigator } from '#react-navigation/drawer';
import { createBottomTabNavigator } from "#react-navigation/bottom-tabs";
import ListView from "./src/screens/components/function_list_view";
import DetailView from "./src/screens/components/detail_view";
import AddPizzeria from "./src/screens/drawer/addPizzeria.js";
import RegForm from "./src/screens/drawer/regForm.js";
import LoginForm from "./src/screens/drawer/loginForm.js";
import TabOne from "./src/screens/tabs/tab1.js";
import TabTwo from "./src/screens/tabs/tab2.js";
const Stack = createStackNavigator();
const Drawer = createDrawerNavigator();
const Tab = createBottomTabNavigator();
renderTabComponents = () => (
<Tab.Navigator>
<Tab.Screen name="Tab 1" component={TabOne} />
<Tab.Screen name="Tab 2" component={TabTwo} />
</Tab.Navigator>
);
renderScreenComponents = () => (
<Stack.Navigator>
<Stack.Screen name="Home" component={ListView} />
<Stack.Screen name="Detail" component={DetailView} />
<Stack.Screen name="Tabs" children={this.renderTabComponents} />
</Stack.Navigator>
);
export default function App() {
return (
<NavigationContainer>
<Drawer.Navigator>
<Drawer.Screen name="Home" children={this.renderScreenComponents} />
<Drawer.Screen name="Add Pizza" component={AddPizzeria} />
<Drawer.Screen name="Registration" component={RegForm} />
<Drawer.Screen name="Login" component={LoginForm} />
</Drawer.Navigator>
</NavigationContainer>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: "#fff",
alignItems: "center",
justifyContent: "center",
},
baseText: {
color: "navy",
fontSize: 30,
fontStyle: "italic",
},
newText:{
color: "red",
},
pizzaImage: {
width: 200,
height: 200,
},
})
Here is my detailview file:
import React, { useState, useEffect } from "react";
import {View, Text, Image, FlatList } from "react-native";
import client from "./../../api/client";
import styles from "./detail_styles";
const DetailView = ({ route }) => {
const [detail, setDetail] = useState("");
const { objurl } = route.params;
const getDetail = async (url) => {
try {
const response = await client.get(url);
if (!response.ok) {
setDetail(response.data);
}
} catch (error) {
console.log(error);
}
};
useEffect(()=>{
getDetail(objurl);
}, [])
return (
<View style={styles.center}>
<FlatList
horizontal={true}
data={detail.pizzeria_images}
keyExtractor={(item) => item.id.toString()}
renderItem={({ item }) => {
return (
<Image
style={styles.pizzaImage}
source={{
uri: item.image,
}}
/>
);
}}
/>
<Text style={styles.title}>Pizzeria: {detail.pizzeria_name}</Text>
<Text style={styles.details}>Address: {detail.street}</Text>
<Text style={styles.details}>
City: {detail.city}, {detail.state},{detail.zip_code}
</Text>
<Text style={styles.details}>Web: {detail.website}</Text>
<Text style={styles.details}>Ph: {detail.phone_number}</Text>
<Text style={styles.details}>Description: {detail.description}</Text>
<Text style={styles.details}>Email: {detail.email}</Text>
// this is where I want to edit the data fields
<Button
style={styles.addButton}
onPress={() => *I don't know what to navigate*}
title="Edit"
/>
</View>
);
}
export default DetailView;
This is the addPizzeria.js file that submits the data
import React, {useState} from "react";
import { SafeAreaView, ScrollView, TextInput, Button, NativeModules,Text, Alert } from "react-native";
import { Formik } from "formik";
import client from "./../../api/client";
import styles from "./addPizzeria_styles";
import validationSchema from "./addPizzeria_valid";
import PhotoPicker from "../components/shared/photo.js";
const AddPizzeria = () => {
const [photo, setPhoto] = useState("");
const postedAlert = () => {
Alert.alert("Success!", "Thank you! ", [
{
text: "Go to main screen",
onPress: () => NativeModules.DevSettings.reload()
},
]);
};
const handleSubmit = async (values) =>{
const data = new FormData();
data.append("pizzeria_name", values.pizzeria);
data.append("street", values.street);
data.append("city", values.city);
data.append("state", values.state);
data.append("zip_code", values.zip_code);
data.append("website", values.website);
data.append("phone_number", values.phone_number);
data.append("pizzeria_name", values.pizzeria);
data.append("description", values.description);
data.append("email", values.email);
data.append("logo_image", {
uri: photo,
name: "filename.jpg",
type: "image/jpg",
});
try {
const response = await client.post("api/create/", data); postedAlert();
} catch(error) {
console.log(error);
};
};
return (
<Formik
initialValues={{
pizzeria: "",
street: "",
city: "",
state: "",
zip_code: "",
website: "",
phone_number: "",
description: "",
email: "",
}}
onSubmit={handleSubmit}
validationSchema={validationSchema}
>
{({ handleChange, handleSubmit, values, errors }) => (
<SafeAreaView style={styles.content}>
<ScrollView>
<PhotoPicker photo={photo} onPressPhoto={(uri) => setPhoto(uri)} />
<TextInput
style={styles.textBox}
value={values.pizzeria}
placeholder="Enter a new pizz place here"
onChangeText={handleChange("pizzeria")}
/>
<Text style={styles.error}>{errors.pizzeria}</Text>
<TextInput
style={styles.textBox}
value={values.street}
placeholder="Street address"
onChangeText={handleChange("street")}
/>
<Text style={styles.error}>{errors.street}</Text>
<TextInput
style={styles.textBox}
value={values.city}
placeholder="City"
onChangeText={handleChange("city")}
/>
<Text style={styles.error}>{errors.city}</Text>
<TextInput
style={styles.textBox}
value={values.state}
placeholder="State"
onChangeText={handleChange("state")}
/>
<Text style={styles.error}>{errors.state}</Text>
<TextInput
style={styles.textBox}
value={values.zip_code}
placeholder="Zip"
onChangeText={handleChange("zip_code")}
/>
<Text style={styles.error}>{errors.zip_code}</Text>
<TextInput
style={styles.textBox}
value={values.website}
placeholder="Website"
onChangeText={handleChange("website")}
/>
<Text style={styles.error}>{errors.website}</Text>
<TextInput
style={styles.textBox}
value={values.phone_number}
placeholder="Phone number"
onChangeText={handleChange("phone_number")}
/>
<Text style={styles.error}>{errors.phone_number}</Text>
<TextInput
style={styles.textBox}
value={values.description}
placeholder="Description"
onChangeText={handleChange("description")}
/>
<Text style={styles.error}>{errors.description}</Text>
<TextInput
style={styles.textBox}
value={values.email}
placeholder="Email"
onChangeText={handleChange("email")}
/>
<Text style={styles.error}>{errors.email}</Text>
<Button
style={styles.addButton}
onPress={handleSubmit}
title="Submit"
/>
</ScrollView>
</SafeAreaView>
)}
</Formik>
)}
export default AddPizzeria;
I tried creating an update file that is same as my addpizzeria file, but I get lost trying to plan out where and how to structure it so I can reference it when I make a request to update that data.
The only viable option is to make a feature on the detailview.js to update the data. This is what I'm trying and unsure of. I don't know where to go from here aside from adding an update and delete button on this page that navigates to another file and does the delete and update functions. your text
Ok I solved the issue. It seems the creator of the book added tabs in app.js in the Bottonnavigator class just in case you'd want to add and Update and Delete feature in detailview.js page.
All that is needed is to just add the navigation.navigate("Tabs") in a button in my detailview.js file.
<Button
style={styles.addButton}
title="Click for tabs"
onPress={() => navigation.navigate("Tabs")}
/>

component is not defined while opened on web

I have a component that work fine on android but it shows an error when I try to open it on the web using expo
Uncaught ReferenceError: TextInputMask is not defined
at ./src/components/atoms/TextInputMask.js (TextInputMask.js:6:1)
Here is TextInputMask code that gives an error
import React, { useEffect, useState } from "react";
import { View, StyleSheet } from "react-native";
import { TextInput, Text } from "react-native-paper";
import { TextInputMask as TextInputMaskDefault } from "react-native-masked-text";
export default TextInputMask = React.forwardRef(
(
{
label,
placeholder,
style,
mode = "outlined",
onChangeValue = () => {},
type = "custom",
options = {},
value,
icon,
disabled,
error,
...props
},
ref
) => {
const [data, setData] = useState();
const [maskValue, setMaskValue] = useState();
useEffect(() => {
if (value === null || value === undefined) {
setMaskValue("");
return;
}
if (value) {
value = String(value);
setMaskValue(value);
}
}, [value]);
useEffect(() => {
let rawValue = data?.getRawValue();
onChangeValue(rawValue, maskValue);
}, [maskValue]);
return (
<View style={style}>
<View style={styles.viewInput}>
<TextInput
style={styles.textInput}
label={label}
placeholder={placeholder}
mode={mode}
autoCapitalize="none"
autoCorrect={false}
autoCompleteType="off"
ref={ref}
disabled={disabled}
value={maskValue}
error={error}
render={(inputProps) => {
return (
<TextInputMaskDefault
{...inputProps}
type={type}
options={options}
onChangeText={(text) => setMaskValue(text)}
ref={(ref) => setData(ref)}
/>
);
}}
{...props}
/>
{icon ? (
<Text style={styles.textInputIcon}>
<TextInput.Icon
name={icon}
onPress={onPress}
size={30}
color={error ? Colors.red800 : null}
disabled={disabled}
/>
</Text>
) : null}
</View>
</View>
);
}
);
Any idea where the error might be? I wonder if there is a difference in creating component for android and web?

React-Native: Picker can't select any other item. Error: Actions may not have and undefined "type" property. Have you misspelled a constant?

I'm learning React Native and I'm stuck on a Picker problem. On clicking it shows up but I can't select anything else other than "Monday". Whenever I do, I get an error
I've tried searching for the problem on the internet and worked with my action creators but nothing seems to work
import React, { Component } from 'react';
import { Picker, Text } from 'react-native';
import { connect } from 'react-redux';
import { employeeUpdate } from '../actions';
import { Card, CardSection, Input, Button } from './common';
class EmployeeCreate extends Component {
render() {
return (
<Card>
<CardSection>
<Input
label='Name'
placeholder='Jane'
value={this.props.name}
onChangeText={value => this.props.employeeUpdate({ prop: 'name', value })}
/>
</CardSection>
<CardSection>
<Input
label='Phone'
placeholder='555-555-5555'
value={this.props.phone}
onChangeText={value => this.props.employeeUpdate({ prop: 'phone', value })}
/>
</CardSection>
<CardSection style={{ flexDirection: 'column' }}>
<Text style={styles.pickerTextStyle}>Shift</Text>
<Picker
style={{ alignItems: "center" }}
selectedValue={this.props.shift}
onValueChange={value => this.props.employeeUpdate({ prop: 'shift', value })}
>
<Picker.Item label="Monday" value="Monday" />
<Picker.Item label="Tuesday" value="Tuesday" />
<Picker.Item label="Wednesday" value="Wednesday" />
<Picker.Item label="Thursday" value="Thursday" />
<Picker.Item label="Friday" value="Friday" />
<Picker.Item label="Saturday" value="Saturday" />
<Picker.Item label="Sunday" value="Sunday" />
</Picker>
</CardSection>
<CardSection>
<Button>Create</Button>
</CardSection>
</Card>
);
}
}
const styles = {
pickerTextStyle: {
fontSize: 18,
paddingLeft: 20
}
};
const mapStateToProps = (state) => {
const { name, phone, shift } = state.employeeForm;
return { name, phone, shift };
};
export default connect(mapStateToProps, { employeeUpdate })(EmployeeCreate);
Here is my EmployeeActions File:
import EMPLOYEE_UPDATE from './types'
export const employeeUpdate = ({ prop, value }) => {
return {
type: EMPLOYEE_UPDATE,
payload: { prop, value }
};
};
Here is the EmployeeFormReducer:
import EMPLOYEE_UPDATE from '../actions/types';
const INITIAL_STATE = { name: '', phone: '', shift: '' };
export default (state = INITIAL_STATE, action) => {
switch (action.type) {
case EMPLOYEE_UPDATE:
//action.payload === { prop: 'name', value: 'jane' }
return { ...state, [action.payload.prop]: action.payload.value };
default:
return state;
}
}
I expect to select any day as per my choice
Solved. Did some changes in types.js file:
export const EMAIL_CHANGED = 'email_changed';
export const PASSWORD_CHANGED = 'password_changed';
export const LOGIN_USER_SUCCESS = 'login_user_success';
export const LOGIN_USER_FAIL = 'login_user_fail';
export const LOGIN_USER = 'login_user';
export const EMPLOYEE_UPDATE = 'employee_update';
export default {
EMAIL_CHANGED,
PASSWORD_CHANGED,
LOGIN_USER_SUCCESS,
LOGIN_USER_FAIL,
LOGIN_USER,
EMPLOYEE_UPDATE
}

How can I use the collapsible menu shown in the left side of demo?

It appears that the Demo page has recently changed. How do I use the foldable features on the left menu layer, such as sales-order relationships? Where's the code?
No related code found in sample/demo code
You haven't searched long enough, it's in examples/demo/src/layout/Menu.js:
import React, { Component } from 'react';
import PropTypes from 'prop-types';
import { connect } from 'react-redux';
import compose from 'recompose/compose';
import SettingsIcon from '#material-ui/icons/Settings';
import LabelIcon from '#material-ui/icons/Label';
import { withRouter } from 'react-router-dom';
import {
translate,
DashboardMenuItem,
MenuItemLink,
Responsive,
} from 'react-admin';
import visitors from '../visitors';
import orders from '../orders';
import invoices from '../invoices';
import products from '../products';
import categories from '../categories';
import reviews from '../reviews';
import SubMenu from './SubMenu';
class Menu extends Component {
state = {
menuCatalog: false,
menuSales: false,
menuCustomers: false,
};
static propTypes = {
onMenuClick: PropTypes.func,
logout: PropTypes.object,
};
handleToggle = menu => {
this.setState(state => ({ [menu]: !state[menu] }));
};
render() {
const { onMenuClick, open, logout, translate } = this.props;
return (
<div>
{' '}
<DashboardMenuItem onClick={onMenuClick} />
<SubMenu
handleToggle={() => this.handleToggle('menuSales')}
isOpen={this.state.menuSales}
sidebarIsOpen={open}
name="pos.menu.sales"
icon={<orders.icon />}
>
<MenuItemLink
to={`/commands`}
primaryText={translate(`resources.commands.name`, {
smart_count: 2,
})}
leftIcon={<orders.icon />}
onClick={onMenuClick}
/>
<MenuItemLink
to={`/invoices`}
primaryText={translate(`resources.invoices.name`, {
smart_count: 2,
})}
leftIcon={<invoices.icon />}
onClick={onMenuClick}
/>
</SubMenu>
<SubMenu
handleToggle={() => this.handleToggle('menuCatalog')}
isOpen={this.state.menuCatalog}
sidebarIsOpen={open}
name="pos.menu.catalog"
icon={<products.icon />}
>
<MenuItemLink
to={`/products`}
primaryText={translate(`resources.products.name`, {
smart_count: 2,
})}
leftIcon={<products.icon />}
onClick={onMenuClick}
/>
<MenuItemLink
to={`/categories`}
primaryText={translate(`resources.categories.name`, {
smart_count: 2,
})}
leftIcon={<categories.icon />}
onClick={onMenuClick}
/>
</SubMenu>
<SubMenu
handleToggle={() => this.handleToggle('menuCustomer')}
isOpen={this.state.menuCustomer}
sidebarIsOpen={open}
name="pos.menu.customers"
icon={<visitors.icon />}
>
<MenuItemLink
to={`/customers`}
primaryText={translate(`resources.customers.name`, {
smart_count: 2,
})}
leftIcon={<visitors.icon />}
onClick={onMenuClick}
/>
<MenuItemLink
to={`/segments`}
primaryText={translate(`resources.segments.name`, {
smart_count: 2,
})}
leftIcon={<LabelIcon />}
onClick={onMenuClick}
/>
</SubMenu>
<MenuItemLink
to={`/reviews`}
primaryText={translate(`resources.reviews.name`, {
smart_count: 2,
})}
leftIcon={<reviews.icon />}
onClick={onMenuClick}
/>
<Responsive
xsmall={
<MenuItemLink
to="/configuration"
primaryText={translate('pos.configuration')}
leftIcon={<SettingsIcon />}
onClick={onMenuClick}
/>
}
medium={null}
/>
<Responsive
small={logout}
medium={null} // Pass null to render nothing on larger devices
/>
</div>
);
}
}
const mapStateToProps = state => ({
open: state.admin.ui.sidebarOpen,
theme: state.theme,
locale: state.i18n.locale,
});
const enhance = compose(
withRouter,
connect(
mapStateToProps,
{}
),
translate
);
export default enhance(Menu);

Getting "not a function"error in React Native/Redux

My code is here
I am getting the error below.
I believe its something to do with the employee create line in the code below and i cannot figure out why this might be happening. I am doing this from a tutorial
import React, { Component } from 'react';
//import { View, Text } from 'react-native';
import { connect } from 'react-redux';
import { Picker, Text } from 'react-native';
import { employeeUpdate, employeeCreate } from '../actions';
import { Card, CardSection, Input, Button } from './common';
class EmployeeCreate extends Component {
onButtonPress() {
const { name, phone, shift } = this.props;
this.props.employeeCreate({ name, phone, shift: shift || 'Monday' });
}
render() {
return (
//
// <View>
// <Text>Employees</Text>
// </View>
<Card>
<CardSection>
<Input
label="Name"
placeholder="Jane"
value={this.props.name}
onChangeText={value => this.props.employeeUpdate({ prop: 'name', value })}
/>
</CardSection>
<CardSection>
<Input
label="Phone"
placeholder="555-555-5555"
value={this.props.phone}
onChangeText={value => this.props.employeeUpdate({ prop: 'phone', value })}
/>
</CardSection>
<CardSection style={{ flexDirection: 'column' }}>
<Text style={styles.pickerLabelStyle}>Pick a Shift: </Text>
<Picker
//style={{ flex: 1 }}
selectedValue={this.props.shift}
onValueChange={value => this.props.employeeUpdate({ prop: 'shift', value })}
>
<Picker.Item label="Monday" value="Monday" />
<Picker.Item label="Tuesday" value="Tuesday" />
<Picker.Item label="Wednesday" value="Wednesday" />
<Picker.Item label="Thursday" value="Thursday" />
<Picker.Item label="Friday" value="Friday" />
<Picker.Item label="Saturday" value="Saturday" />
<Picker.Item label="Sunday" value="Sunday" />
</Picker>
</CardSection>
<CardSection>
<Button onPress={this.onButtonPress.bind(this)}>
Create
</Button>
</CardSection>
</Card>
);
}
}
const styles = {
pickerLabelStyle: {
fontSize: 18,
paddingLeft: 20
}
};
const mapStateToProps = (state) => {
const { name, phone, shift } = state.employeeForm;
return { name, phone, shift };
};
export default connect(mapStateToProps, {
employeeUpdate, employeeCreate
})(EmployeeCreate);
Here is the error:
Folks- this is resolved. I found my answer.
In the
https://github.com/samrao2/manager-4/blob/master/src/actions/index.js
file. I was not exporting all the actions from the Employeeactions file and so was not exporting the employeeCreate action. i should have put a * there.
Issue is resolved! Thanks!