Skip to content

Instantly share code, notes, and snippets.

@iErik
Created May 29, 2019 22:45
Show Gist options
  • Select an option

  • Save iErik/15b6ac4cd806b7d2fa896d0d20cdd28d to your computer and use it in GitHub Desktop.

Select an option

Save iErik/15b6ac4cd806b7d2fa896d0d20cdd28d to your computer and use it in GitHub Desktop.
import { get as getProp } from 'convenia-components'
import request from '@Employee:graphql'
import * as types from '@Employee:types'
import * as get from '@Employee:graphql/queries'
import * as set from '@Employee:graphql/mutations'
/**
* Helpers
* -------
*/
const formatDate = value => {
if (!value) return ''
const date = typeof value === 'string'
? new Date(`${value}T00:00:00`)
: new Date(value)
return date.toLocaleDateString('pt-BR')
}
const formatDateUS = date => {
if (!date) return ''
return date.substr(0, 10).split('/').reverse().join('-')
}
export const mapAnnotation = (annotation) => {
return {
...annotation,
date: formatDate(annotation.date)
}
}
/**
* Getters
* -------
*/
export const getEmployeeAnnotations = async (employeeId) => {
const [err, data] = await request(get.GetAnnotations, { employeeId })
const annotations = (data.annotations || []).map(mapAnnotation)
return [err, annotations]
}
/**
* Setters
* -------
*/
export const setEmployeeAnnotation = async (variables, getters) => {
const isCreating = !variables.id
const mutationName = isCreating ? 'CreateAnnotation' : 'UpdateAnnotation'
const annotationInput = {
...(!isCreating
? { id: variables.id }
: {}),
employee_id: getProp(getters[types.EMPLOYEE], 'id'),
title: variables.title,
date: formatDateUS(variables.date),
notes: variables.notes
}
const [err, data] = await request(set[mutationName], { annotationInput })
return [err, mapAnnotation((data || {}).annotation)]
}
export const deleteEmployeeAnnotation = async (annotationId) => {
const annotationInput = { id: annotationId }
const [err, data] = await request(set.DeleteAnnotation, { annotationInput })
return [err, (data || {}).status]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment