se agrega en mantenedor de persona, subir foto

francisco/photos
Francisco Sandoval 2024-02-14 23:13:20 -03:00
parent ec8d4a7b7f
commit 71cd973e25
5 changed files with 121 additions and 61 deletions

View File

@ -21,6 +21,7 @@
import { storeSession } from '$/stores/global' import { storeSession } from '$/stores/global'
import Toast from '$/components/Toast.svelte'; import Toast from '$/components/Toast.svelte';
import ActualizandoGtfs from '$/components/ActualizandoGtfs.svelte'; import ActualizandoGtfs from '$/components/ActualizandoGtfs.svelte';
import { getPhoto } from '$/services/personas';
let triggerEvent = false; let triggerEvent = false;
@ -39,9 +40,13 @@
async function begin() { async function begin() {
try { try {
$storeSession = await getInfoToken() const data = await getInfoToken()
const avatar_img = await getPhoto(data.persona.rut)
$storeSession = { ...data, avatar_img }
} catch (error) { } catch (error) {
alert(error.message || error) alert(error.message || error)
sessionStorage.clear();
document.location.href = '/';
} }
} }
begin() begin()

View File

@ -1,14 +1,18 @@
<script> <script>
import { getPhoto } from "$/services/personas";
import { getTiposPersona } from "$/services/tipos_persona"; import { getTiposPersona } from "$/services/tipos_persona";
export let form = {} export let form = {}
export let es_nuevo = true export let es_nuevo = true
let inputFile = null;
let tipos_tratamiento = []; let tipos_tratamiento = [];
let rut = ''; let rut = '';
let urlPhoto = null
$: begin(es_nuevo) $: begin(es_nuevo)
$: !es_nuevo && (rut = `${form.rut || ''}-${form.dv || ''}`); $: !es_nuevo && (rut = `${form.rut || ''}-${form.dv || ''}`);
$: cargarPhoto(form.rut)
async function begin(es_nuevo) { async function begin(es_nuevo) {
try { try {
@ -18,6 +22,15 @@
} }
} }
async function cargarPhoto(rut) {
try {
if (!rut) return;
urlPhoto = await getPhoto(rut)
} catch (error) {
globalThis.toast.success(error.detail || error)
}
}
function onChangeRut({ target: { value } }) { function onChangeRut({ target: { value } }) {
const valorRut = value.replace(/[.|-]/,''); const valorRut = value.replace(/[.|-]/,'');
const dv = valorRut.substring(valorRut.length -1) const dv = valorRut.substring(valorRut.length -1)
@ -84,3 +97,18 @@
<input type="text" bind:value={form.direccion} class="form-control"> <input type="text" bind:value={form.direccion} class="form-control">
</div> </div>
</div> </div>
<div class="row">
<div class="col-md-2">
{#if form.rut}
<img src={urlPhoto} class="img-fluid" alt="imagen de persona">
{/if}
</div>
<div class="col-md mb-3">
Fotografia
<div class="form-control">
<input type="file" bind:this={inputFile} accept=".jpg, .jpeg, .png" on:change={() => form.photo = inputFile}>
</div>
</div>
</div>

View File

@ -16,7 +16,7 @@
const params = useParams() const params = useParams()
const navigate = useNavigate() const navigate = useNavigate()
let es_nuevo = true; let es_nuevo = true;
let form = {} let form = { photo: null }
let loading = false; let loading = false;
let escritura = false let escritura = false

View File

@ -23,8 +23,6 @@ export async function getGtfsArchivoId(id) {
export async function createGtfsArchivo(data) { export async function createGtfsArchivo(data) {
const form = new FormData() const form = new FormData()
const directorioUpload = '/uploads'
form.append('ruta_archivo', directorioUpload)
form.append('status', 'PENDIENTE') form.append('status', 'PENDIENTE')
for (let key in data) { for (let key in data) {
if (key === 'archivo') { if (key === 'archivo') {

View File

@ -19,21 +19,50 @@ export async function getPersona(rut) {
} }
export async function createPersona(data) { export async function createPersona(data) {
const form = new FormData()
for (const key in data) {
if (data[key]?.files?.length) {
form.append(key, data[key].files[0], data[key].files[0].name)
} else {
form.append(key, data[key] || '')
}
}
const res = await fetch(`${base}/personas/`, { const res = await fetch(`${base}/personas/`, {
method: 'POST', method: 'POST',
body: JSON.stringify(data), body: form,
headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" } headers: { "Authorization": `Bearer ${getToken()}` }
}) })
if (!res.ok) throw await res.text() if (!res.ok) throw await res.text()
return res.json() return res.json()
} }
export async function updatePersona({ rut: id = null, ...data }) { export async function updatePersona({ rut: id = null, ...data }) {
console.log({ data })
const form = new FormData()
for (const key in data) {
if (data[key]?.files?.length) {
form.append(key, data[key].files[0], data[key].files[0].name)
} else {
form.append(key, data[key] || '')
}
}
const res = await fetch(`${base}/personas/${id}/`, { const res = await fetch(`${base}/personas/${id}/`, {
method: 'PATCH', method: 'PATCH',
body: JSON.stringify(data), body: form,
headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" } headers: { "Authorization": `Bearer ${getToken()}` }
}) })
if (!res.ok) throw await res.text() if (!res.ok) throw await res.text()
return res.json() return res.json()
} }
export async function getPhoto(id) {
const res = await fetch(`${base}/personas/photo/?id=${id}`, {
headers: { "Authorization": `Bearer ${getToken()}` }
})
if (!res.ok) throw await res.text()
const blob = await res.blob()
return URL.createObjectURL(blob);
}