Skip to content

Commit

Permalink
feat(#193): support edit and create rooms
Browse files Browse the repository at this point in the history
  • Loading branch information
Jumpy-Squirrel committed Nov 10, 2024
1 parent 2e2a812 commit 1ff9420
Show file tree
Hide file tree
Showing 8 changed files with 235 additions and 83 deletions.
13 changes: 13 additions & 0 deletions src/html/common/style.css
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,19 @@ input
background-color: #ffffff;
}

input.error
{
width: 300px;
margin: 0px 0px 0px 4px;
font-family: Arial;
font-size: 10pt;
border-width: 1px;
border-style: solid;
border-color: #ff0000;
padding-left: 3px;
background-color: #ffffff;
}

input.tiny
{
width: 32px;
Expand Down
33 changes: 25 additions & 8 deletions src/html/common/vue/apis/roomsrv.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const apiCall = ( method, path, params, body, successParser, apiErrorHandler ) => {
const apiCall = ( method, path, params, body, success2xxHandler, apiErrorHandler ) => {
axios({
url: path,
method: method,
Expand All @@ -10,7 +10,7 @@ const apiCall = ( method, path, params, body, successParser, apiErrorHandler ) =
}).then(function (response) {
// console.log(response);
// only called if 2xx response status
successParser(response.data)
success2xxHandler(response)
}).catch(function (error) {
// console.log(error);
if (error.response) {
Expand All @@ -35,15 +35,32 @@ const apiCall = ( method, path, params, body, successParser, apiErrorHandler ) =
})
}

export const ListAllRooms = ( addRooms, apiErrorHandler ) => {
export const ListAllRooms = ( setRooms, apiErrorHandler ) => {
// console.log('ListAllRooms')
apiCall('get', '/rooms', null, null, (data) => {
if (data.rooms) {
addRooms(data.rooms)
apiCall('get', '/rooms', null, null, (response) => {
// TODO move to a full success handler that gets the response
if (response?.data?.rooms) {
setRooms(response.data.rooms)
}
}, apiErrorHandler )
}

export const CreateRoom = ( room, apiErrorHandler ) => {
apiCall('post', '/rooms', null, room, (data) => {}, apiErrorHandler )
export const CreateRoom = ( room, successHandler, apiErrorHandler ) => {
apiCall('post', '/rooms', null, room, (response) => {
// TODO get id of new room from location and place into room, so handler knows which room to update
room.id = ''
successHandler(room)
}, apiErrorHandler )
}

export const UpdateRoom = ( room, successHandler, apiErrorHandler ) => {
apiCall('put', '/rooms/' + room.id, null, room, (response) => {
successHandler(room)
}, apiErrorHandler )
}

export const GetRoomByID = ( id, successHandler, apiErrorHandler ) => {
apiCall('get', '/rooms/' + id, null, null,(response) => {
successHandler(response.data)
}, apiErrorHandler )
}
15 changes: 14 additions & 1 deletion src/html/common/vue/localizations/de-DE.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,25 @@ export const de_DE = {
final: "Final?",
comments: "Kommentar",
},
click: 'Zimmer anklicken, um zu editieren',
},
create: {
title: 'Neues Zimmer',
name: 'Name',
namehint: 'bitte Namen eingeben',
size: 'Bettenzahl',
save: 'Speichern!',
comments: 'Kommentar',
save: 'Neues Zimmer speichern!',
cancel: 'Abbrechen',
},
edit: {
title: 'Zimmer bearbeiten',
name: 'Name',
namehint: 'bitte Namen eingeben',
size: 'Bettenzahl',
comments: 'Kommentar',
save: 'Änderungen Speichern!',
cancel: 'Abbrechen',
},
},
errors: {
Expand Down
15 changes: 14 additions & 1 deletion src/html/common/vue/localizations/en-US.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,25 @@ export const en_US = {
final: "Final?",
comments: "Comment",
},
click: 'Click on a room to edit it',
},
create: {
title: 'Create Room',
name: 'Name',
namehint: 'please enter a name',
size: 'Beds',
save: 'Save!',
comments: 'Comments',
save: 'Save new room!',
cancel: 'Cancel!',
},
edit: {
title: 'Edit Room',
name: 'Name',
namehint: 'please enter a name',
size: 'Beds',
comments: 'Comments',
save: 'Save changes!',
cancel: 'Cancel!',
},
},
errors: {
Expand Down
32 changes: 21 additions & 11 deletions src/html/common/vue/rooms/app.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,39 @@
import { ErrorList } from '../shared/errorlist.js'
import { RoomList } from './roomlist.js'
import { RoomCreateForm } from './roomcreate.js'
import { RoomForm } from './roomform.js'

const { useI18n } = VueI18n
const { ref } = Vue

export const App = {
setup() {
const { t } = useI18n()
const roomId = ref('')
const updateCount = ref(0)

const setRoomId = (id) => {
console.log('received '+id)
roomId.value = id
}
const reloadRooms = () => {
console.log('reload rooms')
roomId.value = ''
updateCount.value++
}

return {
t
roomId,
updateCount,
setRoomId,
reloadRooms,
}
},
components: {
ErrorList,
RoomForm,
RoomList,
RoomCreateForm,
},
template: `
<ErrorList />
<div class="headline"><br/>{{ t('rooms.list.title') }}</div>
<hr class="contentbox"/>
<RoomList />
<div class="headline"><br/>{{ t('rooms.create.title') }}</div>
<hr class="contentbox"/>
<RoomCreateForm />
<RoomForm :id="roomId" @rooms-possibly-updated="reloadRooms"/>
<RoomList :reload="updateCount" @room-clicked="(id) => setRoomId(id)"/>
`
}
58 changes: 0 additions & 58 deletions src/html/common/vue/rooms/roomcreate.js

This file was deleted.

123 changes: 123 additions & 0 deletions src/html/common/vue/rooms/roomform.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
import { CreateRoom, UpdateRoom, GetRoomByID } from '../apis/roomsrv.js'
import { StoredErrorList } from '../stores/errorlist.js'

const { ref, computed, watch } = Vue
const { useI18n } = VueI18n

export const RoomForm = {
props: ['id'],
emits: ['rooms-possibly-updated'],
setup(props, { emit }) {
const { t } = useI18n()

const id = ref(props.id)
const name = ref('')
const roomsize = ref('2')
const comments = ref('')

const isNew = computed(() => {
return id.value.length === 0
})
const isNameError = computed(() => {
return name.value.length === 0
})
const isSizeError = computed(() => {
return !roomsize.value.match(/^[1-9][0-9]*$/)
})

const errorList = StoredErrorList // get a local reference to the store ref
const resetHandler = () => {
// even a reset will at least de-select the currently edited room
emit('rooms-possibly-updated')
errorList.resetErrors()
id.value = ''
name.value = ''
roomsize.value = '2'
comments.value = ''
}
const submitHandler = () => {
errorList.resetErrors()
if (id.value.length === 0) {
CreateRoom({
name: name.value,
flags: [],
size: Number(roomsize.value),
comments: comments.value,
}, (room) => {
resetHandler()
},(status, apiError) => {
errorList.addError(apiError)
})
} else {
UpdateRoom({
id: id.value,
name: name.value,
flags: [], // TODO
size: Number(roomsize.value),
comments: comments.value,
}, (room) => {
resetHandler()
}, (status, apiError) => {
errorList.addError(apiError)
})
}
}

watch(() => props.id, (newIdValue, oldIdValue) => {
console.log('RoomForm received props.id change: ' + oldIdValue + ' -> ' + newIdValue)
id.value = newIdValue
})

watch(id, (newIdValue, oldIdValue) => {
if (newIdValue !== oldIdValue) {
if (newIdValue.length === 0) {
resetHandler()
} else {
GetRoomByID(newIdValue, (room) => {
id.value = newIdValue
name.value = room.name
roomsize.value = '' + room.size
comments.value = room.comments ?? ""
}, (status, apiError) => {
errorList.addError(apiError)
})
}
}
}, { immediate: true })

return {
t,
name,
roomsize,
comments,
isNew,
isNameError,
isSizeError,
submitHandler,
resetHandler,
errorList,
}
},
template: `
<div class="headline"><br/>{{ isNew ? t('rooms.create.title') : t('rooms.edit.title') }}</div>
<hr class="contentbox"/>
<table width="100%" cellpadding="0" cellspacing="4" border="0">
<tr>
<td class="label" ALIGN="right" VALIGN="middle" width="20%">{{ isNew ? t('rooms.create.name') : t('rooms.edit.name') }}</TD>
<td class="input" ALIGN="left" VALIGN="middle" width="80%"><input type="text" size="40" maxlength="80" v-model.trim="name" :class="{ error: isNameError }" :placeholder="isNew ? t('rooms.create.namehint') : t('rooms.edit.namehint')"/></td>
</tr>
<tr>
<td class="label" ALIGN="right" VALIGN="middle" width="20%">{{ isNew ? t('rooms.create.size') : t('rooms.edit.size') }}</TD>
<td class="input" ALIGN="left" VALIGN="middle" width="80%"><input type="text" size="5" maxlength="5" v-model.trim="roomsize" :class="{ error: isSizeError }"/></td>
</tr>
<tr>
<td class="label" ALIGN="right" VALIGN="middle" width="20%">{{ isNew ? t('rooms.create.comments') : t('rooms.edit.comments') }}</TD>
<td class="input" ALIGN="left" VALIGN="middle" width="80%"><input type="text" size="40" maxlength="80" v-model.trim="comments"/></td>
</tr>
<tr>
<td class="label" ALIGN="right" VALIGN="middle" width="20%">&nbsp;</TD>
<td class="input" ALIGN="left" VALIGN="middle" width="80%"><button :disabled="isNameError || isSizeError" @click="submitHandler">{{ isNew ? t('rooms.create.save') : t('rooms.edit.save') }}</button>&nbsp;<button @click="resetHandler">{{ isNew ? t('rooms.create.cancel') : t('rooms.edit.cancel') }}</button></td>
</tr>
</table>
`
}
Loading

0 comments on commit 1ff9420

Please sign in to comment.