github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/server/frontend/directeurs/src/logic/notifications.ts (about)

     1  import { AxiosError } from "axios";
     2  
     3  function arrayBufferToString(buffer: ArrayBuffer) {
     4    const uintArray = new Uint8Array(buffer);
     5    const encodedString = String.fromCharCode.apply(null, Array.from(uintArray));
     6    return decodeURIComponent(escape(encodedString));
     7  }
     8  
     9  export interface ErrorOutput {
    10    code: number;
    11    kind: string;
    12    messageHtml: string;
    13  }
    14  
    15  function parseAxiosError(error: AxiosError): ErrorOutput {
    16    let kind: string,
    17      messageHtml: string,
    18      code = null;
    19    if (error.response) {
    20      // The request was made and the server responded with a status code
    21      // that falls out of the range of 2xx
    22      kind = `Erreur côté serveur`;
    23      code = error.response.status;
    24  
    25      messageHtml = error.response.data.message;
    26      if (!messageHtml) {
    27        try {
    28          const json = arrayBufferToString(error.response.data);
    29          messageHtml = JSON.parse(json).message;
    30        } catch (error) {
    31          messageHtml = `Le format d'erreur du serveur n'a pu être décodé.<br/>
    32              Détails : <i>${error}</i>`;
    33        }
    34      }
    35    } else if (error.request) {
    36      // The request was made but no response was received
    37      // `error.request` is an instance of XMLHttpRequest in the browser and an instance of
    38      // http.ClientRequest in node.js
    39      kind = "Aucune réponse du serveur";
    40      messageHtml =
    41        "La requête a bien été envoyée, mais le serveur n'a donné aucune réponse...";
    42    } else {
    43      // Something happened in setting up the request that triggered an Error
    44      kind = "Erreur du client";
    45      messageHtml = `La requête n'a pu être mise en place. <br/>
    46                        Détails :  ${error.message} `;
    47    }
    48    return { kind, messageHtml, code: code || 0 };
    49  }
    50  
    51  export interface Monitor {
    52    show: boolean;
    53    value: number;
    54  }
    55  
    56  interface ProgressEvent {
    57    loaded: number;
    58    total: number;
    59  }
    60  
    61  export class Notifications {
    62    error: ErrorOutput | null = null;
    63    private _success = "";
    64    private _progress = false;
    65    monitor: Monitor = { show: false, value: 0 };
    66  
    67    onAxiosError(error: AxiosError) {
    68      this._progress = false;
    69      this.monitor.show = false;
    70      this.error = parseAxiosError(error);
    71    }
    72  
    73    set success(s: string) {
    74      this._progress = false;
    75      this.monitor.show = false;
    76      this._success = s;
    77    }
    78  
    79    get success() {
    80      return this._success;
    81    }
    82  
    83    set progress(b: boolean) {
    84      this._success = "";
    85      this._progress = b;
    86    }
    87  
    88    get progress() {
    89      return this._progress;
    90    }
    91  
    92    setupMonitor() {
    93      this.monitor.show = true;
    94      this.monitor.value = 0;
    95    }
    96  
    97    updateMonitor = (progressEvent: ProgressEvent) => {
    98      this.monitor.value = (progressEvent.loaded * 100) / progressEvent.total;
    99    };
   100  }