<commit_before>// @flow
/**
 * All About Olaf
 * Student Work page
 */

import React from 'react'
import {StyleSheet, Platform, Text} from 'react-native'
import * as c from '../components/colors'
import SimpleListView from '../components/listview'
import {ListSeparator, ListSectionHeader} from '../components/list'
import {tracker} from '../../analytics'
import bugsnag from '../../bugsnag'
import {NoticeView} from '../components/notice'
import LoadingView from '../components/loading'
import delay from 'delay'
import size from 'lodash/size'
import sortBy from 'lodash/sortBy'
import groupBy from 'lodash/groupBy'
import {JobRow} from './student-work/job-row'
import type {JobType} from './student-work/types'

const jobsUrl =
  'https://www.stolaf.edu/apps/stuwork/index.cfm?fuseaction=getall&nostructure=1'

const headerHeight = Platform.OS === 'ios' ? 33 : 41

const jobSort = new Map([
  ['On-campus Work Study', 1],
  ['Off-campus Community Service Work Study', 2],
  ['On-campus Summer Employment', 3],
  ['Off-campus Summer Employment', 4],
  ['Unknown', Infinity],
])

const styles = StyleSheet.create({
  listContainer: {
    backgroundColor: c.white,
  },
  rowSectionHeader: {
    height: headerHeight,
  },
})

export default class StudentWorkView extends React.Component {
  state: {
    jobs: {[key: string]: JobType[]},
    loading: boolean,
    refreshing: boolean,
    error: boolean,
  } = {
    jobs: {},
    loading: false,
    refreshing: false,
    error: false,
  }

  componentWillMount() {
    this.refresh()
  }

  fetchData = async () => {
    try {
      const data: {[key: string]: JobType[]} = await fetchJson(jobsUrl)

      const sorted = sortBy(data, [
        j => jobSort.get(j.type) || jobSort.get('Unknown'),
        j => j.lastModified,
      ])

      const grouped = groupBy(sorted, j => j.type)
      this.setState({jobs: grouped})
    } catch (err) {
      tracker.trackException(err.message)
      bugsnag.notify(err)
      this.setState({error: true})
      console.error(err)
    }

    this.setState({loading: true})
  }

  refresh = async () => {
    const start = Date.now()
    this.setState(() => ({refreshing: true}))

    await this.fetchData()

    // wait 0.5 seconds – if we let it go at normal speed, it feels broken.
    const elapsed = start - Date.now()
    if (elapsed < 500) {
      await delay(500 - elapsed)
    }
    this.setState(() => ({refreshing: false}))
  }

  onPressJob = (title: string, job: JobType) => {
    this.props.navigator.push({
      id: 'JobDetailView',
      index: this.props.route.index + 1,
      title: title,
      backButtonTitle: 'Jobs',<commit_msg>convert setstate to callback functions<commit_after>// @flow
/**
 * All About Olaf
 * Student Work page
 */

import React from 'react'
import {StyleSheet, Platform, Text} from 'react-native'
import * as c from '../components/colors'
import SimpleListView from '../components/listview'
import {ListSeparator, ListSectionHeader} from '../components/list'
import {tracker} from '../../analytics'
import bugsnag from '../../bugsnag'
import {NoticeView} from '../components/notice'
import LoadingView from '../components/loading'
import delay from 'delay'
import size from 'lodash/size'
import sortBy from 'lodash/sortBy'
import groupBy from 'lodash/groupBy'
import {JobRow} from './student-work/job-row'
import type {JobType} from './student-work/types'

const jobsUrl =
  'https://www.stolaf.edu/apps/stuwork/index.cfm?fuseaction=getall&nostructure=1'

const headerHeight = Platform.OS === 'ios' ? 33 : 41

const jobSort = new Map([
  ['On-campus Work Study', 1],
  ['Off-campus Community Service Work Study', 2],
  ['On-campus Summer Employment', 3],
  ['Off-campus Summer Employment', 4],
  ['Unknown', Infinity],
])

const styles = StyleSheet.create({
  listContainer: {
    backgroundColor: c.white,
  },
  rowSectionHeader: {
    height: headerHeight,
  },
})

export default class StudentWorkView extends React.Component {
  state: {
    jobs: {[key: string]: JobType[]},
    loading: boolean,
    refreshing: boolean,
    error: boolean,
  } = {
    jobs: {},
    loading: false,
    refreshing: false,
    error: false,
  }

  componentWillMount() {
    this.refresh()
  }

  fetchData = async () => {
    try {
      const data: {[key: string]: JobType[]} = await fetchJson(jobsUrl)

      const sorted = sortBy(data, [
        j => jobSort.get(j.type) || jobSort.get('Unknown'),
        j => j.lastModified,
      ])

      const grouped = groupBy(sorted, j => j.type)
      this.setState(() => ({jobs: grouped}))
    } catch (err) {
      tracker.trackException(err.message)
      bugsnag.notify(err)
      this.setState(() => ({error: true}))
      console.error(err)
    }

    this.setState(() => ({loading: true}))
  }

  refresh = async () => {
    const start = Date.now()
    this.setState(() => ({refreshing: true}))

    await this.fetchData()

    // wait 0.5 seconds – if we let it go at normal speed, it feels broken.
    const elapsed = start - Date.now()
    if (elapsed < 500) {
      await delay(500 - elapsed)
    }
    this.setState(() => ({refreshing: false}))
  }

  onPressJob = (title: string, job: JobType) => {
    this.props.navigator.push({
      id: 'JobDetailView',
      index: this.props.route.index + 1,
      title: title,
      backButtonTitle: 'Jobs',
{'fraction_non_alphanumeric': 0.1528948847667229, 'fraction_numerical': 0.006745362563237774, 'mean_word_length': 3.134779240898528, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 22, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, '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': '1763113', 'n_tokens_mistral': 1746, 'n_tokens_neox': 1691, 'n_words': 462}