fix: fix broken edit team modal

Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com>
This commit is contained in:
liyasthomas
2021-11-02 14:25:38 +05:30
parent bce88ccd44
commit 8045f26c19
3 changed files with 99 additions and 43 deletions

View File

@@ -75,7 +75,7 @@
</div> </div>
<div v-else> <div v-else>
<div <div
v-for="(member, index) in teamDetails.data.right.team.teamMembers" v-for="(member, index) in membersList"
:key="`member-${index}`" :key="`member-${index}`"
class="divide-x divide-dividerLight flex" class="divide-x divide-dividerLight flex"
> >
@@ -83,7 +83,7 @@
class="bg-transparent flex flex-1 py-2 px-4" class="bg-transparent flex flex-1 py-2 px-4"
:placeholder="$t('team.email')" :placeholder="$t('team.email')"
:name="'param' + index" :name="'param' + index"
:value="member.user.email" :value="member.email"
readonly readonly
/> />
<span> <span>
@@ -119,7 +119,7 @@
label="OWNER" label="OWNER"
@click.native=" @click.native="
() => { () => {
updateMemberRole(member.user.uid, 'OWNER') updateMemberRole(member.userID, 'OWNER')
memberOptions[index].tippy().hide() memberOptions[index].tippy().hide()
} }
" "
@@ -128,7 +128,7 @@
label="EDITOR" label="EDITOR"
@click.native=" @click.native="
() => { () => {
updateMemberRole(member.user.uid, 'EDITOR') updateMemberRole(member.userID, 'EDITOR')
memberOptions[index].tippy().hide() memberOptions[index].tippy().hide()
} }
" "
@@ -137,7 +137,7 @@
label="VIEWER" label="VIEWER"
@click.native=" @click.native="
() => { () => {
updateMemberRole(member.user.uid, 'VIEWER') updateMemberRole(member.userID, 'VIEWER')
memberOptions[index].tippy().hide() memberOptions[index].tippy().hide()
} }
" "
@@ -151,7 +151,7 @@
:title="$t('action.remove')" :title="$t('action.remove')"
svg="trash" svg="trash"
color="red" color="red"
@click.native="removeExistingTeamMember(member.user.uid)" @click.native="removeExistingTeamMember(member.userID)"
/> />
</div> </div>
</div> </div>
@@ -187,7 +187,6 @@ import {
watch, watch,
} from "@nuxtjs/composition-api" } from "@nuxtjs/composition-api"
import * as E from "fp-ts/Either" import * as E from "fp-ts/Either"
import { TeamsTeamMember } from "~/helpers/teams/TeamMemberAdapter"
import { import {
GetTeamDocument, GetTeamDocument,
GetTeamQuery, GetTeamQuery,
@@ -225,8 +224,6 @@ const {
} = useContext() } = useContext()
const t = i18n.t.bind(i18n) const t = i18n.t.bind(i18n)
const members = ref<TeamsTeamMember[]>([])
const name = toRef(props.editingTeam, "name") const name = toRef(props.editingTeam, "name")
watch( watch(
@@ -278,10 +275,74 @@ const teamDetails = useGQLQuery<GetTeamQuery, GetTeamQueryVariables, "">({
}), }),
}) })
const roleUpdates = ref<
{
userID: string
role: TeamMemberRole
}[]
>([])
watch(
() => teamDetails,
() => {
if (teamDetails.loading) return
const data = teamDetails.data
if (E.isRight(data)) {
const members = data.right.team?.teamMembers ?? []
// Remove deleted members
roleUpdates.value = roleUpdates.value.filter(
(update) =>
members.findIndex((y) => y.user.uid === update.userID) !== -1
)
}
}
)
const updateMemberRole = (userID: string, role: TeamMemberRole) => { const updateMemberRole = (userID: string, role: TeamMemberRole) => {
members.value[userID].role = role const updateIndex = roleUpdates.value.findIndex(
(item) => item.userID === userID
)
if (updateIndex !== -1) {
// Role Update exists
roleUpdates.value[updateIndex].role = role
} else {
// Role Update does not exist
roleUpdates.value.push({
userID,
role,
})
}
} }
const membersList = computed(() => {
if (teamDetails.loading) return []
const data = teamDetails.data
if (E.isLeft(data)) return []
if (E.isRight(data)) {
const members = (data.right.team?.teamMembers ?? []).map((member) => {
const updatedRole = roleUpdates.value.find(
(update) => update.userID === member.user.uid
)
return {
userID: member.user.uid,
email: member.user.email!,
role: updatedRole?.role ?? member.role,
}
})
return members
}
return []
})
const removeExistingTeamMember = async (userID: string) => { const removeExistingTeamMember = async (userID: string) => {
const removeTeamMemberResult = await removeTeamMember( const removeTeamMemberResult = await removeTeamMember(
userID, userID,
@@ -310,30 +371,24 @@ const saveTeam = async () => {
icon: "error", icon: "error",
}) })
} else { } else {
$toast.success(t("team.name_updated"), { roleUpdates.value.forEach(async (update) => {
icon: "done", const updateMemberRoleResult = await updateTeamMemberRole(
}) update.userID,
members.value.forEach( props.editingTeamID,
async (element: { user: { uid: any }; role: any }) => { update.role
const updateMemberRoleResult = await updateTeamMemberRole( )()
element.user.uid, if (E.isLeft(updateMemberRoleResult)) {
props.editingTeamID, $toast.error(t("error.something_went_wrong"), {
element.role icon: "error",
)() })
if (E.isLeft(updateMemberRoleResult)) { console.error(updateMemberRoleResult.left.error)
$toast.error(t("error.something_went_wrong"), {
icon: "error",
})
console.error(updateMemberRoleResult.left.error)
} else {
$toast.success(t("team.member_role_updated"), {
icon: "done",
})
}
} }
) })
} }
hideModal() hideModal()
$toast.success(t("team.saved"), {
icon: "done",
})
} else { } else {
return $toast.error(t("team.name_length_insufficient"), { return $toast.error(t("team.name_length_insufficient"), {
icon: "error_outline", icon: "error_outline",

View File

@@ -35,7 +35,9 @@
<div <div
v-else-if="!myTeams.loading && E.isRight(myTeams.data)" v-else-if="!myTeams.loading && E.isRight(myTeams.data)"
class="grid gap-4" class="grid gap-4"
:class="modal ? 'grid-cols-1' : 'sm:grid-cols-3 md:grid-cols-4'" :class="
modal ? 'grid-cols-1' : 'sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4'
"
> >
<TeamsTeam <TeamsTeam
v-for="(team, index) in myTeams.data.right.myTeams" v-for="(team, index) in myTeams.data.right.myTeams"

View File

@@ -187,19 +187,17 @@ export const useGQLQuery = <DocType, DocVarType, DocErrorType extends string>(
const pollerTick: Ref<boolean> = ref(true) const pollerTick: Ref<boolean> = ref(true)
stops.push( stops.push(
watchEffect( watchEffect((onInvalidate) => {
(onInvalidate) => { if (pollDuration.value !== null && !isPaused.value) {
if (pollDuration.value !== null && !isPaused.value) { const handle = setInterval(() => {
const handle = setInterval(() => { pollerTick.value = !pollerTick.value
pollerTick.value = !pollerTick.value }, pollDuration.value)
}, pollDuration.value)
onInvalidate(() => { onInvalidate(() => {
clearInterval(handle) clearInterval(handle)
}) })
}
} }
) })
) )
stops.push( stops.push(
@@ -222,6 +220,7 @@ export const useGQLQuery = <DocType, DocVarType, DocErrorType extends string>(
watchEffect( watchEffect(
() => { () => {
// Just listen to the polling ticks // Just listen to the polling ticks
// eslint-disable-next-line no-unused-expressions
pollerTick.value pollerTick.value
source.value = !isPaused.value source.value = !isPaused.value