| 8 | import {arrayMove} from '@dnd-kit/sortable'; |
| 9 | |
| 10 | export class AppStore extends BaseStore<IApplication> { |
| 11 | public onDelete: () => void = () => {}; |
| 12 | |
| 13 | public constructor(private readonly snack: SnackReporter) { |
| 14 | super(); |
| 15 | } |
| 16 | |
| 17 | protected requestItems = (): Promise<IApplication[]> => |
| 18 | axios |
| 19 | .get<IApplication[]>(`${config.get('url')}application`) |
| 20 | .then((response) => response.data); |
| 21 | |
| 22 | protected requestDelete = (id: number): Promise<void> => |
| 23 | axios.delete(`${config.get('url')}application/${id}`).then(() => { |
| 24 | this.onDelete(); |
| 25 | return this.snack('Application deleted'); |
| 26 | }); |
| 27 | |
| 28 | @action |
| 29 | public uploadImage = async (id: number, file: Blob): Promise<void> => { |
| 30 | const formData = new FormData(); |
| 31 | formData.append('file', file); |
| 32 | await axios.post(`${config.get('url')}application/${id}/image`, formData, { |
| 33 | headers: {'content-type': 'multipart/form-data'}, |
| 34 | }); |
| 35 | await this.refresh(); |
| 36 | this.snack('Application image updated'); |
| 37 | }; |
| 38 | |
| 39 | public async deleteImage(id: number): Promise<void> { |
| 40 | try { |
| 41 | await axios.delete(`${config.get('url')}application/${id}/image`); |
| 42 | await this.refresh(); |
| 43 | this.snack('Application image deleted'); |
| 44 | } catch (error) { |
| 45 | console.error('Error deleting application image:', error); |
| 46 | throw error; |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | @action |
| 51 | public reorder = async (fromId: number, toId: number): Promise<void> => { |
| 52 | const fromIndex = this.items.findIndex((app) => app.id === fromId); |
| 53 | const toIndex = this.items.findIndex((app) => app.id === toId); |
| 54 | if (fromIndex === -1 || toIndex === -1) { |
| 55 | throw Error('unknown apps'); |
| 56 | } |
| 57 | |
| 58 | const toUpdate = this.items[fromIndex]; |
| 59 | |
| 60 | const normalizedIndex = |
| 61 | toUpdate.sortKey > this.items[toIndex].sortKey ? toIndex - 1 : toIndex; |
| 62 | |
| 63 | const newSortKey = generateKeyBetween( |
| 64 | this.items[normalizedIndex]?.sortKey, |
| 65 | this.items[normalizedIndex + 1]?.sortKey |
| 66 | ); |
| 67 |
nothing calls this directly
no test coverage detected
searching dependent graphs…