Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix admin profil #546

Merged
merged 12 commits into from
Oct 1, 2024
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
## Current (in progress)

- Adapt to discussion csv export refactor [#543](https://github.com/datagouv/udata-front/pull/543)
- Fix Beta admin profile page [#546](https://github.com/datagouv/udata-front/pull/546)

## 5.2.2 (2024-09-23)

Expand Down
13 changes: 7 additions & 6 deletions udata_front/theme/gouvfr/assets/js/api/organizations.ts
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
import type { NewOrganization, Organization } from "@datagouv/components/ts";
import { type MaybeRefOrGetter, toValue } from "vue";
import { api } from "../plugins/api";
import { getLocalizedUrl } from "../i18n";
import type { Member, MemberRole, OrganizationV1, PendingMembershipRequest } from "../types";
import type { Member, MemberRole, PendingMembershipRequest } from "../types";

type UploadLogoResponse = {
image: string;
success: boolean;
};

export function createOrganization(organization: MaybeRefOrGetter<OrganizationV1>) {
return api.post<OrganizationV1>("organizations/", {
export function createOrganization(organization: MaybeRefOrGetter<NewOrganization>) {
return api.post<Organization>("organizations/", {
...toValue(organization),
}).then(resp => resp.data);
}
Expand All @@ -20,15 +21,15 @@ export function uploadLogo(oid: string, file: File) {
}).then(resp => resp.data);
}

export function updateOrganization(organization: MaybeRefOrGetter<OrganizationV1>) {
export function updateOrganization(organization: MaybeRefOrGetter<Organization>) {
const organizationValue = toValue(organization);
return api.put<OrganizationV1>(`organizations/${organizationValue.id}/`, {
return api.put<Organization>(`organizations/${organizationValue.id}/`, {
...organizationValue,
}).then(resp => resp.data);
}

export function getOrganization(oid: string) {
return api.get<OrganizationV1>(getLocalizedUrl(`organizations/${oid}/`))
return api.get<Organization>(getLocalizedUrl(`organizations/${oid}/`))
.then(res => res.data);
}

Expand Down
5 changes: 3 additions & 2 deletions udata_front/theme/gouvfr/assets/js/locales/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -541,5 +541,6 @@
"Publish the reuse": "Publish the reuse",
"{n} reuses of your organization": "{n} reuses of your organization",
"Reuse title": "Reuse title",
"You haven't published a reuse yet": "You haven't published a reuse yet"
}
"You haven't published a reuse yet": "You haven't published a reuse yet",
"See the organization page": ""
}
5 changes: 3 additions & 2 deletions udata_front/theme/gouvfr/assets/js/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -541,5 +541,6 @@
"Publish the reuse": "Publish the reuse",
"{n} reuses of your organization": "{n} reuses of your organization",
"Reuse title": "Reuse title",
"You haven't published a reuse yet": "You haven't published a reuse yet"
}
"You haven't published a reuse yet": "You haven't published a reuse yet",
"See the organization page": "See the organization page"
}
5 changes: 3 additions & 2 deletions udata_front/theme/gouvfr/assets/js/locales/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -541,5 +541,6 @@
"Publish the reuse": "Publish the reuse",
"{n} reuses of your organization": "{n} reuses of your organization",
"Reuse title": "Reuse title",
"You haven't published a reuse yet": "You haven't published a reuse yet"
}
"You haven't published a reuse yet": "You haven't published a reuse yet",
"See the organization page": ""
}
5 changes: 3 additions & 2 deletions udata_front/theme/gouvfr/assets/js/locales/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -541,5 +541,6 @@
"Publish the reuse": "Publier la réutilisation",
"{n} reuses of your organization": "{n} réutilisations de votre organisation",
"Reuse title": "Titre de la réutilisation",
"You haven't published a reuse yet": "Vous n'avez pas encore publié de réutilisation"
}
"You haven't published a reuse yet": "Vous n'avez pas encore publié de réutilisation",
"See the organization page": ""
}
5 changes: 3 additions & 2 deletions udata_front/theme/gouvfr/assets/js/locales/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -541,5 +541,6 @@
"Publish the reuse": "Publish the reuse",
"{n} reuses of your organization": "{n} reuses of your organization",
"Reuse title": "Reuse title",
"You haven't published a reuse yet": "You haven't published a reuse yet"
}
"You haven't published a reuse yet": "You haven't published a reuse yet",
"See the organization page": ""
}
5 changes: 3 additions & 2 deletions udata_front/theme/gouvfr/assets/js/locales/pt.json
Original file line number Diff line number Diff line change
Expand Up @@ -541,5 +541,6 @@
"Publish the reuse": "Publish the reuse",
"{n} reuses of your organization": "{n} reuses of your organization",
"Reuse title": "Reuse title",
"You haven't published a reuse yet": "You haven't published a reuse yet"
}
"You haven't published a reuse yet": "You haven't published a reuse yet",
"See the organization page": ""
}
5 changes: 3 additions & 2 deletions udata_front/theme/gouvfr/assets/js/locales/sr.json
Original file line number Diff line number Diff line change
Expand Up @@ -541,5 +541,6 @@
"Publish the reuse": "Publish the reuse",
"{n} reuses of your organization": "{n} reuses of your organization",
"Reuse title": "Reuse title",
"You haven't published a reuse yet": "You haven't published a reuse yet"
}
"You haven't published a reuse yet": "You haven't published a reuse yet",
"See the organization page": ""
}
87 changes: 73 additions & 14 deletions udata_front/theme/gouvfr/assets/js/pages/Admin/Profile/Profile.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,47 @@
</a>
</li>
</Breadcrumb>
<h1 class="fr-h3 fr-mb-5v">{{ t("Profile") }}</h1>
<Container
v-if="organization"
class="fr-mb-5v"
>
<div class="fr-grid-row fr-grid-row--gutters fr-grid-row--middle justify-between">
<div class="fr-col-12 fr-col-md fr-grid-row fr-grid-row--middle">
<Placeholder
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it slightly counter-intuitive that the logo appears at the top of the page but is editable at the end of the form only
cc @agarrone

