<commit_before>import log from '../../core/log';
import React from 'react';
import LinkInstance from './LinkInstance';
import ErrorPage from '../../components/common/ErrorPage';
import fetch from '../../core/fetch';

const getRatings = async (graphqlRequest, linkInstanceUuid, userUuid) => {

  const { data } = await graphqlRequest(
    `query {
      ratings(userUuid: "${userUuid || ''}", linkInstanceUuid: "${linkInstanceUuid}") {
        userUuid,
        linkInstanceUuid,
        avgRating,
        avgAvailabilityRating,
        avgDepartureRating,
        avgArrivalRating,
        avgAwesomeRating,
        userAvailabilityRating,
        userDepartureRating,
        userArrivalRating,
        userAwesomeRating
      }
    }`
  );

  log.info("event=received-link-instance-ratings", "data:", data);

  return data.ratings;

};

export default {

  path: '/link-instance/:uuid?/:action?',

  async action({ params, context }) {
    
    const { graphqlRequest } = context.store.helpers;
    
    let userUuid = null; 
    const state = context.store.getState();
    const { auth } = state.auth;
    if (auth.loggedIn) {
      userUuid = auth.user.uuid;
    }
    
    const getTransportTypes = async () => {

      const { data } = await graphqlRequest(
        `query {
          transportTypes { slug }
        }`
      );

      return data.transportTypes;

    };
    
    try { 
    
      if (params.uuid) {
        
        const { data } = await graphqlRequest(
          `query {
            linkInstance(uuid: "${params.uuid}") {
              uuid,
              link {
                from { apiId, name, description, countryLong, lat, lng },
                to { apiId, name, description, countryLong, lat, lng} 
              },
              transport { slug },
              mode, identifier,
              departureDate, departureHour, departureMinute, departureDescription,
              departureLat, departureLng, departureAddress,
              arrivalDate, arrivalHour, arrivalMinute, arrivalDescription,
              arrivalLat, arrivalLng, arrivalAddress,
              priceAmount, priceCurrency,
              description,
              upVotes, downVotes,
              durationMinutes
            }
          }`
        );
        
        log.info("event=received-link-instance", "data:", data);
        
        const ratings = await getRatings(graphqlRequest, params.uuid, userUuid); 
        
        const edit = params.action === 'edit';
        const props = { edit, linkInstance: data.linkInstance, ratings };
        
        if (edit) {
          props.transportTypes = await getTransportTypes();
        }
        
        return <LinkInstance {...props} />;
    
      } else {
        
        return <LinkInstance edit={true} 
          linkInstance={{}} 
          transportTypes={await getTransportTypes()} />;
      
      }

    } catch (error) {
      return <ErrorPage errors={error.errors} />
    }
  
  }

};<commit_msg>Refactored queries to use fragments<commit_after>import { getLog } from '../../core/log';
const log = getLog('routes/linkInstance');

import React from 'react';
import LinkInstance from './LinkInstance';
import ErrorPage from '../../components/common/ErrorPage';
import fetch from '../../core/fetch';

export default {

  path: '/link-instance/:uuid?/:action?',

  async action({ params, context }) {
    
    const { graphqlRequest } = context.store.helpers;
    
    let userUuid = null; 
    const state = context.store.getState();
    const { auth } = state.auth;
    if (auth.loggedIn) {
      userUuid = auth.user.uuid;
    }
     
    try { 
    
      if (params.uuid) {
        
        const { data } = await graphqlRequest(
          `query {
            linkInstance(uuid: "${params.uuid}") {
              uuid,
              link {
                from { apiId, name, description, countryLong, lat, lng },
                to { apiId, name, description, countryLong, lat, lng} 
              },
              transport { slug },
              mode, identifier,
              departureDate, departureHour, departureMinute, departureDescription,
              departureLat, departureLng, departureAddress,
              arrivalDate, arrivalHour, arrivalMinute, arrivalDescription,
              arrivalLat, arrivalLng, arrivalAddress,
              priceAmount, priceCurrency,
              description,
              upVotes, downVotes,
              durationMinutes
            },
            transportTypes { slug },
            ratings(userUuid: "${userUuid || ''}", linkInstanceUuid: "${params.uuid}") {
              userUuid,
              linkInstanceUuid,
              avgRating,
              avgAvailabilityRating,
              avgDepartureRating,
              avgArrivalRating,
              avgAwesomeRating,
              userAvailabilityRating,
              userDepartureRating,
              userArrivalRating,
              userAwesomeRating
            }
          }`
        );
        
        log.info("event=received-link-instance", "data:", data);
        
        const edit = params.action === 'edit';
        const props = { 
          edit, 
          linkInstance: data.linkInstance,
          ratings: data.ratings 
        };
        
        if (edit) {
          props.transportTypes = data.transportTypes;
        }
        
        return <LinkInstance {...props} />;
    
      } else {
        
        const { data } = await graphqlRequest(
          `query {
            transportTypes { slug },
          }`
        );
        
        return <LinkInstance edit={true} 
          linkInstance={{}} 
          transportTypes={data.transportTypes} />;
      
      }

    } catch (error) {
      log.error(error);
      return <ErrorPage errors={error.errors} />
    }
  
  }

};
{'fraction_non_alphanumeric': 0.11027008429382419, 'fraction_numerical': 0.0, 'mean_word_length': 1.7041860465116279, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 11, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5195052', 'n_tokens_mistral': 1587, 'n_tokens_neox': 1546, 'n_words': 334}