Skip to content

Instantly share code, notes, and snippets.

@djaxho
Created August 25, 2023 12:38
Show Gist options
  • Save djaxho/8fb2855ce2a81357a968697fce54477a to your computer and use it in GitHub Desktop.
Save djaxho/8fb2855ce2a81357a968697fce54477a to your computer and use it in GitHub Desktop.
import { defineStore } from 'pinia'
import { computed, ref } from 'vue'
import moment from 'moment/moment'
import { TimesheetType } from '@/components/PaymentRequest/TimesheetType'
import { PaymentRequestType } from '@/components/PaymentRequest/PaymentRequestType'
import { required, requiredUnless, requiredIf, helpers } from '@vuelidate/validators'
import { useVuelidate } from '@vuelidate/core'
export const usePaymentRequestStore = defineStore('payment-request', () => {
const paymentRequestType = ref(PaymentRequestType.TIME_X_RATE)
const updatePaymentRequestType = (newPaymentRequestType: PaymentRequestType) =>
(paymentRequestType.value = newPaymentRequestType)
const currencySymbol = ref('GBP')
// Dates
const dateStart = ref(moment().format('YYYY-MM-DD'))
const dateEnd = ref(null)
// billing
const billableTime = ref(0.0)
const billableTotal = ref(0)
const rate = ref(10)
const workingHours = ref(8)
// timesheets
const timesheetTypeOptions = [
{ value: TimesheetType.NONE, label: 'None' },
{ value: TimesheetType.FILE, label: 'Upload file' },
{ value: TimesheetType.TIMESHEET, label: 'Timesheet' },
{ value: TimesheetType.COMMENT, label: 'Comment' },
]
const timesheetType = ref(timesheetTypeOptions[1])
// Validation
const validationRules = computed(() => ({
dateStart: { required: helpers.withMessage('An end date must be provided', required) },
dateEnd: {
requiredWhenFixedAmount: helpers.withMessage(
'An end date must be provided',
requiredUnless(paymentRequestType.value === PaymentRequestType.SPECIFY_AMOUNT),
),
},
billableTime: {
requiredWhenTimeXRate: helpers.withMessage(
'Billable time is required',
(value: number) =>
paymentRequestType.value === PaymentRequestType.SPECIFY_AMOUNT || value > 0,
),
},
billableTotal: {
requiredWhenFixedAmount: helpers.withMessage(
'Billable total is required',
(value: number) => paymentRequestType.value === PaymentRequestType.TIME_X_RATE || value > 0,
),
},
}))
const v$ = useVuelidate(validationRules, {
dateStart,
dateEnd,
billableTime,
billableTotal,
})
return {
currencySymbol,
dateStart,
dateEnd,
billableTime,
billableTotal,
rate,
workingHours,
timesheetType,
timesheetTypeOptions,
paymentRequestType,
updatePaymentRequestType,
v$,
}
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment