aboutsummaryrefslogtreecommitdiff
path: root/frontend/src/components/pages/summary_modview.js
blob: 3541c485a35cfda87d487a3ddeb9ec2a6b76e664 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
import React from 'react';
import { useLocation }  from "react-router-dom";
import ReactMarkdown from 'react-markdown'

import "./summary_modview.css";


export default function Modview(prop) {
const {selectedRun,data,token} = prop

const [menu,setMenu] = React.useState(0)
React.useEffect(()=>{
if(menu===3){ // add
    document.querySelector("#modview-route-name>input").value=""
    document.querySelector("#modview-route-score>input").value=""
    document.querySelector("#modview-route-date>input").value=""
    document.querySelector("#modview-route-showcase>input").value=""
    document.querySelector("#modview-route-description>textarea").value=""
    }
if(menu===2){ // edit
    document.querySelector("#modview-route-id>input").value=data.summary.routes[selectedRun].route_id
    document.querySelector("#modview-route-name>input").value=data.summary.routes[selectedRun].history.runner_name
    document.querySelector("#modview-route-score>input").value=data.summary.routes[selectedRun].history.score_count
    document.querySelector("#modview-route-date>input").value=data.summary.routes[selectedRun].history.date.split("T")[0]
    document.querySelector("#modview-route-showcase>input").value=data.summary.routes[selectedRun].showcase
    document.querySelector("#modview-route-description>textarea").value=data.summary.routes[selectedRun].description
}   // eslint-disable-next-line 
},[menu])

function compressImage(file) {
    const reader = new FileReader();
    reader.readAsDataURL(file);
    return new Promise(resolve => {
      reader.onload = () => {
        const img = new Image();
        img.src = reader.result;
        img.onload = () => {
          let {width, height} = img;
          if (width > 550) {
            height *= 550 / width;
            width = 550;
          }
          if (height > 320) {
            width *= 320 / height;
            height = 320;
          }
          const canvas = document.createElement('canvas');
          canvas.width = width;
          canvas.height = height;
          canvas.getContext('2d').drawImage(img, 0, 0, width, height);
          resolve(canvas.toDataURL(file.type, 0.6));
        };
      };
    });
}
const [image,setImage] = React.useState(null)
function uploadImage(){
    if(window.confirm("Are you sure you want to submit this to the database?")){
    fetch(`/api/v1/maps/${location.pathname.split('/')[2]}/image`,{
        method: 'PUT',
        headers: {Authorization: token},
        body: JSON.stringify({"image": image})
    }).then(r=>window.location.reload())
    }
}
const location = useLocation()
function editRoute(){
if(window.confirm("Are you sure you want to submit this to the database?")){
    let payload = {
        "description": document.querySelector("#modview-route-description>textarea").value===""?"No description available.":document.querySelector("#modview-route-description>textarea").value,
        "record_date": document.querySelector("#modview-route-date>input").value+"T00:00:00Z",
        "route_id": parseInt(document.querySelector("#modview-route-id>input").value),
        "score_count": parseInt(document.querySelector("#modview-route-score>input").value),
        "showcase": document.querySelector("#modview-route-showcase>input").value,
        "user_name": document.querySelector("#modview-route-name>input").value
      }
      fetch(`/api/v1/maps/${location.pathname.split('/')[2]}/summary`,{
        method: 'PUT',
        headers: {Authorization: token},
        body: JSON.stringify(payload)
      }).then(r=>window.location.reload())
    }
}


function addRoute(){
    if(window.confirm("Are you sure you want to submit this to the database?")){
    let payload = {
        "category_id": parseInt(document.querySelector("#modview-route-category>select").value),
        "description": document.querySelector("#modview-route-description>textarea").value===""?"No description available.":document.querySelector("#modview-route-description>textarea").value,
        "record_date": document.querySelector("#modview-route-date>input").value+"T00:00:00Z",
        "score_count": parseInt(document.querySelector("#modview-route-score>input").value),
        "showcase": document.querySelector("#modview-route-showcase>input").value,
        "user_name": document.querySelector("#modview-route-name>input").value
      }
      fetch(`/api/v1/maps/${location.pathname.split('/')[2]}/summary`,{
        method: 'POST',
        headers: {Authorization: token},
        body: JSON.stringify(payload)
        }).then(r=>window.location.reload())
    }
}

function deleteRoute(){
if(data.summary.routes[0].category==='')
{window.alert("no run selected")}else{
if(window.confirm(`Are you sure you want to delete this run from the database?
${data.summary.routes[selectedRun].category.name}    ${data.summary.routes[selectedRun].history.score_count} portals    ${data.summary.routes[selectedRun].history.runner_name}`)===true){
    console.log("deleted:",selectedRun)
    fetch(`/api/v1/maps/${location.pathname.split('/')[2]}/summary`,{
        method: 'DELETE',
        headers: {Authorization: token},
        body: JSON.stringify({"route_id":data.summary.routes[selectedRun].route_id})
      }).then(r=>window.location.reload())
}}

}

const [showButton, setShowButton] = React.useState(1)
const modview = document.querySelector("div#modview")
React.useEffect(()=>{
    if(modview!==null){
        showButton ? modview.style.transform="translateY(-68%)"
        : modview.style.transform="translateY(0%)"
    }
    let modview_block = document.querySelector("#modview_block")
    showButton===1?modview_block.style.display="none":modview_block.style.display="block"// eslint-disable-next-line 
},[showButton])

const [md,setMd] = React.useState("")

return (
    <>
    <div id="modview_block"></div>
        <div id='modview'>
            <div>
                <button onClick={()=>setMenu(1)}>edit image</button>
                <button onClick={            
                data.summary.routes[0].category===''?()=>window.alert("no run selected"):()=>setMenu(2)}>edit selected route</button>
                <button onClick={()=>setMenu(3)}>add new route</button>
                <button onClick={()=>deleteRoute()}>delete selected route</button>
            </div>
            <div>
                 {showButton ?(
                    <button onClick={()=>setShowButton(0)}>Show</button>
                ) : (
                    <button onClick={()=>{setShowButton(1);setMenu(0)}}>Hide</button>
                )} 
            </div>
        </div>
        {menu!==0? (
        <div id='modview-menu'>
        {menu===1? ( 
            // image
            <div id='modview-menu-image'>
                <div>
                    <span>current image:</span>
                    <img src={data.map.image} alt="missing" />
                </div>

                <div>
                    <span>new image:
                    <input type="file" accept='image/*' onChange={e=>
                            compressImage(e.target.files[0])
                            .then(d=>setImage(d))
                    }/></span>
                    {image!==null?(<button onClick={()=>uploadImage()}>upload</button>):<span></span>}
                    <img src={image} alt="" id='modview-menu-image-file'/>
                    
                </div>
            </div>
        ):menu===2?(
            // edit route
            <div id='modview-menu-edit'>
                <div id='modview-route-id'>
                    <span>route id:</span>
                    <input type="number"  disabled/>
                </div>
                <div id='modview-route-name'>
                    <span>runner name:</span>
                    <input type="text"/>
                </div>
                <div id='modview-route-score'>
                    <span>score:</span>
                    <input type="number"/>
                </div>
                <div id='modview-route-date'>
                    <span>date:</span>
                    <input type="date"/>
                </div>
                <div id='modview-route-showcase'>
                    <span>showcase video:</span>
                    <input type="text"/>
                </div>
                <div id='modview-route-description' style={{height:"180px",gridColumn:"1 / span 5"}}>
                    <span>description:</span>
                    <textarea onChange={()=>setMd(document.querySelector("#modview-route-description>textarea").value)}></textarea>
                </div>
                <button style={{gridColumn:"2 / span 3",height:"40px"}} onClick={editRoute}>Apply</button>
            </div>
        ):menu===3?(
            // add route
            <div id='modview-menu-add'>
                <div id='modview-route-category'>
                    <span>category:</span>
                    <select>
                        <option value="1" key="1">CM</option>
                        <option value="2" key="2">No SLA</option>
                        {data.map.game_name==="Portal 2 - Cooperative"?"":(
                        <option value="3" key="3">Inbounds SLA</option>)}
                        <option value="4" key="4">Any%</option>
                    </select>
                </div>
                <div id='modview-route-name'>
                    <span>runner name:</span>
                    <input type="text" />
                </div>
                <div id='modview-route-score'>
                    <span>score:</span>
                    <input type="number" />
                </div>
                <div id='modview-route-date'>
                    <span>date:</span>
                    <input type="date" />
                </div>
                <div id='modview-route-showcase'>
                    <span>showcase video:</span>
                    <input type="text" />
                </div>
                <div id='modview-route-description' style={{height:"180px",gridColumn:"1 / span 5"}}>
                    <span>description:</span>
                    <textarea defaultValue={"No description available."} onChange={()=>setMd(document.querySelector("#modview-route-description>textarea").value)}></textarea>
                </div>
                <button style={{gridColumn:"2 / span 3",height:"40px"}} onClick={addRoute}>Apply</button>
            </div>
        ):("error")}

            {menu!==1?(
            <div id='modview-md'>
                <span>Markdown preview</span> 
                <span><a href="https://commonmark.org/help/" rel="noreferrer" target='_blank'>documentation</a></span> 
                <span><a href="https://remarkjs.github.io/react-markdown/" rel="noreferrer" target='_blank'>demo</a></span> 
                    <p>
                    <ReactMarkdown>{md}
                    </ReactMarkdown>
                    </p>
            </div>
            ):""}
        </div>):""}
        
    </>
)
}