How to Update image in react native - image

I was trying to update the user profile but the profile picture only changes ones.To change it each time i had to change the image name each time how to solve it.I have tried to send the request in post method but my api does not support post.I will post my code below.Could some one help me and Thanks in advance.
class ProfileEdit extends Component {
state = {
username: '',
email: '',
about: '',
userInfo: '',
avatarSource: null,
showAlert: false,
showCancelButton: false,
showConfirmButton: false,
};
constructor(props) {
super(props);
this.selectPhotoTapped = this.selectPhotoTapped.bind(this);
}
hideAlert = () => {
this.setState({
showAlert: false
});
};
selectPhotoTapped() {
const options = {
quality: 1.0,
maxWidth: 500,
maxHeight: 500,
storageOptions: {
skipBackup: true,
},
};
ImagePicker.showImagePicker(options, response => {
console.log('Response = ', response);
if (response.didCancel) {
console.log('User cancelled photo picker');
} else if (response.error) {
console.log('ImagePicker Error: ', response.error);
} else if (response.customButton) {
console.log('User tapped custom button: ', response.customButton);
} else {
const source = { uri: response.uri }
this.setState({
avatarSource: source,
});
this.upLoadImage(response.uri);
}
});
}
upLoadImage = async (image_uri) => {
const value = await AsyncStorage.getItem('userToken');
//alert(value)
var url = 'http://www.dev.beta.duklr.com:8000/api/v2/profile/';
var b_url = url + value + '/';
let data = new FormData();
data.append('photo', { type: 'image/jpg', uri: image_uri, name: 'profile_image1.jpg' });
data.append('Content-Type', 'image/jpg');
fetch(b_url, {
method: 'PUT',
body: data
}).then((res) => res.json())
.then((res) => {
// alert("response" + JSON.stringify(res));
})
.catch((e) => this.setState({
showAlert: true,
message: e,
showCancelButton: true,
cancelText: 'close',
}))
.done()
}
componentDidMount = async () => {
const value = await AsyncStorage.getItem('userToken');
//alert(value)
var url = 'http://www.dev.beta.duklr.com:8000/api/v2/profile/';
var b_url = url + value + '/';
//alert(value);
return fetch(b_url)
.then(res => res.json())
.then(res => {
this.setState(
{
isLoading: false,
refreshing: false,
userInfo: res,
},
function () { }
);
})
.catch(error => {
this.setState({
showAlert: true,
message: error,
showCancelButton: true,
cancelText: 'close',
})
});
}
onUpdate = async () => {
const value = await AsyncStorage.getItem('userToken');
var url = 'my_api';
var b_url = url + value + '/';
//alert(b_url);
const { email, about, avatarSource } = this.state;
//alert(`${email},${about}`);
fetch(b_url, {
method: 'PUT',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json',
},
body: JSON.stringify({
email: email,
about_us: about,
}),
})
.then((response) => response.json())
.then((responseJson) => {
this.setState({
showAlert: true,
message: "Saved successfully",
showCancelButton: true,
cancelText: 'close',
})
// this.setState({
// dataSource: responseJson.promptmsg,
// })
})
.catch((error) => {
this.setState({
showAlert: true,
message: error,
showCancelButton: true,
cancelText: 'close',
})
});
}
catch(errors) {
this.setState({
showAlert: true,
message: errors,
showCancelButton: true,
cancelText: 'close',
});
}
render() {
const value_email = this.state.userInfo.email;
const value_about = this.state.userInfo.about_us;
return (
<View style={styles.continer}>
<ScrollView>
<View style={{ alignItems: 'center', padding: 20 }}>
<Avatar
source={this.state.avatarSource}
size="xlarge"
// showEditButton
onPress={this.selectPhotoTapped.bind(this)}
/>
</View>
<View style={styles.textContiner}>
{/* <TextField
label='User Name'
title={this.state.userInfo.name}
value={this.state.username}
onChangeText={(username) => this.setState({ username })}
/> */}
<TextField
label='Email Id'
placeholder={value_email}
//value={value_email}
onChangeText={(email) => this.setState({ email })}
/>
<TextField
label='About'
//value={value_about}
placeholder={value_about}
onChangeText={(about) => this.setState({ about })}
/>
<View style={{ marginTop: 20 }}>
<Button
title="Save"
onPress={this.onUpdate.bind(this)}>
</Button>
</View>
</View>
</ScrollView>
<AwesomeAlert
show={this.state.showAlert}
showProgress={false}
title="Hello There"
message={this.state.message}
closeOnTouchOutside={true}
closeOnHardwareBackPress={true}
showCancelButton={this.state.showCancelButton}
showConfirmButton={this.state.showConfirmButton}
cancelText={this.state.cancelText}
confirmText={this.state.confirmText}
confirmButtonColor="#DD6B55"
onCancelPressed={() => {
this.hideAlert();
}}
onConfirmPressed={() => {
this.hideAlert();
}}
/>
</View>
);
}
}
export default ProfileEdit;

Is Avatar based on React Native's Image? To my experience, the Image component shipped with React Native is very buggy. Indeed, reloading issue is one of them. I often use FastImage as replacement for Image.

Related

Source and Layer component does not show if data changes

I am trying to build a GPS-Tracker using react-map-gl and want to draw a line with the data retreived from the where-is-iss-API. If the coordinates in the source JSON are initilized staticaly the line is shown.
const [geojson, setGeojson]: any = useState(
{
type: "Feature",
geometry: {
type: "LineString",
coordinates: [
[-77.0323, 38.9131],
[-80.0323, 40.9131]
]
},
properties: {
"name": "Route"
}
});
but when addind data via useEffect it isn't displayed
const [data, setData]: any = useState(dataProp);
const updateLayer = () => {
console.log(data)
if (data.latitude !== undefined && data.longitude !== undefined) {
setGeojson({
...geojson,
geometry: {
coordinates: [
...geojson.geometry.coordinates,
[data.longitude, data.latitude]
]
}
})
}
console.log(geojson);
}
useEffect(() => {
setData(dataProp);
if(data !== undefined && data !== null){
updateLayer();
}
}, [dataProp]);
In this case the line is only displayed for the first two coodinates.
const route: LayerProps = {
id: 'route',
type: 'line',
source: 'geojson',
paint: { 'line-color': 'red', "line-width": 4 }
};
return (
<div>
<ReactMapGL
{...viewport}
mapboxApiAccessToken={token}
mapStyle={"mapbox://styles/gobsej/ckomzwjdg377w18ozdhm1by36"}
width="100vw"
height="100vh"
onViewportChange={(viewport: React.SetStateAction<{ latitude: number; longitude: number; zoom: number; }>) => setViewport(viewport)}
>
<Source id="my-data" type="geojson" data={geojson}>
<Layer {...route} />
</Source>
</ReactMapGL>
</div >
);
The data is retreived with axios and given to the component as a prop
const getGPSLocation = async () => {
await Axios.get('https://api.wheretheiss.at/v1/satellites/25544').then((response) => {
console.log(response.data);
setData(response.data);
}).catch((error) => {
console.log(error);
});
};
useEffect(() => {
getGPSLocation();
const interval = setInterval(getGPSLocation, 1000)
return () => {
clearInterval(interval); }
}, []);
return (
<div>
<Map dataProp={data}></Map>
</div>
);
The console output is the following:
https://i.stack.imgur.com/oZIlb.png

Property not defined error But it is defined Vue js Laravel

I have defined the function in my vue js file but it is giving me error for nameWithLang() function Please have a look
My Form
<multiselect v-model="selected" track-by="id" label="name" :options="options" :loading="isLoading" :internal-search="false" #search-change="getData" :multiple="true" :close-on-select="false" :hide-selected="true":internal-search="false" name="books[]" :show-labels="false" :custom-label="nameWithLang"></multiselect>
My vue js file
import AppForm from '../app-components/Form/AppForm';
Vue.component('coupon-form', {
mixins: [AppForm],
data: function() {
return {
form: {
name: '' ,
description: '' ,
valid_from: '' ,
valid_till: '' ,
discount: '' ,
enabled: false,
books: [],
},
isLoading: false,
options: [],
selected: [],
}
},
methods: {
nameWithLang({ name, sku }) {
return `${name} — ${sku}`
},
getData(query){
this.isLoading = true;
axios.post('/admin/books/find/'+query)
.then((response) => {
this.options = response.data;
this.isLoading = false;
})
.catch((error) => {
this.isLoading = false;
});
},
},
watch: {
selected (newValues) {
this.form.books = newValues.map(obj => obj.id)
}
}
});
Other properties and functions are working nameWithLang is not working
It gives me error this
Property or method "nameWithLang" is not defined on the instance but referenced during render.
why not you just return the value into a variable in data, and set the function into mounted/watch instead using the function to get the value.
just for refer, you can make the script like this :
import AppForm from '../app-components/Form/AppForm';
Vue.component('coupon-form', {
mixins: [AppForm],
data: function() {
return {
form: {
name: '' ,
description: '' ,
valid_from: '' ,
valid_till: '' ,
discount: '' ,
enabled: false,
books: [],
},
isLoading: false,
options: [],
selected: [],
newName: '',
}
},
methods: {
nameWithLang({ name, sku }) {
this.newName = `${name} — ${sku}`;
},
getData(query){
this.isLoading = true;
axios.post('/admin/books/find/'+query)
.then((response) => {
this.options = response.data;
this.isLoading = false;
})
.catch((error) => {
this.isLoading = false;
});
},
},
watch: {
selected (newValues) {
this.form.books = newValues.map(obj => obj.id)
this.nameWithLang();
}
}
});
then you can make the template like this:
<multiselect v-model="selected" track-by="id" label="name" :options="options" :loading="isLoading" :internal-search="false" #search-change="getData" :multiple="true" :close-on-select="false" :hide-selected="true":internal-search="false" name="books[]" :show-labels="false" :custom-label="newName"></multiselect>
this is just another way you can make it and the way i'm understand what actually you want to do. you want to pass the name with lang value into the :custom-label right? so why not just defined one more variable and add the value into the variable. so you just need to pass the value instead the function. in v-bind it's more appropiate to pass a property instead of a method

SweetAlert2 : Validation required for one of the fields

