477 lines
14 KiB
Vue
Raw Normal View History

2021-09-28 12:39:12 +02:00
<template>
<div id="workout-edition">
<Card :without-title="false">
2021-09-29 11:32:05 +02:00
<template #title>{{
t(`workouts.${isCreation ? 'ADD' : 'EDIT'}_WORKOUT`)
}}</template>
2021-09-28 12:39:12 +02:00
<template #content>
<div id="workout-form">
<form @submit.prevent="updateWorkout">
<div class="form-items">
2021-09-29 11:32:05 +02:00
<div class="form-item-radio" v-if="isCreation">
<div class="radio">
<input
id="withGpx"
type="radio"
:checked="withGpx"
:disabled="loading"
@click="updateWithGpx"
/>
<label for="withGpx">{{ t('workouts.WITH_GPX') }}</label>
</div>
<div class="radio">
<input
id="withoutGpx"
type="radio"
:checked="!withGpx"
:disabled="loading"
@click="updateWithGpx"
/>
<label for="withoutGpx">{{
t('workouts.WITHOUT_GPX')
}}</label>
</div>
</div>
2021-09-28 12:39:12 +02:00
<div class="form-item">
<label> {{ t('workouts.SPORT', 1) }}: </label>
2021-09-29 11:32:05 +02:00
<select
id="sport"
required
:disabled="loading"
v-model="workoutDataObject.sport_id"
>
2021-09-28 12:39:12 +02:00
<option
v-for="sport in translatedSports"
:value="sport.id"
:key="sport.id"
>
{{ sport.label }}
</option>
</select>
</div>
2021-09-29 11:32:05 +02:00
<div class="form-item" v-if="isCreation && withGpx">
<label for="gpxFile">
{{ t('workouts.GPX_FILE') }}
<sup>
<i class="fa fa-question-circle" aria-hidden="true" />
</sup>
{{ t('workouts.ZIP_FILE') }}
<sup
><i class="fa fa-question-circle" aria-hidden="true" /></sup
>:
</label>
<input
id="gpxFile"
name="gpxFile"
type="file"
accept=".gpx, .zip"
:disabled="loading"
@input="updateFile"
/>
</div>
<div class="form-item" v-else>
2021-09-28 12:39:12 +02:00
<label for="title"> {{ t('workouts.TITLE') }}: </label>
<input
id="title"
name="title"
type="text"
2021-09-29 11:32:05 +02:00
:required="!isCreation"
:disabled="loading"
2021-09-28 12:39:12 +02:00
v-model="workoutDataObject.title"
/>
</div>
2021-09-29 11:32:05 +02:00
<div v-if="!withGpx">
<div class="workout-date-duration">
<div class="form-item">
<label>{{ t('workouts.WORKOUT_DATE') }}:</label>
<div class="workout-date-time">
<input
id="workout-date"
name="workout-date"
type="date"
required
:disabled="loading"
v-model="workoutDataObject.workoutDate"
/>
<input
id="workout-time"
name="workout-time"
class="workout-time"
type="time"
required
:disabled="loading"
v-model="workoutDataObject.workoutTime"
/>
</div>
</div>
<div class="form-item">
<label>{{ t('workouts.DURATION') }}:</label>
<div>
<input
id="workout-duration-hour"
name="workout-duration-hour"
class="workout-duration"
type="text"
placeholder="HH"
pattern="^([0-9]*[0-9])$"
required
:disabled="loading"
v-model="workoutDataObject.workoutDurationHour"
/>
:
<input
id="workout-duration-minutes"
name="workout-duration-minutes"
class="workout-duration"
type="text"
pattern="^([0-5][0-9])$"
placeholder="MM"
required
:disabled="loading"
v-model="workoutDataObject.workoutDurationMinutes"
/>
:
<input
id="workout-duration-seconds"
name="workout-duration-seconds"
class="workout-duration"
type="text"
pattern="^([0-5][0-9])$"
placeholder="SS"
required
:disabled="loading"
v-model="workoutDataObject.workoutDurationSeconds"
/>
</div>
</div>
</div>
<div class="form-item">
<label>{{ t('workouts.DISTANCE') }} (km):</label>
<input
type="number"
min="0"
step="0.1"
required
:disabled="loading"
v-model="workoutDataObject.workoutDistance"
/>
</div>
</div>
2021-09-28 12:39:12 +02:00
<div class="form-item">
<label> {{ t('workouts.NOTES') }}: </label>
<CustomTextArea
name="notes"
:input="workoutDataObject.notes"
2021-09-29 11:32:05 +02:00
:disabled="loading"
2021-09-28 12:39:12 +02:00
@updateValue="updateNotes"
/>
</div>
</div>
<ErrorMessage :message="errorMessages" v-if="errorMessages" />
2021-09-29 11:32:05 +02:00
<div v-if="loading">
<Loader />
</div>
<div v-else class="form-buttons">
<button class="confirm" type="submit" :disabled="loading">
2021-09-28 12:39:12 +02:00
{{ t('buttons.SUBMIT') }}
</button>
2021-09-29 11:32:05 +02:00
<button class="cancel" @click.prevent="onCancel">
2021-09-28 12:39:12 +02:00
{{ t('buttons.CANCEL') }}
</button>
</div>
</form>
</div>
</template>
</Card>
</div>
</template>
<script lang="ts">
import {
ComputedRef,
PropType,
defineComponent,
computed,
reactive,
2021-09-29 11:32:05 +02:00
ref,
2021-09-28 12:39:12 +02:00
watch,
2021-09-29 11:32:05 +02:00
onMounted,
2021-09-28 12:39:12 +02:00
onUnmounted,
} from 'vue'
import { useI18n } from 'vue-i18n'
2021-09-29 11:32:05 +02:00
import { useRouter } from 'vue-router'
2021-09-28 12:39:12 +02:00
import Card from '@/components/Common/Card.vue'
import CustomTextArea from '@/components/Common/CustomTextArea.vue'
import ErrorMessage from '@/components/Common/ErrorMessage.vue'
2021-09-29 11:32:05 +02:00
import Loader from '@/components/Common/Loader.vue'
2021-09-28 12:39:12 +02:00
import { ROOT_STORE, WORKOUTS_STORE } from '@/store/constants'
import { ISport } from '@/types/sports'
2021-09-29 11:32:05 +02:00
import { IAuthUserProfile } from '@/types/user'
import { IWorkout, IWorkoutForm } from '@/types/workouts'
2021-09-28 12:39:12 +02:00
import { useStore } from '@/use/useStore'
2021-09-29 11:32:05 +02:00
import { formatWorkoutDate, getDateWithTZ } from '@/utils/dates'
2021-09-28 12:39:12 +02:00
import { translateSports } from '@/utils/sports'
export default defineComponent({
2021-09-28 17:48:17 +02:00
name: 'WorkoutEdition',
2021-09-28 12:39:12 +02:00
components: {
Card,
CustomTextArea,
ErrorMessage,
2021-09-29 11:32:05 +02:00
Loader,
2021-09-28 12:39:12 +02:00
},
props: {
2021-09-29 11:32:05 +02:00
authUser: {
type: Object as PropType<IAuthUserProfile>,
required: true,
},
isCreation: {
type: Boolean,
default: false,
},
loading: {
type: Boolean,
default: false,
},
2021-09-28 12:39:12 +02:00
sports: {
type: Object as PropType<ISport[]>,
required: true,
},
workout: {
type: Object as PropType<IWorkout>,
2021-09-29 11:32:05 +02:00
required: false,
2021-09-28 12:39:12 +02:00
},
},
setup(props) {
const { t } = useI18n()
const store = useStore()
2021-09-29 11:32:05 +02:00
const router = useRouter()
onMounted(() => {
if (props.workout && props.workout.id) {
formatWorkoutForm(props.workout)
}
})
2021-09-28 12:39:12 +02:00
const translatedSports: ComputedRef<ISport[]> = computed(() =>
translateSports(props.sports, t)
)
const errorMessages: ComputedRef<string | string[] | null> = computed(
() => store.getters[ROOT_STORE.GETTERS.ERROR_MESSAGES]
)
const workoutForm = reactive({
2021-09-29 11:32:05 +02:00
sport_id: '',
2021-09-28 12:39:12 +02:00
title: '',
notes: '',
2021-09-29 11:32:05 +02:00
workoutDate: '',
workoutTime: '',
workoutDurationHour: '',
workoutDurationMinutes: '',
workoutDurationSeconds: '',
workoutDistance: '',
2021-09-28 12:39:12 +02:00
})
2021-09-29 11:32:05 +02:00
let withGpx = ref(
props.workout ? props.workout.with_gpx : props.isCreation
)
let gpxFile: File | null = null
2021-09-28 12:39:12 +02:00
function updateNotes(value: string) {
workoutForm.notes = value
}
2021-09-29 11:32:05 +02:00
function updateWithGpx() {
withGpx.value = !withGpx.value
}
function updateFile(event: Event & { target: HTMLInputElement }) {
if (event.target.files) {
gpxFile = event.target.files[0]
}
}
function formatWorkoutForm(workout: IWorkout) {
workoutForm.sport_id = `${workout.sport_id}`
workoutForm.title = workout.title
workoutForm.notes = workout.notes
if (!workout.with_gpx) {
const workoutDateTime = formatWorkoutDate(
getDateWithTZ(workout.workout_date, props.authUser.timezone),
'yyyy-MM-dd'
)
const duration = workout.duration.split(':')
workoutForm.workoutDistance = `${workout.distance}`
workoutForm.workoutDate = workoutDateTime.workout_date
workoutForm.workoutTime = workoutDateTime.workout_time
workoutForm.workoutDurationHour = duration[0]
workoutForm.workoutDurationMinutes = duration[1]
workoutForm.workoutDurationSeconds = duration[2]
}
}
function formatPayload(payload: IWorkoutForm) {
payload.title = workoutForm.title
payload.distance = +workoutForm.workoutDistance
payload.duration =
+workoutForm.workoutDurationHour * 3600 +
+workoutForm.workoutDurationMinutes * 60 +
+workoutForm.workoutDurationSeconds
payload.workout_date = `${workoutForm.workoutDate} ${workoutForm.workoutTime}`
}
2021-09-28 12:39:12 +02:00
function updateWorkout() {
2021-09-29 11:32:05 +02:00
const payload: IWorkoutForm = {
sport_id: +workoutForm.sport_id,
notes: workoutForm.notes,
}
2021-09-28 12:39:12 +02:00
if (props.workout) {
2021-09-29 11:32:05 +02:00
if (props.workout.with_gpx) {
store.dispatch(WORKOUTS_STORE.ACTIONS.EDIT_WORKOUT, {
workoutId: props.workout.id,
data: payload,
})
} else {
formatPayload(payload)
store.dispatch(
WORKOUTS_STORE.ACTIONS.ADD_WORKOUT_WITHOUT_GPX,
payload
)
}
} else {
if (withGpx.value) {
if (!gpxFile) {
throw new Error('No file provided !!')
}
payload.file = gpxFile
store.dispatch(WORKOUTS_STORE.ACTIONS.ADD_WORKOUT, payload)
} else {
formatPayload(payload)
store.dispatch(
WORKOUTS_STORE.ACTIONS.ADD_WORKOUT_WITHOUT_GPX,
payload
)
}
}
}
function onCancel() {
if (props.workout) {
router.push({
name: 'Workout',
params: { workoutId: props.workout.id },
2021-09-28 12:39:12 +02:00
})
2021-09-29 11:32:05 +02:00
} else {
router.go(-1)
2021-09-28 12:39:12 +02:00
}
}
watch(
() => props.workout,
2021-09-29 11:32:05 +02:00
async (
newWorkout: IWorkout | undefined,
previousWorkout: IWorkout | undefined
) => {
if (newWorkout !== previousWorkout && newWorkout && newWorkout.id) {
formatWorkoutForm(newWorkout)
2021-09-28 12:39:12 +02:00
}
}
)
onUnmounted(() => store.commit(ROOT_STORE.MUTATIONS.EMPTY_ERROR_MESSAGES))
return {
errorMessages,
t,
translatedSports,
2021-09-29 11:32:05 +02:00
withGpx,
2021-09-28 12:39:12 +02:00
workoutDataObject: workoutForm,
2021-09-29 11:32:05 +02:00
onCancel,
updateFile,
2021-09-28 12:39:12 +02:00
updateNotes,
2021-09-29 11:32:05 +02:00
updateWithGpx,
2021-09-28 12:39:12 +02:00
updateWorkout,
}
},
})
</script>
<style lang="scss" scoped>
@import '~@/scss/base';
#workout-edition {
2021-09-29 11:32:05 +02:00
margin: 100px auto;
2021-09-28 12:39:12 +02:00
width: 700px;
2021-09-29 11:32:05 +02:00
@media screen and (max-width: $small-limit) {
width: 100%;
margin: 0 auto 50px auto;
}
2021-09-28 12:39:12 +02:00
::v-deep(.card) {
.card-title {
text-align: center;
text-transform: uppercase;
}
2021-09-29 11:32:05 +02:00
2021-09-28 12:39:12 +02:00
.card-content {
2021-09-29 11:32:05 +02:00
@media screen and (max-width: $small-limit) {
padding: $default-padding 0;
}
2021-09-28 12:39:12 +02:00
2021-09-29 11:32:05 +02:00
#workout-form {
.form-items {
2021-09-28 12:39:12 +02:00
display: flex;
flex-direction: column;
2021-09-29 11:32:05 +02:00
input {
height: 20px;
}
.workout-date-duration {
display: flex;
flex-direction: row;
justify-content: space-between;
@media screen and (max-width: $small-limit) {
flex-direction: column;
}
}
.form-item {
display: flex;
flex-direction: column;
padding: $default-padding;
.workout-date-time {
display: flex;
#workout-date {
margin-right: $default-margin;
}
}
.workout-duration {
width: 25px;
}
}
.form-item-radio {
display: flex;
justify-content: space-around;
.radio {
label {
font-weight: normal;
}
input {
margin-top: -2px;
vertical-align: middle;
}
}
2021-09-28 12:39:12 +02:00
}
}
2021-09-29 11:32:05 +02:00
.form-buttons {
display: flex;
justify-content: flex-end;
button {
margin: $default-padding * 0.5;
}
2021-09-28 12:39:12 +02:00
}
}
}
}
}
</style>