se agrega en mantenedor de persona, subir foto
parent
ec8d4a7b7f
commit
71cd973e25
|
@ -21,6 +21,7 @@
|
|||
import { storeSession } from '$/stores/global'
|
||||
import Toast from '$/components/Toast.svelte';
|
||||
import ActualizandoGtfs from '$/components/ActualizandoGtfs.svelte';
|
||||
import { getPhoto } from '$/services/personas';
|
||||
|
||||
|
||||
let triggerEvent = false;
|
||||
|
@ -39,9 +40,13 @@
|
|||
|
||||
async function begin() {
|
||||
try {
|
||||
$storeSession = await getInfoToken()
|
||||
const data = await getInfoToken()
|
||||
const avatar_img = await getPhoto(data.persona.rut)
|
||||
$storeSession = { ...data, avatar_img }
|
||||
} catch (error) {
|
||||
alert(error.message || error)
|
||||
sessionStorage.clear();
|
||||
document.location.href = '/';
|
||||
}
|
||||
}
|
||||
begin()
|
||||
|
|
|
@ -1,14 +1,18 @@
|
|||
<script>
|
||||
import { getPhoto } from "$/services/personas";
|
||||
import { getTiposPersona } from "$/services/tipos_persona";
|
||||
|
||||
export let form = {}
|
||||
export let es_nuevo = true
|
||||
|
||||
let inputFile = null;
|
||||
let tipos_tratamiento = [];
|
||||
let rut = '';
|
||||
let urlPhoto = null
|
||||
|
||||
$: begin(es_nuevo)
|
||||
$: !es_nuevo && (rut = `${form.rut || ''}-${form.dv || ''}`);
|
||||
$: cargarPhoto(form.rut)
|
||||
|
||||
async function begin(es_nuevo) {
|
||||
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 } }) {
|
||||
const valorRut = value.replace(/[.|-]/,'');
|
||||
const dv = valorRut.substring(valorRut.length -1)
|
||||
|
@ -84,3 +97,18 @@
|
|||
<input type="text" bind:value={form.direccion} class="form-control">
|
||||
</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>
|
|
@ -16,7 +16,7 @@
|
|||
const params = useParams()
|
||||
const navigate = useNavigate()
|
||||
let es_nuevo = true;
|
||||
let form = {}
|
||||
let form = { photo: null }
|
||||
let loading = false;
|
||||
let escritura = false
|
||||
|
||||
|
|
|
@ -23,8 +23,6 @@ export async function getGtfsArchivoId(id) {
|
|||
|
||||
export async function createGtfsArchivo(data) {
|
||||
const form = new FormData()
|
||||
const directorioUpload = '/uploads'
|
||||
form.append('ruta_archivo', directorioUpload)
|
||||
form.append('status', 'PENDIENTE')
|
||||
for (let key in data) {
|
||||
if (key === 'archivo') {
|
||||
|
|
|
@ -19,21 +19,50 @@ export async function getPersona(rut) {
|
|||
}
|
||||
|
||||
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/`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(data),
|
||||
headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" }
|
||||
body: form,
|
||||
headers: { "Authorization": `Bearer ${getToken()}` }
|
||||
})
|
||||
if (!res.ok) throw await res.text()
|
||||
return res.json()
|
||||
}
|
||||
|
||||
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}/`, {
|
||||
method: 'PATCH',
|
||||
body: JSON.stringify(data),
|
||||
headers: { "Authorization": `Bearer ${getToken()}`, "Content-Type": "application/json" }
|
||||
body: form,
|
||||
headers: { "Authorization": `Bearer ${getToken()}` }
|
||||
})
|
||||
if (!res.ok) throw await res.text()
|
||||
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);
|
||||
}
|
Loading…
Reference in New Issue