async getProcessesStations()

in source/console/src/views/Client.tsx [611:668]


  async getProcessesStations(selectedArea: ISelectedData) {
    try {
      const areaId = selectedArea.id;
      let processes: IGeneralQueryData[] = [];
      let stations: IGeneralQueryData[] = [];

      /**
       * Permission is for users in Associate Group only.
       * If there's no permission for the associate user, the user gets nothing.
       */
      if (this.userGroups.length > 0 ||
        (this.userGroups.length === 0 && this.state.permission.id !== '')) {
        processes = await this.graphQlCommon.listProcesses(areaId as string);
        stations = await this.graphQlCommon.listStations(areaId as string);

        if (this.userGroups.length === 0) {
          const permittedProcesses = this.state.permission.processes;
          processes = processes.filter(process =>
            permittedProcesses.map(permittedProcess => {
              return permittedProcess.id;
            }).includes(process.id)
          );

          const permittedStations = this.state.permission.stations;
          stations = stations.filter(station =>
            permittedStations.map(permittedStation => {
              return permittedStation.id;
            }).includes(station.id)
          );
        }
      }

      const initialProcess = this.getInitialSelectItem('Process', processes);
      if (initialProcess) {
        this.setLocalStorage('selectedProcess', initialProcess);
        this.setState({ selectedProcess: initialProcess });
      }
      processes.sort((a, b) => a.name.localeCompare(b.name));

      const initialStation = this.getInitialSelectItem('Station', stations);
      if (initialStation) this.setLocalStorage('selectedStation', initialStation);

      const selectedStation = this.getLocalStorage('selectedStation');
      if (selectedStation && selectedStation.id && selectedStation.id !== '') {
        this.setState({ selectedStation });
        this.getDevices(selectedStation);
      }

      stations.sort((a, b) => a.name.localeCompare(b.name));
      this.setState({
        processes,
        stations
      });
    } catch (error) {
      LOGGER.error('Error while getting processes and stations', error);
      this.setState({ error: I18n.get('error.get.processes.stations') });
    }
  }