<commit_before><template>
    <app-dialog
            ref="dialogBox"
            :title="title">
        <template>
            <app-text-input
                    id="username"
                    label="Username"
                    v-model="user.login"/>
            <app-text-input
                    id="first-name"
                    label="First Name"
                    v-model="user.firstName"/>
            <app-text-input
                    id="last-name"
                    label="Last Name"
                    v-model="user.lastName"/>
            <app-row>
                <app-text-input
                    id="groups"
                    label="Groups"
                    action="plus solid"
                    @action="addToGroup"/>
            </app-row>
            <app-row>
                <app-tag
                        v-for="group in user.memberships"
                        action="times solid"
                        @action="removeFromGroup(group)">
                    {{group}}
                </app-tag>
            </app-row>
        </template>
        <template #actions>
            <button
                    type="button"
                    class="mdl-button"
                    @click="submit">
                OK
            </button>
            <button
                    type="button"
                    class="mdl-button"
                    @click="hide">
                Cancel
            </button>
        </template>
    </app-dialog>
</template>

<script>
import service from '@/service/user'
import groupService from '@/service/group'

const DEFAULT_USER = {
    login: "",
    firstName: "",
    lastName: "",
    memberships: []
}

export default {
    name: "DialogUser",
    data() {
        return {
            user: DEFAULT_USER
        }
    },
    computed: {
        title() {
            return this.user.id ? "Edit User" : "Add User"
        }
    },
    methods: {
        show(user) {
            if (user) this.user = {...user}
            this.$refs.dialogBox.show()
        },
        hide() {
            this.user = DEFAULT_USER
            this.$refs.dialogBox.hide()
        },
        submit() {
            if (this.user.id) {
                this.update()
            } else {
                this.create()
            }
        },
        create() {
            service.create({
                password: "<PASSWORD>",
                ...this.user
            }).then(user => {
                this.$emit('create', user)
                this.$bus$emit('success', "User created successfully.")
            })
            this.hide()
        },
        update() {
            service.update(this.user).then(user => {
                this.$emit('update', user)
                this.$bus$emit('success', "User updated successfully.")
            })
            this.hide()
        },
        async addToGroup(name) {
            if (await groupService.exists(name)) {
                this.user.memberships.push(name)
            } else {
                this.$bus$emit('error', "Group name doesn't exist.")
            }
        },
        removeFromGroup(name) {
            [name, ...this.user.memberships] = this.user.memberships
        }
    }
}
</script><commit_msg>Fix group deletion<commit_after><template>
    <app-dialog
            ref="dialogBox"
            :title="title">
        <template>
            <app-text-input
                    id="username"
                    label="Username"
                    v-model="user.login"/>
            <app-text-input
                    id="first-name"
                    label="First Name"
                    v-model="user.firstName"/>
            <app-text-input
                    id="last-name"
                    label="Last Name"
                    v-model="user.lastName"/>
            <app-row>
                <app-text-input
                    id="groups"
                    label="Groups"
                    action="plus solid"
                    @action="addToGroup"/>
            </app-row>
            <app-row>
                <app-tag
                        v-for="group in user.memberships"
                        action="times solid"
                        :key="group.id"
                        @action="removeFromGroup(group)">
                    {{group}}
                </app-tag>
            </app-row>
        </template>
        <template #actions>
            <button
                    type="button"
                    class="mdl-button"
                    @click="submit">
                OK
            </button>
            <button
                    type="button"
                    class="mdl-button"
                    @click="hide">
                Cancel
            </button>
        </template>
    </app-dialog>
</template>

<script>
import service from '@/service/user'
import groupService from '@/service/group'

const DEFAULT_USER = {
    login: "",
    firstName: "",
    lastName: "",
    memberships: []
}

export default {
    name: "DialogUser",
    data() {
        return {
            user: DEFAULT_USER
        }
    },
    computed: {
        title() {
            return this.user.id ? "Edit User" : "Add User"
        }
    },
    methods: {
        show(user) {
            if (user) this.user = {...user}
            this.$refs.dialogBox.show()
        },
        hide() {
            this.user = DEFAULT_USER
            this.$refs.dialogBox.hide()
        },
        submit() {
            if (this.user.id) {
                this.update()
            } else {
                this.create()
            }
        },
        create() {
            service.create({
                password: "<PASSWORD>",
                ...this.user
            }).then(user => {
                this.$emit('create', user)
                this.$bus$emit('success', "User created successfully.")
            })
            this.hide()
        },
        update() {
            service.update(this.user).then(user => {
                this.$emit('update', user)
                this.$bus$emit('success', "User updated successfully.")
            })
            this.hide()
        },
        async addToGroup(name) {
            if (await groupService.exists(name)) {
                this.user.memberships.push(name)
            } else {
                this.$bus$emit('error', "Group name doesn't exist.")
            }
        },
        removeFromGroup(name) {
            this.user.memberships = this.user.memberships.filter(item => item !== name)
        }
    }
}
</script>
{'fraction_non_alphanumeric': 0.12890980868508958, 'fraction_numerical': 0.0, 'mean_word_length': 1.0070079219987813, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 58, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '980620', 'n_tokens_mistral': 1729, 'n_tokens_neox': 1636, 'n_words': 289}