:src="organization.logo"
type="organization"
:size="80"
class="rounded-xxs border"
/>
<div class="fr-ml-3v fr-my-0 fr-h3">
{{ organization.name }}
<span
v-if="organizationCertified"
class="fr-icon-success-line fr-icon--lg text-blue-400"
:title="t('The identity of this public service is certified by {certifier}', { certifier: title })"
aria-hidden="true"
>
</span>
</div>
</div>
<div class="fr-col-auto">
<a
:href="organization.page"
class="fr-btn fr-btn--sm fr-btn--secondary fr-btn--secondary-grey-500 fr-btn--icon-left fr-icon-eye-line"
>
{{ t('See the organization page') }}
</a>
</div>
</div>
</Container>
<h2 class="subtitle subtitle--uppercase fr-mb-5v" v-if="form" :id="form.legend">
{{ t("Edit profile") }}
</h2>
<AdminLoader v-if="loading && !organization" />
<DescribeOrganizationFrom
v-if="currentOrganization"
:organization="currentOrganization"
v-if="organization"
:organization="organization"
:errors="errors"
:showLegend="false"
:showWell="false"
Expand Down Expand Up @@ -112,27 +147,29 @@
</div>
</template>
<script setup lang="ts">
import { getRandomId } from '@datagouv/components/ts';
import { getRandomId, NewOrganization, Placeholder, useOrganizationCertified, type Organization } from '@datagouv/components/ts';
import { onMounted, ref } from 'vue';
import { useI18n } from "vue-i18n";
import { useRouter } from 'vue-router';
import { fetchMe } from "../../../api/me";
import { deleteOrganization, updateOrganization } from '../../../api/organizations';
import { deleteOrganization, getOrganization, updateOrganization, uploadLogo } from '../../../api/organizations';
import Breadcrumb from "../../../components/Breadcrumb/Breadcrumb.vue";
import AdminDangerZone from "../../../components/AdminDangerZone/AdminDangerZone.vue";
import { useCurrentOrganization } from '../../../composables/admin/useCurrentOrganization';
import { useToast } from '../../../composables/useToast';
import DescribeOrganizationFrom from "../../OrganizationPublishingForm/Step2DescribeOrganization.vue";
import type { Me, OrganizationV1 } from "../../../types";
import AdminLoader from '../../../components/AdminLoader/AdminLoader.vue';
import Container from '../../../components/Ui/Container/Container.vue';
import { title } from '../../../config';

