Advertisement
Guest User

Untitled

a guest
Dec 20th, 2018
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.49 KB | None | 0 0
  1. import React, { Component } from 'react';
  2. import Navigation from './Navigation.js';
  3. import { Redirect } from 'react-router-dom';
  4. import qs from 'qs';
  5.  
  6. class UserProfile extends Component {
  7. constructor(props) {
  8. super(props);
  9. this.state = {
  10. data: null,
  11. userID: props.match.params.id,
  12. redirect: false,
  13. name: null,
  14. nif: null,
  15. address: null,
  16. phone: null,
  17. orders: null,
  18. clicked: false,
  19. inputUsername: '',
  20. inputPassword: '',
  21.  
  22. };
  23. this.handleClick = this.handleClick.bind(this);
  24. this.handlePassChange = this.handlePassChange.bind(this);
  25. this.handleUserChange = this.handleUserChange.bind(this);
  26. this.handleSubmit = this.handleSubmit.bind(this);
  27.  
  28. }
  29. handleSubmit(evt) {
  30. evt.preventDefault();
  31.  
  32.  
  33. //this.validateUser();
  34. console.log(this.state.inputPassword);
  35. //this.getToken();
  36. }
  37.  
  38. handleUserChange(evt) {
  39. this.setState({
  40. inputUsername: evt.target.value,
  41. });
  42. console.log(this.state.inputUsername);
  43.  
  44. };
  45.  
  46. handlePassChange(evt) {
  47. this.setState({
  48. inputPassword: evt.target.value,
  49. });
  50. console.log(this.state.inputPassword);
  51.  
  52. }
  53.  
  54. componentWillMount() {
  55. if (sessionStorage.getItem('token')) {
  56. console.log("existe token");
  57. }
  58. else {
  59. this.setState({ redirect: true });
  60. }
  61. }
  62. componentDidMount() {
  63. this.getToken();
  64. }
  65.  
  66. getToken() {
  67. var obj;
  68. fetch('http://localhost:2018/WebApi/token', {
  69. method: 'POST',
  70. headers: {
  71. 'Content-Type': 'application/x-www-form-urlencoded'
  72. },
  73. body: qs.stringify({
  74. username: 'FEUP',
  75. password: 'qualquer1',
  76. company: 'FRUITSHOP',
  77. instance: 'Default',
  78. grant_type: 'password',
  79. Line: 'professional'
  80. })
  81. }).then(response => response.json())
  82. .then(function (data) {
  83. obj = JSON.parse(JSON.stringify(data));
  84. })
  85. .then(() => {
  86. this.userExists(obj.access_token);
  87.  
  88. });
  89. }
  90. getUpdateToken() {
  91. var obj;
  92. fetch('http://localhost:2018/WebApi/token', {
  93. method: 'POST',
  94. headers: {
  95. 'Content-Type': 'application/x-www-form-urlencoded'
  96. },
  97. body: qs.stringify({
  98. username: 'FEUP',
  99. password: 'qualquer1',
  100. company: 'FRUITSHOP',
  101. instance: 'Default',
  102. grant_type: 'password',
  103. Line: 'professional'
  104. })
  105. }).then(response => response.json())
  106. .then(function (data) {
  107. obj = JSON.parse(JSON.stringify(data));
  108. })
  109. .then(() => {
  110. //this.userExists(obj.access_token);
  111. this.removeUser(obj.access_token);
  112. this.updateUser(obj.access_token);
  113. });
  114. }
  115.  
  116. userExists(token) {
  117.  
  118. var baseExists = 'http://localhost:2018/WebApi/Base/Clientes/Existe/'
  119. var existsURL = baseExists + this.state.userID;
  120. console.log(existsURL);
  121. var obj;
  122. fetch(existsURL, {
  123. method: 'GET',
  124. headers: {
  125. 'Authorization': 'Bearer ' + token
  126. },
  127. }).then(response => response.json())
  128. .then(function (data) {
  129. obj = JSON.parse(JSON.stringify(data));
  130. console.log(obj);
  131. })
  132. .then(() => {
  133. if (obj === true) {
  134. this.getClient(token);
  135. console.log("cliente existe");
  136. }
  137. else {
  138. console.log("artigo nao existe");
  139. }
  140. });
  141. }
  142. getClient(token) {
  143.  
  144. var nome;
  145. var nif;
  146. var morada;
  147. var telefone;
  148.  
  149. var baseURL = 'http://localhost:2018/WebApi/Base/Clientes/Edita/';
  150. var newURL = baseURL + this.state.userID;
  151. console.log(newURL);
  152. fetch(newURL, {
  153. method: 'GET',
  154. headers: {
  155. 'Content-Type': 'application/x-www-form-urlencoded',
  156. 'cache-control': 'no-cache',
  157. 'Authorization': 'Bearer ' + token
  158. },
  159. }).then(response => response.json())
  160. .then(function (data) {
  161. //console.log(data);
  162. nome = data.Nome;
  163. morada = data.Morada + ',' + data.Localidade;
  164. telefone = data.Telefone;
  165. nif = data.NumContribuinte;
  166. //console.log(telefone);
  167. })
  168. .then(() => {
  169. this.setState({ name: nome })
  170. this.setState({ phone: telefone })
  171. this.setState({ address: morada })
  172. this.setState({ nif: nif })
  173. this.getClientOrders(token);
  174. });
  175.  
  176. }
  177.  
  178.  
  179. getClientOrders(token) {
  180.  
  181. let entity = sessionStorage.getItem('userID');
  182. entity = "\'" + entity + "\'";
  183. const query = "SELECT CD.Data, CD.TipoDoc, CD.Documento, CD.NumDoc, CD.TotalDocumento, CD.Nome, CD.Entidade,CDS.Estado FROM CabecDoc CD INNER JOIN CabecDocStatus CDS ON CDS.IdCabecDoc = CD.Id WHERE CD.TipoDoc='ECL' and CD.Entidade =";
  184. entity = query + entity;
  185. var url = JSON.stringify(entity);
  186. //console.log(url)
  187. /**
  188. "SELECT CD.Data, CD.TipoDoc, CD.Documento, CD.NumDoc, CD.TotalDocumento, CD.Nome, CD.Entidade,
  189. CDS.Estado FROM CabecDoc CD INNER JOIN CabecDocStatus CDS ON CDS.IdCabecDoc = CD.Id WHERE CD.TipoDoc='ECL' and CD.Entidade = 'C0001'"
  190. */
  191. var obj;
  192. fetch('http://localhost:2018/WebApi/Administrador/Consulta', {
  193. method: 'POST',
  194. headers: {
  195. 'Content-Type': 'application/json',
  196. 'Authorization': 'Bearer ' + token,
  197. },
  198. body: url,
  199. }).then(response => response.json())
  200. .then(function (data) {
  201. obj = JSON.parse(JSON.stringify(data));
  202. obj = obj.DataSet.Table;
  203. console.log(obj);
  204.  
  205. })
  206. .then(() => {
  207. this.setState({ orders: obj })
  208. });
  209.  
  210. }
  211. // <th class="scope">{s.Data}</th>
  212. orderItems = () => {
  213. if (this.state.orders != null) {
  214. return this.state.orders.map(s => {
  215. return <tr>
  216. <th scope="row">{s.NumDoc}</th>
  217. <td>{s.Data}</td>
  218. <td>{s.TotalDocumento}</td>
  219. <td>{s.Estado}</td>
  220. </tr>
  221. })
  222. }
  223. }
  224.  
  225. handleClick(e) {
  226. e.preventDefault();
  227. console.log('The btn was clicked.');
  228. this.setState({ clicked: true });
  229. }
  230. open = () => {
  231. if (this.state.clicked)
  232. return <EditForm />
  233.  
  234. }
  235. handleClick() {
  236. this.setState(state => ({
  237. clicked: !state.clicked
  238. }));
  239. }
  240.  
  241.  
  242. removeUser(token) {
  243. let baseURL = 'http://localhost:2018/WebApi/Base/Clientes/Remove/'+this.state.userID;
  244. fetch(baseURL, {
  245. method: 'POST',
  246. headers: {
  247. 'Authorization': 'Bearer ' + token
  248. },
  249. });
  250.  
  251. }
  252.  
  253.  
  254. updateUser(token) {
  255. var baseURL = 'http://localhost:2018/WebApi/Base/Clientes/Actualiza/';
  256.  
  257. fetch(baseURL, {
  258. method: 'POST',
  259. headers: {
  260. 'Content-Type': 'application/json',
  261. 'cache-control': 'no-cache',
  262. 'Authorization': 'Bearer ' + token
  263. },
  264. body: JSON.stringify({
  265. Cliente: this.state.inputUsername,
  266. Nome: this.state.name,
  267. NumContribuinte: this.state.nif,
  268. Pais: 'PT',
  269. Moeda: 'EUR',
  270. CamposUtil: [
  271. {
  272. Conteudo: "ValorValoruser1",
  273. Nome: "CDU_CampoVar1",
  274. Valor: "user1",
  275. Objecto: null,
  276. Tipo: 1,
  277. ChaveLog: "Nome",
  278. EstadoBE: "",
  279. TipoSimplificado: 1
  280. },
  281. {
  282. Conteudo: "ValorValorpassword",
  283. Nome: "CDU_CampoVar2",
  284. Valor: this.state.inputPassword,
  285. Objecto: null,
  286. Tipo: 1,
  287. ChaveLog: "Nome",
  288. EstadoBE: "",
  289. TipoSimplificado: 1
  290. }
  291. ]
  292. })
  293. }).then(() => this.setState({ redirect: true }));
  294. }
  295.  
  296.  
  297.  
  298. render() {
  299. if (this.state.redirect) {
  300. return (<Redirect to={'/login'} />)
  301. }
  302.  
  303. return (
  304. <div>
  305. <Navigation />
  306. <div className="container main-container mt-5">
  307. <div className="row">
  308. <div className="col-lg-12">
  309. <h3>{this.state.userID}</h3>
  310. </div>
  311. <div className="col-lg-3 text-center">
  312. <img className="img-fluid" src="https://img.icons8.com/ios/1600/user-male-circle-filled.png" alt="Chania"></img>
  313. <br /><br />
  314. <button className="btn btn-primary float-center" onClick={this.handleClick}> Edit profile</button>
  315. {this.open()}
  316.  
  317. </div>
  318.  
  319. <div className="col-lg-9">
  320. <p className="user-bio">
  321. <strong>Nome: </strong> {this.state.name}
  322. </p>
  323. <p className="user-bio">
  324. <strong>NIF: </strong>{this.state.nif}
  325.  
  326. </p>
  327. <p className="user-bio">
  328. <strong>Telefone: </strong> {this.state.phone}
  329. </p>
  330. <p className="user-bio">
  331. <strong>Morada:</strong> {this.state.address}
  332. </p>
  333. </div>
  334. </div>
  335. <br />
  336. <br />
  337.  
  338. <div className="row">
  339. <div className="col-lg-12">
  340. <h3>Shopping History</h3>
  341. </div>
  342. <br />
  343. <br />
  344. <table className="table table-hover">
  345. <thead>
  346. <tr>
  347. <th scope="col">NumDocumento</th>
  348. <th scope="col">Data</th>
  349. <th scope="col">TotalDocumento</th>
  350. <th scope="col">Estado</th>
  351. </tr>
  352. </thead>
  353. <tbody>
  354. {this.orderItems()}
  355. </tbody>
  356. </table>
  357. </div>
  358. <br />
  359. <br />
  360. </div>
  361. </div>
  362. );
  363. }
  364. }
  365.  
  366. class EditForm extends Component {
  367. constructor(props) {
  368. super(props);
  369. this.state = {
  370.  
  371. };
  372. }
  373. render() {
  374. return (
  375. <div>
  376. <form className="form-signin" onSubmit={this.handleSubmit}>
  377. <div className="form-label-group mb-3">
  378. <label>Novo username: </label>
  379. <input type="text" data-test="name" value={this.state.inputUsername} onChange={this.handleUserChange} />
  380. </div>
  381. <div className="form-label-group mb-3">
  382. <label>Nova password: </label>
  383. <input type="password" data-test="passwrod" value={this.state.inputPassword} onChange={this.handlePassChange} />
  384. </div>
  385.  
  386. <button className="btn btn-primary btn-block text-uppercase" type="submit">Sign in</button>
  387.  
  388. </form>
  389. </div>
  390.  
  391. );
  392.  
  393. }
  394. }
  395.  
  396. export default UserProfile;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement