Skip to content

Commit

Permalink
Add archive banner to dataset and dataservice (#229)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolaskempf57 authored Dec 31, 2024
1 parent 8da7ed7 commit 8a1729f
Show file tree
Hide file tree
Showing 8 changed files with 160 additions and 45 deletions.
115 changes: 77 additions & 38 deletions components/Dataservices/AdminUpdateDataservicePage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,52 +15,69 @@
{{ t("Save") }}
</button>
</template>
<BannerAction
class="mt-5"
type="danger"
:title="$t('Delete the dataservice')"
>
{{ $t("Be careful, this action can't be reverse.") }}
<template #button>
<ModalWithButton
:title="$t('Are you sure you want to delete this dataservice ?')"
size="lg"
>
<template #button="{ attrs, listeners }">
<BrandedButton
color="danger"
size="xs"
:icon="RiDeleteBin6Line"
v-bind="attrs"
v-on="listeners"
>
{{ $t('Delete') }}
</BrandedButton>
</template>
<p class="fr-text--bold">
{{ $t("This action can't be reverse.") }}
</p>
<template #footer>
<div class="flex-1 fr-btns-group fr-btns-group--right fr-btns-group--inline-reverse fr-btns-group--inline-lg fr-btns-group--icon-left">
<div class="mt-5 space-y-5">
<BannerAction
type="warning"
:title="dataservice.archived_at ? $t('Unarchive the dataservice') : $t('Archive the dataservice')"
>
{{ $t("An archived dataservice is no longer indexed but still accessible for users with the direct link.") }}

<template #button>
<BannerActionButton
:icon="RiArchiveLine"
type="button"
@click="archiveDataservice"
>
{{ dataservice.archived_at ? $t('Unarchive') : $t('Archive') }}
</BannerActionButton>
</template>
</BannerAction>
<BannerAction
type="danger"
:title="$t('Delete the dataservice')"
>
{{ $t("Be careful, this action can't be reverse.") }}
<template #button>
<ModalWithButton
:title="$t('Are you sure you want to delete this dataservice ?')"
size="lg"
>
<template #button="{ attrs, listeners }">
<BrandedButton
color="danger"
:disabled="loading"
@click="deleteDataservice"
size="xs"
:icon="RiDeleteBin6Line"
v-bind="attrs"
v-on="listeners"
>
{{ $t("Delete the dataservice") }}
{{ $t('Delete') }}
</BrandedButton>
</div>
</template>
</ModalWithButton>
</template>
</BannerAction>
</template>
<p class="fr-text--bold">
{{ $t("This action can't be reverse.") }}
</p>
<template #footer>
<div class="flex-1 fr-btns-group fr-btns-group--right fr-btns-group--inline-reverse fr-btns-group--inline-lg fr-btns-group--icon-left">
<BrandedButton
color="danger"
:disabled="loading"
@click="deleteDataservice"
>
{{ $t("Delete the dataservice") }}
</BrandedButton>
</div>
</template>
</ModalWithButton>
</template>
</BannerAction>
</div>
</DescribeDataservice>
</div>
</template>

<script setup lang="ts">
import type { Dataservice } from '@datagouv/components'
import { RiDeleteBin6Line } from '@remixicon/vue'
import { RiArchiveLine, RiDeleteBin6Line } from '@remixicon/vue'
import DescribeDataservice from '~/components/Dataservices/DescribeDataservice.vue'
import type { ContactPoint, DataserviceForm } from '~/types/types'
import { toForm, toApi } from '~/utils/dataservices'
Expand All @@ -75,13 +92,13 @@ const loading = ref(false)
const localePath = useLocalePath()
const url = computed(() => `/api/1/dataservices/${route.params.id}`)
const { data: dataservice } = await useAPI<Dataservice>(url, { lazy: true })
const { data: dataservice, refresh } = await useAPI<Dataservice>(url, { lazy: true })
const dataserviceForm = ref<DataserviceForm | null>(null)
watchEffect(() => {
dataserviceForm.value = toForm(dataservice.value)
})
const save = async () => {
async function save() {
if (!dataserviceForm.value) throw new Error('No dataservice form')
try {
Expand Down Expand Up @@ -115,6 +132,28 @@ const save = async () => {
}
}
async function archiveDataservice() {
if (!dataserviceForm.value) throw new Error('No dataservice form')
try {
loading.value = true
await $api(`/api/1/dataservices/${dataservice.value.id}/`, {
method: 'PATCH',
body: JSON.stringify(toApi(dataserviceForm.value, { archived_at: dataservice.value.archived_at ? null : new Date().toISOString() })),
})
refresh()
if (dataservice.value.archived_at) {
toast.success(t('Dataservice unarchived!'))
}
else {
toast.success(t('Dataservice archived!'))
}
}
finally {
loading.value = false
}
}
async function deleteDataservice() {
loading.value = true
try {
Expand Down
42 changes: 39 additions & 3 deletions components/Datasets/AdminUpdateDatasetPage.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,22 @@
:subject="dataset"
:label="$t('Transfer dataset')"
/>
<BannerAction
type="warning"
:title="dataset.archived ? $t('Unarchive the dataset') : $t('Archive the dataset')"
>
{{ $t("An archived dataset is no longer indexed but still accessible for users with the direct link.") }}

<template #button>
<BannerActionButton
:icon="RiArchiveLine"
type="button"
@click="archiveDataset"
>
{{ dataset.archived ? $t('Unarchive') : $t('Archive') }}
</BannerActionButton>
</template>
</BannerAction>
<BannerAction
type="danger"
:title="$t('Delete the dataset')"
Expand Down Expand Up @@ -58,7 +73,7 @@

<script setup lang="ts">
import type { Dataset, Frequency, License } from '@datagouv/components'
import { RiDeleteBin6Line } from '@remixicon/vue'
import { RiArchiveLine, RiDeleteBin6Line } from '@remixicon/vue'
import DescribeDataset from '~/components/Datasets/DescribeDataset.vue'
import type { DatasetForm, EnrichedLicense, SpatialGranularity } from '~/types/types'
import { toForm, toApi } from '~/utils/datasets'
Expand Down Expand Up @@ -99,7 +114,7 @@ const licenses = computed(() => {
const { data: granularities } = await useAPI<Array<SpatialGranularity>>('/api/1/spatial/granularities/', { lazy: true })
const url = computed(() => `/api/1/datasets/${route.params.id}`)
const { data: dataset } = await useAPI<Dataset>(url, { lazy: true })
const { data: dataset, refresh } = await useAPI<Dataset>(url, { lazy: true })
const datasetForm = ref<DatasetForm | null>(null)
watchEffect(() => {
if (dataset.value && licenses.value && frequencies.value && granularities.value) {
Expand All @@ -113,7 +128,7 @@ watchEffect(() => {
}
})
const save = async () => {
async function save() {
if (!datasetForm.value) throw new Error('No dataset form')
try {
Expand Down Expand Up @@ -149,4 +164,25 @@ async function deleteDataset() {
loading.value = false
}
}
async function archiveDataset() {
if (!datasetForm.value) throw new Error('No dataset form')
loading.value = true
try {
await $api(`/api/1/datasets/${dataset.value.id}/`, {
method: 'PUT',
body: JSON.stringify(toApi(datasetForm.value, { archived: dataset.value.archived ? null : new Date().toISOString() })),
})
refresh()
if (dataset.value.archived) {
toast.success(t('Dataset unarchived!'))
}
else {
toast.success(t('Dataset archived!'))
}
}
finally {
loading.value = false
}
}
</script>
12 changes: 12 additions & 0 deletions lang/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -766,5 +766,17 @@
"{n} posts": "{n} posts | {n} post | {n} posts",
"Published the {date}": "Published the {date}",
"Posts": "Posts",
"Unarchive the dataservice": "Unarchive the dataservice",
"Archive the dataservice": "Archive the dataservice",
"An archived dataservice is no longer indexed but still accessible for users with the direct link.": "An archived dataservice is no longer indexed but still accessible for users with the direct link.",
"Unarchive": "Unarchive",
"Archive": "Archive",
"Dataservice unarchived!": "Dataservice unarchived!",
"Dataservice archived!": "Dataservice archived!",
"Unarchive the dataset": "Unarchive the dataset",
"Archive the dataset": "Archive the dataset",
"An archived dataset is no longer indexed but still accessible for users with the direct link.": "An archived dataset is no longer indexed but still accessible for users with the direct link.",
"Dataset unarchived!": "Dataset unarchived!",
"Dataset archived!": "Dataset archived!",
"": ""
}
12 changes: 12 additions & 0 deletions lang/es-ES.json
Original file line number Diff line number Diff line change
Expand Up @@ -759,5 +759,17 @@
"{n} posts": "{n} posts | {n} post | {n} posts",
"Published the {date}": "Published the {date}",
"Posts": "Posts",
"Unarchive the dataservice": "Unarchive the dataservice",
"Archive the dataservice": "Archive the dataservice",
"An archived dataservice is no longer indexed but still accessible for users with the direct link.": "An archived dataservice is no longer indexed but still accessible for users with the direct link.",
"Unarchive": "Unarchive",
"Archive": "Archive",
"Dataservice unarchived!": "Dataservice unarchived!",
"Dataservice archived!": "Dataservice archived!",
"Unarchive the dataset": "Unarchive the dataset",
"Archive the dataset": "Archive the dataset",
"An archived dataset is no longer indexed but still accessible for users with the direct link.": "An archived dataset is no longer indexed but still accessible for users with the direct link.",
"Dataset unarchived!": "Dataset unarchived!",
"Dataset archived!": "Dataset archived!",
"": ""
}
12 changes: 12 additions & 0 deletions lang/fr-FR.json
Original file line number Diff line number Diff line change
Expand Up @@ -787,5 +787,17 @@
"{n} posts": "{n} articles | {n} article | {n} articles",
"Published the {date}": "Publié le {date}",
"Posts": "Articles",
"Unarchive the dataservice": "Désarchiver cette API",
"Archive the dataservice": "Archiver cette API",
"An archived dataservice is no longer indexed but still accessible for users with the direct link.": "Une API archivée n'est plus indexée mais reste accessible aux utilisateurs avec un lien direct.",
"Unarchive": "Désarchiver",
"Archive": "Archiver",
"Dataservice unarchived!": "API désarchivée!",
"Dataservice archived!": "API archivée!",
"Unarchive the dataset": "Désarchiver le jeu de données",
"Archive the dataset": "Archiver le jeu de données",
"An archived dataset is no longer indexed but still accessible for users with the direct link.": "Un jeu de données archivé n'est plus indexé mais reste accessible aux utilisateurs avec un lien direct.",
"Dataset unarchived!": "Jeu de données désarchivé!",
"Dataset archived!": "Jeu de données archivé!",
"": ""
}
2 changes: 2 additions & 0 deletions types/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ export type DatasetForm = {
export type NewDatasetForApi = {
title: string
private?: boolean
archived?: string | null
acronym?: string
description: string
organization?: string
Expand Down Expand Up @@ -202,6 +203,7 @@ export type NewDataserviceForApi = {
organization?: string
owner?: string
title: string
archived_at?: string | null
private?: boolean
acronym?: string
description: string
Expand Down
6 changes: 4 additions & 2 deletions utils/dataservices.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ export function getDataserviceAdminUrl(dataservice: Dataservice): string {
}
else if (dataservice.organization) {
return `/beta/admin/organizations/${dataservice.organization.id}/dataservices/${dataservice.id}`
} else {
}
else {
return ''
}
}
Expand All @@ -31,14 +32,15 @@ export function toForm(dataservice: Dataservice): DataserviceForm {
}
}

export function toApi(form: DataserviceForm, overrides: { datasets?: Array<Dataset | DatasetSuggest>, private?: boolean } = {}): NewDataserviceForApi {
export function toApi(form: DataserviceForm, overrides: { archived_at?: string | null, datasets?: Array<Dataset | DatasetSuggest>, private?: boolean } = {}): NewDataserviceForApi {
return {
organization: form.owned?.organization?.id,
owner: form.owned?.owner?.id,
title: form.title,
description: form.description,
acronym: form.acronym,
private: overrides.private,
archived_at: overrides.archived_at,
datasets: overrides.datasets ? overrides.datasets.map(({ id }) => id) : undefined,
contact_point: form.contact_point && 'id' in form.contact_point ? form.contact_point.id : undefined,
is_restricted: form.is_restricted,
Expand Down
4 changes: 2 additions & 2 deletions utils/datasets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import type { Dataset, DatasetV2, Frequency, License, RegisteredSchema, Resource
import type { FetchError } from 'ofetch'
import type { Component } from 'vue'
import { v4 as uuidv4 } from 'uuid'
import { useNuxt } from 'nuxt/kit'
import Archive from '~/components/Icons/Archive.vue'
import Code from '~/components/Icons/Code.vue'
import Documentation from '~/components/Icons/Documentation.vue'
Expand Down Expand Up @@ -117,12 +116,13 @@ export function toForm(dataset: Dataset, licenses: Array<License>, frequencies:
}
}

export function toApi(form: DatasetForm, overrides: { private?: boolean } = {}): NewDatasetForApi {
export function toApi(form: DatasetForm, overrides: { private?: boolean, archived?: string | null } = {}): NewDatasetForApi {
return {
organization: form.owned?.organization?.id,
owner: form.owned?.owner?.id,
title: form.title,
private: overrides.private,
archived: overrides.archived,
description: form.description,
acronym: form.acronym,
tags: form.tags.map(t => t.text),
Expand Down

0 comments on commit 8a1729f

Please sign in to comment.