Add safety and state refresh when adding to collection (#1535)

This commit is contained in:
Carter 2024-01-07 16:58:30 -08:00 committed by GitHub
parent 42a80a41ca
commit 683b0f5c48
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 15 additions and 5 deletions

View File

@ -76,13 +76,21 @@ export const initUserProjects = async () => {
export const userCollectProject = async (collection, projectId) => {
const user = (await useUser()).value
await initUserCollections()
const add = !collection.projects.includes(projectId)
const collectionId = collection.id
const latestCollection = user.collections.find((x) => x.id === collectionId)
if (!latestCollection) {
throw new Error('This collection was not found. Has it been deleted?')
}
const add = !latestCollection.projects.includes(projectId)
const projects = add
? [...collection.projects, projectId]
: [...collection.projects].filter((x) => x !== projectId)
? [...latestCollection.projects, projectId]
: [...latestCollection.projects].filter((x) => x !== projectId)
const idx = user.collections.findIndex((x) => x.id === collection.id)
const idx = user.collections.findIndex((x) => x.id === latestCollection.id)
if (idx >= 0) {
user.collections[idx].projects = projects
}

View File

@ -305,7 +305,7 @@
:key="option.id"
:model-value="option.projects.includes(project.id)"
class="popout-checkbox"
@update:model-value="userCollectProject(option, project.id)"
@update:model-value="() => onUserCollectProject(option, project.id)"
>
{{ option.name }}
</Checkbox>
@ -1036,6 +1036,8 @@ if (!route.name.startsWith('type-id-settings')) {
})
}
const onUserCollectProject = useClientTry(userCollectProject)
async function clearMessage() {
startLoading()