I am trying to perform validation on one of the fields in the form.
Only if the value for that field exists will I be able to invoke the API, if not an error message will be thrown.
I tried various examples from SweetAlert2's website. I just want the validation for one of the fields.
Swal.fire({
title: 'Are you sure you want to Save the Notes?',
type: 'info',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes'
}).then((result) => {
console.log('result.value',result.value);
if (result.value) {
Swal.fire( {
title: 'Download Notes',
html:"<div class='b'><p>ID</p></div><input id='swal-input2' class='swal2-input' required/><div class='b'><p>Notes</p></div><input id='swal-input1' class='swal2-input' autofocus minlength='500' >",
confirmButtonText: 'Save',
preConfirm: (login) => {
console.log('document.getElementById(swal-input2).value',document.getElementById('swal-input2').value);
request_string = {
"Request":
[
{
"Col1": "value1",
"Col1": "value2",
"Col1": document.getElementById('swal-input2').value,
"Col1": document.getElementById('swal-input1').value,
}
]
};
fetch('API_URL', {
headers: {
'Accept': 'application/json, text/plain, application/xml, */*',
'Content-Type': 'application/json',
'Access-Control-Allow-Headers': 'Content-Type',
},
method: 'POST',
body: JSON.stringify(request_string)
}
).then(response => {
if (response.status !== 200) {
return;
}
response.text().then(data => {
response_data = data;
response_jsonObj = JSON.parse(response_data);
});
}).catch(error => this.setState({ error }));
},
allowOutsideClick: () => !Swal.isLoading()
}).then((result) => {
swal({
title: " Your request is being processed!",
icon: "success",
confirmButtonText: 'OK'
}).then((okay) => {
if (okay) {
history.push('/page1');
history.push('/page2');
}
});
});
}
})
If you just want to make sure that the first input (i.e. swal-input2) is not null, then you simply need to add preConfirm like that:
preConfirm: () => {
if (document.getElementById('swal-input2').value) {
// Handle return value
} else {
Swal.showValidationMessage('First input missing')
}
}
You can find the working solution here
For those who try to get every inputs with a required attribute try this :
inputAttributes: {
input: 'number',
required: 'true'
}
Hi folks this as been fixed here is the code sample for the same :
Swal.fire({
title: 'Are you sure you want to Save the Notes?',
type: 'info',
showCancelButton: true,
confirmButtonColor: '#3085d6',
cancelButtonColor: '#d33',
confirmButtonText: 'Yes'
}).then((result) => {
console.log('result.value',result.value);
if (result.value) {
Swal.fire( {
title: 'Download Notes',
html:"<div class='b'><p>ID</p></div><input id='swal-input2' class='swal2-input' required/><div class='b'><p>Notes</p></div><input id='swal-input1' class='swal2-input' autofocus minlength='500' >",
confirmButtonText: 'Save',
preConfirm: () => {
if((document.getElementById('swal-input2').value == "") || (document.getElementById('swal-input2').value == '') || ((document.getElementById('swal-input2').value == null)) ){
Swal.showValidationMessage(
`ID is a required field`
)
}
}
}).then((result) => {
request-string = {
"Request":
[
{
"COL1": VALUE1,
"COL2": VALUE2,
"COL3": VALUE3,
"COL4": VALUE4,
"COL5" : VALUE5,
"COL6" : VALUE6,
"COL7": VALUE7
}
]
};
;
fetch('API_URL', {
headers: {
'Accept': 'application/json, text/plain, application/xml, */*',
'Content-Type': 'application/json',
'Access-Control-Allow-Headers': 'Content-Type',
},
method: 'POST',
body: JSON.stringify(request-string)
}
).then(response => {
if (response.status !== 200) {
return;
}
response.text().then(data => {
response_data = data;
response_jsonObj = JSON.parse(response_data);
this.setState({ state: response_jsonObj });
});
}).catch(error => this.setState({ error }));
swal({
title: "Request Submitted Successfully!",
icon: "success",
confirmButtonText: 'OK'
}).then((okay) => {
if (okay) {
history.push('/page1');
history.push('/page2');
}
});
});

How to pass a property from a component to another to perform an ajax request in ReactJS?

I've recently started to study reactjs and I'm currently experimenting with ajax requests and passing properties from parent to children. I have a react component Competitions which performs an ajax request:
var Competitions = React.createClass({
getInitialState: function() {
return {
compData: [],
}
},
componentDidMount: function() {
axios.get(this.props.source, {
headers: {'X-Auth-Token': '*******************',
'Content-type': 'application/json'}
})
.then(result => {
this.setState({compData: result.data});
})
.catch(error => {
console.log(error);
});
},
render: function() {
return (
<CompTable compData={this.state.compData} />
);
}
});
module.exports = Competitions;
The Competitions component passes the results data to CompTable
var CompTable = React.createClass({
propTypes: {
compData: React.PropTypes.array.isRequired
},
handleClick: function(e) {
var url = 'http://api.football-data.org/v1/competitions/x/teams';
var source = url.replace(url.split('/')[5], e);
console.log(source);
},
render: function() {
var list = this.props.compData.map(function (comp, i) {
return (
<tr key={i+1}>
<th scope="row">{i+1}</th>
<td className={comp.id} onClick={this.handleClick.bind(this, comp.id)} >{comp.caption}</td>
</tr>
);
}, this);
return (
<tbody>{list}</tbody>
)
}
});
module.exports = CompTable;
This is the Teams component
var Teams = React.createClass({
getInitialState: function() {
return {
teamData: [],
}
},
componentDidMount: function() {
axios.get(this.props.source, {
headers: {'X-Auth-Token': '*******************',
'Content-type': 'application/json'}
})
.then(result => {
this.setState({teamData: result.teams.data});
})
.catch(error => {
console.log(error);
});
},
render: function() {
return (
<TeamsTable teamData={this.state.teamData} />,
);
}
});
module.exports = Teams;
What I'm trying to do is take on click the compData.id property of the CompTable component with a handleClick function and use it as a source property on another component named Teams (identical with the Competitions component) that uses the given property as a source url in order to perform a new ajax request. Is there a way to do that? Thank you
I think I found a solution to my problem.
So, Competitions is the Parent and CompTable and Teams are the children. I don't know if there is a simpler way, but this one seems to work. It's not perfect, I have other problems to solve, but I managed to make a second ajax call inside a child component using my first ajax call inside the parent component, by grabbing the compData.id property and passing it to the children, on click. Any comments are welcome.
Competitions component
var Competitions = React.createClass({
getInitialState: function() {
return {
compData: [],
id: "",
}
},
componentDidMount: function() {
axios.get(this.props.source, {
headers: {'X-Auth-Token': '********************',
'Content-type': 'application/json'}
})
.then(result => {
this.setState({compData: result.data});
})
.catch(error => {
console.log(error);
});
},
changeId: function (newId) {
this.setState({
id: newId
});
},
render: function() {
return (
<CompTable compData={this.state.compData} id={this.state.id} onClick= {this.changeId} />
);
}
});
module.exports = Competitions;
CompTable component
var CompTable = React.createClass({
propTypes: {
compData: React.PropTypes.array.isRequired
},
getInitialState: function() {
return {
showTeams: false,
hide: false,
};
},
teamsClick: function() {
this.setState({
showTeams: true,
hide: true,
});
},
handleClick: function(e) {
this.props.onClick(e);
},
render: function() {
var list = this.props.compData.map(function (comp, i) {
return (
<tr key={i+1}>
<th scope="row">{i+1}</th>
<td className={comp.id} onClick={function() { this.teamsClick(); this.handleClick(comp.id); }.bind(this)}> {comp.caption} </td>
<td>{comp.league}</td>
<td>{comp.numberOfTeams}</td>
</tr>
);
}, this);
return (
<div > { this.state.showTeams ? <Teams id={this.props.id}/> : null } </div>
<tbody>{list}</tbody>
)
}
});
module.exports = CompTable;
Teams component
var Teams = React.createClass({
getInitialState: function() {
return {
teamData: [],
}
},
componentDidMount: function() {
var url = 'http://api.football-data.org/v1/competitions/x/teams';
var source = url.replace(url.split('/')[5], this.props.id);
axios.get(source, {
headers: {'X-Auth-Token': '********************',
'Content-type': 'application/json'}
})
.then(result => {
this.setState({teamData: result.data.teams});
})
.catch(error => {
console.log(error);
});
},
render: function() {
return (
<TeamsTable teamData={this.state.teamData}/>
);
}
});
module.exports = Teams;

SweetAlert2 - Dynamic queue without clicking confirm button?

I am using the latest version of the jQuery plugin SweetAlert2. I want to use the "Dynamic queue"-function to make a AJAX call. So on the homepage there is a nice example, but you have to click a confirm button first to execute the AJAX call. I do not want this, when the alert is shown the AJAX call should execute immediately, without clicking a button. So how to do this?
Here the example from the homepage
swal.queue
([{
title: 'Your public IP',
confirmButtonText: 'Show my public IP',
text: 'Your public IP will be received via AJAX request',
showLoaderOnConfirm: true,
preConfirm: function()
{
return new Promise(function (resolve)
{
$.get('https://api.ipify.org?format=json').done(function(data)
{
swal.insertQueueStep(data.ip);
resolve();
});
});
}
}])
You should pass the callback with the AJAX request to onOpen parameter:
Swal.queue([{
title: 'Your public IP',
confirmButtonText: 'Show my public IP',
text:
'Your public IP will be received ' +
'via AJAX request',
onOpen: () => {
fetch('https://api.ipify.org?format=json')
.then(response => response.json())
.then(data => {
Swal.insertQueueStep(data.ip)
})
}
}])
<script src="https://cdn.jsdelivr.net/npm/sweetalert2#8"></script>
My working example for auto submit form with sweetalert loading and display results.
var preMessage = $('#new-ad-form').attr('pre-message');
var formData = $('#new-ad-form').serialize();
var formUrl = $('#new-ad-form').attr('action');
Swal.queue([{
allowOutsideClick: false,
allowEscapeKey: false,
title: preMessage,
showConfirmButton: false,
showCloseButton: false,
showCancelButton: false,
onOpen: () => {
Swal.showLoading();
return fetch(formUrl, {
method: 'POST',
body: formData,
headers: {
'Accept': 'application/json, text/plain, */*',
'Content-Type': "application/x-www-form-urlencoded",
}
})
.then(response => response.json())
.then(data => {
Swal.hideLoading();
if (data.status == 'success') {
Swal.update({
allowEscapeKey: false,
allowOutsideClick: false,
showConfirmButton: false,
showCloseButton: false,
showCancelButton: false,
type: 'success',
title: false,
html: data.html
})
} else {
Swal.update({
type: 'error',
title: false,
html: data.html,
allowEscapeKey: true,
allowOutsideClick: true,
showConfirmButton: true,
})
}
})
.catch(() => {
Swal.hideLoading();
Swal.update({
type: 'error',
title: 'Save request error!',
html: false
})
})
}
}]);

Resources