Commit 044f2a0b authored by MAGHOUZ's avatar MAGHOUZ
Browse files

Adding tickets works using HttpClient

parent cbc876af
......@@ -40,3 +40,4 @@ testem.log
# System Files
.DS_Store
Thumbs.db
/.github
import {Component, Inject} from '@angular/core';
import {MAT_DIALOG_DATA} from '@angular/material/dialog';
import {MonitoredItem} from '../monitored_item.model';
import {MatListOption} from '@angular/material/list';
@Component({
selector: 'update-tickets-dialog',
......@@ -12,22 +11,26 @@ export class updateTicketsDialog {
private monitoredItem: MonitoredItem
private tickets : string[]
private tickets_to_add : string[]
private tickets_to_add : string
private tickets_to_delete: string[];
private list_tickets: string[]
constructor(@Inject(MAT_DIALOG_DATA) public params) {
this.monitoredItem = params.data
this.tickets = params.data.tickets == null ? [] : params.data.tickets;
this.tickets_to_add = []
this.tickets_to_add = ""
this.tickets_to_delete = []
this.list_tickets = []
}
addTickets() {
alert(this.tickets_to_add)
this.params.monitoreditemsService.addTicketsToMonitoredItem(this.monitoredItem.id,this.tickets_to_add).subscribe()
this.params.monitoreditemsService.searchMessages();
let tickets_to_add = this.tickets_to_add.split(",");
this.params.monitoreditemsService.addTicketsToMonitoredItem(
this.monitoredItem.id,tickets_to_add)
.subscribe(
() =>this.params.monitoreditemsService.searchMessages()
);
}
removeTickets() {
......
import { Injectable } from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {Observable, of} from 'rxjs';
import {catchError, map, tap} from 'rxjs/operators';
import * as converter from 'xml-js';
import {catchError, map} from 'rxjs/operators';
import {MonitoredItem} from './monitored_item.model';
@Injectable({
providedIn: 'root'
})
export class Monitored_itemsService {
private serverUrl = 'http://localhost/graphql'
constructor(private http: HttpClient) { }
private serverUrl = 'http://localhost/graphql?'
searchMessages(type: string): Observable<MonitoredItem[]> {
// tslint:disable-next-line:ban-types
const requestOptions: Object = {
console.log("searchMessages")
const requestOptions: Object = {
/* other options here */
responseType: 'text',
headers: new HttpHeaders()
......@@ -27,9 +27,10 @@ export class Monitored_itemsService {
'Access-Control-Allow-Headers, Access-Control-Allow-Origin, Access-Control-Request-Method'
),
withCredentials: true
};
const url = 'assets/all_monitored_items.json';
//const url = 'assets/all_monitored_items.json';
const query = "{\n" +
" allMonitoredItems {\n" +
" id \n" +
......@@ -45,18 +46,14 @@ export class Monitored_itemsService {
" data\n" +
" }\n" +
"}"
//const url = this.serverUrl + 'query=' + query;
// const url = 'https://cersat-console-exp.ifremer.fr/emm/messages/search?date_min=2020-11-01T00:00:00Z&operator_state=2';
// const url = '/emm/messages/search?date_min=2020-11-01T00:00:00Z&operator_state=2';
// url += '&ticket=' + window.sessionStorage.getItem('ticketUser');
const url = this.serverUrl + '?query=' + query;
console.log("searchMessages")
return this.http.get<MonitoredItem[]>(url, requestOptions)
.pipe(
map(response => this.json2MI(response.toString()))
// ,tap(messages => console.log(messages))
);
console.log("searchMessages")
}
}
/**
* Handle Http operation that failed.
......@@ -65,7 +62,7 @@ export class Monitored_itemsService {
* @param result - optional value to return as the observable result
*/
private handleError<T>(operation = 'operation', result?: T): (error: any) => Observable<T> {
console.log("Erooor")
console.log("handleError",result)
return (error: any): Observable<T> => {
......@@ -103,36 +100,31 @@ export class Monitored_itemsService {
}
addTicketsToMonitoredItem(monitoredItemId, tickets: string[]){
console.log("addTicketsToMonitoredItem")
let formatted_tickets = ""
for(var i = 0;i<tickets.length;i++) {
formatted_tickets += "\""+tickets[i]+"\",";
}
const query = "mutation {\n" +
" updateMonitoredItem (\n" +
" id: \"" + monitoredItemId + "\", \n" +
" tickets: [" + tickets + "]\n" +
" tickets: [" + formatted_tickets + "]\n" +
" ) \n" +
" {\n" +
" monitoredItem {\n" +
" id\n" +
" type\n" +
" logicalPath\n" +
" statusFk\n" +
" creationDate\n" +
" updateDate\n" +
" lastEventDate\n" +
" logicalGroup\n" +
" tickets\n" +
" labels\n" +
" data\n" +
" }\n" +
" }\n" +
"}\n"
const url = this.serverUrl;
"}"
//const url = this.serverUrl + "?query=mutation%20%7BupdateMonitoredItem(id%3A%22"+monitoredItemId+"%22%2Ctickets%3A%5B\""+encodeURIComponent(tickets.toString())+"\"%5D)%7BmonitoredItem%7Bid%7D%7D%7D%0A";
const requestOptions: Object = {
/* other options here */
responseType: 'text',
responseType: "json",
headers: new HttpHeaders()
.set('Content-Type', 'any')
.set('Content-Type', 'application/json')
.append('Access-Control-Allow-Methods', 'POST')
.append('Access-Control-Allow-Origin', '*')
.append(
......@@ -142,13 +134,11 @@ export class Monitored_itemsService {
withCredentials: true
};
return this.http.post(url, {"query":query}, requestOptions)
//let json_query = JSON.parse(query)
return this.http.post(this.serverUrl, {"query":query}, requestOptions)
.pipe(
map(response => console.log(response)),
catchError(this.handleError('addHero', null))
catchError(err => this.handleError('addTickets', err))
);
console.log("addTicketsToMonitoredItem")
}
}
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