Skip to content

Commit

Permalink
Allow other packages and apps to use the collection store
Browse files Browse the repository at this point in the history
  • Loading branch information
bperel committed Oct 19, 2023
1 parent a7c95c0 commit 0837e0e
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 41 deletions.
2 changes: 2 additions & 0 deletions apps/web/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@ export {
} from "./src/api";
import { bookcase } from "./src/stores/bookcase";
import { coa } from "./src/stores/coa";
import { collection } from "./src/stores/collection";
export const stores = {
coa,
collection,
bookcase,
};

Expand Down
6 changes: 6 additions & 0 deletions apps/web/src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
</template>

<script setup lang="ts">
import axios from "axios";
import { buildWebStorage } from "axios-cache-interceptor";
import { coa } from "~/stores/coa";
Expand All @@ -13,6 +14,11 @@ import { createCachedCoaApi } from "./api";
collection().loadUser();
onBeforeMount(() => {
collection().setApi(
axios.create({
baseURL: import.meta.env.VITE_GATEWAY_URL,
}),
);
coa().setApi(
createCachedCoaApi(
buildWebStorage(sessionStorage),
Expand Down
4 changes: 0 additions & 4 deletions apps/web/src/components/ShortStats.vue
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,3 @@ const totalPerPublication = $computed(
const { t } = useI18n();
</script>

<style scoped>
</style>
35 changes: 18 additions & 17 deletions apps/web/src/stores/coa.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,12 @@ const addPartInfo = (issueDetails: InducksIssueDetails) => {
};
};

let coaApi: AxiosInstance;

export const ISSUECODE_REGEX =
/^(?<countrycode>[^/]+)\/(?<magazinecode>[^ ]+) (?<issuenumber>.+)/;
let api: AxiosInstance;

export const coa = defineStore("coa", () => {
const locale = useI18n().locale,
const ISSUECODE_REGEX =
/^(?<countrycode>[^/]+)\/(?<magazinecode>[^ ]+) (?<issuenumber>.+)/,
locale = useI18n().locale,
coverUrls = ref({} as { [issuenumber: string]: string }),
countryNames = ref(null as { [countrycode: string]: string } | null),
publicationNames = ref({} as POST__coa__list__publications["resBody"]),
Expand Down Expand Up @@ -142,7 +141,7 @@ export const coa = defineStore("coa", () => {
isLoadingCountryNames.value = true;
countryNames.value = (
await call(
coaApi,
api,
new GET__coa__list__countries__$locale({
query: { countryCodes: null },
params: {
Expand All @@ -168,7 +167,7 @@ export const coa = defineStore("coa", () => {
addPublicationNames(
(
await call(
coaApi,
api,
new POST__coa__list__publications({
reqBody: { publicationCodes: actualNewPublicationCodes },
}),
Expand All @@ -194,7 +193,7 @@ export const coa = defineStore("coa", () => {
await getChunkedRequests<GET__coa__quotations__publications>({
callFn: (chunk) =>
call(
coaApi,
api,
new GET__coa__quotations__publications({
query: { publicationCodes: chunk },
}),
Expand All @@ -220,7 +219,7 @@ export const coa = defineStore("coa", () => {
if (publicationNamesFullCountries.value.includes(countrycode)) return;

return call(
coaApi,
api,
new GET__coa__list__publications__$countrycode({
params: { countrycode },
}),
Expand Down Expand Up @@ -251,7 +250,7 @@ export const coa = defineStore("coa", () => {
...(await getChunkedRequests<GET__coa__authorsfullnames__$authors>({
callFn: (chunk) =>
call(
coaApi,
api,
new GET__coa__authorsfullnames__$authors({
params: { authors: chunk },
}),
Expand All @@ -265,7 +264,7 @@ export const coa = defineStore("coa", () => {
fetchIssueNumbersWithTitles = async (publicationcode: string) => {
issuesWithTitles.value[publicationcode] = (
await call(
coaApi,
api,
new GET__coa__list__issues__withTitle({
query: { publicationcode },
}),
Expand All @@ -287,7 +286,7 @@ export const coa = defineStore("coa", () => {
{
callFn: async (chunk) =>
call(
coaApi,
api,
new GET__coa__list__issues__by_publication_codes({
query: { publicationCodes: chunk },
}),
Expand Down Expand Up @@ -326,7 +325,7 @@ export const coa = defineStore("coa", () => {
await getChunkedRequests<POST__coa__issues__decompose>({
callFn: (chunk) =>
call(
coaApi,
api,
new POST__coa__issues__decompose({
reqBody: { issueCodes: chunk },
}),
Expand All @@ -340,11 +339,11 @@ export const coa = defineStore("coa", () => {
fetchIssueCounts = async () => {
if (!issueCounts.value)
issueCounts.value = (
await call(coaApi, new GET__coa__list__issues__count({}))
await call(api, new GET__coa__list__issues__count({}))
).data;
},
fetchRecentIssues = async () =>
(await call(coaApi, new GET__coa__list__issues__recent())).data,
(await call(api, new GET__coa__list__issues__recent())).data,
fetchIssueUrls = async ({
publicationcode,
issuenumber,
Expand All @@ -356,7 +355,7 @@ export const coa = defineStore("coa", () => {
if (!issueDetails.value[issueCode]) {
const newIssueDetails = (
await call(
coaApi,
api,
new GET__coa__list__issues__details({
query: { publicationcode, issuenumber },
}),
Expand All @@ -371,7 +370,7 @@ export const coa = defineStore("coa", () => {
};
return {
setApi: (apiInstance: AxiosInstance) => {
coaApi = apiInstance;
api = apiInstance;
},
coverUrls,
countryNames,
Expand Down Expand Up @@ -403,5 +402,7 @@ export const coa = defineStore("coa", () => {
fetchIssueCodesDetails,
fetchIssueCounts,
fetchIssueUrls,

ISSUECODE_REGEX,
};
});
46 changes: 26 additions & 20 deletions apps/web/src/stores/collection.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import axios from "axios";
import { AxiosInstance } from "axios";
import Cookies from "js-cookie";
import { defineStore } from "pinia";

Expand Down Expand Up @@ -74,6 +74,8 @@ export type QuotedIssue = {
estimationGivenCondition: number;
};

let api: AxiosInstance;

export const collection = defineStore("collection", () => {
const collection = ref(null as IssueWithPublicationcode[] | null),
watchedPublicationsWithSales = ref(null as string[] | null),
Expand Down Expand Up @@ -306,7 +308,7 @@ export const collection = defineStore("collection", () => {
}),
updateCollectionSingleIssue = async (data: CollectionUpdateSingleIssue) => {
await call(
axios,
api,
new POST__collection__issues__single({
reqBody: data,
}),
Expand All @@ -317,7 +319,7 @@ export const collection = defineStore("collection", () => {
data: CollectionUpdateMultipleIssues,
) => {
await call(
axios,
api,
new POST__collection__issues__multiple({
reqBody: data,
}),
Expand All @@ -326,7 +328,7 @@ export const collection = defineStore("collection", () => {
},
createPurchase = async (date: string, description: string) => {
await call(
axios,
api,
new PUT__collection__purchases({
reqBody: { date, description },
}),
Expand All @@ -335,7 +337,7 @@ export const collection = defineStore("collection", () => {
},
deletePurchase = async (id: number) => {
await call(
axios,
api,
new DELETE__collection__purchases__$id({
params: { id: String(id) },
}),
Expand All @@ -349,15 +351,16 @@ export const collection = defineStore("collection", () => {
collectionIssueNumber === issuenumber,
),
loadPreviousVisit = async () => {
previousVisit.value = (
await call(axios, new POST__collection__lastvisit())
).data?.previousVisit;
previousVisit.value = (await call(api, new POST__collection__lastvisit()))
.data?.previousVisit;
},
loadCollection = async (afterUpdate = false) => {
console.log("loadCollection");

if (afterUpdate || (!isLoadingCollection.value && !collection.value)) {
isLoadingCollection.value = true;
collection.value = (
await call(axios, new GET__collection__issues())
await call(api, new GET__collection__issues())
).data.map((issue) => ({
...issue,
publicationcode: `${issue.country}/${issue.magazine}`,
Expand All @@ -369,7 +372,7 @@ export const collection = defineStore("collection", () => {
if (afterUpdate || (!isLoadingPurchases.value && !purchases.value)) {
isLoadingPurchases.value = true;
purchases.value = (
await call(axios, new GET__collection__purchases())
await call(api, new GET__collection__purchases())
).data;
isLoadingPurchases.value = false;
}
Expand All @@ -381,7 +384,7 @@ export const collection = defineStore("collection", () => {
) {
isLoadingWatchedAuthors.value = true;
watchedAuthors.value = (
await call(axios, new GET__collection__authors__watched())
await call(api, new GET__collection__authors__watched())
).data;
isLoadingWatchedAuthors.value = false;
}
Expand All @@ -395,7 +398,7 @@ export const collection = defineStore("collection", () => {
isLoadingWatchedPublicationsWithSales.value = true;
watchedPublicationsWithSales.value = (
await call(
axios,
api,
new GET__collection__options__$optionName({
params: {
optionName: "sales_notification_publications",
Expand All @@ -415,7 +418,7 @@ export const collection = defineStore("collection", () => {
isLoadingMarketplaceContactMethods.value = true;
marketplaceContactMethods.value = (
await call(
axios,
api,
new GET__collection__options__$optionName({
params: { optionName: "marketplace_contact_methods" },
}),
Expand All @@ -426,7 +429,7 @@ export const collection = defineStore("collection", () => {
},
updateMarketplaceContactMethods = async () =>
await call(
axios,
api,
new POST__collection__options__$optionName({
reqBody: { values: marketplaceContactMethods.value! },
params: {
Expand All @@ -436,7 +439,7 @@ export const collection = defineStore("collection", () => {
),
updateWatchedPublicationsWithSales = async () =>
await call(
axios,
api,
new POST__collection__options__$optionName({
reqBody: {
values: watchedPublicationsWithSales.value!,
Expand All @@ -459,7 +462,7 @@ export const collection = defineStore("collection", () => {
isLoadingSuggestions.value = true;
suggestions.value = (
await call(
axios,
api,
new GET__collection__stats__suggestedissues__$countrycode__$sincePreviousVisit__$sort__$limit(
{
reqBody: {
Expand All @@ -484,7 +487,7 @@ export const collection = defineStore("collection", () => {
) {
isLoadingSubscriptions.value = true;
subscriptions.value = (
await call(axios, new GET__collection__subscriptions())
await call(api, new GET__collection__subscriptions())
).data.map((subscription: SubscriptionTransformedStringDates) => ({
...subscription,
startDate: new Date(Date.parse(subscription.startDate)),
Expand All @@ -496,7 +499,7 @@ export const collection = defineStore("collection", () => {
loadPopularIssuesInCollection = async () => {
if (!popularIssuesInCollection.value) {
popularIssuesInCollection.value = (
await call(axios, new GET__collection__popular())
await call(api, new GET__collection__popular())
).data.reduce(
(acc, issue) => ({
...acc,
Expand All @@ -510,7 +513,7 @@ export const collection = defineStore("collection", () => {
loadLastPublishedEdgesForCurrentUser = async () => {
if (!lastPublishedEdgesForCurrentUser.value) {
lastPublishedEdgesForCurrentUser.value = (
await call(axios, new GET__collection__edges__lastPublished())
await call(api, new GET__collection__edges__lastPublished())
).data;
}
},
Expand All @@ -519,7 +522,7 @@ export const collection = defineStore("collection", () => {
isLoadingUser.value = true;
try {
if (Cookies.get("token")) {
user.value = (await call(axios, new GET__collection__user())).data;
user.value = (await call(api, new GET__collection__user())).data;
}
} catch (e) {
console.error(e);
Expand All @@ -531,6 +534,9 @@ export const collection = defineStore("collection", () => {
}
};
return {
setApi: (apiInstance: AxiosInstance) => {
api = apiInstance;
},
collection,
watchedPublicationsWithSales,
purchases,
Expand Down

0 comments on commit 0837e0e

Please sign in to comment.