Skip to content

Commit

Permalink
feat(state): create organization service/state
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasRichel committed Oct 29, 2021
1 parent 214a52a commit 430741a
Show file tree
Hide file tree
Showing 11 changed files with 159 additions and 23 deletions.
4 changes: 4 additions & 0 deletions src/i18n/lang/de.json
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,10 @@
"Error": "Fehler",
"Success": "Erfolg",
"Errors": {
"organizationsFetchError": "Fail to fetch organizations",
"organizationCreateError": "Fail to create organizations",
"organizationUpdateError": "Fail to update organizations",
"organizationDeleteError": "Fail to delete organizations",
"spacesFetchError": "Unmöglich, die Bereichsliste abzurufen",
"spaceCreateError": "Fehler beim Erstellen des Bereichs",
"spaceUpdateError": "Fehler bei der Aktualisierung des Bereichs",
Expand Down
4 changes: 4 additions & 0 deletions src/i18n/lang/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -517,6 +517,10 @@
"Success": "Success",

"Errors": {
"organizationsFetchError": "Fail to fetch organizations",
"organizationCreateError": "Fail to create organizations",
"organizationUpdateError": "Fail to update organizations",
"organizationDeleteError": "Fail to delete organizations",
"spacesFetchError": "Fail to fetch spaces",
"spaceCreateError": "Fail to create space",
"spaceUpdateError": "Fail to update space",
Expand Down
4 changes: 4 additions & 0 deletions src/i18n/lang/es.json
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,10 @@
"Error": "Error",
"Success": "Éxito",
"Errors": {
"organizationsFetchError": "Imposible recuperar la lista de organizaciones",
"organizationCreateError": "Error durante la creación del organización",
"organizationUpdateError": "Error de puesta en marcha del organización",
"organizationDeleteError": "Error en la supresión del organización",
"spacesFetchError": "Imposible recuperar la lista de espacios",
"spaceCreateError": "Error durante la creación del espacio",
"spaceUpdateError": "Error de puesta en marcha del espacio",
Expand Down
4 changes: 4 additions & 0 deletions src/i18n/lang/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,10 @@
"Success": "Succès",

"Errors": {
"organizationsFetchError": "Impossible de récupérer la liste des organizations",
"organizationCreateError": "Erreur lors de la création de l'organization",
"organizationUpdateError": "Erreur lors le de mise à jour de l'organization",
"organizationDeleteError": "Erreur lors de la suppression de l'organization",
"spacesFetchError": "Impossible de récupérer la liste des espaces",
"spaceCreateError": "Erreur lors de la création de l'espace",
"spaceUpdateError": "Erreur de mise à jour de l'espace",
Expand Down
4 changes: 4 additions & 0 deletions src/i18n/lang/it.json
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,10 @@
"Error": "Errore",
"Success": "Riuscito",
"Errors": {
"organizationsFetchError": "Impossibile recuperare organizzazioni",
"organizationCreateError": "Impossibile creare organizzazione",
"organizationUpdateError": "Impossibile caricare organizzazione",
"organizationDeleteError": "Impossibile eliminare organizzazione",
"spacesFetchError": "Impossibile recuperare gli spazi",
"spaceCreateError": "Impossibile creare spazio",
"spaceUpdateError": "Impossibile caricare spazio",
Expand Down
4 changes: 4 additions & 0 deletions src/services/ErrorService.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import i18n from "@/i18n";
import { useNotifications } from "@/composables/notifications";

const ERRORS = Object.freeze({
ORGANIZATIONS_FETCH_ERROR: "organizationsFetchError",
ORGANIZATION_CREATE_ERROR: "organizationCreateError",
ORGANIZATION_UPDATE_ERROR: "organizationUpdateError",
ORGANIZATION_DELETE_ERROR: "organizationDeleteError",
SPACES_FETCH_ERROR: "spacesFetchError",
SPACE_CREATE_ERROR: "spaceCreateError",
SPACE_UPDATE_ERROR: "spaceUpdateError",
Expand Down
59 changes: 47 additions & 12 deletions src/services/OrganizationService.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,52 @@
import apiClient from "../services/api-client.js";
import { privateApiClient } from "./api-client.js";
import { ERRORS, ErrorService, RuntimeError } from "./ErrorService.js";

class OrganizationService {
async retrieveOrganizations() {
// return apiClient.getOrganizations();
const response = await fetch(`${process.env.VUE_APP_API_BASE_URL}/private/organization`, {
headers: {
Authorization: apiClient.config.accessToken(),
},
});
if (response.ok) {
return await response.json();
} else {
throw response;
async fetchOrganizations() {
try {
return await privateApiClient.get("/organization");
} catch (error) {
ErrorService.handleError(
new RuntimeError(ERRORS.ORGANIZATIONS_FETCH_ERROR, error)
);
return [];
}
}

async fecthOrganizationSpaces(organization) {
try {
return await privateApiClient.get(
`/organization/${organization.id}/cloud`
);
} catch (error) {
return [];
}
}

async createOrganization(organization) {
try {
return await privateApiClient.post("/organization", organization);
} catch (error) {
throw new RuntimeError(ERRORS.ORGANIZATION_CREATE_ERROR, error);
}
}

async updateOrganization(organization) {
try {
return await privateApiClient.patch(
`/organization/${organization.id}`,
organization
);
} catch (error) {
throw new RuntimeError(ERRORS.ORGANIZATION_UPDATE_ERROR, error);
}
}

async deleteOrganization(organization) {
try {
return await privateApiClient.delete(`/organization/${organization.id}`);
} catch (error) {
throw new RuntimeError(ERRORS.ORGANIZATION_DELETE_ERROR, error);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/services/SpaceService.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import apiClient from "./api-client";
import { ERRORS, RuntimeError, ErrorService } from "./ErrorService";
import { ERRORS, ErrorService, RuntimeError } from "./ErrorService";

class SpaceService {
async fetchUserSpaces() {
Expand Down
31 changes: 31 additions & 0 deletions src/services/api-client.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,35 @@ const apiClient = makeBIMDataApiClient({
apiUrl: process.env.VUE_APP_API_BASE_URL
});

const privateApiFetch = async ({ method = "GET", path, body }) => {
const response = await fetch(
`${process.env.VUE_APP_API_BASE_URL}/private${path}`,
{
method,
headers: {
Authorization: apiClient.config.accessToken()
},
body: body ? JSON.stringify(body) : undefined
}
);
return await response.json();
};

const privateApiClient = {
get(path) {
return privateApiFetch({ path });
},
post(path, body) {
return privateApiFetch({ method: "POST", path, body });
},
patch(path, body) {
return privateApiFetch({ method: "PATCH", path, body });
},
delete(path) {
return privateApiFetch({ method: "DELETE", path });
}
};

export default apiClient;

export { privateApiClient };
62 changes: 54 additions & 8 deletions src/state/organizations.js
Original file line number Diff line number Diff line change
@@ -1,27 +1,73 @@
import { reactive, readonly, toRefs } from "vue";
import OrganizationService from "@/services/OrganizationService.js";

const state = reactive({
userOrganizations: [],
organizationSpaces: []
});

const retrieveOrganizations = async () => {
return await OrganizationService.retrieveOrganizations();
const organizations = await OrganizationService.fetchOrganizations();
state.userOrganizations = organizations;
return organizations;
};

const fecthOrganizationSpaces = async organization => {
const spaces = await OrganizationService.fecthOrganizationSpaces(
organization
);
state.organizationSpaces = spaces;
return spaces;
};

const createOrganization = async organization => {
const newOrganization = await OrganizationService.createOrganization(
organization
);
state.userOrganizations = [newOrganization].concat(state.userOrganizations);
return newOrganization;
};

const updateOrganization = async organization => {
const newOrganization = await OrganizationService.updateOrganization(
organization
);
softUpdateOrganization(newOrganization);
return newOrganization;
};

const createOrganization = async () => {
// TODO
const deleteOrganization = async organization => {
await OrganizationService.deleteOrganization(organization);
softDeleteOrganization(organization);
return organization;
};

const updateOrganization = async () => {
// TODO
const softUpdateOrganization = organization => {
state.userOrganizations = state.userOrganizations.map(orga =>
orga.id === organization.id ? { ...orga, ...organization } : orga
);
return organization;
};

const deleteOrganization = async () => {
// TODO
const softDeleteOrganization = organization => {
state.userOrganizations = state.userOrganizations.filter(
orga => orga.id !== organization.id
);
return organization;
};

export function useOrganizations() {
const readonlyState = readonly(state);
return {
// References
...toRefs(readonlyState),
// Methods
retrieveOrganizations,
fecthOrganizationSpaces,
createOrganization,
updateOrganization,
deleteOrganization
deleteOrganization,
softUpdateOrganization,
softDeleteOrganization
};
}
4 changes: 2 additions & 2 deletions src/views/platform-subscription/PlatformSubscription.vue
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@
>
<template #header> dropdown list example </template>
<template #element="{ element }">
{{ element.name }}
</template>
{{ element.name }}
</template>
</BIMDataDropdownList>
</header>
<aside class="platform-subscription__content m-t-18">
Expand Down

0 comments on commit 430741a

Please sign in to comment.