aboutsummaryrefslogtreecommitdiffstats
path: root/apps/settings/src/store/apps-store.ts
blob: eaf8a0d7f86dd29a98a322998f7c441fad125c29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/**
 * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

import type { IAppstoreApp, IAppstoreCategory } from '../app-types.ts'

import { showError } from '@nextcloud/dialogs'
import { loadState } from '@nextcloud/initial-state'
import { translate as t } from '@nextcloud/l10n'
import { generateUrl } from '@nextcloud/router'
import { defineStore } from 'pinia'

import axios from '@nextcloud/axios'

import logger from '../logger'
import APPSTORE_CATEGORY_ICONS from '../constants/AppstoreCategoryIcons.ts'

const showApiError = () => showError(t('settings', 'An error occurred during the request. Unable to proceed.'))

export const useAppsStore = defineStore('settings-apps', {
	state: () => ({
		apps: [] as IAppstoreApp[],
		categories: [] as IAppstoreCategory[],
		updateCount: loadState<number>('settings', 'appstoreUpdateCount', 0),
		loading: {
			apps: false,
			categories: false,
		},
		loadingList: false,
		gettingCategoriesPromise: null,
	}),

	actions: {
		async loadCategories(force = false) {
			if (this.categories.length > 0 && !force) {
				return
			}

			try {
				this.loading.categories = true
				const { data: categories } = await axios.get<IAppstoreCategory[]>(generateUrl('settings/apps/categories'))

				for (const category of categories) {
					category.icon = APPSTORE_CATEGORY_ICONS[category.id] ?? ''
				}

				this.$patch({
					categories,
				})
			} catch (error) {
				logger.error(error as Error)
				showApiError()
			} finally {
				this.loading.categories = false
			}
		},

		async loadApps(force = false) {
			if (this.apps.length > 0 && !force) {
				return
			}

			try {
				this.loading.apps = true
				const { data } = await axios.get<{ apps: IAppstoreApp[] }>(generateUrl('settings/apps/list'))

				this.$patch({
					apps: data.apps,
				})
			} catch (error) {
				logger.error(error as Error)
				showApiError()
			} finally {
				this.loading.apps = false
			}
		},

		getCategoryById(categoryId: string) {
			return this.categories.find(({ id }) => id === categoryId) ?? null
		},

		getAppById(appId: string): IAppstoreApp|null {
			return this.apps.find(({ id }) => id === appId) ?? null
		},
	},
})