added note update
parent
3555b89805
commit
ca97fc3e5b
@ -1,3 +1,37 @@
|
|||||||
<template>
|
<template>
|
||||||
<!-- card class css located at /assets/css/tailwind.css -->
|
<div
|
||||||
|
class="note"
|
||||||
|
@click="noteDetail(props.id)">
|
||||||
|
<div class="title">
|
||||||
|
{{ props.title }}
|
||||||
|
</div>
|
||||||
|
<div>
|
||||||
|
{{ props.description }}
|
||||||
|
</div>
|
||||||
|
</div>
|
||||||
</template>
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
interface Props {
|
||||||
|
id: string | number;
|
||||||
|
title: string;
|
||||||
|
description: string;
|
||||||
|
}
|
||||||
|
|
||||||
|
const props = defineProps<Props>();
|
||||||
|
const router = useRouter();
|
||||||
|
|
||||||
|
const noteDetail = (id: string | number) => {
|
||||||
|
router.push(`/notes/${id}`);
|
||||||
|
};
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.note {
|
||||||
|
@apply bg-secondary flex w-96 cursor-pointer flex-col gap-4 rounded-lg p-4;
|
||||||
|
}
|
||||||
|
|
||||||
|
.title {
|
||||||
|
@apply w-full text-center text-2xl font-bold;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
|
|||||||
@ -0,0 +1,63 @@
|
|||||||
|
<template>
|
||||||
|
<div class="app">
|
||||||
|
<button @click="submit()">
|
||||||
|
<Icon
|
||||||
|
name="fa6-solid:floppy-disk"
|
||||||
|
size="24" />
|
||||||
|
</button>
|
||||||
|
<textarea
|
||||||
|
v-model="description"
|
||||||
|
class="noteTextArea"></textarea>
|
||||||
|
</div>
|
||||||
|
</template>
|
||||||
|
|
||||||
|
<script setup lang="ts">
|
||||||
|
definePageMeta({
|
||||||
|
middleware: ['auth'],
|
||||||
|
});
|
||||||
|
|
||||||
|
const description = ref<string>('');
|
||||||
|
const route = useRoute();
|
||||||
|
const isLoading = ref<Boolean>(false);
|
||||||
|
const isSubmitting = ref<Boolean>(false);
|
||||||
|
|
||||||
|
const { refresh } = await useFetch('/api/note', {
|
||||||
|
method: 'GET',
|
||||||
|
query: {
|
||||||
|
id: route.params.id[0],
|
||||||
|
},
|
||||||
|
onResponse({ response }) {
|
||||||
|
description.value = response._data.note.description;
|
||||||
|
console.log(response);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const submit = async () => {
|
||||||
|
isSubmitting.value = true;
|
||||||
|
await useFetch('/api/note', {
|
||||||
|
method: 'POST',
|
||||||
|
query: {
|
||||||
|
id: route.params.id[0],
|
||||||
|
},
|
||||||
|
body: {
|
||||||
|
description: description.value,
|
||||||
|
},
|
||||||
|
onResponse({ response }) {
|
||||||
|
console.log('POST:', response._data.message);
|
||||||
|
},
|
||||||
|
});
|
||||||
|
isSubmitting.value = false;
|
||||||
|
};
|
||||||
|
|
||||||
|
onMounted(() => {
|
||||||
|
isLoading.value = true;
|
||||||
|
refresh();
|
||||||
|
isLoading.value = false;
|
||||||
|
});
|
||||||
|
</script>
|
||||||
|
|
||||||
|
<style scoped>
|
||||||
|
.noteTextArea {
|
||||||
|
@apply h-full w-full max-w-2xl p-4;
|
||||||
|
}
|
||||||
|
</style>
|
||||||
@ -0,0 +1,18 @@
|
|||||||
|
import { serverSupabaseUser, serverSupabaseClient } from '#supabase/server';
|
||||||
|
import { Database } from 'utils/database.types';
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const user = await serverSupabaseUser(event);
|
||||||
|
const supabase = serverSupabaseClient<Database>(event);
|
||||||
|
const query = getQuery(event);
|
||||||
|
|
||||||
|
const { data: note, error } = await supabase
|
||||||
|
.from('notes')
|
||||||
|
.select('*')
|
||||||
|
.eq('id', query.id);
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
throw createError({ statusCode: 401, message: 'Unauthorized' });
|
||||||
|
}
|
||||||
|
return { note: note ? note[0] : null, error: error };
|
||||||
|
});
|
||||||
@ -0,0 +1,21 @@
|
|||||||
|
import { serverSupabaseUser, serverSupabaseClient } from '#supabase/server';
|
||||||
|
import { Database } from 'utils/database.types';
|
||||||
|
|
||||||
|
export default defineEventHandler(async (event) => {
|
||||||
|
const user = await serverSupabaseUser(event);
|
||||||
|
const supabase = serverSupabaseClient<Database>(event);
|
||||||
|
const query = getQuery(event);
|
||||||
|
const body = await readBody(event);
|
||||||
|
|
||||||
|
const description: string = body.description;
|
||||||
|
|
||||||
|
const { data: note, error } = await supabase
|
||||||
|
.from('notes')
|
||||||
|
.update({ description: description })
|
||||||
|
.eq('id', query.id);
|
||||||
|
|
||||||
|
if (!user) {
|
||||||
|
throw createError({ statusCode: 401, message: 'Unauthorized' });
|
||||||
|
}
|
||||||
|
return { statusCode: 200, message: 'Note Updated', error: error };
|
||||||
|
});
|
||||||
@ -0,0 +1,19 @@
|
|||||||
|
export interface Database {
|
||||||
|
public: {
|
||||||
|
Tables: {
|
||||||
|
notes: {
|
||||||
|
Row: {
|
||||||
|
id: number;
|
||||||
|
title: string;
|
||||||
|
description: string;
|
||||||
|
user_id: string;
|
||||||
|
created_at: string;
|
||||||
|
}; // The data expected to be returned from a "select" statement.
|
||||||
|
Insert: {}; // The data expected passed to an "insert" statement.
|
||||||
|
Update: {
|
||||||
|
description: string;
|
||||||
|
}; // The data expected passed to an "update" statement.
|
||||||
|
};
|
||||||
|
};
|
||||||
|
};
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue