import React, { Component } from 'react';

class Refresher extends Component {
  componentDidMount() {
    const {
      // Put all your params here
      params: {
        patientUuid
      },
      // Everything that comes from state
      user,
      patient,
      // Your actions
      setUserUuid,
      getPatient

    } = this.props;

    // Get pathname from router
    const pathname = window.location.pathname; //eslint-disable-line

    // Refreshers array -- populate your url specific refreshers here
    // Be careful about providing what refreshers needs, otherwise they will fail
    const refreshers = [
      {
        // any url you want to match with this entities, you can use multiple links in array
        urls: ['/new/conditions', '/login'],
        // an array to define multiple checks and attach actions
        entities: [
          {
            exist: patient, // How to check if it is already filled
            action: () => getPatient(patientUuid) // what to do if it doesn't exist
          },
          {
            exist: user,
            action: () => setUserUuid()
          }
        ]

      },
    ];
    // Filter refs based on if pathname matches with any url in array
    const filteredRefreshers = refreshers.filter(ref => ref.urls.find(url => pathname.includes(url)));
    // If found
    if (filteredRefreshers.length > 0) {
      // Map over all the refreshers
      filteredRefreshers.map((refresher) => {
        // Map over the required entities
        refresher.entities.map((entity) => {
          // If doesn't exist
          if (!entity.exist) {
            // Trigger action
            entity.action();
          }
          return null;
        });
        return null;
      });
    }
  }

  render() {
    return null;
  }
}

export default Refresher;
