Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Imp[NoteSettings] add func to sort team in correct order #308

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 44 additions & 6 deletions src/presentation/components/team/Team.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
:caption="t('noteSettings.team.caption')"
>
<Row
v-for="(member, memberIndex) in team"
v-for="(member, memberIndex) in sortedTeam"
:key="member.id"
:title="member.user.name || member.user.email"
:has-delimiter="memberIndex !== team.length - 1"
:has-delimiter="memberIndex !== sortedTeam.length - 1"
data-dimensions="medium"
>
<template #right>
Expand All @@ -33,24 +33,62 @@
</template>

<script setup lang="ts">
import { Team } from '@/domain/entities/Team';
import { TeamMember, MemberRole } from '@/domain/entities/Team';
import { NoteId } from '@/domain/entities/Note';
import { Section, Row, Avatar } from 'codex-ui/vue';
import RoleSelect from './RoleSelect.vue';
import { useI18n } from 'vue-i18n';
import { computed, defineProps } from 'vue';

defineProps<{
const { t } = useI18n();

const props = defineProps<{
/**
* Team of the current note
*/
team: Team;
team: TeamMember[];
/**
* Id of the current note
*/
noteId: NoteId;
}>();

const { t } = useI18n();
/**
* Sorts the team members based on their role and whether they are the creator of the note.
*
* @param members - Array of TeamMember objects to be sorted.
* @returns {TeamMember} - Sorted array of TeamMember objects.
*/
const sortMembers = (members: TeamMember[]): TeamMember[] => {
return members.sort((a, b) => {
const roleOrder = {
[MemberRole.Write]: 0,
[MemberRole.Read]: 1,
};

const isCreatorA = String(a.user.id) === props.noteId;
const isCreatorB = String(b.user.id) === props.noteId;
Comment on lines +69 to +70
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

user.id should never be equal to noteId


if (isCreatorA && !isCreatorB) {
return -1;
}
if (!isCreatorA && isCreatorB) {
return 1;
}

if (!roleOrder[a.role] && !roleOrder[b.role]) {
return 0;
}
Comment on lines +79 to +81
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this condition will work if both roles are write and you will get (!0 && !0) condition

however below you have condition that will return same thing for same data (0 === 0)

so this condition seems redundant


if (roleOrder[a.role] === roleOrder[b.role]) {
return 0;
}

return roleOrder[a.role] < roleOrder[b.role] ? -1 : 1;
});
};

const sortedTeam = computed(() => sortMembers(props.team));
</script>

<style scoped>
Expand Down
Loading