aboutsummaryrefslogtreecommitdiff
path: root/src/site/store/images.js
blob: acac2866ad993b578966f43bf252dc101565c458 (plain) (blame)
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import Vue from 'vue';

export const getDefaultState = () => ({
	files: [],
	isLoading: false,
	pagination: {
		page: 1,
		limit: 30,
		totalFiles: 0,
	},
	name: null,
	downloadEnabled: false,
	fileAlbumsMap: {}, // map of file ids with a list of album objects the file is in
	filesTags: {}, // map of file ids with a list of tag objects for the file
});

export const state = getDefaultState;

export const getters = {
	getTotalFiles: ({ pagination }) => pagination.totalFiles,
	getFetchedCount: ({ files }) => files.length,
	shouldPaginate: ({ pagination }) => pagination.totalFiles > pagination.limit,
	getLimit: ({ pagination }) => pagination.limit,
	getName: ({ name }) => name,
};

export const actions = {
	async fetch({ commit, dispatch, state }, page) {
		commit('setIsLoading');

		page = page || 1;

		try {
			const response = await this.$axios.$get('files', { params: { limit: state.pagination.limit, page } });

			commit('setFilesAndMeta', { ...response, page });

			return response;
		} catch (e) {
			dispatch('alert/set', { text: e.message, error: true }, { root: true });
		}

		return null;
	},
	async fetchByAlbumId({ commit, state }, { id, page }) {
		commit('setIsLoading');

		page = page || 1;

		const response = await this.$axios.$get(`album/${id}/full`, {
			params: { limit: state.pagination.limit, page },
		});

		commit('setFilesAndMeta', { ...response, page });

		return response;
	},
	async getFileAlbums({ commit }, fileId) {
		const response = await this.$axios.$get(`file/${fileId}/albums`);

		commit('setFileAlbums', { ...response, fileId });

		return response;
	},
	async addToAlbum({ commit }, { fileId, albumId }) {
		const response = await this.$axios.$post('file/album/add', { fileId, albumId });

		commit('addAlbumToFile', { fileId, albumId, ...response.data });

		return response;
	},
	async removeFromAlbum({ commit }, { fileId, albumId }) {
		const response = await this.$axios.$post('file/album/del', { fileId, albumId });

		commit('removeAlbumFromFile', { fileId, albumId });

		return response;
	},
	async deleteFile({ commit }, fileId) {
		const response = await this.$axios.$delete(`file/${fileId}`);

		commit('removeFile', fileId);

		return response;
	},
};

export const mutations = {
	setIsLoading(state) {
		state.isLoading = true;
	},
	setFilesAndMeta(state, {
		files, name, page, count,
	}) {
		state.files = files || [];
		state.name = name ?? null;
		state.isLoading = false;
		state.pagination.page = page || 1;
		state.pagination.totalFiles = count || 0;
	},
	removeFile(state, fileId) {
		const foundIndex = state.files.findIndex(({ id }) => id === fileId);
		if (foundIndex > -1) {
			state.files.splice(foundIndex, 1);
			state.pagination.totalFiles -= 1;
		}
	},
	setFileAlbums(state, { fileId, albums }) {
		Vue.set(state.fileAlbumsMap, fileId, albums);
	},
	addAlbumToFile(state, { fileId, album }) {
		if (!state.fileAlbumsMap[fileId]) return;

		state.fileAlbumsMap[fileId].push(album);
	},
	removeAlbumFromFile(state, { fileId, albumId }) {
		if (!state.fileAlbumsMap[fileId]) return;

		const foundIndex = state.fileAlbumsMap[fileId].findIndex(({ id }) => id === albumId);
		if (foundIndex > -1) {
			state.fileAlbumsMap[fileId].splice(foundIndex, 1);
		}
	},
	resetState(state) {
		Object.assign(state, getDefaultState());
	},
};