Skip to content

Commit

Permalink
Allow deleting profiles
Browse files Browse the repository at this point in the history
  • Loading branch information
rafaellehmkuhl committed Sep 26, 2023
1 parent c29b42a commit dae5426
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/components/EditMenu.vue
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
>
<p class="overflow-hidden text-sm text-ellipsis ml-7 whitespace-nowrap">{{ profile.name }}</p>
<div class="grow" />
<div
v-if="!store.isDefaultProfile(profile)"
class="icon-btn mdi mdi-trash-can"
@click.stop="store.deleteProfile(profile)"
/>
</Button>
</div>
<div class="flex mt-2">
Expand Down
37 changes: 35 additions & 2 deletions src/stores/widgetManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,15 +41,14 @@ export const useWidgetManagerStore = defineStore('widget-manager', () => {
return allProfiles.value[currentProfileIndex.value]
},
set(newValue) {
const userProfileHashs = savedProfiles.value.map((p) => p.hash)
const allProfileHashs = allProfiles.value.map((p) => p.hash)

if (!allProfileHashs.includes(newValue.hash)) {
Swal.fire({ icon: 'error', text: 'Could not find profile.', timer: 3000 })
return
}

if (allProfileHashs.includes(newValue.hash) && !userProfileHashs.includes(newValue.hash)) {
if (isDefaultProfile(newValue)) {
Swal.fire({ icon: 'error', text: 'Cannot edit a default profile. Please pick another one.', timer: 3000 })
return
}
Expand Down Expand Up @@ -180,6 +179,38 @@ export const useWidgetManagerStore = defineStore('widget-manager', () => {
addView()
}

const isUserProfile = (profile: Profile): boolean => {
return savedProfiles.value.map((p) => p.hash).includes(profile.hash)
}

const isDefaultProfile = (profile: Profile): boolean => {
return widgetProfiles.map((p) => p.hash).includes(profile.hash)
}

/**
* Deletes a profile from the store
* @param { Profile } profile - Profile
*/
function deleteProfile(profile: Profile): void {
if (!isUserProfile(profile) && !isDefaultProfile(profile)) {
Swal.fire({ icon: 'error', text: 'Could not find profile.', timer: 3000 })
return
}

if (isDefaultProfile(profile)) {
Swal.fire({ icon: 'error', text: 'Cannot delete a default profile.', timer: 3000 })
return
}

const currentProfileHash = currentProfile.value.hash
const savedProfileIndex = savedProfiles.value.findIndex((p) => p.hash === profile.hash)
currentProfileIndex.value = 0
savedProfiles.value.splice(savedProfileIndex, 1)
if (currentProfileHash !== profile.hash) {
currentProfileIndex.value = allProfiles.value.findIndex((p) => p.hash === currentProfileHash)
}
}

/**
* Deletes a view from the store
* @param { View } view - View
Expand Down Expand Up @@ -406,12 +437,14 @@ export const useWidgetManagerStore = defineStore('widget-manager', () => {
currentMiniWidgetsProfile,
savedProfiles,
allProfiles,
isDefaultProfile,
loadProfile,
saveProfile,
resetSavedProfiles,
exportCurrentProfile,
importProfile,
addProfile,
deleteProfile,
addView,
deleteView,
renameView,
Expand Down

0 comments on commit dae5426

Please sign in to comment.