modified js sample
parent
fc56bb79f7
commit
2937b3ae21
@ -1,26 +1,88 @@
|
||||
class Clock extends React.Component {
|
||||
render() { return }
|
||||
}
|
||||
import { SET_TOKEN } from "./authTypes";
|
||||
|
||||
console.log("hello world!")
|
||||
let car1 = "Saab";
|
||||
let car2 = "Volvo";
|
||||
let car3 = "BMW";
|
||||
const cars = [ car1: "Saab", ca32 "Volvo", car3 = "BMW" ];
|
||||
function isJson(str) {
|
||||
try {
|
||||
JSON.parse(str);
|
||||
} catch (e) {
|
||||
//the json is not ok
|
||||
return false;
|
||||
}
|
||||
//the json is ok
|
||||
return true;
|
||||
}
|
||||
|
||||
const person = {
|
||||
firstName : "John",
|
||||
lastName : "Doe",
|
||||
age : 46
|
||||
export const login = async (data, dispatchToken) => {
|
||||
const requestOptions = {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
};
|
||||
|
||||
const fruits = [ "Banana", "Orange", "Apple", "Mango" ];
|
||||
let fLen = fruits.length;
|
||||
try {
|
||||
const response = await fetch(
|
||||
`http://127.0.0.1:8000/api/token`,
|
||||
requestOptions
|
||||
);
|
||||
|
||||
text = "<ul>";
|
||||
for (let i = 0; i < fLen; i++) {
|
||||
text += "<li>" + fruits[i] + "</li>";
|
||||
if (response.ok) {
|
||||
const res = await response.json();
|
||||
window.localStorage.setItem("userToken", res.token);
|
||||
dispatchToken({ type: SET_TOKEN, token: res.token });
|
||||
return {
|
||||
ok: true,
|
||||
successMessage: "Login Successfull.",
|
||||
errors: null,
|
||||
};
|
||||
} else {
|
||||
const res = await response.json();
|
||||
return {
|
||||
ok: false,
|
||||
successMessage: null,
|
||||
errors: { password: res.errors["non_field_errors"] },
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
alert(error);
|
||||
}
|
||||
text += "</ul>";
|
||||
};
|
||||
|
||||
var arr = {key1 : 'value1', key2 : 'value2'}
|
||||
export const register = async (data, dispatchToken) => {
|
||||
const requestOptions = {
|
||||
method: "POST",
|
||||
headers: {
|
||||
Accept: "application/json",
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(data),
|
||||
};
|
||||
|
||||
try {
|
||||
const response = await fetch(
|
||||
`http://127.0.0.1:8000/api/register`,
|
||||
requestOptions
|
||||
);
|
||||
|
||||
if (response.ok) {
|
||||
const res = await response.json();
|
||||
window.localStorage.setItem("userToken", res.token);
|
||||
dispatchToken({ type: SET_TOKEN, token: res.token });
|
||||
return {
|
||||
ok: true,
|
||||
successMessage: "Register Successfull.",
|
||||
errors: null,
|
||||
};
|
||||
} else {
|
||||
const res = await response.json();
|
||||
return {
|
||||
ok: false,
|
||||
successMessage: null,
|
||||
errors: res["errors"],
|
||||
};
|
||||
}
|
||||
} catch (error) {
|
||||
alert(error);
|
||||
}
|
||||
};
|
||||
|
Loading…
Reference in New Issue