Client - display ascent record depending on user preference
This commit is contained in:
		@@ -42,7 +42,8 @@
 | 
			
		||||
      props.user.records,
 | 
			
		||||
      translateSports(props.sports, t),
 | 
			
		||||
      props.user.timezone,
 | 
			
		||||
      props.user.imperial_units
 | 
			
		||||
      props.user.imperial_units,
 | 
			
		||||
      props.user.display_ascent
 | 
			
		||||
    )
 | 
			
		||||
  )
 | 
			
		||||
</script>
 | 
			
		||||
 
 | 
			
		||||
@@ -29,10 +29,20 @@ export const formatRecord = (
 | 
			
		||||
      )} ${distanceUnitTo}/h`
 | 
			
		||||
      break
 | 
			
		||||
    case 'FD':
 | 
			
		||||
      value = `${convertDistance(+record.value, distanceUnitFrom, distanceUnitTo, 3)} ${distanceUnitTo}`
 | 
			
		||||
      value = `${convertDistance(
 | 
			
		||||
        +record.value,
 | 
			
		||||
        distanceUnitFrom,
 | 
			
		||||
        distanceUnitTo,
 | 
			
		||||
        3
 | 
			
		||||
      )} ${distanceUnitTo}`
 | 
			
		||||
      break
 | 
			
		||||
    case 'HA':
 | 
			
		||||
      value = `${convertDistance(+record.value, ascentUnitFrom, ascentUnitTo, 2)} ${ascentUnitTo}`
 | 
			
		||||
      value = `${convertDistance(
 | 
			
		||||
        +record.value,
 | 
			
		||||
        ascentUnitFrom,
 | 
			
		||||
        ascentUnitTo,
 | 
			
		||||
        2
 | 
			
		||||
      )} ${ascentUnitTo}`
 | 
			
		||||
      break
 | 
			
		||||
    case 'LD':
 | 
			
		||||
      value = record.value
 | 
			
		||||
@@ -62,21 +72,24 @@ export const getRecordsBySports = (
 | 
			
		||||
  records: IRecord[],
 | 
			
		||||
  translatedSports: ITranslatedSport[],
 | 
			
		||||
  tz: string,
 | 
			
		||||
  useImperialUnits: boolean
 | 
			
		||||
  useImperialUnits: boolean,
 | 
			
		||||
  display_ascent: boolean
 | 
			
		||||
): IRecordsBySports =>
 | 
			
		||||
  records.reduce((sportList: IRecordsBySports, record) => {
 | 
			
		||||
    const sport = translatedSports.find((s) => s.id === record.sport_id)
 | 
			
		||||
    if (sport && sport.label) {
 | 
			
		||||
      if (sportList[sport.translatedLabel] === void 0) {
 | 
			
		||||
        sportList[sport.translatedLabel] = {
 | 
			
		||||
          label: sport.label,
 | 
			
		||||
          color: sport.color,
 | 
			
		||||
          records: [],
 | 
			
		||||
  records
 | 
			
		||||
    .filter((r) => (display_ascent ? true : r.record_type !== 'HA'))
 | 
			
		||||
    .reduce((sportList: IRecordsBySports, record) => {
 | 
			
		||||
      const sport = translatedSports.find((s) => s.id === record.sport_id)
 | 
			
		||||
      if (sport && sport.label) {
 | 
			
		||||
        if (sportList[sport.translatedLabel] === void 0) {
 | 
			
		||||
          sportList[sport.translatedLabel] = {
 | 
			
		||||
            label: sport.label,
 | 
			
		||||
            color: sport.color,
 | 
			
		||||
            records: [],
 | 
			
		||||
          }
 | 
			
		||||
        }
 | 
			
		||||
        sportList[sport.translatedLabel].records.push(
 | 
			
		||||
          formatRecord(record, tz, useImperialUnits)
 | 
			
		||||
        )
 | 
			
		||||
      }
 | 
			
		||||
      sportList[sport.translatedLabel].records.push(
 | 
			
		||||
        formatRecord(record, tz, useImperialUnits)
 | 
			
		||||
      )
 | 
			
		||||
    }
 | 
			
		||||
    return sportList
 | 
			
		||||
  }, {})
 | 
			
		||||
      return sportList
 | 
			
		||||
    }, {})
 | 
			
		||||
 
 | 
			
		||||
@@ -400,7 +400,8 @@ describe('getRecordsBySports', () => {
 | 
			
		||||
          testParams.input.records,
 | 
			
		||||
          translatedSports,
 | 
			
		||||
          testParams.input.tz,
 | 
			
		||||
          false
 | 
			
		||||
          false,
 | 
			
		||||
          true
 | 
			
		||||
        ),
 | 
			
		||||
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
 | 
			
		||||
        // @ts-ignore
 | 
			
		||||
@@ -530,6 +531,7 @@ describe('getRecordsBySports after conversion', () => {
 | 
			
		||||
          testParams.input.records,
 | 
			
		||||
          translatedSports,
 | 
			
		||||
          testParams.input.tz,
 | 
			
		||||
          true,
 | 
			
		||||
          true
 | 
			
		||||
        ),
 | 
			
		||||
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
 | 
			
		||||
@@ -539,3 +541,73 @@ describe('getRecordsBySports after conversion', () => {
 | 
			
		||||
    })
 | 
			
		||||
  )
 | 
			
		||||
})
 | 
			
		||||
 | 
			
		||||
describe('getRecordsBySports with HA record', () => {
 | 
			
		||||
  const testsParams = [
 | 
			
		||||
    {
 | 
			
		||||
      description: 'returns empty object if no records',
 | 
			
		||||
      input: {
 | 
			
		||||
        records: [],
 | 
			
		||||
        tz: 'Europe/Paris',
 | 
			
		||||
      },
 | 
			
		||||
      expected: {},
 | 
			
		||||
    },
 | 
			
		||||
    {
 | 
			
		||||
      description: 'returns records except HA record',
 | 
			
		||||
      input: {
 | 
			
		||||
        records: [
 | 
			
		||||
          {
 | 
			
		||||
            id: 9,
 | 
			
		||||
            record_type: 'AS',
 | 
			
		||||
            sport_id: 1,
 | 
			
		||||
            user: 'admin',
 | 
			
		||||
            value: 18,
 | 
			
		||||
            workout_date: 'Sun, 07 Jul 2019 08:00:00 GMT',
 | 
			
		||||
            workout_id: 'hvYBqYBRa7wwXpaStWR4V2',
 | 
			
		||||
          },
 | 
			
		||||
          {
 | 
			
		||||
            id: 9,
 | 
			
		||||
            record_type: 'HA',
 | 
			
		||||
            sport_id: 1,
 | 
			
		||||
            user: 'admin',
 | 
			
		||||
            value: 235,
 | 
			
		||||
            workout_date: 'Sun, 07 Jul 2019 08:00:00 GMT',
 | 
			
		||||
            workout_id: 'hvYBqYBRa7wwXpaStWR4V2',
 | 
			
		||||
          },
 | 
			
		||||
        ],
 | 
			
		||||
        tz: 'Europe/Paris',
 | 
			
		||||
      },
 | 
			
		||||
      expected: {
 | 
			
		||||
        'Cycling (Sport)': {
 | 
			
		||||
          color: null,
 | 
			
		||||
          label: 'Cycling (Sport)',
 | 
			
		||||
          records: [
 | 
			
		||||
            {
 | 
			
		||||
              id: 9,
 | 
			
		||||
              record_type: 'AS',
 | 
			
		||||
              value: '18 km/h',
 | 
			
		||||
              workout_date: '2019/07/07',
 | 
			
		||||
              workout_id: 'hvYBqYBRa7wwXpaStWR4V2',
 | 
			
		||||
            },
 | 
			
		||||
          ],
 | 
			
		||||
        },
 | 
			
		||||
      },
 | 
			
		||||
    },
 | 
			
		||||
  ]
 | 
			
		||||
  testsParams.map((testParams) =>
 | 
			
		||||
    it(testParams.description, () => {
 | 
			
		||||
      assert.deepEqual(
 | 
			
		||||
        getRecordsBySports(
 | 
			
		||||
          testParams.input.records,
 | 
			
		||||
          translatedSports,
 | 
			
		||||
          testParams.input.tz,
 | 
			
		||||
          false,
 | 
			
		||||
          false
 | 
			
		||||
        ),
 | 
			
		||||
        // eslint-disable-next-line @typescript-eslint/ban-ts-comment
 | 
			
		||||
        // @ts-ignore
 | 
			
		||||
        testParams.expected
 | 
			
		||||
      )
 | 
			
		||||
    })
 | 
			
		||||
  )
 | 
			
		||||
})
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user