const { t } = useI18n();
const { toast } = useToast();
defineProps<{oid: string}>();
const props = defineProps<{oid: string}>();
const router = useRouter();
const me = ref<Me | null>(null);
const form = ref<InstanceType<typeof DescribeOrganizationFrom> | null>(null);

const { currentOrganization } = useCurrentOrganization();
const organization = ref<Organization | null>(null);
const { organizationCertified } = useOrganizationCertified(organization);
const errors = ref([]);
const loading = ref(false);
const modalId = getRandomId("modal");
Expand All @@ -151,13 +188,35 @@ function deleteCurrentOrganization() {
}
}

function updateCurrentOrganization(organization: OrganizationV1) {
async function updateCurrentOrganization(updatedOrganization: NewOrganization | Organization, file: File | null) {
loading.value = true;
updateOrganization(organization)
.then(() => toast.success(t("Organization updated !")))
.catch(() => toast.error(t("An error occured when updating the organization.")))
.finally(() => loading.value = false);
try {
organization.value = await updateOrganization(updatedOrganization as Organization)
toast.success(t("Organization updated !"))
} catch (e) {
toast.error(t("An error occured when updating the organization."))
} finally {
loading.value = false;
}
if (file && organization.value) {
loading.value = true;
try {
const resp = await uploadLogo(organization.value.id, file);
organization.value.logo_thumbnail = resp.image;
} catch (e) {
toast.error(t("Failed to upload logo, you can upload it again in your management panel"));
} finally {
loading.value = false;
}
}
}

onMounted(async () => me.value = await fetchMe());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to fetch me.value here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No, this was necessary before the Dataset PR merge. Now we have access to the useCurrentOrganization composable instead

onMounted(async () => {
loading.value = true;
try {
organization.value = await getOrganization(props.oid);
} finally {
loading.value = false;
}
});
</script>
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,16 @@
@submit="createOrganizationAndMoveToNextStep"
/>
<Step3CompleteTheOrganization
v-else-if="currentStep === 2"
:organization="organization"
v-else-if="currentStep === 2 && savedOrganization"
:organization="savedOrganization"
:errors="errors"
:datasetLink="datasetAdminUrl"
:reuseLink="reuseAdminUrl"
/>
</div>
</template>
<script setup lang="ts">
import { type NewOrganization } from '@datagouv/components/ts';
import type { NewOrganization, Organization } from '@datagouv/components/ts';
import { ref } from 'vue';
import { useI18n } from 'vue-i18n';
import Stepper from "../../components/Form/Stepper/Stepper.vue";
Expand Down Expand Up @@ -49,9 +49,10 @@ const organization = ref<NewOrganization>({
description: "",
url: "",
logo: "",
logo_thumbnail: "",
});

const savedOrganization = ref<Organization | null>(null);

const errors = ref<Array<string>>([]);

const moveToStep = (step: number) => {
Expand All @@ -67,15 +68,17 @@ async function createOrganizationAndMoveToNextStep(org: NewOrganization, file: F
errors.value = [];
let moveToNextStep = false;
try {
organization.value = await createOrganization(org);
savedOrganization.value = await createOrganization(org);
moveToNextStep = true;
} catch (e: Error) {
errors.value.push(e.message);
} catch (e) {
if(e instanceof Error) {
errors.value.push(e.message);
}
}
if (file) {
if (file && savedOrganization.value) {
try {
const resp = await uploadLogo(organization.value.id, file);
organization.value.logo_thumbnail = resp.image
const resp = await uploadLogo(savedOrganization.value.id, file);
savedOrganization.value.logo_thumbnail = resp.image
} catch (e) {
errors.value.push(t("Failed to upload logo, you can upload it again in your management panel"));
}
Expand Down
Loading