Skip to content

Commit

Permalink
Fix types in subscription page
Browse files Browse the repository at this point in the history
  • Loading branch information
bperel committed Nov 26, 2023
1 parent e71f583 commit 9b5002c
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 48 deletions.
2 changes: 0 additions & 2 deletions apps/web/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import Cookies from "js-cookie";
import { addTokenRequestInterceptor } from "~axios-helper";
import { createCachedCoaApi } from "./api";
import { publicCollection } from "./stores/public-collection";
import { users } from "./stores/users";
const usersStore = users();
const statsStore = stats();
Expand Down
14 changes: 5 additions & 9 deletions apps/web/src/components/Subscription.vue
Original file line number Diff line number Diff line change
Expand Up @@ -77,18 +77,14 @@
</template>

<script setup lang="ts">
import { SubscriptionTransformed } from "~/stores/collection";
import { subscription } from "~prisma-clients/client_dm";
const { isEdit, subscription } = defineProps<{
isEdit?: boolean;
subscription: EditSubscription;
subscription: SubscriptionTransformed;
}>();
type EditSubscription = {
id: number | null;
publicationcode: string | null;
startDate: Date | null;
endDate: Date | null;
};
const editSubscription = $ref(subscription);
const startDateAsString = $ref(
Expand Down Expand Up @@ -116,7 +112,7 @@ watch($$(endDateAsString), (newValue) => {
defineEmits<{
(e: "delete"): void;
(e: "edit", editSubscription: EditSubscription): void;
(e: "edit", editSubscription: SubscriptionTransformed): void;
(e: "start-edit"): void;
(e: "cancel-edit"): void;
}>();
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/pages/[...all].vue
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ meta:
<div>
{{
$t(
" pour que l'immersion soit complète, vous pouvez dans la plupart des cas voir la couverture et quelques extraits de chacun de vos numéros!",
"...Et pour que l'immersion soit complète, vous pouvez dans la plupart des cas voir la couverture et quelques extraits de chacun de vos numéros!",
)
}}
</div>
Expand Down
66 changes: 32 additions & 34 deletions apps/web/src/pages/collection/subscriptions.vue
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,9 @@ alias: [/collection/abonnements]
:key="subscription.id"
:is-edit="currentSubscription?.id === subscription.id"
:subscription="subscription"
@start-edit="
currentSubscription = toSubscriptionWithStringDates(subscription)
"
@start-edit="currentSubscription = subscription"
@cancel-edit="currentSubscription = null"
@edit="editSubscription($event)"
@edit="editSubscription"
@delete="deleteSubscription(subscription.id)"
/>
<b-row v-if="currentSubscription === null" class="mt-3 align-items-center">
Expand All @@ -69,11 +67,11 @@ alias: [/collection/abonnements]
</b-row>
<Subscription
v-if="currentSubscription && currentSubscription.id === null"
:subscription="currentSubscription!"
:subscription="currentSubscription"
is-edit
@start-edit="currentSubscription = newSubscription"
@cancel-edit="currentSubscription = null"
@edit="createSubscription($event)"
@edit="createSubscription"
/>
</div>
<div v-else>
Expand All @@ -83,25 +81,33 @@ alias: [/collection/abonnements]

<script setup lang="ts">
import axios from "axios";
import dayjs from "dayjs";
import { onMounted, watch } from "vue";
import { SubscriptionTransformed } from "~/stores/collection";
import {
SubscriptionTransformed,
SubscriptionTransformedStringDates,
} from "~/stores/collection";
import { call } from "~axios-helper";
import { EditSubscription } from "~dm-types/EditSubscription";
type AssociatedPublication = {
referencePublicationcode: string;
publicationcode: string;
};
const { fetchPublicationNames } = coa();
const { publicationNames } = storeToRefs(coa());
const { loadSubscriptions } = collection();
const { subscriptions } = storeToRefs(collection());
const newSubscription = $ref({
id: null,
publicationcode: null,
startDate: null,
endDate: null,
} as EditSubscription);
publicationcode: "fr/SPG",
startDate: new Date(),
endDate: dayjs(new Date()).add(1, "year").toDate(),
} as SubscriptionTransformed);
let currentSubscription = $ref(null as EditSubscription | null);
let currentSubscription = $ref(null as SubscriptionTransformed | null);
let hasPublicationNames = $ref(false as boolean);
let currentAssociatedPublications = $ref([] as AssociatedPublication[]);
Expand All @@ -112,22 +118,20 @@ const associatedPublications = $ref([
},
] as AssociatedPublication[]);
const { fetchPublicationNames } = coa();
const { publicationNames } = storeToRefs(coa());
const { loadSubscriptions } = collection();
const { subscriptions } = storeToRefs(collection());
const toSubscriptionWithStringDates = (
subscription: SubscriptionTransformed,
): SubscriptionTransformedStringDates => ({
...subscription,
startDate: subscription.startDate.toISOString().split("Z")[0],
endDate: subscription.endDate.toISOString().split("Z")[0],
});
const createSubscription = async (subscription: SubscriptionTransformed) => {
await call(
axios,
new PUT__collection__subscriptions({
reqBody: {
subscription: {
...subscription,
startDate: subscription.startDate.toISOString().split("Z")[0],
endDate: subscription.endDate.toISOString().split("Z")[0],
},
subscription: toSubscriptionWithStringDates(subscription),
},
}),
);
Expand All @@ -147,11 +151,13 @@ const createSubscriptionLike = async (
});
};
const editSubscription = async (subscription: EditSubscription) => {
const editSubscription = async (subscription: SubscriptionTransformed) => {
await call(
axios,
new POST__collection__subscriptions__$id({
reqBody: { subscription },
reqBody: {
subscription: toSubscriptionWithStringDates(subscription),
},
params: { id: String(subscription.id) },
}),
);
Expand All @@ -169,14 +175,6 @@ const deleteSubscription = async (id: number) => {
currentSubscription = null;
};
const toSubscriptionWithStringDates = (
subscription: SubscriptionTransformed,
): EditSubscription => ({
...subscription,
startDate: subscription.startDate.toISOString().split("Z")[0],
endDate: subscription.endDate.toISOString().split("Z")[0],
});
watch(
subscriptions,
async (newValue) => {
Expand Down
2 changes: 1 addition & 1 deletion apps/web/src/stores/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ export type SubscriptionTransformed = Omit<
publicationcode: string;
};

type SubscriptionTransformedStringDates = Omit<
export type SubscriptionTransformedStringDates = Omit<
SubscriptionTransformed,
"startDate" | "endDate"
> & {
Expand Down
7 changes: 6 additions & 1 deletion apps/web/vite.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,12 @@ export default defineConfig({
"vue-i18n",
],
dts: true,
dirs: ["./src/composables", "./types", "../../packages/api-routes"],
dirs: [
"./src/composables",
"./src/stores",
"./types",
"../../packages/api-routes",
],
vueTemplate: true,
}),

Expand Down

0 comments on commit 9b5002c

Please sign in to comment.