|
| 1 | +<template> |
| 2 | + <NewModal ref="modal" :header="formatMessage(messages.title)"> |
| 3 | + <div class="min-w-md flex max-w-md flex-col gap-3"> |
| 4 | + <CreateLimitAlert v-model="hasHitLimit" type="collection" /> |
| 5 | + <div class="flex flex-col gap-2"> |
| 6 | + <label for="name"> |
| 7 | + <span class="text-lg font-semibold text-contrast"> |
| 8 | + {{ formatMessage(messages.nameLabel) }} |
| 9 | + <span class="text-brand-red">*</span> |
| 10 | + </span> |
| 11 | + </label> |
| 12 | + <input |
| 13 | + id="name" |
| 14 | + v-model="name" |
| 15 | + type="text" |
| 16 | + maxlength="64" |
| 17 | + :placeholder="formatMessage(messages.namePlaceholder)" |
| 18 | + autocomplete="off" |
| 19 | + :disabled="hasHitLimit" |
| 20 | + /> |
| 21 | + </div> |
| 22 | + <div class="flex flex-col gap-2"> |
| 23 | + <label for="additional-information" class="flex flex-col gap-1"> |
| 24 | + <span class="text-lg font-semibold text-contrast">{{ |
| 25 | + formatMessage(messages.summaryLabel) |
| 26 | + }}</span> |
| 27 | + <span>{{ formatMessage(messages.summaryDescription) }}</span> |
| 28 | + </label> |
| 29 | + <div class="textarea-wrapper"> |
| 30 | + <textarea |
| 31 | + id="additional-information" |
| 32 | + v-model="description" |
| 33 | + maxlength="256" |
| 34 | + :placeholder="formatMessage(messages.summaryPlaceholder)" |
| 35 | + :disabled="hasHitLimit" |
| 36 | + /> |
| 37 | + </div> |
| 38 | + </div> |
| 39 | + <p class="m-0"> |
| 40 | + {{ formatMessage(messages.collectionInfo, { count: projectIds.length }) }} |
| 41 | + </p> |
| 42 | + <div class="flex justify-end gap-2"> |
| 43 | + <ButtonStyled class="w-24"> |
| 44 | + <button @click="modal.hide()"> |
| 45 | + <XIcon aria-hidden="true" /> |
| 46 | + {{ formatMessage(messages.cancel) }} |
| 47 | + </button> |
| 48 | + </ButtonStyled> |
| 49 | + <ButtonStyled color="brand" class="w-36"> |
| 50 | + <button :disabled="hasHitLimit" @click="create"> |
| 51 | + <PlusIcon aria-hidden="true" /> |
| 52 | + {{ formatMessage(messages.createCollection) }} |
| 53 | + </button> |
| 54 | + </ButtonStyled> |
| 55 | + </div> |
| 56 | + </div> |
| 57 | + </NewModal> |
| 58 | +</template> |
| 59 | +<script setup> |
| 60 | +import { PlusIcon, XIcon } from '@modrinth/assets' |
| 61 | +import { ButtonStyled, injectNotificationManager, NewModal } from '@modrinth/ui' |
| 62 | +import { defineMessages } from '@vintl/vintl' |
| 63 | +
|
| 64 | +import CreateLimitAlert from './CreateLimitAlert.vue' |
| 65 | +
|
| 66 | +const { addNotification } = injectNotificationManager() |
| 67 | +const { formatMessage } = useVIntl() |
| 68 | +const router = useNativeRouter() |
| 69 | +
|
| 70 | +const messages = defineMessages({ |
| 71 | + title: { |
| 72 | + id: 'create.collection.title', |
| 73 | + defaultMessage: 'Creating a collection', |
| 74 | + }, |
| 75 | + nameLabel: { |
| 76 | + id: 'create.collection.name-label', |
| 77 | + defaultMessage: 'Name', |
| 78 | + }, |
| 79 | + namePlaceholder: { |
| 80 | + id: 'create.collection.name-placeholder', |
| 81 | + defaultMessage: 'Enter collection name...', |
| 82 | + }, |
| 83 | + summaryLabel: { |
| 84 | + id: 'create.collection.summary-label', |
| 85 | + defaultMessage: 'Summary', |
| 86 | + }, |
| 87 | + summaryDescription: { |
| 88 | + id: 'create.collection.summary-description', |
| 89 | + defaultMessage: 'A sentence or two that describes your collection.', |
| 90 | + }, |
| 91 | + summaryPlaceholder: { |
| 92 | + id: 'create.collection.summary-placeholder', |
| 93 | + defaultMessage: 'This is a collection of...', |
| 94 | + }, |
| 95 | + collectionInfo: { |
| 96 | + id: 'create.collection.collection-info', |
| 97 | + defaultMessage: |
| 98 | + 'Your new collection will be created as a public collection with {count, plural, =0 {no projects} one {# project} other {# projects}}.', |
| 99 | + }, |
| 100 | + cancel: { |
| 101 | + id: 'create.collection.cancel', |
| 102 | + defaultMessage: 'Cancel', |
| 103 | + }, |
| 104 | + createCollection: { |
| 105 | + id: 'create.collection.create-collection', |
| 106 | + defaultMessage: 'Create collection', |
| 107 | + }, |
| 108 | + errorTitle: { |
| 109 | + id: 'create.collection.error-title', |
| 110 | + defaultMessage: 'An error occurred', |
| 111 | + }, |
| 112 | +}) |
| 113 | +
|
| 114 | +const name = ref('') |
| 115 | +const description = ref('') |
| 116 | +const hasHitLimit = ref(false) |
| 117 | +
|
| 118 | +const modal = ref() |
| 119 | +
|
| 120 | +const props = defineProps({ |
| 121 | + projectIds: { |
| 122 | + type: Array, |
| 123 | + default() { |
| 124 | + return [] |
| 125 | + }, |
| 126 | + }, |
| 127 | +}) |
| 128 | +
|
| 129 | +async function create() { |
| 130 | + startLoading() |
| 131 | + try { |
| 132 | + const result = await useBaseFetch('collection', { |
| 133 | + method: 'POST', |
| 134 | + body: { |
| 135 | + name: name.value.trim(), |
| 136 | + description: description.value.trim() || undefined, |
| 137 | + projects: props.projectIds, |
| 138 | + }, |
| 139 | + apiVersion: 3, |
| 140 | + }) |
| 141 | +
|
| 142 | + await initUserCollections() |
| 143 | +
|
| 144 | + modal.value.hide() |
| 145 | + await router.push(`/collection/${result.id}`) |
| 146 | + } catch (err) { |
| 147 | + addNotification({ |
| 148 | + title: formatMessage(messages.errorTitle), |
| 149 | + text: err?.data?.description || err?.message || err, |
| 150 | + type: 'error', |
| 151 | + }) |
| 152 | + } |
| 153 | + stopLoading() |
| 154 | +} |
| 155 | +function show(event) { |
| 156 | + name.value = '' |
| 157 | + description.value = '' |
| 158 | + modal.value.show(event) |
| 159 | +} |
| 160 | +
|
| 161 | +defineExpose({ |
| 162 | + show, |
| 163 | +}) |
| 164 | +</script> |
| 165 | +
|
| 166 | +<style scoped lang="scss"> |
| 167 | +.modal-creation { |
| 168 | + input { |
| 169 | + width: 20rem; |
| 170 | + max-width: 100%; |
| 171 | + } |
| 172 | +
|
| 173 | + .text-input-wrapper { |
| 174 | + width: 100%; |
| 175 | + } |
| 176 | +
|
| 177 | + textarea { |
| 178 | + min-height: 5rem; |
| 179 | + } |
| 180 | +
|
| 181 | + .input-group { |
| 182 | + margin-top: var(--gap-md); |
| 183 | + } |
| 184 | +} |
| 185 | +</style> |
0 commit comments