Skip to content

Commit

Permalink
Add SavingState and useSavingState composable and component
Browse files Browse the repository at this point in the history
  • Loading branch information
wssheldon committed Nov 30, 2023
1 parent d1da008 commit 6bd7842
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
51 changes: 51 additions & 0 deletions src/dispatch/static/dispatch/src/components/SavingState.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<template>
<div>
<v-progress-circular
v-if="saving"
indeterminate
size="14"
color="grey-lighten-1"
class="pl-6"
/>
<v-icon size="x-small" class="pl-4" v-else>mdi-check</v-icon>
<span class="pl-4 dispatch-text-subtitle">updated {{ formattedUpdatedAt }}</span>
</div>
</template>

<script setup lang="ts">
import { ref, watch, watchEffect } from "vue"
import { formatDistanceToNow, parseISO } from "date-fns"
import { useSavingState } from "@/composables/useSavingState"
const { saving } = useSavingState()
let formattedUpdatedAt = ref("")
let updatedAtRef = ref("")
watchEffect(() => {
if (updatedAtRef.value) {
formattedUpdatedAt.value = formatDistanceToNow(parseISO(updatedAtRef.value)) + " ago"
}
})
watch(saving, (newVal, oldVal) => {
if (oldVal === true && newVal === false) {
updatedAtRef.value = new Date().toISOString()
}
})
const props = defineProps({
updatedAt: {
type: String,
required: true,
},
})
watch(
() => props.updatedAt,
(newVal) => {
if (newVal) {
updatedAtRef.value = newVal
}
}
)
</script>
25 changes: 25 additions & 0 deletions src/dispatch/static/dispatch/src/composables/useSavingState.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { computed, ComputedRef } from "vue"
import { useStore } from "vuex"
import { Store } from "vuex"
import { CaseState } from "@/store/case"

interface UseSavingStateReturns {
saving: ComputedRef<boolean>
// eslint-disable-next-line no-unused-vars
setSaving: (value: boolean) => void
}

export function useSavingState(): UseSavingStateReturns {
const store = useStore<Store<{ case: CaseState }>>()

const saving = computed(() => store.state.case_management.selected.saving)

const setSaving = (value: boolean) => {
store.commit("case_management/SET_SELECTED_SAVING", value)
}

return {
saving,
setSaving,
}
}

0 comments on commit 6bd7842

Please sign in to comment.